1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
81 local_specialization_stack::local_specialization_stack (lss_policy policy
)
82 : saved (local_specializations
)
84 if (policy
== lss_blank
|| !saved
)
85 local_specializations
= new hash_map
<tree
, tree
>;
87 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations
;
93 local_specializations
= saved
;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth
;
99 struct GTY((for_user
)) spec_entry
106 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
108 static hashval_t
hash (spec_entry
*);
109 static bool equal (spec_entry
*, spec_entry
*);
112 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
114 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result
{
133 tbr_ambiguous_baseclass
,
137 static void push_access_scope (tree
);
138 static void pop_access_scope (tree
);
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
153 static void tsubst_enum (tree
, tree
, tree
);
154 static tree
add_to_template_args (tree
, tree
);
155 static tree
add_outermost_template_args (tree
, tree
);
156 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
157 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
, int,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static void template_parm_level_and_index (tree
, int*, int*);
184 static int unify_pack_expansion (tree
, tree
, tree
,
185 tree
, unification_kind_t
, bool, bool);
186 static tree
copy_template_args (tree
);
187 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
189 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
190 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
191 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
192 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
193 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
194 static bool check_specialization_scope (void);
195 static tree
process_partial_specialization (tree
);
196 static void set_current_access_from_decl (tree
);
197 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
199 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
200 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
202 static bool template_template_parm_bindings_ok_p (tree
, tree
);
203 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
204 static tree
for_each_template_parm_r (tree
*, int *, void *);
205 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
206 static void copy_default_args_to_explicit_spec (tree
);
207 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
208 static bool dependent_template_arg_p (tree
);
209 static bool any_template_arguments_need_structural_equality_p (tree
);
210 static bool dependent_type_p_r (tree
);
211 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
212 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
213 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
214 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
216 static tree
listify (tree
);
217 static tree
listify_autos (tree
, tree
);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
220 static bool complex_alias_template_p (const_tree tmpl
);
221 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
222 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
223 static tree
make_argument_pack (tree
);
224 static void register_parameter_specializations (tree
, tree
);
225 static tree
enclosing_instantiation_of (tree tctx
);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
233 push_access_scope (tree t
)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
236 || TREE_CODE (t
) == TYPE_DECL
);
238 if (DECL_FRIEND_CONTEXT (t
))
239 push_nested_class (DECL_FRIEND_CONTEXT (t
));
240 else if (DECL_CLASS_SCOPE_P (t
))
241 push_nested_class (DECL_CONTEXT (t
));
243 push_to_top_level ();
245 if (TREE_CODE (t
) == FUNCTION_DECL
)
247 saved_access_scope
= tree_cons
248 (NULL_TREE
, current_function_decl
, saved_access_scope
);
249 current_function_decl
= t
;
253 /* Restore the scope set up by push_access_scope. T is the node we
257 pop_access_scope (tree t
)
259 if (TREE_CODE (t
) == FUNCTION_DECL
)
261 current_function_decl
= TREE_VALUE (saved_access_scope
);
262 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
265 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
277 finish_member_template_decl (tree decl
)
279 if (decl
== error_mark_node
)
280 return error_mark_node
;
282 gcc_assert (DECL_P (decl
));
284 if (TREE_CODE (decl
) == TYPE_DECL
)
288 type
= TREE_TYPE (decl
);
289 if (type
== error_mark_node
)
290 return error_mark_node
;
291 if (MAYBE_CLASS_TYPE_P (type
)
292 && CLASSTYPE_TEMPLATE_INFO (type
)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
295 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
296 check_member_template (tmpl
);
301 else if (TREE_CODE (decl
) == FIELD_DECL
)
302 error ("data member %qD cannot be a member template", decl
);
303 else if (DECL_TEMPLATE_INFO (decl
))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
307 check_member_template (DECL_TI_TEMPLATE (decl
));
308 return DECL_TI_TEMPLATE (decl
);
314 error ("invalid member template declaration %qD", decl
);
316 return error_mark_node
;
319 /* Create a template info node. */
322 build_template_info (tree template_decl
, tree template_args
)
324 tree result
= make_node (TEMPLATE_INFO
);
325 TI_TEMPLATE (result
) = template_decl
;
326 TI_ARGS (result
) = template_args
;
330 /* Return the template info node corresponding to T, whatever T is. */
333 get_template_info (const_tree t
)
335 tree tinfo
= NULL_TREE
;
337 if (!t
|| t
== error_mark_node
)
340 if (TREE_CODE (t
) == NAMESPACE_DECL
341 || TREE_CODE (t
) == PARM_DECL
)
344 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
345 tinfo
= DECL_TEMPLATE_INFO (t
);
347 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
350 if (OVERLOAD_TYPE_P (t
))
351 tinfo
= TYPE_TEMPLATE_INFO (t
);
352 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
353 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
358 /* Returns the template nesting level of the indicated class TYPE.
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
377 template_class_depth (tree type
)
381 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
383 tree tinfo
= get_template_info (type
);
385 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
390 type
= CP_DECL_CONTEXT (type
);
391 else if (LAMBDA_TYPE_P (type
))
392 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
394 type
= CP_TYPE_CONTEXT (type
);
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
404 inline_needs_template_parms (tree decl
, bool nsdmi
)
406 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
410 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
419 push_inline_template_parms_recursive (tree parmlist
, int levels
)
421 tree parms
= TREE_VALUE (parmlist
);
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
427 ++processing_template_decl
;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl
),
430 parms
, current_template_parms
);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
435 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
437 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
439 if (error_operand_p (parm
))
442 gcc_assert (DECL_P (parm
));
444 switch (TREE_CODE (parm
))
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
467 maybe_begin_member_template_processing (tree decl
)
471 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
475 tree ctx
= DECL_CONTEXT (decl
);
476 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx
)
479 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
482 if (inline_needs_template_parms (decl
, nsdmi
))
484 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
485 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
490 parms
= TREE_CHAIN (parms
);
493 push_inline_template_parms_recursive (parms
, levels
);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels
.safe_push (levels
);
501 /* Undo the effects of maybe_begin_member_template_processing. */
504 maybe_end_member_template_processing (void)
509 if (inline_parm_levels
.length () == 0)
512 last
= inline_parm_levels
.pop ();
513 for (i
= 0; i
< last
; ++i
)
515 --processing_template_decl
;
516 current_template_parms
= TREE_CHAIN (current_template_parms
);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
525 add_to_template_args (tree args
, tree extra_args
)
532 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
535 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
536 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
538 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
539 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
541 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
555 add_outermost_template_args (tree args
, tree extra_args
)
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
565 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
571 new_args
= add_to_template_args (args
, extra_args
);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
579 /* Return the N levels of innermost template arguments from the ARGS. */
582 get_innermost_template_args (tree args
, int n
)
590 /* If N is 1, just return the innermost set of template arguments. */
592 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
594 /* If we're not removing anything, just return the arguments we were
596 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
597 gcc_assert (extra_levels
>= 0);
598 if (extra_levels
== 0)
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args
= make_tree_vec (n
);
603 for (i
= 1; i
<= n
; ++i
)
604 SET_TMPL_ARGS_LEVEL (new_args
, i
,
605 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
614 strip_innermost_template_args (tree args
, int extra_levels
)
617 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
622 /* If N is 1, just return the outermost set of template arguments. */
624 return TMPL_ARGS_LEVEL (args
, 1);
626 /* If we're not removing anything, just return the arguments we were
628 gcc_assert (extra_levels
>= 0);
629 if (extra_levels
== 0)
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args
= make_tree_vec (n
);
634 for (i
= 1; i
<= n
; ++i
)
635 SET_TMPL_ARGS_LEVEL (new_args
, i
,
636 TMPL_ARGS_LEVEL (args
, i
));
641 /* We've got a template header coming up; push to a new level for storing
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms
, NULL
);
662 ++processing_template_decl
;
663 ++processing_template_parmlist
;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl
),
670 current_template_parms
);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
678 check_specialization_scope (void)
680 tree scope
= current_scope ();
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
691 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
693 error ("explicit specialization in non-namespace scope %qD", scope
);
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms
)
707 error ("enclosing class templates are not explicitly specialized");
714 /* We've just seen template <>. */
717 begin_specialization (void)
719 begin_scope (sk_template_spec
, NULL
);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
728 end_specialization (void)
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
738 reset_specialization (void)
740 processing_specialization
= 0;
741 template_header_count
= 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
748 note_template_header (int specialization
)
750 processing_specialization
= specialization
;
751 template_header_count
++;
754 /* We're beginning an explicit instantiation. */
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation
);
760 processing_explicit_instantiation
= true;
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation
);
768 processing_explicit_instantiation
= false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
777 check_specialization_namespace (tree tmpl
)
779 tree tpl_ns
= decl_namespace_context (tmpl
);
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl
)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl
);
795 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
796 /* Same or enclosing namespace. */
800 permerror (input_location
,
801 "specialization of %qD in different namespace", tmpl
);
802 inform (DECL_SOURCE_LOCATION (tmpl
),
803 " from definition of %q#D", tmpl
);
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
812 check_explicit_instantiation_namespace (tree spec
)
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns
= decl_namespace_context (spec
);
819 if (!is_nested_namespace (current_namespace
, ns
))
820 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec
, current_namespace
, ns
);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
829 maybe_new_partial_specialization (tree type
)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
837 // template<typename T>
840 // Here, S<T*> is an implicit instantiation of S whose type
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
863 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
864 tree args
= CLASSTYPE_TI_ARGS (type
);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms
)
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr
= current_template_constraints ();
879 if (type
== TREE_TYPE (tmpl
))
881 tree main_constr
= get_constraints (tmpl
);
882 if (equivalent_constraints (type_constr
, main_constr
))
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
891 tree spec_tmpl
= TREE_VALUE (specs
);
892 tree spec_args
= TREE_PURPOSE (specs
);
893 tree spec_constr
= get_constraints (spec_tmpl
);
894 if (comp_template_args (args
, spec_args
)
895 && equivalent_constraints (type_constr
, spec_constr
))
897 specs
= TREE_CHAIN (specs
);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t
= make_class_type (TREE_CODE (type
));
903 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
904 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
911 // Build the corresponding type decl.
912 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
913 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
914 DECL_SOURCE_LOCATION (d
) = input_location
;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
926 maybe_process_partial_specialization (tree type
)
930 if (type
== error_mark_node
)
931 return error_mark_node
;
933 /* A lambda that appears in specialization context is not itself a
935 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
938 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
940 error ("name of class shadows template template parameter %qD",
942 return error_mark_node
;
945 context
= TYPE_CONTEXT (type
);
947 if (TYPE_ALIAS_P (type
))
949 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
951 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo
));
955 error ("explicit specialization of non-template %qT", type
);
956 return error_mark_node
;
958 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t
= maybe_new_partial_specialization (type
))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
974 && !at_namespace_scope_p ())
975 return error_mark_node
;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
978 if (processing_template_decl
)
980 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
981 if (decl
== error_mark_node
)
982 return error_mark_node
;
983 return TREE_TYPE (decl
);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
987 error ("specialization of %qT after instantiation", type
);
988 else if (errorcount
&& !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node
;
996 else if (CLASS_TYPE_P (type
)
997 && !CLASSTYPE_USE_TEMPLATE (type
)
998 && CLASSTYPE_TEMPLATE_INFO (type
)
999 && context
&& CLASS_TYPE_P (context
)
1000 && CLASSTYPE_TEMPLATE_INFO (context
))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1019 && !COMPLETE_TYPE_P (type
))
1022 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl
))
1027 permerror (input_location
,
1028 "specializing %q#T in different namespace", type
);
1029 permerror (DECL_SOURCE_LOCATION (tmpl
),
1030 " from definition of %q#D", tmpl
);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1039 t
; t
= TREE_CHAIN (t
))
1041 tree inst
= TREE_VALUE (t
);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1052 elt
.tmpl
= most_general_template (tmpl
);
1053 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1056 type_specializations
->remove_elt (&elt
);
1059 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1062 = type_specializations
->find_slot (&elt
, INSERT
);
1063 entry
= ggc_alloc
<spec_entry
> ();
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1079 CLASSTYPE_TI_ARGS (type
)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1083 else if (processing_specialization
)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1088 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1089 "of %qD not allowed by ISO C++", type
);
1092 error ("explicit specialization of non-template %qT", type
);
1093 return error_mark_node
;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1105 optimize_specialization_lookup_p (tree tmpl
)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1108 && DECL_CLASS_SCOPE_P (tmpl
)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1119 && !DECL_CONV_FN_P (tmpl
)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1137 verify_unstripped_args (tree args
)
1139 ++processing_template_decl
;
1140 if (!any_dependent_template_arguments_p (args
))
1142 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1143 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1145 tree arg
= TREE_VEC_ELT (inner
, i
);
1146 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1148 else if (TYPE_P (arg
))
1149 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1150 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1154 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1157 --processing_template_decl
;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1175 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1177 if (tmpl
== NULL_TREE
)
1180 if (args
== error_mark_node
)
1183 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl
) == FIELD_DECL
);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args
)
1189 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1191 : template_class_depth (DECL_CONTEXT (tmpl
))));
1194 verify_unstripped_args (args
);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl
))
1201 if (optimize_specialization_lookup_p (tmpl
))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1206 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1207 tree class_specialization
1208 = retrieve_specialization (class_template
, args
, 0);
1209 if (!class_specialization
)
1212 /* Find the instance of TMPL. */
1213 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1214 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1217 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn
) == class_specialization
)
1229 hash_table
<spec_hasher
> *specializations
;
1233 elt
.spec
= NULL_TREE
;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1236 specializations
= type_specializations
;
1238 specializations
= decl_specializations
;
1241 hash
= spec_hasher::hash (&elt
);
1242 found
= specializations
->find_with_hash (&elt
, hash
);
1250 /* Like retrieve_specialization, but for local declarations. */
1253 retrieve_local_specialization (tree tmpl
)
1255 if (local_specializations
== NULL
)
1258 tree
*slot
= local_specializations
->get (tmpl
);
1259 return slot
? *slot
: NULL_TREE
;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl
, tree tmpl
)
1269 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1273 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1279 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1281 for (t
= TREE_TYPE (decl
);
1283 t
= CLASSTYPE_USE_TEMPLATE (t
)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1285 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1296 is_specialization_of_friend (tree decl
, tree friend_decl
)
1298 bool need_template
= true;
1301 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1302 || TREE_CODE (decl
) == TYPE_DECL
);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1307 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl
)
1309 && !DECL_USE_TEMPLATE (friend_decl
))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1313 need_template
= false;
1315 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl
))
1317 need_template
= false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1323 if (is_specialization_of (decl
, friend_decl
))
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1348 && DECL_CLASS_SCOPE_P (decl
)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context
= DECL_CONTEXT (decl
);
1369 tree args
= NULL_TREE
;
1370 int current_depth
= 0;
1372 while (current_depth
< template_depth
)
1374 if (CLASSTYPE_TEMPLATE_INFO (context
))
1376 if (current_depth
== 0)
1377 args
= TYPE_TI_ARGS (context
);
1379 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1382 context
= TYPE_CONTEXT (context
);
1385 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1390 tree friend_args_type
;
1391 tree decl_args_type
;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1395 is_template
= DECL_TEMPLATE_INFO (decl
)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1397 if (need_template
^ is_template
)
1399 else if (is_template
)
1401 /* If both are templates, check template parameter list. */
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1410 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1413 decl_type
= TREE_TYPE (decl
);
1415 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1416 tf_none
, NULL_TREE
);
1417 if (friend_type
== error_mark_node
)
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1427 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1429 friend_args_type
= TREE_CHAIN (friend_args_type
);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1431 decl_args_type
= TREE_CHAIN (decl_args_type
);
1433 return compparms (decl_args_type
, friend_args_type
);
1437 /* DECL is a TYPE_DECL */
1439 tree decl_type
= TREE_TYPE (decl
);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1447 if (need_template
^ is_template
)
1449 else if (is_template
)
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1455 != DECL_NAME (friend_decl
))
1458 /* Now check template parameter list. */
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1467 return (DECL_NAME (decl
)
1468 == DECL_NAME (friend_decl
));
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1483 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1487 spec_entry
**slot
= NULL
;
1490 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1491 || (TREE_CODE (tmpl
) == FIELD_DECL
1492 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1494 if (TREE_CODE (spec
) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec
)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1509 if (optimize_specialization_lookup_p (tmpl
))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn
= retrieve_specialization (tmpl
, args
, 0);
1520 hash
= spec_hasher::hash (&elt
);
1523 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1525 fn
= ((spec_entry
*) *slot
)->spec
;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1537 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1541 if (DECL_ODR_USED (fn
)
1542 || DECL_EXPLICIT_INSTANTIATION (fn
))
1544 error ("specialization of %qD after instantiation",
1546 return error_mark_node
;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn
) = NULL_TREE
;
1575 duplicate_decls (spec
, fn
, is_friend
);
1576 /* The call to duplicate_decls will have applied
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone
, fn
)
1588 DECL_DECLARED_INLINE_P (clone
)
1589 = DECL_DECLARED_INLINE_P (fn
);
1590 DECL_SOURCE_LOCATION (clone
)
1591 = DECL_SOURCE_LOCATION (fn
);
1592 DECL_DELETED_FN (clone
)
1593 = DECL_DELETED_FN (fn
);
1595 check_specialization_namespace (tmpl
);
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1602 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1603 if (dd
== error_mark_node
)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node
;
1607 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1611 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1617 return duplicate_decls (spec
, fn
, is_friend
);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1622 && !check_specialization_namespace (tmpl
))
1623 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1625 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1627 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1628 gcc_assert (tmpl
&& args
&& spec
);
1631 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1632 && PRIMARY_TEMPLATE_P (tmpl
)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1634 || variable_template_p (tmpl
))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1643 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1651 int comparing_specializations
;
1654 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1658 ++comparing_specializations
;
1659 equal
= (e1
->tmpl
== e2
->tmpl
1660 && comp_template_args (e1
->args
, e2
->args
));
1661 if (equal
&& flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1665 && uses_template_parms (e1
->args
))
1667 /* Partial specializations of a variable template can be distinguished by
1669 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1670 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1671 equal
= equivalent_constraints (c1
, c2
);
1673 --comparing_specializations
;
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1681 hash_tmpl_and_args (tree tmpl
, tree args
)
1683 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1684 return iterative_hash_template_arg (args
, val
);
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1691 spec_hasher::hash (spec_entry
*e
)
1693 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1700 iterative_hash_template_arg (tree arg
, hashval_t val
)
1702 unsigned HOST_WIDE_INT i
;
1703 enum tree_code code
;
1706 if (arg
== NULL_TREE
)
1707 return iterative_hash_object (arg
, val
);
1712 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1715 code
= TREE_CODE (arg
);
1716 tclass
= TREE_CODE_CLASS (code
);
1718 val
= iterative_hash_object (code
, val
);
1725 case IDENTIFIER_NODE
:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1730 int i
, len
= TREE_VEC_LENGTH (arg
);
1731 for (i
= 0; i
< len
; ++i
)
1732 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1736 case TYPE_PACK_EXPANSION
:
1737 case EXPR_PACK_EXPANSION
:
1738 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1741 case TYPE_ARGUMENT_PACK
:
1742 case NONTYPE_ARGUMENT_PACK
:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1746 for (; arg
; arg
= TREE_CHAIN (arg
))
1747 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1751 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1752 val
= iterative_hash_template_arg (*iter
, val
);
1758 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1761 val
= iterative_hash_template_arg (field
, val
);
1762 val
= iterative_hash_template_arg (value
, val
);
1768 if (!DECL_ARTIFICIAL (arg
))
1770 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1771 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1773 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1779 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1782 case TEMPLATE_PARM_INDEX
:
1783 val
= iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1785 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1789 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1790 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1794 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1800 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1801 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1802 val
= iterative_hash_object (code
, val
);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1806 /* A lambda can't appear in a template arg, but don't crash on
1808 gcc_assert (seen_error ());
1812 case IMPLICIT_CONV_EXPR
:
1813 case STATIC_CAST_EXPR
:
1814 case REINTERPRET_CAST_EXPR
:
1815 case CONST_CAST_EXPR
:
1816 case DYNAMIC_CAST_EXPR
:
1818 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1819 /* Now hash operands as usual. */
1829 if (alias_template_specialization_p (arg
))
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1841 if (TYPE_CANONICAL (arg
))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1844 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1846 /* Otherwise just compare the types during lookup. */
1849 case tcc_declaration
:
1851 return iterative_hash_expr (arg
, val
);
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1856 unsigned n
= cp_tree_operand_length (arg
);
1857 for (i
= 0; i
< n
; ++i
)
1858 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1873 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1878 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1879 elt
.args
= TI_ARGS (tinfo
);
1880 elt
.spec
= NULL_TREE
;
1882 entry
= decl_specializations
->find (&elt
);
1885 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1886 gcc_assert (new_spec
!= NULL_TREE
);
1887 entry
->spec
= new_spec
;
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1898 register_local_specialization (tree spec
, tree tmpl
)
1900 gcc_assert (tmpl
!= spec
);
1901 local_specializations
->put (tmpl
, spec
);
1904 /* TYPE is a class type. Returns true if TYPE is an explicitly
1905 specialized class. */
1908 explicit_class_specialization_p (tree type
)
1910 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1912 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1915 /* Print the list of functions at FNS, going through all the overloads
1916 for each element of the list. Alternatively, FNS can not be a
1917 TREE_LIST, in which case it will be printed together with all the
1920 MORE and *STR should respectively be FALSE and NULL when the function
1921 is called from the outside. They are used internally on recursive
1922 calls. print_candidates manages the two parameters and leaves NULL
1923 in *STR when it ends. */
1926 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1928 if (TREE_CODE (fns
) == TREE_LIST
)
1929 for (; fns
; fns
= TREE_CHAIN (fns
))
1930 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1932 for (lkp_iterator
iter (fns
); iter
;)
1937 const char *pfx
= *str
;
1941 pfx
= _("candidates are:");
1943 pfx
= _("candidate is:");
1944 *str
= get_spaces (pfx
);
1946 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1950 /* Print the list of candidate FNS in an error message. FNS can also
1951 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1954 print_candidates (tree fns
)
1957 print_candidates_1 (fns
, &str
);
1961 /* Get a (possibly) constrained template declaration for the
1962 purpose of ordering candidates. */
1964 get_template_for_ordering (tree list
)
1966 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1967 tree f
= TREE_VALUE (list
);
1968 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1969 return TI_TEMPLATE (ti
);
1973 /* Among candidates having the same signature, return the
1974 most constrained or NULL_TREE if there is no best candidate.
1975 If the signatures of candidates vary (e.g., template
1976 specialization vs. member function), then there can be no
1979 Note that we don't compare constraints on the functions
1980 themselves, but rather those of their templates. */
1982 most_constrained_function (tree candidates
)
1984 // Try to find the best candidate in a first pass.
1985 tree champ
= candidates
;
1986 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1988 int winner
= more_constrained (get_template_for_ordering (champ
),
1989 get_template_for_ordering (c
));
1991 champ
= c
; // The candidate is more constrained
1992 else if (winner
== 0)
1993 return NULL_TREE
; // Neither is more constrained
1996 // Verify that the champ is better than previous candidates.
1997 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
1998 if (!more_constrained (get_template_for_ordering (champ
),
1999 get_template_for_ordering (c
)))
2007 /* Returns the template (one of the functions given by TEMPLATE_ID)
2008 which can be specialized to match the indicated DECL with the
2009 explicit template args given in TEMPLATE_ID. The DECL may be
2010 NULL_TREE if none is available. In that case, the functions in
2011 TEMPLATE_ID are non-members.
2013 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2014 specialization of a member template.
2016 The TEMPLATE_COUNT is the number of references to qualifying
2017 template classes that appeared in the name of the function. See
2018 check_explicit_specialization for a more accurate description.
2020 TSK indicates what kind of template declaration (if any) is being
2021 declared. TSK_TEMPLATE indicates that the declaration given by
2022 DECL, though a FUNCTION_DECL, has template parameters, and is
2023 therefore a template function.
2025 The template args (those explicitly specified and those deduced)
2026 are output in a newly created vector *TARGS_OUT.
2028 If it is impossible to determine the result, an error message is
2029 issued. The error_mark_node is returned to indicate failure. */
2032 determine_specialization (tree template_id
,
2035 int need_member_template
,
2041 tree explicit_targs
;
2042 tree candidates
= NULL_TREE
;
2044 /* A TREE_LIST of templates of which DECL may be a specialization.
2045 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2046 corresponding TREE_PURPOSE is the set of template arguments that,
2047 when used to instantiate the template, would produce a function
2048 with the signature of DECL. */
2049 tree templates
= NULL_TREE
;
2051 cp_binding_level
*b
;
2053 *targs_out
= NULL_TREE
;
2055 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2056 return error_mark_node
;
2058 /* We shouldn't be specializing a member template of an
2059 unspecialized class template; we already gave an error in
2060 check_specialization_scope, now avoid crashing. */
2061 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2062 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2064 gcc_assert (errorcount
);
2065 return error_mark_node
;
2068 fns
= TREE_OPERAND (template_id
, 0);
2069 explicit_targs
= TREE_OPERAND (template_id
, 1);
2071 if (fns
== error_mark_node
)
2072 return error_mark_node
;
2074 /* Check for baselinks. */
2075 if (BASELINK_P (fns
))
2076 fns
= BASELINK_FUNCTIONS (fns
);
2078 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2080 error ("%qD is not a function template", fns
);
2081 return error_mark_node
;
2083 else if (VAR_P (decl
) && !variable_template_p (fns
))
2085 error ("%qD is not a variable template", fns
);
2086 return error_mark_node
;
2089 /* Count the number of template headers specified for this
2092 for (b
= current_binding_level
;
2093 b
->kind
== sk_template_parms
;
2097 tree orig_fns
= fns
;
2099 if (variable_template_p (fns
))
2101 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2102 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2103 tf_warning_or_error
,
2104 /*req_all*/true, /*use_defarg*/true);
2105 if (targs
!= error_mark_node
)
2106 templates
= tree_cons (targs
, fns
, templates
);
2108 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2112 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2114 tree decl_arg_types
;
2118 /* In case of explicit specialization, we need to check if
2119 the number of template headers appearing in the specialization
2120 is correct. This is usually done in check_explicit_specialization,
2121 but the check done there cannot be exhaustive when specializing
2122 member functions. Consider the following code:
2124 template <> void A<int>::f(int);
2125 template <> template <> void A<int>::f(int);
2127 Assuming that A<int> is not itself an explicit specialization
2128 already, the first line specializes "f" which is a non-template
2129 member function, whilst the second line specializes "f" which
2130 is a template member function. So both lines are syntactically
2131 correct, and check_explicit_specialization does not reject
2134 Here, we can do better, as we are matching the specialization
2135 against the declarations. We count the number of template
2136 headers, and we check if they match TEMPLATE_COUNT + 1
2137 (TEMPLATE_COUNT is the number of qualifying template classes,
2138 plus there must be another header for the member template
2141 Notice that if header_count is zero, this is not a
2142 specialization but rather a template instantiation, so there
2143 is no check we can perform here. */
2144 if (header_count
&& header_count
!= template_count
+ 1)
2147 /* Check that the number of template arguments at the
2148 innermost level for DECL is the same as for FN. */
2149 if (current_binding_level
->kind
== sk_template_parms
2150 && !current_binding_level
->explicit_spec_p
2151 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2152 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2153 (current_template_parms
))))
2156 /* DECL might be a specialization of FN. */
2157 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2158 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2160 /* For a non-static member function, we need to make sure
2161 that the const qualification is the same. Since
2162 get_bindings does not try to merge the "this" parameter,
2163 we must do the comparison explicitly. */
2164 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2165 && !same_type_p (TREE_VALUE (fn_arg_types
),
2166 TREE_VALUE (decl_arg_types
)))
2169 /* Skip the "this" parameter and, for constructors of
2170 classes with virtual bases, the VTT parameter. A
2171 full specialization of a constructor will have a VTT
2172 parameter, but a template never will. */
2174 = skip_artificial_parms_for (decl
, decl_arg_types
);
2176 = skip_artificial_parms_for (fn
, fn_arg_types
);
2178 /* Function templates cannot be specializations; there are
2179 no partial specializations of functions. Therefore, if
2180 the type of DECL does not match FN, there is no
2183 Note that it should never be the case that we have both
2184 candidates added here, and for regular member functions
2186 if (tsk
== tsk_template
)
2188 if (compparms (fn_arg_types
, decl_arg_types
))
2189 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2193 /* See whether this function might be a specialization of this
2194 template. Suppress access control because we might be trying
2195 to make this specialization a friend, and we have already done
2196 access control for the declaration of the specialization. */
2197 push_deferring_access_checks (dk_no_check
);
2198 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2199 pop_deferring_access_checks ();
2202 /* We cannot deduce template arguments that when used to
2203 specialize TMPL will produce DECL. */
2206 if (uses_template_parms (targs
))
2207 /* We deduced something involving 'auto', which isn't a valid
2208 template argument. */
2211 /* Remove, from the set of candidates, all those functions
2212 whose constraints are not satisfied. */
2213 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2216 // Then, try to form the new function type.
2217 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2218 if (insttype
== error_mark_node
)
2221 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2222 if (!compparms (fn_arg_types
, decl_arg_types
))
2225 /* Save this template, and the arguments deduced. */
2226 templates
= tree_cons (targs
, fn
, templates
);
2228 else if (need_member_template
)
2229 /* FN is an ordinary member function, and we need a
2230 specialization of a member template. */
2232 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2233 /* We can get IDENTIFIER_NODEs here in certain erroneous
2236 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2237 /* This is just an ordinary non-member function. Nothing can
2238 be a specialization of that. */
2240 else if (DECL_ARTIFICIAL (fn
))
2241 /* Cannot specialize functions that are created implicitly. */
2245 tree decl_arg_types
;
2247 /* This is an ordinary member function. However, since
2248 we're here, we can assume its enclosing class is a
2249 template class. For example,
2251 template <typename T> struct S { void f(); };
2252 template <> void S<int>::f() {}
2254 Here, S<int>::f is a non-template, but S<int> is a
2255 template class. If FN has the same type as DECL, we
2256 might be in business. */
2258 if (!DECL_TEMPLATE_INFO (fn
))
2259 /* Its enclosing class is an explicit specialization
2260 of a template class. This is not a candidate. */
2263 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2264 TREE_TYPE (TREE_TYPE (fn
))))
2265 /* The return types differ. */
2268 /* Adjust the type of DECL in case FN is a static member. */
2269 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2270 if (DECL_STATIC_FUNCTION_P (fn
)
2271 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2272 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2274 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2278 // If the deduced arguments do not satisfy the constraints,
2279 // this is not a candidate.
2280 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2283 // Add the candidate.
2284 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2288 if (templates
&& TREE_CHAIN (templates
))
2294 It is possible for a specialization with a given function
2295 signature to be instantiated from more than one function
2296 template. In such cases, explicit specification of the
2297 template arguments must be used to uniquely identify the
2298 function template specialization being specialized.
2300 Note that here, there's no suggestion that we're supposed to
2301 determine which of the candidate templates is most
2302 specialized. However, we, also have:
2306 Partial ordering of overloaded function template
2307 declarations is used in the following contexts to select
2308 the function template to which a function template
2309 specialization refers:
2311 -- when an explicit specialization refers to a function
2314 So, we do use the partial ordering rules, at least for now.
2315 This extension can only serve to make invalid programs valid,
2316 so it's safe. And, there is strong anecdotal evidence that
2317 the committee intended the partial ordering rules to apply;
2318 the EDG front end has that behavior, and John Spicer claims
2319 that the committee simply forgot to delete the wording in
2320 [temp.expl.spec]. */
2321 tree tmpl
= most_specialized_instantiation (templates
);
2322 if (tmpl
!= error_mark_node
)
2325 TREE_CHAIN (templates
) = NULL_TREE
;
2329 // Concepts allows multiple declarations of member functions
2330 // with the same signature. Like above, we need to rely on
2331 // on the partial ordering of those candidates to determine which
2333 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2335 if (tree cand
= most_constrained_function (candidates
))
2338 TREE_CHAIN (cand
) = NULL_TREE
;
2342 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2344 error ("template-id %qD for %q+D does not match any template "
2345 "declaration", template_id
, decl
);
2346 if (header_count
&& header_count
!= template_count
+ 1)
2347 inform (input_location
, "saw %d %<template<>%>, need %d for "
2348 "specializing a member function template",
2349 header_count
, template_count
+ 1);
2351 print_candidates (orig_fns
);
2352 return error_mark_node
;
2354 else if ((templates
&& TREE_CHAIN (templates
))
2355 || (candidates
&& TREE_CHAIN (candidates
))
2356 || (templates
&& candidates
))
2358 error ("ambiguous template specialization %qD for %q+D",
2360 candidates
= chainon (candidates
, templates
);
2361 print_candidates (candidates
);
2362 return error_mark_node
;
2365 /* We have one, and exactly one, match. */
2368 tree fn
= TREE_VALUE (candidates
);
2369 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2371 // Propagate the candidate's constraints to the declaration.
2372 set_constraints (decl
, get_constraints (fn
));
2374 /* DECL is a re-declaration or partial instantiation of a template
2376 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2378 /* It was a specialization of an ordinary member function in a
2380 return DECL_TI_TEMPLATE (fn
);
2383 /* It was a specialization of a template. */
2384 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2385 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2387 *targs_out
= copy_node (targs
);
2388 SET_TMPL_ARGS_LEVEL (*targs_out
,
2389 TMPL_ARGS_DEPTH (*targs_out
),
2390 TREE_PURPOSE (templates
));
2393 *targs_out
= TREE_PURPOSE (templates
);
2394 return TREE_VALUE (templates
);
2397 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2398 but with the default argument values filled in from those in the
2402 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2405 tree new_spec_types
;
2410 if (spec_types
== void_list_node
)
2411 return void_list_node
;
2413 /* Substitute into the rest of the list. */
2415 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2416 TREE_CHAIN (tmpl_types
));
2418 /* Add the default argument for this parameter. */
2419 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2420 TREE_VALUE (spec_types
),
2424 /* DECL is an explicit specialization. Replicate default arguments
2425 from the template it specializes. (That way, code like:
2427 template <class T> void f(T = 3);
2428 template <> void f(double);
2431 works, as required.) An alternative approach would be to look up
2432 the correct default arguments at the call-site, but this approach
2433 is consistent with how implicit instantiations are handled. */
2436 copy_default_args_to_explicit_spec (tree decl
)
2441 tree new_spec_types
;
2445 tree object_type
= NULL_TREE
;
2446 tree in_charge
= NULL_TREE
;
2447 tree vtt
= NULL_TREE
;
2449 /* See if there's anything we need to do. */
2450 tmpl
= DECL_TI_TEMPLATE (decl
);
2451 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2452 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2453 if (TREE_PURPOSE (t
))
2458 old_type
= TREE_TYPE (decl
);
2459 spec_types
= TYPE_ARG_TYPES (old_type
);
2461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2463 /* Remove the this pointer, but remember the object's type for
2465 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2466 spec_types
= TREE_CHAIN (spec_types
);
2467 tmpl_types
= TREE_CHAIN (tmpl_types
);
2469 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2471 /* DECL may contain more parameters than TMPL due to the extra
2472 in-charge parameter in constructors and destructors. */
2473 in_charge
= spec_types
;
2474 spec_types
= TREE_CHAIN (spec_types
);
2476 if (DECL_HAS_VTT_PARM_P (decl
))
2479 spec_types
= TREE_CHAIN (spec_types
);
2483 /* Compute the merged default arguments. */
2485 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2487 /* Compute the new FUNCTION_TYPE. */
2491 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2496 /* Put the in-charge parameter back. */
2497 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2498 TREE_VALUE (in_charge
),
2501 new_type
= build_method_type_directly (object_type
,
2502 TREE_TYPE (old_type
),
2506 new_type
= build_function_type (TREE_TYPE (old_type
),
2508 new_type
= cp_build_type_attribute_variant (new_type
,
2509 TYPE_ATTRIBUTES (old_type
));
2510 new_type
= build_exception_variant (new_type
,
2511 TYPE_RAISES_EXCEPTIONS (old_type
));
2513 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2514 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2516 TREE_TYPE (decl
) = new_type
;
2519 /* Return the number of template headers we expect to see for a definition
2520 or specialization of CTYPE or one of its non-template members. */
2523 num_template_headers_for_class (tree ctype
)
2525 int num_templates
= 0;
2527 while (ctype
&& CLASS_TYPE_P (ctype
))
2529 /* You're supposed to have one `template <...>' for every
2530 template class, but you don't need one for a full
2531 specialization. For example:
2533 template <class T> struct S{};
2534 template <> struct S<int> { void f(); };
2535 void S<int>::f () {}
2537 is correct; there shouldn't be a `template <>' for the
2538 definition of `S<int>::f'. */
2539 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2540 /* If CTYPE does not have template information of any
2541 kind, then it is not a template, nor is it nested
2542 within a template. */
2544 if (explicit_class_specialization_p (ctype
))
2546 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2549 ctype
= TYPE_CONTEXT (ctype
);
2552 return num_templates
;
2555 /* Do a simple sanity check on the template headers that precede the
2556 variable declaration DECL. */
2559 check_template_variable (tree decl
)
2561 tree ctx
= CP_DECL_CONTEXT (decl
);
2562 int wanted
= num_template_headers_for_class (ctx
);
2563 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2566 if (cxx_dialect
< cxx14
)
2567 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2568 "variable templates only available with "
2569 "-std=c++14 or -std=gnu++14");
2571 // Namespace-scope variable templates should have a template header.
2574 if (template_header_count
> wanted
)
2576 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2577 "too many template headers for %qD "
2580 if (warned
&& CLASS_TYPE_P (ctx
)
2581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2582 inform (DECL_SOURCE_LOCATION (decl
),
2583 "members of an explicitly specialized class are defined "
2584 "without a template header");
2588 /* An explicit specialization whose declarator-id or class-head-name is not
2589 qualified shall be declared in the nearest enclosing namespace of the
2590 template, or, if the namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set.
2593 If the name declared in the explicit instantiation is an unqualified name,
2594 the explicit instantiation shall appear in the namespace where its template
2595 is declared or, if that namespace is inline (7.3.1), any namespace from its
2596 enclosing namespace set. */
2599 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2601 tree tmpl
= most_general_template (t
);
2602 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2603 && !is_nested_namespace (current_namespace
,
2604 CP_DECL_CONTEXT (tmpl
), true))
2606 if (processing_specialization
)
2607 permerror (loc
, "explicit specialization of %qD outside its "
2608 "namespace must use a nested-name-specifier", tmpl
);
2609 else if (processing_explicit_instantiation
2610 && cxx_dialect
>= cxx11
)
2611 /* This was allowed in C++98, so only pedwarn. */
2612 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2613 "outside its namespace must use a nested-name-"
2618 /* Check to see if the function just declared, as indicated in
2619 DECLARATOR, and in DECL, is a specialization of a function
2620 template. We may also discover that the declaration is an explicit
2621 instantiation at this point.
2623 Returns DECL, or an equivalent declaration that should be used
2624 instead if all goes well. Issues an error message if something is
2625 amiss. Returns error_mark_node if the error is not easily
2628 FLAGS is a bitmask consisting of the following flags:
2630 2: The function has a definition.
2631 4: The function is a friend.
2633 The TEMPLATE_COUNT is the number of references to qualifying
2634 template classes that appeared in the name of the function. For
2637 template <class T> struct S { void f(); };
2640 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2641 classes are not counted in the TEMPLATE_COUNT, so that in
2643 template <class T> struct S {};
2644 template <> struct S<int> { void f(); }
2645 template <> void S<int>::f();
2647 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2648 invalid; there should be no template <>.)
2650 If the function is a specialization, it is marked as such via
2651 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2652 is set up correctly, and it is added to the list of specializations
2653 for that template. */
2656 check_explicit_specialization (tree declarator
,
2661 int have_def
= flags
& 2;
2662 int is_friend
= flags
& 4;
2663 bool is_concept
= flags
& 8;
2664 int specialization
= 0;
2665 int explicit_instantiation
= 0;
2666 int member_specialization
= 0;
2667 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2668 tree dname
= DECL_NAME (decl
);
2673 if (!processing_specialization
)
2676 tsk
= tsk_excessive_parms
;
2679 tsk
= current_tmpl_spec_kind (template_count
);
2684 if (processing_specialization
&& !VAR_P (decl
))
2687 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2689 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2692 /* This could be something like:
2694 template <class T> void f(T);
2695 class S { friend void f<>(int); } */
2699 /* This case handles bogus declarations like template <>
2700 template <class T> void f<int>(); */
2702 error ("template-id %qD in declaration of primary template",
2709 case tsk_invalid_member_spec
:
2710 /* The error has already been reported in
2711 check_specialization_scope. */
2712 return error_mark_node
;
2714 case tsk_invalid_expl_inst
:
2715 error ("template parameter list used in explicit instantiation");
2721 error ("definition provided for explicit instantiation");
2723 explicit_instantiation
= 1;
2726 case tsk_excessive_parms
:
2727 case tsk_insufficient_parms
:
2728 if (tsk
== tsk_excessive_parms
)
2729 error ("too many template parameter lists in declaration of %qD",
2731 else if (template_header_count
)
2732 error("too few template parameter lists in declaration of %qD", decl
);
2734 error("explicit specialization of %qD must be introduced by "
2735 "%<template <>%>", decl
);
2740 error ("explicit specialization declared %<concept%>");
2742 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2743 /* In cases like template<> constexpr bool v = true;
2744 We'll give an error in check_template_variable. */
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2749 member_specialization
= 1;
2755 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2757 /* This case handles bogus declarations like template <>
2758 template <class T> void f<int>(); */
2760 if (!uses_template_parms (declarator
))
2761 error ("template-id %qD in declaration of primary template",
2763 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2765 /* Partial specialization of variable template. */
2766 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2770 else if (cxx_dialect
< cxx14
)
2771 error ("non-type partial specialization %qD "
2772 "is not allowed", declarator
);
2774 error ("non-class, non-variable partial specialization %qD "
2775 "is not allowed", declarator
);
2780 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2781 /* This is a specialization of a member template, without
2782 specialization the containing class. Something like:
2784 template <class T> struct S {
2785 template <class U> void f (U);
2787 template <> template <class U> void S<int>::f(U) {}
2789 That's a specialization -- but of the entire template. */
2797 if ((specialization
|| member_specialization
)
2798 /* This doesn't apply to variable templates. */
2799 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2800 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2802 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2803 for (; t
; t
= TREE_CHAIN (t
))
2804 if (TREE_PURPOSE (t
))
2806 permerror (input_location
,
2807 "default argument specified in explicit specialization");
2812 if (specialization
|| member_specialization
|| explicit_instantiation
)
2814 tree tmpl
= NULL_TREE
;
2815 tree targs
= NULL_TREE
;
2816 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2818 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2819 if (!was_template_id
)
2823 gcc_assert (identifier_p (declarator
));
2828 /* If there is no class context, the explicit instantiation
2829 must be at namespace scope. */
2830 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2832 /* Find the namespace binding, using the declaration
2834 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2836 if (fns
== error_mark_node
)
2837 /* If lookup fails, look for a friend declaration so we can
2838 give a better diagnostic. */
2839 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2840 /*type*/false, /*complain*/true,
2843 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2845 error ("%qD is not a template function", dname
);
2846 fns
= error_mark_node
;
2850 declarator
= lookup_template_function (fns
, NULL_TREE
);
2853 if (declarator
== error_mark_node
)
2854 return error_mark_node
;
2856 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2858 if (!explicit_instantiation
)
2859 /* A specialization in class scope. This is invalid,
2860 but the error will already have been flagged by
2861 check_specialization_scope. */
2862 return error_mark_node
;
2865 /* It's not valid to write an explicit instantiation in
2868 class C { template void f(); }
2870 This case is caught by the parser. However, on
2873 template class C { void f(); };
2875 (which is invalid) we can get here. The error will be
2882 else if (ctype
!= NULL_TREE
2883 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2885 // We'll match variable templates in start_decl.
2889 /* Find the list of functions in ctype that have the same
2890 name as the declared function. */
2891 tree name
= TREE_OPERAND (declarator
, 0);
2893 if (constructor_name_p (name
, ctype
))
2895 if (DECL_CONSTRUCTOR_P (decl
)
2896 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2897 : !CLASSTYPE_DESTRUCTOR (ctype
))
2899 /* From [temp.expl.spec]:
2901 If such an explicit specialization for the member
2902 of a class template names an implicitly-declared
2903 special member function (clause _special_), the
2904 program is ill-formed.
2906 Similar language is found in [temp.explicit]. */
2907 error ("specialization of implicitly-declared special member function");
2908 return error_mark_node
;
2911 name
= DECL_NAME (decl
);
2914 /* For a type-conversion operator, We might be looking for
2915 `operator int' which will be a specialization of
2916 `operator T'. Grab all the conversion operators, and
2917 then select from them. */
2918 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2919 ? conv_op_identifier
: name
);
2921 if (fns
== NULL_TREE
)
2923 error ("no member function %qD declared in %qT", name
, ctype
);
2924 return error_mark_node
;
2927 TREE_OPERAND (declarator
, 0) = fns
;
2930 /* Figure out what exactly is being specialized at this point.
2931 Note that for an explicit instantiation, even one for a
2932 member function, we cannot tell a priori whether the
2933 instantiation is for a member template, or just a member
2934 function of a template class. Even if a member template is
2935 being instantiated, the member template arguments may be
2936 elided if they can be deduced from the rest of the
2938 tmpl
= determine_specialization (declarator
, decl
,
2940 member_specialization
,
2944 if (!tmpl
|| tmpl
== error_mark_node
)
2945 /* We couldn't figure out what this declaration was
2947 return error_mark_node
;
2950 if (TREE_CODE (decl
) == FUNCTION_DECL
2951 && DECL_HIDDEN_FRIEND_P (tmpl
))
2953 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2954 "friend declaration %qD is not visible to "
2955 "explicit specialization", tmpl
))
2956 inform (DECL_SOURCE_LOCATION (tmpl
),
2957 "friend declaration here");
2959 else if (!ctype
&& !is_friend
2960 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2961 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2963 tree gen_tmpl
= most_general_template (tmpl
);
2965 if (explicit_instantiation
)
2967 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2968 is done by do_decl_instantiation later. */
2970 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2971 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2973 if (arg_depth
> parm_depth
)
2975 /* If TMPL is not the most general template (for
2976 example, if TMPL is a friend template that is
2977 injected into namespace scope), then there will
2978 be too many levels of TARGS. Remove some of them
2983 new_targs
= make_tree_vec (parm_depth
);
2984 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2985 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2986 = TREE_VEC_ELT (targs
, i
);
2990 return instantiate_template (tmpl
, targs
, tf_error
);
2993 /* If we thought that the DECL was a member function, but it
2994 turns out to be specializing a static member function,
2995 make DECL a static member function as well. */
2996 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2997 && DECL_STATIC_FUNCTION_P (tmpl
)
2998 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2999 revert_static_member_fn (decl
);
3001 /* If this is a specialization of a member template of a
3002 template class, we want to return the TEMPLATE_DECL, not
3003 the specialization of it. */
3004 if (tsk
== tsk_template
&& !was_template_id
)
3006 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3007 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3008 DECL_INITIAL (result
) = NULL_TREE
;
3012 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3013 DECL_SOURCE_LOCATION (result
)
3014 = DECL_SOURCE_LOCATION (decl
);
3015 /* We want to use the argument list specified in the
3016 definition, not in the original declaration. */
3017 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3018 for (parm
= DECL_ARGUMENTS (result
); parm
;
3019 parm
= DECL_CHAIN (parm
))
3020 DECL_CONTEXT (parm
) = result
;
3022 return register_specialization (tmpl
, gen_tmpl
, targs
,
3026 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3027 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3029 if (was_template_id
)
3030 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3032 /* Inherit default function arguments from the template
3033 DECL is specializing. */
3034 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3035 copy_default_args_to_explicit_spec (decl
);
3037 /* This specialization has the same protection as the
3038 template it specializes. */
3039 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3040 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3042 /* 7.1.1-1 [dcl.stc]
3044 A storage-class-specifier shall not be specified in an
3045 explicit specialization...
3047 The parser rejects these, so unless action is taken here,
3048 explicit function specializations will always appear with
3051 The action recommended by the C++ CWG in response to C++
3052 defect report 605 is to make the storage class and linkage
3053 of the explicit specialization match the templated function:
3055 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3057 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3059 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3060 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3062 /* A concept cannot be specialized. */
3063 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3065 error ("explicit specialization of function concept %qD",
3067 return error_mark_node
;
3070 /* This specialization has the same linkage and visibility as
3071 the function template it specializes. */
3072 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3073 if (! TREE_PUBLIC (decl
))
3075 DECL_INTERFACE_KNOWN (decl
) = 1;
3076 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3078 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3079 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3081 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3082 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3086 /* If DECL is a friend declaration, declared using an
3087 unqualified name, the namespace associated with DECL may
3088 have been set incorrectly. For example, in:
3090 template <typename T> void f(T);
3092 struct S { friend void f<int>(int); }
3095 we will have set the DECL_CONTEXT for the friend
3096 declaration to N, rather than to the global namespace. */
3097 if (DECL_NAMESPACE_SCOPE_P (decl
))
3098 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3100 if (is_friend
&& !have_def
)
3101 /* This is not really a declaration of a specialization.
3102 It's just the name of an instantiation. But, it's not
3103 a request for an instantiation, either. */
3104 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3105 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3106 /* A specialization is not necessarily COMDAT. */
3107 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3108 && DECL_DECLARED_INLINE_P (decl
));
3109 else if (VAR_P (decl
))
3110 DECL_COMDAT (decl
) = false;
3112 /* If this is a full specialization, register it so that we can find
3113 it again. Partial specializations will be registered in
3114 process_partial_specialization. */
3115 if (!processing_template_decl
)
3116 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3119 /* A 'structor should already have clones. */
3120 gcc_assert (decl
== error_mark_node
3121 || variable_template_p (tmpl
)
3122 || !(DECL_CONSTRUCTOR_P (decl
)
3123 || DECL_DESTRUCTOR_P (decl
))
3124 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3131 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3132 parameters. These are represented in the same format used for
3133 DECL_TEMPLATE_PARMS. */
3136 comp_template_parms (const_tree parms1
, const_tree parms2
)
3141 if (parms1
== parms2
)
3144 for (p1
= parms1
, p2
= parms2
;
3145 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3146 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3148 tree t1
= TREE_VALUE (p1
);
3149 tree t2
= TREE_VALUE (p2
);
3152 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3153 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3155 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3158 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3160 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3161 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3163 /* If either of the template parameters are invalid, assume
3164 they match for the sake of error recovery. */
3165 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3168 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3171 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3172 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3173 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3175 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3180 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3181 /* One set of parameters has more parameters lists than the
3188 /* Determine whether PARM is a parameter pack. */
3191 template_parameter_pack_p (const_tree parm
)
3193 /* Determine if we have a non-type template parameter pack. */
3194 if (TREE_CODE (parm
) == PARM_DECL
)
3195 return (DECL_TEMPLATE_PARM_P (parm
)
3196 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3197 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3198 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3200 /* If this is a list of template parameters, we could get a
3201 TYPE_DECL or a TEMPLATE_DECL. */
3202 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3203 parm
= TREE_TYPE (parm
);
3205 /* Otherwise it must be a type template parameter. */
3206 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3207 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3208 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3211 /* Determine if T is a function parameter pack. */
3214 function_parameter_pack_p (const_tree t
)
3216 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3217 return DECL_PACK_P (t
);
3221 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3222 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3225 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3227 if (! primary_func_tmpl_inst
3228 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3229 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3232 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3235 /* Return true iff the function parameter PARAM_DECL was expanded
3236 from the function parameter pack PACK. */
3239 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3241 if (DECL_ARTIFICIAL (param_decl
)
3242 || !function_parameter_pack_p (pack
))
3245 /* The parameter pack and its pack arguments have the same
3247 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3250 /* Determine whether ARGS describes a variadic template args list,
3251 i.e., one that is terminated by a template argument pack. */
3254 template_args_variadic_p (tree args
)
3259 if (args
== NULL_TREE
)
3262 args
= INNERMOST_TEMPLATE_ARGS (args
);
3263 nargs
= TREE_VEC_LENGTH (args
);
3268 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3270 return ARGUMENT_PACK_P (last_parm
);
3273 /* Generate a new name for the parameter pack name NAME (an
3274 IDENTIFIER_NODE) that incorporates its */
3277 make_ith_pack_parameter_name (tree name
, int i
)
3279 /* Munge the name to include the parameter index. */
3280 #define NUMBUF_LEN 128
3281 char numbuf
[NUMBUF_LEN
];
3285 if (name
== NULL_TREE
)
3287 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3288 newname_len
= IDENTIFIER_LENGTH (name
)
3289 + strlen (numbuf
) + 2;
3290 newname
= (char*)alloca (newname_len
);
3291 snprintf (newname
, newname_len
,
3292 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3293 return get_identifier (newname
);
3296 /* Return true if T is a primary function, class or alias template
3297 specialization, not including the template pattern. */
3300 primary_template_specialization_p (const_tree t
)
3305 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3306 return (DECL_LANG_SPECIFIC (t
)
3307 && DECL_USE_TEMPLATE (t
)
3308 && DECL_TEMPLATE_INFO (t
)
3309 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3310 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3311 return (CLASSTYPE_TEMPLATE_INFO (t
)
3312 && CLASSTYPE_USE_TEMPLATE (t
)
3313 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3314 else if (alias_template_specialization_p (t
))
3319 /* Return true if PARM is a template template parameter. */
3322 template_template_parameter_p (const_tree parm
)
3324 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3327 /* Return true iff PARM is a DECL representing a type template
3331 template_type_parameter_p (const_tree parm
)
3334 && (TREE_CODE (parm
) == TYPE_DECL
3335 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3336 && DECL_TEMPLATE_PARM_P (parm
));
3339 /* Return the template parameters of T if T is a
3340 primary template instantiation, NULL otherwise. */
3343 get_primary_template_innermost_parameters (const_tree t
)
3345 tree parms
= NULL
, template_info
= NULL
;
3347 if ((template_info
= get_template_info (t
))
3348 && primary_template_specialization_p (t
))
3349 parms
= INNERMOST_TEMPLATE_PARMS
3350 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3355 /* Return the template parameters of the LEVELth level from the full list
3356 of template parameters PARMS. */
3359 get_template_parms_at_level (tree parms
, int level
)
3363 || TREE_CODE (parms
) != TREE_LIST
3364 || level
> TMPL_PARMS_DEPTH (parms
))
3367 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3368 if (TMPL_PARMS_DEPTH (p
) == level
)
3374 /* Returns the template arguments of T if T is a template instantiation,
3378 get_template_innermost_arguments (const_tree t
)
3380 tree args
= NULL
, template_info
= NULL
;
3382 if ((template_info
= get_template_info (t
))
3383 && TI_ARGS (template_info
))
3384 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3389 /* Return the argument pack elements of T if T is a template argument pack,
3393 get_template_argument_pack_elems (const_tree t
)
3395 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3396 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3399 return ARGUMENT_PACK_ARGS (t
);
3402 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3403 ARGUMENT_PACK_SELECT represents. */
3406 argument_pack_select_arg (tree t
)
3408 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3409 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3411 /* If the selected argument is an expansion E, that most likely means we were
3412 called from gen_elem_of_pack_expansion_instantiation during the
3413 substituting of an argument pack (of which the Ith element is a pack
3414 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3415 In this case, the Ith element resulting from this substituting is going to
3416 be a pack expansion, which pattern is the pattern of E. Let's return the
3417 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3418 resulting pack expansion from it. */
3419 if (PACK_EXPANSION_P (arg
))
3421 /* Make sure we aren't throwing away arg info. */
3422 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3423 arg
= PACK_EXPANSION_PATTERN (arg
);
3430 /* True iff FN is a function representing a built-in variadic parameter
3434 builtin_pack_fn_p (tree fn
)
3437 || TREE_CODE (fn
) != FUNCTION_DECL
3438 || !DECL_IS_BUILTIN (fn
))
3441 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3447 /* True iff CALL is a call to a function representing a built-in variadic
3451 builtin_pack_call_p (tree call
)
3453 if (TREE_CODE (call
) != CALL_EXPR
)
3455 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3458 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3461 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3464 tree ohi
= CALL_EXPR_ARG (call
, 0);
3465 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3466 false/*fn*/, true/*int_cst*/);
3468 if (value_dependent_expression_p (hi
))
3472 call
= copy_node (call
);
3473 CALL_EXPR_ARG (call
, 0) = hi
;
3475 tree ex
= make_pack_expansion (call
, complain
);
3476 tree vec
= make_tree_vec (1);
3477 TREE_VEC_ELT (vec
, 0) = ex
;
3482 hi
= cxx_constant_value (hi
);
3483 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3485 /* Calculate the largest value of len that won't make the size of the vec
3486 overflow an int. The compiler will exceed resource limits long before
3487 this, but it seems a decent place to diagnose. */
3488 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3490 if (len
< 0 || len
> max
)
3492 if ((complain
& tf_error
)
3493 && hi
!= error_mark_node
)
3494 error ("argument to __integer_pack must be between 0 and %d", max
);
3495 return error_mark_node
;
3498 tree vec
= make_tree_vec (len
);
3500 for (int i
= 0; i
< len
; ++i
)
3501 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3507 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3511 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3514 if (!builtin_pack_call_p (call
))
3517 tree fn
= CALL_EXPR_FN (call
);
3519 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3520 return expand_integer_pack (call
, args
, complain
, in_decl
);
3525 /* Structure used to track the progress of find_parameter_packs_r. */
3526 struct find_parameter_pack_data
3528 /* TREE_LIST that will contain all of the parameter packs found by
3530 tree
* parameter_packs
;
3532 /* Set of AST nodes that have been visited by the traversal. */
3533 hash_set
<tree
> *visited
;
3535 /* True iff we're making a type pack expansion. */
3536 bool type_pack_expansion_p
;
3539 /* Identifies all of the argument packs that occur in a template
3540 argument and appends them to the TREE_LIST inside DATA, which is a
3541 find_parameter_pack_data structure. This is a subroutine of
3542 make_pack_expansion and uses_parameter_packs. */
3544 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3547 struct find_parameter_pack_data
* ppd
=
3548 (struct find_parameter_pack_data
*)data
;
3549 bool parameter_pack_p
= false;
3551 /* Handle type aliases/typedefs. */
3552 if (TYPE_ALIAS_P (t
))
3554 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3555 cp_walk_tree (&TI_ARGS (tinfo
),
3556 &find_parameter_packs_r
,
3562 /* Identify whether this is a parameter pack or not. */
3563 switch (TREE_CODE (t
))
3565 case TEMPLATE_PARM_INDEX
:
3566 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3567 parameter_pack_p
= true;
3570 case TEMPLATE_TYPE_PARM
:
3571 t
= TYPE_MAIN_VARIANT (t
);
3573 case TEMPLATE_TEMPLATE_PARM
:
3574 /* If the placeholder appears in the decl-specifier-seq of a function
3575 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3576 is a pack expansion, the invented template parameter is a template
3578 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3579 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3580 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3581 parameter_pack_p
= true;
3586 if (DECL_PACK_P (t
))
3588 /* We don't want to walk into the type of a PARM_DECL,
3589 because we don't want to see the type parameter pack. */
3591 parameter_pack_p
= true;
3596 if (DECL_PACK_P (t
))
3598 /* We don't want to walk into the type of a variadic capture proxy,
3599 because we don't want to see the type parameter pack. */
3601 parameter_pack_p
= true;
3603 else if (variable_template_specialization_p (t
))
3605 cp_walk_tree (&DECL_TI_ARGS (t
),
3606 find_parameter_packs_r
,
3613 if (builtin_pack_call_p (t
))
3614 parameter_pack_p
= true;
3618 parameter_pack_p
= true;
3621 /* Not a parameter pack. */
3625 if (parameter_pack_p
)
3627 /* Add this parameter pack to the list. */
3628 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3632 cp_walk_tree (&TYPE_CONTEXT (t
),
3633 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3635 /* This switch statement will return immediately if we don't find a
3637 switch (TREE_CODE (t
))
3639 case TEMPLATE_PARM_INDEX
:
3642 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3643 /* Check the template itself. */
3644 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3645 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3646 /* Check the template arguments. */
3647 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3652 case TEMPLATE_TYPE_PARM
:
3653 case TEMPLATE_TEMPLATE_PARM
:
3660 /* Ignore the declaration of a capture proxy for a parameter pack. */
3661 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3666 if (TYPE_PTRMEMFUNC_P (t
))
3672 if (TYPE_TEMPLATE_INFO (t
))
3673 cp_walk_tree (&TYPE_TI_ARGS (t
),
3674 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3680 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3685 cp_walk_tree (&TREE_TYPE (t
),
3686 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3690 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3695 case TYPE_PACK_EXPANSION
:
3696 case EXPR_PACK_EXPANSION
:
3701 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3706 case IDENTIFIER_NODE
:
3707 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3714 /* Look at explicit captures. */
3715 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3716 cap
; cap
= TREE_CHAIN (cap
))
3717 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3719 /* Since we defer implicit capture, look in the body as well. */
3720 tree fn
= lambda_function (t
);
3721 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3729 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3730 type_pack_expansion_p to false so that any placeholders
3731 within the expression don't get marked as parameter packs. */
3732 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3733 ppd
->type_pack_expansion_p
= false;
3734 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3736 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3748 /* Determines if the expression or type T uses any parameter packs. */
3750 uses_parameter_packs (tree t
)
3752 tree parameter_packs
= NULL_TREE
;
3753 struct find_parameter_pack_data ppd
;
3754 ppd
.parameter_packs
= ¶meter_packs
;
3755 ppd
.visited
= new hash_set
<tree
>;
3756 ppd
.type_pack_expansion_p
= false;
3757 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3759 return parameter_packs
!= NULL_TREE
;
3762 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3763 representation a base-class initializer into a parameter pack
3764 expansion. If all goes well, the resulting node will be an
3765 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3768 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3771 tree parameter_packs
= NULL_TREE
;
3772 bool for_types
= false;
3773 struct find_parameter_pack_data ppd
;
3775 if (!arg
|| arg
== error_mark_node
)
3778 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3780 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3781 class initializer. In this case, the TREE_PURPOSE will be a
3782 _TYPE node (representing the base class expansion we're
3783 initializing) and the TREE_VALUE will be a TREE_LIST
3784 containing the initialization arguments.
3786 The resulting expansion looks somewhat different from most
3787 expansions. Rather than returning just one _EXPANSION, we
3788 return a TREE_LIST whose TREE_PURPOSE is a
3789 TYPE_PACK_EXPANSION containing the bases that will be
3790 initialized. The TREE_VALUE will be identical to the
3791 original TREE_VALUE, which is a list of arguments that will
3792 be passed to each base. We do not introduce any new pack
3793 expansion nodes into the TREE_VALUE (although it is possible
3794 that some already exist), because the TREE_PURPOSE and
3795 TREE_VALUE all need to be expanded together with the same
3796 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3797 resulting TREE_PURPOSE will mention the parameter packs in
3798 both the bases and the arguments to the bases. */
3801 tree parameter_packs
= NULL_TREE
;
3803 /* Determine which parameter packs will be used by the base
3805 ppd
.visited
= new hash_set
<tree
>;
3806 ppd
.parameter_packs
= ¶meter_packs
;
3807 ppd
.type_pack_expansion_p
= true;
3808 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3809 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3812 if (parameter_packs
== NULL_TREE
)
3814 if (complain
& tf_error
)
3815 error ("base initializer expansion %qT contains no parameter packs",
3818 return error_mark_node
;
3821 if (TREE_VALUE (arg
) != void_type_node
)
3823 /* Collect the sets of parameter packs used in each of the
3824 initialization arguments. */
3825 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3827 /* Determine which parameter packs will be expanded in this
3829 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3836 /* Create the pack expansion type for the base type. */
3837 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3838 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3839 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3840 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3842 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3843 they will rarely be compared to anything. */
3844 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3846 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3849 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3852 /* Build the PACK_EXPANSION_* node. */
3854 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3855 : make_node (EXPR_PACK_EXPANSION
);
3856 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3857 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3859 /* Propagate type and const-expression information. */
3860 TREE_TYPE (result
) = TREE_TYPE (arg
);
3861 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3862 /* Mark this read now, since the expansion might be length 0. */
3863 mark_exp_read (arg
);
3866 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3867 they will rarely be compared to anything. */
3868 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3870 /* Determine which parameter packs will be expanded. */
3871 ppd
.parameter_packs
= ¶meter_packs
;
3872 ppd
.visited
= new hash_set
<tree
>;
3873 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3874 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3877 /* Make sure we found some parameter packs. */
3878 if (parameter_packs
== NULL_TREE
)
3880 if (complain
& tf_error
)
3883 error ("expansion pattern %qT contains no argument packs", arg
);
3885 error ("expansion pattern %qE contains no argument packs", arg
);
3887 return error_mark_node
;
3889 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3891 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3896 /* Checks T for any "bare" parameter packs, which have not yet been
3897 expanded, and issues an error if any are found. This operation can
3898 only be done on full expressions or types (e.g., an expression
3899 statement, "if" condition, etc.), because we could have expressions like:
3901 foo(f(g(h(args)))...)
3903 where "args" is a parameter pack. check_for_bare_parameter_packs
3904 should not be called for the subexpressions args, h(args),
3905 g(h(args)), or f(g(h(args))), because we would produce erroneous
3908 Returns TRUE and emits an error if there were bare parameter packs,
3909 returns FALSE otherwise. */
3911 check_for_bare_parameter_packs (tree t
)
3913 tree parameter_packs
= NULL_TREE
;
3914 struct find_parameter_pack_data ppd
;
3916 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3919 /* A lambda might use a parameter pack from the containing context. */
3920 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
3923 if (TREE_CODE (t
) == TYPE_DECL
)
3926 ppd
.parameter_packs
= ¶meter_packs
;
3927 ppd
.visited
= new hash_set
<tree
>;
3928 ppd
.type_pack_expansion_p
= false;
3929 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3932 if (parameter_packs
)
3934 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3935 error_at (loc
, "parameter packs not expanded with %<...%>:");
3936 while (parameter_packs
)
3938 tree pack
= TREE_VALUE (parameter_packs
);
3939 tree name
= NULL_TREE
;
3941 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3942 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3943 name
= TYPE_NAME (pack
);
3944 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3945 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3946 else if (TREE_CODE (pack
) == CALL_EXPR
)
3947 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3949 name
= DECL_NAME (pack
);
3952 inform (loc
, " %qD", name
);
3954 inform (loc
, " <anonymous>");
3956 parameter_packs
= TREE_CHAIN (parameter_packs
);
3965 /* Expand any parameter packs that occur in the template arguments in
3968 expand_template_argument_pack (tree args
)
3970 if (args
== error_mark_node
)
3971 return error_mark_node
;
3973 tree result_args
= NULL_TREE
;
3974 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3975 int num_result_args
= -1;
3976 int non_default_args_count
= -1;
3978 /* First, determine if we need to expand anything, and the number of
3979 slots we'll need. */
3980 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3982 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3983 if (arg
== NULL_TREE
)
3985 if (ARGUMENT_PACK_P (arg
))
3987 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3988 if (num_result_args
< 0)
3989 num_result_args
= in_arg
+ num_packed
;
3991 num_result_args
+= num_packed
;
3995 if (num_result_args
>= 0)
4000 /* If no expansion is necessary, we're done. */
4001 if (num_result_args
< 0)
4004 /* Expand arguments. */
4005 result_args
= make_tree_vec (num_result_args
);
4006 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4007 non_default_args_count
=
4008 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4009 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4011 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4012 if (ARGUMENT_PACK_P (arg
))
4014 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4015 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4016 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4017 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4018 if (non_default_args_count
> 0)
4019 non_default_args_count
+= num_packed
- 1;
4023 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4027 if (non_default_args_count
>= 0)
4028 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4032 /* Checks if DECL shadows a template parameter.
4034 [temp.local]: A template-parameter shall not be redeclared within its
4035 scope (including nested scopes).
4037 Emits an error and returns TRUE if the DECL shadows a parameter,
4038 returns FALSE otherwise. */
4041 check_template_shadow (tree decl
)
4045 /* If we're not in a template, we can't possibly shadow a template
4047 if (!current_template_parms
)
4050 /* Figure out what we're shadowing. */
4051 decl
= OVL_FIRST (decl
);
4052 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4054 /* If there's no previous binding for this name, we're not shadowing
4055 anything, let alone a template parameter. */
4059 /* If we're not shadowing a template parameter, we're done. Note
4060 that OLDDECL might be an OVERLOAD (or perhaps even an
4061 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4063 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4066 /* We check for decl != olddecl to avoid bogus errors for using a
4067 name inside a class. We check TPFI to avoid duplicate errors for
4068 inline member templates. */
4070 || (DECL_TEMPLATE_PARM_P (decl
)
4071 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4074 /* Don't complain about the injected class name, as we've already
4075 complained about the class itself. */
4076 if (DECL_SELF_REFERENCE_P (decl
))
4079 if (DECL_TEMPLATE_PARM_P (decl
))
4080 error ("declaration of template parameter %q+D shadows "
4081 "template parameter", decl
);
4083 error ("declaration of %q+#D shadows template parameter", decl
);
4084 inform (DECL_SOURCE_LOCATION (olddecl
),
4085 "template parameter %qD declared here", olddecl
);
4089 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4090 ORIG_LEVEL, DECL, and TYPE. */
4093 build_template_parm_index (int index
,
4099 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4100 TEMPLATE_PARM_IDX (t
) = index
;
4101 TEMPLATE_PARM_LEVEL (t
) = level
;
4102 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4103 TEMPLATE_PARM_DECL (t
) = decl
;
4104 TREE_TYPE (t
) = type
;
4105 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4106 TREE_READONLY (t
) = TREE_READONLY (decl
);
4111 /* Find the canonical type parameter for the given template type
4112 parameter. Returns the canonical type parameter, which may be TYPE
4113 if no such parameter existed. */
4116 canonical_type_parameter (tree type
)
4119 int idx
= TEMPLATE_TYPE_IDX (type
);
4120 if (!canonical_template_parms
)
4121 vec_alloc (canonical_template_parms
, idx
+ 1);
4123 if (canonical_template_parms
->length () <= (unsigned) idx
)
4124 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4126 list
= (*canonical_template_parms
)[idx
];
4127 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4128 list
= TREE_CHAIN (list
);
4131 return TREE_VALUE (list
);
4134 (*canonical_template_parms
)[idx
]
4135 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4140 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4141 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4142 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4143 new one is created. */
4146 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4147 tsubst_flags_t complain
)
4149 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4150 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4151 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4152 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4154 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4157 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4158 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4159 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4160 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4161 DECL_ARTIFICIAL (decl
) = 1;
4162 SET_DECL_TEMPLATE_PARM_P (decl
);
4164 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4165 TEMPLATE_PARM_LEVEL (index
) - levels
,
4166 TEMPLATE_PARM_ORIG_LEVEL (index
),
4168 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4169 TEMPLATE_PARM_PARAMETER_PACK (t
)
4170 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4172 /* Template template parameters need this. */
4173 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4175 DECL_TEMPLATE_RESULT (decl
)
4176 = build_decl (DECL_SOURCE_LOCATION (decl
),
4177 TYPE_DECL
, DECL_NAME (decl
), type
);
4178 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4179 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4180 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4184 return TEMPLATE_PARM_DESCENDANTS (index
);
4187 /* Process information from new template parameter PARM and append it
4188 to the LIST being built. This new parameter is a non-type
4189 parameter iff IS_NON_TYPE is true. This new parameter is a
4190 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4194 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4195 bool is_non_type
, bool is_parameter_pack
)
4200 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4201 tree defval
= TREE_PURPOSE (parm
);
4202 tree constr
= TREE_TYPE (parm
);
4206 tree p
= tree_last (list
);
4208 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4211 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4212 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4214 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4222 parm
= TREE_VALUE (parm
);
4224 SET_DECL_TEMPLATE_PARM_P (parm
);
4226 if (TREE_TYPE (parm
) != error_mark_node
)
4230 The top-level cv-qualifiers on the template-parameter are
4231 ignored when determining its type. */
4232 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4233 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4234 TREE_TYPE (parm
) = error_mark_node
;
4235 else if (uses_parameter_packs (TREE_TYPE (parm
))
4236 && !is_parameter_pack
4237 /* If we're in a nested template parameter list, the template
4238 template parameter could be a parameter pack. */
4239 && processing_template_parmlist
== 1)
4241 /* This template parameter is not a parameter pack, but it
4242 should be. Complain about "bare" parameter packs. */
4243 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4245 /* Recover by calling this a parameter pack. */
4246 is_parameter_pack
= true;
4250 /* A template parameter is not modifiable. */
4251 TREE_CONSTANT (parm
) = 1;
4252 TREE_READONLY (parm
) = 1;
4253 decl
= build_decl (parm_loc
,
4254 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4255 TREE_CONSTANT (decl
) = 1;
4256 TREE_READONLY (decl
) = 1;
4257 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4258 = build_template_parm_index (idx
, processing_template_decl
,
4259 processing_template_decl
,
4260 decl
, TREE_TYPE (parm
));
4262 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4263 = is_parameter_pack
;
4268 parm
= TREE_VALUE (TREE_VALUE (parm
));
4270 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4272 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4273 /* This is for distinguishing between real templates and template
4274 template parameters */
4275 TREE_TYPE (parm
) = t
;
4276 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4281 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4282 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4283 decl
= build_decl (parm_loc
,
4284 TYPE_DECL
, parm
, t
);
4287 TYPE_NAME (t
) = decl
;
4288 TYPE_STUB_DECL (t
) = decl
;
4290 TEMPLATE_TYPE_PARM_INDEX (t
)
4291 = build_template_parm_index (idx
, processing_template_decl
,
4292 processing_template_decl
,
4293 decl
, TREE_TYPE (parm
));
4294 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4295 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4297 DECL_ARTIFICIAL (decl
) = 1;
4298 SET_DECL_TEMPLATE_PARM_P (decl
);
4300 /* Build requirements for the type/template parameter.
4301 This must be done after SET_DECL_TEMPLATE_PARM_P or
4302 process_template_parm could fail. */
4303 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4307 /* Build the parameter node linking the parameter declaration,
4308 its default argument (if any), and its constraints (if any). */
4309 parm
= build_tree_list (defval
, parm
);
4310 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4312 return chainon (list
, parm
);
4315 /* The end of a template parameter list has been reached. Process the
4316 tree list into a parameter vector, converting each parameter into a more
4317 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4321 end_template_parm_list (tree parms
)
4325 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4327 /* Pop the dummy parameter level and add the real one. */
4328 current_template_parms
= TREE_CHAIN (current_template_parms
);
4330 current_template_parms
4331 = tree_cons (size_int (processing_template_decl
),
4332 saved_parmlist
, current_template_parms
);
4334 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4336 next
= TREE_CHAIN (parm
);
4337 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4338 TREE_CHAIN (parm
) = NULL_TREE
;
4341 --processing_template_parmlist
;
4343 return saved_parmlist
;
4346 // Explicitly indicate the end of the template parameter list. We assume
4347 // that the current template parameters have been constructed and/or
4348 // managed explicitly, as when creating new template template parameters
4349 // from a shorthand constraint.
4351 end_template_parm_list ()
4353 --processing_template_parmlist
;
4356 /* end_template_decl is called after a template declaration is seen. */
4359 end_template_decl (void)
4361 reset_specialization ();
4363 if (! processing_template_decl
)
4366 /* This matches the pushlevel in begin_template_parm_list. */
4369 --processing_template_decl
;
4370 current_template_parms
= TREE_CHAIN (current_template_parms
);
4373 /* Takes a TREE_LIST representing a template parameter and convert it
4374 into an argument suitable to be passed to the type substitution
4375 functions. Note that If the TREE_LIST contains an error_mark
4376 node, the returned argument is error_mark_node. */
4379 template_parm_to_arg (tree t
)
4383 || TREE_CODE (t
) != TREE_LIST
)
4386 if (error_operand_p (TREE_VALUE (t
)))
4387 return error_mark_node
;
4391 if (TREE_CODE (t
) == TYPE_DECL
4392 || TREE_CODE (t
) == TEMPLATE_DECL
)
4396 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4398 /* Turn this argument into a TYPE_ARGUMENT_PACK
4399 with a single element, which expands T. */
4400 tree vec
= make_tree_vec (1);
4402 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4404 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4406 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4407 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4412 t
= DECL_INITIAL (t
);
4414 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4416 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4417 with a single element, which expands T. */
4418 tree vec
= make_tree_vec (1);
4420 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4422 t
= convert_from_reference (t
);
4423 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4425 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4426 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4429 t
= convert_from_reference (t
);
4434 /* Given a single level of template parameters (a TREE_VEC), return it
4435 as a set of template arguments. */
4438 template_parms_level_to_args (tree parms
)
4440 tree a
= copy_node (parms
);
4441 TREE_TYPE (a
) = NULL_TREE
;
4442 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4443 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4446 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4451 /* Given a set of template parameters, return them as a set of template
4452 arguments. The template parameters are represented as a TREE_VEC, in
4453 the form documented in cp-tree.h for template arguments. */
4456 template_parms_to_args (tree parms
)
4459 tree args
= NULL_TREE
;
4460 int length
= TMPL_PARMS_DEPTH (parms
);
4463 /* If there is only one level of template parameters, we do not
4464 create a TREE_VEC of TREE_VECs. Instead, we return a single
4465 TREE_VEC containing the arguments. */
4467 args
= make_tree_vec (length
);
4469 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4471 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4474 TREE_VEC_ELT (args
, --l
) = a
;
4482 /* Within the declaration of a template, return the currently active
4483 template parameters as an argument TREE_VEC. */
4486 current_template_args (void)
4488 return template_parms_to_args (current_template_parms
);
4491 /* Update the declared TYPE by doing any lookups which were thought to be
4492 dependent, but are not now that we know the SCOPE of the declarator. */
4495 maybe_update_decl_type (tree orig_type
, tree scope
)
4497 tree type
= orig_type
;
4499 if (type
== NULL_TREE
)
4502 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4503 type
= TREE_TYPE (type
);
4505 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4506 && dependent_type_p (type
)
4507 /* Don't bother building up the args in this case. */
4508 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4510 /* tsubst in the args corresponding to the template parameters,
4511 including auto if present. Most things will be unchanged, but
4512 make_typename_type and tsubst_qualified_id will resolve
4513 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4514 tree args
= current_template_args ();
4515 tree auto_node
= type_uses_auto (type
);
4519 tree auto_vec
= make_tree_vec (1);
4520 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4521 args
= add_to_template_args (args
, auto_vec
);
4523 pushed
= push_scope (scope
);
4524 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4529 if (type
== error_mark_node
)
4532 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4534 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4537 type
= TYPE_NAME (type
);
4542 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4543 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4544 the new template is a member template. */
4547 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4549 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4550 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4551 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4552 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4553 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4558 struct template_parm_data
4560 /* The level of the template parameters we are currently
4564 /* The index of the specialization argument we are currently
4568 /* An array whose size is the number of template parameters. The
4569 elements are nonzero if the parameter has been used in any one
4570 of the arguments processed so far. */
4573 /* An array whose size is the number of template arguments. The
4574 elements are nonzero if the argument makes use of template
4575 parameters of this level. */
4576 int* arg_uses_template_parms
;
4579 /* Subroutine of push_template_decl used to see if each template
4580 parameter in a partial specialization is used in the explicit
4581 argument list. If T is of the LEVEL given in DATA (which is
4582 treated as a template_parm_data*), then DATA->PARMS is marked
4586 mark_template_parm (tree t
, void* data
)
4590 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4592 template_parm_level_and_index (t
, &level
, &idx
);
4594 if (level
== tpd
->level
)
4596 tpd
->parms
[idx
] = 1;
4597 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4600 /* In C++17 the type of a non-type argument is a deduced context. */
4601 if (cxx_dialect
>= cxx17
4602 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4603 for_each_template_parm (TREE_TYPE (t
),
4604 &mark_template_parm
,
4607 /*include_nondeduced_p=*/false);
4609 /* Return zero so that for_each_template_parm will continue the
4610 traversal of the tree; we want to mark *every* template parm. */
4614 /* Process the partial specialization DECL. */
4617 process_partial_specialization (tree decl
)
4619 tree type
= TREE_TYPE (decl
);
4620 tree tinfo
= get_template_info (decl
);
4621 tree maintmpl
= TI_TEMPLATE (tinfo
);
4622 tree specargs
= TI_ARGS (tinfo
);
4623 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4624 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4627 int nargs
= TREE_VEC_LENGTH (inner_args
);
4630 bool did_error_intro
= false;
4631 struct template_parm_data tpd
;
4632 struct template_parm_data tpd2
;
4634 gcc_assert (current_template_parms
);
4636 /* A concept cannot be specialized. */
4637 if (flag_concepts
&& variable_concept_p (maintmpl
))
4639 error ("specialization of variable concept %q#D", maintmpl
);
4640 return error_mark_node
;
4643 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4644 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4646 /* We check that each of the template parameters given in the
4647 partial specialization is used in the argument list to the
4648 specialization. For example:
4650 template <class T> struct S;
4651 template <class T> struct S<T*>;
4653 The second declaration is OK because `T*' uses the template
4654 parameter T, whereas
4656 template <class T> struct S<int>;
4658 is no good. Even trickier is:
4669 The S2<T> declaration is actually invalid; it is a
4670 full-specialization. Of course,
4673 struct S2<T (*)(U)>;
4675 or some such would have been OK. */
4676 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4677 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4678 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4680 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4681 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4682 for (i
= 0; i
< nargs
; ++i
)
4684 tpd
.current_arg
= i
;
4685 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4686 &mark_template_parm
,
4689 /*include_nondeduced_p=*/false);
4691 for (i
= 0; i
< ntparms
; ++i
)
4692 if (tpd
.parms
[i
] == 0)
4694 /* One of the template parms was not used in a deduced context in the
4696 if (!did_error_intro
)
4698 error ("template parameters not deducible in "
4699 "partial specialization:");
4700 did_error_intro
= true;
4703 inform (input_location
, " %qD",
4704 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4707 if (did_error_intro
)
4708 return error_mark_node
;
4710 /* [temp.class.spec]
4712 The argument list of the specialization shall not be identical to
4713 the implicit argument list of the primary template. */
4715 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4716 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4718 || !strictly_subsumes (current_template_constraints (),
4719 get_constraints (maintmpl
))))
4722 error ("partial specialization %q+D does not specialize "
4723 "any template arguments", decl
);
4725 error ("partial specialization %q+D does not specialize any "
4726 "template arguments and is not more constrained than", decl
);
4727 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4730 /* A partial specialization that replaces multiple parameters of the
4731 primary template with a pack expansion is less specialized for those
4733 if (nargs
< DECL_NTPARMS (maintmpl
))
4735 error ("partial specialization is not more specialized than the "
4736 "primary template because it replaces multiple parameters "
4737 "with a pack expansion");
4738 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4739 /* Avoid crash in process_partial_specialization. */
4743 /* If we aren't in a dependent class, we can actually try deduction. */
4744 else if (tpd
.level
== 1
4745 /* FIXME we should be able to handle a partial specialization of a
4746 partial instantiation, but currently we can't (c++/41727). */
4747 && TMPL_ARGS_DEPTH (specargs
) == 1
4748 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4750 if (permerror (input_location
, "partial specialization %qD is not "
4751 "more specialized than", decl
))
4752 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4756 /* [temp.class.spec]
4758 A partially specialized non-type argument expression shall not
4759 involve template parameters of the partial specialization except
4760 when the argument expression is a simple identifier.
4762 The type of a template parameter corresponding to a specialized
4763 non-type argument shall not be dependent on a parameter of the
4766 Also, we verify that pack expansions only occur at the
4767 end of the argument list. */
4768 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4770 for (i
= 0; i
< nargs
; ++i
)
4772 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4773 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4774 tree packed_args
= NULL_TREE
;
4777 if (ARGUMENT_PACK_P (arg
))
4779 /* Extract the arguments from the argument pack. We'll be
4780 iterating over these in the following loop. */
4781 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4782 len
= TREE_VEC_LENGTH (packed_args
);
4785 for (j
= 0; j
< len
; j
++)
4788 /* Get the Jth argument in the parameter pack. */
4789 arg
= TREE_VEC_ELT (packed_args
, j
);
4791 if (PACK_EXPANSION_P (arg
))
4793 /* Pack expansions must come at the end of the
4795 if ((packed_args
&& j
< len
- 1)
4796 || (!packed_args
&& i
< nargs
- 1))
4798 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4799 error ("parameter pack argument %qE must be at the "
4800 "end of the template argument list", arg
);
4802 error ("parameter pack argument %qT must be at the "
4803 "end of the template argument list", arg
);
4807 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4808 /* We only care about the pattern. */
4809 arg
= PACK_EXPANSION_PATTERN (arg
);
4811 if (/* These first two lines are the `non-type' bit. */
4813 && TREE_CODE (arg
) != TEMPLATE_DECL
4814 /* This next two lines are the `argument expression is not just a
4815 simple identifier' condition and also the `specialized
4816 non-type argument' bit. */
4817 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4818 && !(REFERENCE_REF_P (arg
)
4819 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4821 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4822 || (packed_args
&& uses_template_parms (arg
)))
4823 error ("template argument %qE involves template parameter(s)",
4827 /* Look at the corresponding template parameter,
4828 marking which template parameters its type depends
4830 tree type
= TREE_TYPE (parm
);
4834 /* We haven't yet initialized TPD2. Do so now. */
4835 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4836 /* The number of parameters here is the number in the
4837 main template, which, as checked in the assertion
4839 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4841 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4844 /* Mark the template parameters. But this time, we're
4845 looking for the template parameters of the main
4846 template, not in the specialization. */
4847 tpd2
.current_arg
= i
;
4848 tpd2
.arg_uses_template_parms
[i
] = 0;
4849 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4850 for_each_template_parm (type
,
4851 &mark_template_parm
,
4854 /*include_nondeduced_p=*/false);
4856 if (tpd2
.arg_uses_template_parms
[i
])
4858 /* The type depended on some template parameters.
4859 If they are fully specialized in the
4860 specialization, that's OK. */
4863 for (j
= 0; j
< nargs
; ++j
)
4864 if (tpd2
.parms
[j
] != 0
4865 && tpd
.arg_uses_template_parms
[j
])
4868 error_n (input_location
, count
,
4869 "type %qT of template argument %qE depends "
4870 "on a template parameter",
4871 "type %qT of template argument %qE depends "
4872 "on template parameters",
4881 /* We should only get here once. */
4882 if (TREE_CODE (decl
) == TYPE_DECL
)
4883 gcc_assert (!COMPLETE_TYPE_P (type
));
4885 // Build the template decl.
4886 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4887 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4888 TREE_TYPE (tmpl
) = type
;
4889 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4890 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4891 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4892 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4894 /* Give template template parms a DECL_CONTEXT of the template
4895 for which they are a parameter. */
4896 for (i
= 0; i
< ntparms
; ++i
)
4898 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4899 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4900 DECL_CONTEXT (parm
) = tmpl
;
4904 /* We didn't register this in check_explicit_specialization so we could
4905 wait until the constraints were set. */
4906 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4908 associate_classtype_constraints (type
);
4910 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4911 = tree_cons (specargs
, tmpl
,
4912 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4913 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4915 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4916 inst
= TREE_CHAIN (inst
))
4918 tree instance
= TREE_VALUE (inst
);
4919 if (TYPE_P (instance
)
4920 ? (COMPLETE_TYPE_P (instance
)
4921 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4922 : DECL_TEMPLATE_INSTANTIATION (instance
))
4924 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4925 tree inst_decl
= (DECL_P (instance
)
4926 ? instance
: TYPE_NAME (instance
));
4929 else if (spec
== error_mark_node
)
4930 permerror (input_location
,
4931 "declaration of %qD ambiguates earlier template "
4932 "instantiation for %qD", decl
, inst_decl
);
4933 else if (TREE_VALUE (spec
) == tmpl
)
4934 permerror (input_location
,
4935 "partial specialization of %qD after instantiation "
4936 "of %qD", decl
, inst_decl
);
4943 /* PARM is a template parameter of some form; return the corresponding
4944 TEMPLATE_PARM_INDEX. */
4947 get_template_parm_index (tree parm
)
4949 if (TREE_CODE (parm
) == PARM_DECL
4950 || TREE_CODE (parm
) == CONST_DECL
)
4951 parm
= DECL_INITIAL (parm
);
4952 else if (TREE_CODE (parm
) == TYPE_DECL
4953 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4954 parm
= TREE_TYPE (parm
);
4955 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4956 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4957 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4958 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4959 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4963 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4964 parameter packs used by the template parameter PARM. */
4967 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4969 /* A type parm can't refer to another parm. */
4970 if (TREE_CODE (parm
) == TYPE_DECL
)
4972 else if (TREE_CODE (parm
) == PARM_DECL
)
4974 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4979 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4981 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4982 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4983 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4986 /* PARM is a template parameter pack. Return any parameter packs used in
4987 its type or the type of any of its template parameters. If there are
4988 any such packs, it will be instantiated into a fixed template parameter
4989 list by partial instantiation rather than be fully deduced. */
4992 fixed_parameter_pack_p (tree parm
)
4994 /* This can only be true in a member template. */
4995 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4997 /* This can only be true for a parameter pack. */
4998 if (!template_parameter_pack_p (parm
))
5000 /* A type parm can't refer to another parm. */
5001 if (TREE_CODE (parm
) == TYPE_DECL
)
5004 tree parameter_packs
= NULL_TREE
;
5005 struct find_parameter_pack_data ppd
;
5006 ppd
.parameter_packs
= ¶meter_packs
;
5007 ppd
.visited
= new hash_set
<tree
>;
5008 ppd
.type_pack_expansion_p
= false;
5010 fixed_parameter_pack_p_1 (parm
, &ppd
);
5013 return parameter_packs
;
5016 /* Check that a template declaration's use of default arguments and
5017 parameter packs is not invalid. Here, PARMS are the template
5018 parameters. IS_PRIMARY is true if DECL is the thing declared by
5019 a primary template. IS_PARTIAL is true if DECL is a partial
5022 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5023 function template declaration or a friend class template
5024 declaration. In the function case, 1 indicates a declaration, 2
5025 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5026 emitted for extraneous default arguments.
5028 Returns TRUE if there were no errors found, FALSE otherwise. */
5031 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5032 bool is_partial
, int is_friend_decl
)
5035 int last_level_to_check
;
5037 bool no_errors
= true;
5041 A default template-argument shall not be specified in a
5042 function template declaration or a function template definition, nor
5043 in the template-parameter-list of the definition of a member of a
5046 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5047 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5048 /* You can't have a function template declaration in a local
5049 scope, nor you can you define a member of a class template in a
5053 if ((TREE_CODE (decl
) == TYPE_DECL
5055 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5056 || (TREE_CODE (decl
) == FUNCTION_DECL
5057 && LAMBDA_FUNCTION_P (decl
)))
5058 /* A lambda doesn't have an explicit declaration; don't complain
5059 about the parms of the enclosing class. */
5062 if (current_class_type
5063 && !TYPE_BEING_DEFINED (current_class_type
)
5064 && DECL_LANG_SPECIFIC (decl
)
5065 && DECL_DECLARES_FUNCTION_P (decl
)
5066 /* If this is either a friend defined in the scope of the class
5067 or a member function. */
5068 && (DECL_FUNCTION_MEMBER_P (decl
)
5069 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5070 : DECL_FRIEND_CONTEXT (decl
)
5071 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5073 /* And, if it was a member function, it really was defined in
5074 the scope of the class. */
5075 && (!DECL_FUNCTION_MEMBER_P (decl
)
5076 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5077 /* We already checked these parameters when the template was
5078 declared, so there's no need to do it again now. This function
5079 was defined in class scope, but we're processing its body now
5080 that the class is complete. */
5083 /* Core issue 226 (C++0x only): the following only applies to class
5086 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5090 If a template-parameter has a default template-argument, all
5091 subsequent template-parameters shall have a default
5092 template-argument supplied. */
5093 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5095 tree inner_parms
= TREE_VALUE (parm_level
);
5096 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5097 int seen_def_arg_p
= 0;
5100 for (i
= 0; i
< ntparms
; ++i
)
5102 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5104 if (parm
== error_mark_node
)
5107 if (TREE_PURPOSE (parm
))
5109 else if (seen_def_arg_p
5110 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5112 error ("no default argument for %qD", TREE_VALUE (parm
));
5113 /* For better subsequent error-recovery, we indicate that
5114 there should have been a default argument. */
5115 TREE_PURPOSE (parm
) = error_mark_node
;
5118 else if (!is_partial
5120 /* Don't complain about an enclosing partial
5122 && parm_level
== parms
5123 && TREE_CODE (decl
) == TYPE_DECL
5125 && template_parameter_pack_p (TREE_VALUE (parm
))
5126 /* A fixed parameter pack will be partially
5127 instantiated into a fixed length list. */
5128 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5130 /* A primary class template can only have one
5131 parameter pack, at the end of the template
5134 error ("parameter pack %q+D must be at the end of the"
5135 " template parameter list", TREE_VALUE (parm
));
5137 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5145 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5149 /* For an ordinary class template, default template arguments are
5150 allowed at the innermost level, e.g.:
5151 template <class T = int>
5153 but, in a partial specialization, they're not allowed even
5154 there, as we have in [temp.class.spec]:
5156 The template parameter list of a specialization shall not
5157 contain default template argument values.
5159 So, for a partial specialization, or for a function template
5160 (in C++98/C++03), we look at all of them. */
5163 /* But, for a primary class template that is not a partial
5164 specialization we look at all template parameters except the
5166 parms
= TREE_CHAIN (parms
);
5168 /* Figure out what error message to issue. */
5169 if (is_friend_decl
== 2)
5170 msg
= G_("default template arguments may not be used in function template "
5171 "friend re-declaration");
5172 else if (is_friend_decl
)
5173 msg
= G_("default template arguments may not be used in template "
5174 "friend declarations");
5175 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5176 msg
= G_("default template arguments may not be used in function templates "
5177 "without -std=c++11 or -std=gnu++11");
5178 else if (is_partial
)
5179 msg
= G_("default template arguments may not be used in "
5180 "partial specializations");
5181 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5182 msg
= G_("default argument for template parameter for class enclosing %qD");
5184 /* Per [temp.param]/9, "A default template-argument shall not be
5185 specified in the template-parameter-lists of the definition of
5186 a member of a class template that appears outside of the member's
5187 class.", thus if we aren't handling a member of a class template
5188 there is no need to examine the parameters. */
5191 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5192 /* If we're inside a class definition, there's no need to
5193 examine the parameters to the class itself. On the one
5194 hand, they will be checked when the class is defined, and,
5195 on the other, default arguments are valid in things like:
5196 template <class T = double>
5197 struct S { template <class U> void f(U); };
5198 Here the default argument for `S' has no bearing on the
5199 declaration of `f'. */
5200 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5202 /* Check everything. */
5203 last_level_to_check
= 0;
5205 for (parm_level
= parms
;
5206 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5207 parm_level
= TREE_CHAIN (parm_level
))
5209 tree inner_parms
= TREE_VALUE (parm_level
);
5213 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5214 for (i
= 0; i
< ntparms
; ++i
)
5216 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5219 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5224 if (is_friend_decl
== 2)
5231 /* Clear out the default argument so that we are not
5233 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5237 /* At this point, if we're still interested in issuing messages,
5238 they must apply to classes surrounding the object declared. */
5240 msg
= G_("default argument for template parameter for class "
5247 /* Worker for push_template_decl_real, called via
5248 for_each_template_parm. DATA is really an int, indicating the
5249 level of the parameters we are interested in. If T is a template
5250 parameter of that level, return nonzero. */
5253 template_parm_this_level_p (tree t
, void* data
)
5255 int this_level
= *(int *)data
;
5258 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5259 level
= TEMPLATE_PARM_LEVEL (t
);
5261 level
= TEMPLATE_TYPE_LEVEL (t
);
5262 return level
== this_level
;
5265 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5266 DATA is really an int, indicating the innermost outer level of parameters.
5267 If T is a template parameter of that level or further out, return
5271 template_parm_outer_level (tree t
, void *data
)
5273 int this_level
= *(int *)data
;
5276 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5277 level
= TEMPLATE_PARM_LEVEL (t
);
5279 level
= TEMPLATE_TYPE_LEVEL (t
);
5280 return level
<= this_level
;
5283 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5284 parameters given by current_template_args, or reuses a
5285 previously existing one, if appropriate. Returns the DECL, or an
5286 equivalent one, if it is replaced via a call to duplicate_decls.
5288 If IS_FRIEND is true, DECL is a friend declaration. */
5291 push_template_decl_real (tree decl
, bool is_friend
)
5299 int new_template_p
= 0;
5300 /* True if the template is a member template, in the sense of
5302 bool member_template_p
= false;
5304 if (decl
== error_mark_node
|| !current_template_parms
)
5305 return error_mark_node
;
5307 /* See if this is a partial specialization. */
5308 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5309 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5310 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5312 && DECL_LANG_SPECIFIC (decl
)
5313 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5314 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5316 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5320 /* For a friend, we want the context of the friend, not
5321 the type of which it is a friend. */
5322 ctx
= CP_DECL_CONTEXT (decl
);
5323 else if (CP_DECL_CONTEXT (decl
)
5324 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5325 /* In the case of a virtual function, we want the class in which
5327 ctx
= CP_DECL_CONTEXT (decl
);
5329 /* Otherwise, if we're currently defining some class, the DECL
5330 is assumed to be a member of the class. */
5331 ctx
= current_scope ();
5333 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5336 if (!DECL_CONTEXT (decl
))
5337 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5339 /* See if this is a primary template. */
5340 if (is_friend
&& ctx
5341 && uses_template_parms_level (ctx
, processing_template_decl
))
5342 /* A friend template that specifies a class context, i.e.
5343 template <typename T> friend void A<T>::f();
5346 else if (TREE_CODE (decl
) == TYPE_DECL
5347 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5350 is_primary
= template_parm_scope_p ();
5354 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5356 if (DECL_CLASS_SCOPE_P (decl
))
5357 member_template_p
= true;
5358 if (TREE_CODE (decl
) == TYPE_DECL
5359 && anon_aggrname_p (DECL_NAME (decl
)))
5361 error ("template class without a name");
5362 return error_mark_node
;
5364 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5366 if (member_template_p
)
5368 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5369 error ("member template %qD may not have virt-specifiers", decl
);
5371 if (DECL_DESTRUCTOR_P (decl
))
5375 A destructor shall not be a member template. */
5376 error ("destructor %qD declared as member template", decl
);
5377 return error_mark_node
;
5379 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5380 && (!prototype_p (TREE_TYPE (decl
))
5381 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5382 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5383 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5384 == void_list_node
)))
5386 /* [basic.stc.dynamic.allocation]
5388 An allocation function can be a function
5389 template. ... Template allocation functions shall
5390 have two or more parameters. */
5391 error ("invalid template declaration of %qD", decl
);
5392 return error_mark_node
;
5395 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5396 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5398 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5399 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5400 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5402 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5403 if (TREE_CODE (t
) == TYPE_DECL
)
5405 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5406 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5409 else if (TREE_CODE (decl
) == TYPE_DECL
5410 && TYPE_DECL_ALIAS_P (decl
))
5411 /* alias-declaration */
5412 gcc_assert (!DECL_ARTIFICIAL (decl
));
5413 else if (VAR_P (decl
))
5414 /* C++14 variable template. */;
5417 error ("template declaration of %q#D", decl
);
5418 return error_mark_node
;
5422 /* Check to see that the rules regarding the use of default
5423 arguments are not being violated. We check args for a friend
5424 functions when we know whether it's a definition, introducing
5425 declaration or re-declaration. */
5426 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5427 check_default_tmpl_args (decl
, current_template_parms
,
5428 is_primary
, is_partial
, is_friend
);
5430 /* Ensure that there are no parameter packs in the type of this
5431 declaration that have not been expanded. */
5432 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5434 /* Check each of the arguments individually to see if there are
5435 any bare parameter packs. */
5436 tree type
= TREE_TYPE (decl
);
5437 tree arg
= DECL_ARGUMENTS (decl
);
5438 tree argtype
= TYPE_ARG_TYPES (type
);
5440 while (arg
&& argtype
)
5442 if (!DECL_PACK_P (arg
)
5443 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5445 /* This is a PARM_DECL that contains unexpanded parameter
5446 packs. We have already complained about this in the
5447 check_for_bare_parameter_packs call, so just replace
5448 these types with ERROR_MARK_NODE. */
5449 TREE_TYPE (arg
) = error_mark_node
;
5450 TREE_VALUE (argtype
) = error_mark_node
;
5453 arg
= DECL_CHAIN (arg
);
5454 argtype
= TREE_CHAIN (argtype
);
5457 /* Check for bare parameter packs in the return type and the
5458 exception specifiers. */
5459 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5460 /* Errors were already issued, set return type to int
5461 as the frontend doesn't expect error_mark_node as
5463 TREE_TYPE (type
) = integer_type_node
;
5464 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5465 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5467 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5468 && TYPE_DECL_ALIAS_P (decl
))
5469 ? DECL_ORIGINAL_TYPE (decl
)
5470 : TREE_TYPE (decl
)))
5472 TREE_TYPE (decl
) = error_mark_node
;
5473 return error_mark_node
;
5477 return process_partial_specialization (decl
);
5479 args
= current_template_args ();
5482 || TREE_CODE (ctx
) == FUNCTION_DECL
5483 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5484 || (TREE_CODE (decl
) == TYPE_DECL
5485 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5486 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5488 if (DECL_LANG_SPECIFIC (decl
)
5489 && DECL_TEMPLATE_INFO (decl
)
5490 && DECL_TI_TEMPLATE (decl
))
5491 tmpl
= DECL_TI_TEMPLATE (decl
);
5492 /* If DECL is a TYPE_DECL for a class-template, then there won't
5493 be DECL_LANG_SPECIFIC. The information equivalent to
5494 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5495 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5496 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5497 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5499 /* Since a template declaration already existed for this
5500 class-type, we must be redeclaring it here. Make sure
5501 that the redeclaration is valid. */
5502 redeclare_class_template (TREE_TYPE (decl
),
5503 current_template_parms
,
5504 current_template_constraints ());
5505 /* We don't need to create a new TEMPLATE_DECL; just use the
5506 one we already had. */
5507 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5511 tmpl
= build_template_decl (decl
, current_template_parms
,
5515 if (DECL_LANG_SPECIFIC (decl
)
5516 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5518 /* A specialization of a member template of a template
5520 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5521 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5522 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5528 tree a
, t
, current
, parms
;
5530 tree tinfo
= get_template_info (decl
);
5534 error ("template definition of non-template %q#D", decl
);
5535 return error_mark_node
;
5538 tmpl
= TI_TEMPLATE (tinfo
);
5540 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5541 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5542 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5543 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5547 /* The declaration is a specialization of a member
5548 template, declared outside the class. Therefore, the
5549 innermost template arguments will be NULL, so we
5550 replace them with the arguments determined by the
5551 earlier call to check_explicit_specialization. */
5552 args
= DECL_TI_ARGS (decl
);
5555 = build_template_decl (decl
, current_template_parms
,
5557 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5558 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5559 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5560 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5561 DECL_TEMPLATE_INFO (new_tmpl
)
5562 = build_template_info (tmpl
, args
);
5564 register_specialization (new_tmpl
,
5565 most_general_template (tmpl
),
5571 /* Make sure the template headers we got make sense. */
5573 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5574 i
= TMPL_PARMS_DEPTH (parms
);
5575 if (TMPL_ARGS_DEPTH (args
) != i
)
5577 error ("expected %d levels of template parms for %q#D, got %d",
5578 i
, decl
, TMPL_ARGS_DEPTH (args
));
5579 DECL_INTERFACE_KNOWN (decl
) = 1;
5580 return error_mark_node
;
5583 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5585 a
= TMPL_ARGS_LEVEL (args
, i
);
5586 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5588 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5590 if (current
== decl
)
5591 error ("got %d template parameters for %q#D",
5592 TREE_VEC_LENGTH (a
), decl
);
5594 error ("got %d template parameters for %q#T",
5595 TREE_VEC_LENGTH (a
), current
);
5596 error (" but %d required", TREE_VEC_LENGTH (t
));
5597 /* Avoid crash in import_export_decl. */
5598 DECL_INTERFACE_KNOWN (decl
) = 1;
5599 return error_mark_node
;
5602 if (current
== decl
)
5604 else if (current
== NULL_TREE
)
5605 /* Can happen in erroneous input. */
5608 current
= get_containing_scope (current
);
5611 /* Check that the parms are used in the appropriate qualifying scopes
5612 in the declarator. */
5613 if (!comp_template_args
5615 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5617 error ("template arguments to %qD do not match original "
5618 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5619 if (!uses_template_parms (TI_ARGS (tinfo
)))
5620 inform (input_location
, "use %<template<>%> for"
5621 " an explicit specialization");
5622 /* Avoid crash in import_export_decl. */
5623 DECL_INTERFACE_KNOWN (decl
) = 1;
5624 return error_mark_node
;
5628 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5629 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5631 /* Push template declarations for global functions and types. Note
5632 that we do not try to push a global template friend declared in a
5633 template class; such a thing may well depend on the template
5634 parameters of the class. */
5635 if (new_template_p
&& !ctx
5636 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5638 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5639 if (tmpl
== error_mark_node
)
5640 return error_mark_node
;
5642 /* Hide template friend classes that haven't been declared yet. */
5643 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5645 DECL_ANTICIPATED (tmpl
) = 1;
5646 DECL_FRIEND_P (tmpl
) = 1;
5652 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5654 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5656 /* Give template template parms a DECL_CONTEXT of the template
5657 for which they are a parameter. */
5658 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5659 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5661 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5662 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5663 DECL_CONTEXT (parm
) = tmpl
;
5666 if (TREE_CODE (decl
) == TYPE_DECL
5667 && TYPE_DECL_ALIAS_P (decl
)
5668 && complex_alias_template_p (tmpl
))
5669 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5672 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5673 back to its most general template. If TMPL is a specialization,
5674 ARGS may only have the innermost set of arguments. Add the missing
5675 argument levels if necessary. */
5676 if (DECL_TEMPLATE_INFO (tmpl
))
5677 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5679 info
= build_template_info (tmpl
, args
);
5681 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5682 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5686 retrofit_lang_decl (decl
);
5687 if (DECL_LANG_SPECIFIC (decl
))
5688 DECL_TEMPLATE_INFO (decl
) = info
;
5691 if (flag_implicit_templates
5693 && TREE_PUBLIC (decl
)
5694 && VAR_OR_FUNCTION_DECL_P (decl
))
5695 /* Set DECL_COMDAT on template instantiations; if we force
5696 them to be emitted by explicit instantiation or -frepo,
5697 mark_needed will tell cgraph to do the right thing. */
5698 DECL_COMDAT (decl
) = true;
5700 return DECL_TEMPLATE_RESULT (tmpl
);
5704 push_template_decl (tree decl
)
5706 return push_template_decl_real (decl
, false);
5709 /* FN is an inheriting constructor that inherits from the constructor
5710 template INHERITED; turn FN into a constructor template with a matching
5714 add_inherited_template_parms (tree fn
, tree inherited
)
5717 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5718 inner_parms
= copy_node (inner_parms
);
5720 = tree_cons (size_int (processing_template_decl
+ 1),
5721 inner_parms
, current_template_parms
);
5722 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5723 tree args
= template_parms_to_args (parms
);
5724 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5725 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5726 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5727 DECL_ARTIFICIAL (tmpl
) = true;
5728 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5732 /* Called when a class template TYPE is redeclared with the indicated
5733 template PARMS, e.g.:
5735 template <class T> struct S;
5736 template <class T> struct S {}; */
5739 redeclare_class_template (tree type
, tree parms
, tree cons
)
5745 if (!TYPE_TEMPLATE_INFO (type
))
5747 error ("%qT is not a template type", type
);
5751 tmpl
= TYPE_TI_TEMPLATE (type
);
5752 if (!PRIMARY_TEMPLATE_P (tmpl
))
5753 /* The type is nested in some template class. Nothing to worry
5754 about here; there are no new template parameters for the nested
5760 error ("template specifiers not specified in declaration of %qD",
5765 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5766 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5768 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5770 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5771 "redeclared with %d template parameter",
5772 "redeclared with %d template parameters",
5773 TREE_VEC_LENGTH (parms
));
5774 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5775 "previous declaration %qD used %d template parameter",
5776 "previous declaration %qD used %d template parameters",
5777 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5781 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5788 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5789 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5792 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5793 if (error_operand_p (tmpl_parm
))
5796 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5797 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5798 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5800 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5802 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5803 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5804 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5805 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5806 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5807 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5808 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5809 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5810 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5812 error ("template parameter %q+#D", tmpl_parm
);
5813 error ("redeclared here as %q#D", parm
);
5817 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5819 /* We have in [temp.param]:
5821 A template-parameter may not be given default arguments
5822 by two different declarations in the same scope. */
5823 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5824 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5825 "original definition appeared here");
5829 if (parm_default
!= NULL_TREE
)
5830 /* Update the previous template parameters (which are the ones
5831 that will really count) with the new default value. */
5832 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5833 else if (tmpl_default
!= NULL_TREE
)
5834 /* Update the new parameters, too; they'll be used as the
5835 parameters for any members. */
5836 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5838 /* Give each template template parm in this redeclaration a
5839 DECL_CONTEXT of the template for which they are a parameter. */
5840 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5842 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5843 DECL_CONTEXT (parm
) = tmpl
;
5846 if (TREE_CODE (parm
) == TYPE_DECL
)
5847 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5850 // Cannot redeclare a class template with a different set of constraints.
5851 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5853 error_at (input_location
, "redeclaration %q#D with different "
5854 "constraints", tmpl
);
5855 inform (DECL_SOURCE_LOCATION (tmpl
),
5856 "original declaration appeared here");
5862 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5863 to be used when the caller has already checked
5864 (processing_template_decl
5865 && !instantiation_dependent_expression_p (expr)
5866 && potential_constant_expression (expr))
5867 and cleared processing_template_decl. */
5870 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5872 return tsubst_copy_and_build (expr
,
5875 /*in_decl=*/NULL_TREE
,
5876 /*function_p=*/false,
5877 /*integral_constant_expression_p=*/true);
5880 /* Simplify EXPR if it is a non-dependent expression. Returns the
5881 (possibly simplified) expression. */
5884 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5886 if (expr
== NULL_TREE
)
5889 /* If we're in a template, but EXPR isn't value dependent, simplify
5890 it. We're supposed to treat:
5892 template <typename T> void f(T[1 + 1]);
5893 template <typename T> void f(T[2]);
5895 as two declarations of the same function, for example. */
5896 if (processing_template_decl
5897 && is_nondependent_constant_expression (expr
))
5899 processing_template_decl_sentinel s
;
5900 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5906 instantiate_non_dependent_expr (tree expr
)
5908 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5911 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5912 an uninstantiated expression. */
5915 instantiate_non_dependent_or_null (tree expr
)
5917 if (expr
== NULL_TREE
)
5919 if (processing_template_decl
)
5921 if (!is_nondependent_constant_expression (expr
))
5925 processing_template_decl_sentinel s
;
5926 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5932 /* True iff T is a specialization of a variable template. */
5935 variable_template_specialization_p (tree t
)
5937 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5939 tree tmpl
= DECL_TI_TEMPLATE (t
);
5940 return variable_template_p (tmpl
);
5943 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5944 template declaration, or a TYPE_DECL for an alias declaration. */
5947 alias_type_or_template_p (tree t
)
5951 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5954 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5955 || DECL_ALIAS_TEMPLATE_P (t
));
5958 /* Return TRUE iff T is a specialization of an alias template. */
5961 alias_template_specialization_p (const_tree t
)
5963 /* It's an alias template specialization if it's an alias and its
5964 TYPE_NAME is a specialization of a primary template. */
5965 if (TYPE_ALIAS_P (t
))
5966 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5967 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5972 /* An alias template is complex from a SFINAE perspective if a template-id
5973 using that alias can be ill-formed when the expansion is not, as with
5974 the void_t template. We determine this by checking whether the
5975 expansion for the alias template uses all its template parameters. */
5977 struct uses_all_template_parms_data
5984 uses_all_template_parms_r (tree t
, void *data_
)
5986 struct uses_all_template_parms_data
&data
5987 = *(struct uses_all_template_parms_data
*)data_
;
5988 tree idx
= get_template_parm_index (t
);
5990 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5991 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5996 complex_alias_template_p (const_tree tmpl
)
5998 struct uses_all_template_parms_data data
;
5999 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6000 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6001 data
.level
= TMPL_PARMS_DEPTH (parms
);
6002 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6003 data
.seen
= XALLOCAVEC (bool, len
);
6004 for (int i
= 0; i
< len
; ++i
)
6005 data
.seen
[i
] = false;
6007 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6008 for (int i
= 0; i
< len
; ++i
)
6014 /* Return TRUE iff T is a specialization of a complex alias template with
6015 dependent template-arguments. */
6018 dependent_alias_template_spec_p (const_tree t
)
6020 if (!alias_template_specialization_p (t
))
6023 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6024 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6027 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6028 if (!any_dependent_template_arguments_p (args
))
6034 /* Return the number of innermost template parameters in TMPL. */
6037 num_innermost_template_parms (tree tmpl
)
6039 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6040 return TREE_VEC_LENGTH (parms
);
6043 /* Return either TMPL or another template that it is equivalent to under DR
6044 1286: An alias that just changes the name of a template is equivalent to
6045 the other template. */
6048 get_underlying_template (tree tmpl
)
6050 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6051 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6053 /* Determine if the alias is equivalent to an underlying template. */
6054 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6055 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6059 tree underlying
= TI_TEMPLATE (tinfo
);
6060 if (!PRIMARY_TEMPLATE_P (underlying
)
6061 || (num_innermost_template_parms (tmpl
)
6062 != num_innermost_template_parms (underlying
)))
6065 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6066 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6067 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6070 /* Alias is equivalent. Strip it and repeat. */
6077 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6078 must be a reference-to-function or a pointer-to-function type, as specified
6079 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6080 and check that the resulting function has external linkage. */
6083 convert_nontype_argument_function (tree type
, tree expr
,
6084 tsubst_flags_t complain
)
6088 linkage_kind linkage
;
6090 fn
= instantiate_type (type
, fns
, tf_none
);
6091 if (fn
== error_mark_node
)
6092 return error_mark_node
;
6094 if (value_dependent_expression_p (fn
))
6097 fn_no_ptr
= strip_fnptr_conv (fn
);
6098 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6099 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6100 if (BASELINK_P (fn_no_ptr
))
6101 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6103 /* [temp.arg.nontype]/1
6105 A template-argument for a non-type, non-template template-parameter
6108 -- the address of an object or function with external [C++11: or
6109 internal] linkage. */
6111 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6113 if (complain
& tf_error
)
6115 error ("%qE is not a valid template argument for type %qT",
6117 if (TYPE_PTR_P (type
))
6118 inform (input_location
, "it must be the address of a function "
6119 "with external linkage");
6121 inform (input_location
, "it must be the name of a function with "
6122 "external linkage");
6127 linkage
= decl_linkage (fn_no_ptr
);
6128 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6130 if (complain
& tf_error
)
6132 if (cxx_dialect
>= cxx11
)
6133 error ("%qE is not a valid template argument for type %qT "
6134 "because %qD has no linkage",
6135 expr
, type
, fn_no_ptr
);
6137 error ("%qE is not a valid template argument for type %qT "
6138 "because %qD does not have external linkage",
6139 expr
, type
, fn_no_ptr
);
6145 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6147 if (REFERENCE_REF_P (fn
))
6148 fn
= TREE_OPERAND (fn
, 0);
6150 fn
= build_address (fn
);
6152 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6153 fn
= build_nop (type
, fn
);
6158 /* Subroutine of convert_nontype_argument.
6159 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6160 Emit an error otherwise. */
6163 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6164 tsubst_flags_t complain
)
6166 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6167 tree orig_expr
= expr
;
6169 if (null_ptr_cst_p (expr
))
6171 if (TREE_CODE (expr
) == PTRMEM_CST
6172 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6173 PTRMEM_CST_CLASS (expr
)))
6175 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6177 if (processing_template_decl
6178 && TREE_CODE (expr
) == ADDR_EXPR
6179 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6181 if (complain
& tf_error
)
6183 error_at (loc
, "%qE is not a valid template argument for type %qT",
6185 if (TREE_CODE (expr
) != PTRMEM_CST
)
6186 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6188 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6193 /* Returns TRUE iff the address of OP is value-dependent.
6195 14.6.2.4 [temp.dep.temp]:
6196 A non-integral non-type template-argument is dependent if its type is
6197 dependent or it has either of the following forms
6200 and contains a nested-name-specifier which specifies a class-name that
6201 names a dependent type.
6203 We generalize this to just say that the address of a member of a
6204 dependent class is value-dependent; the above doesn't cover the
6205 address of a static data member named with an unqualified-id. */
6208 has_value_dependent_address (tree op
)
6210 /* We could use get_inner_reference here, but there's no need;
6211 this is only relevant for template non-type arguments, which
6212 can only be expressed as &id-expression. */
6215 tree ctx
= CP_DECL_CONTEXT (op
);
6216 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6223 /* The next set of functions are used for providing helpful explanatory
6224 diagnostics for failed overload resolution. Their messages should be
6225 indented by two spaces for consistency with the messages in
6229 unify_success (bool /*explain_p*/)
6234 /* Other failure functions should call this one, to provide a single function
6235 for setting a breakpoint on. */
6238 unify_invalid (bool /*explain_p*/)
6244 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6247 inform (input_location
,
6248 " couldn't deduce template parameter %qD", parm
);
6249 return unify_invalid (explain_p
);
6253 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6256 inform (input_location
,
6257 " types %qT and %qT have incompatible cv-qualifiers",
6259 return unify_invalid (explain_p
);
6263 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6266 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6267 return unify_invalid (explain_p
);
6271 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6274 inform (input_location
,
6275 " template parameter %qD is not a parameter pack, but "
6278 return unify_invalid (explain_p
);
6282 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6285 inform (input_location
,
6286 " template argument %qE does not match "
6287 "pointer-to-member constant %qE",
6289 return unify_invalid (explain_p
);
6293 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6296 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6297 return unify_invalid (explain_p
);
6301 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6304 inform (input_location
,
6305 " inconsistent parameter pack deduction with %qT and %qT",
6307 return unify_invalid (explain_p
);
6311 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6316 inform (input_location
,
6317 " deduced conflicting types for parameter %qT (%qT and %qT)",
6318 parm
, first
, second
);
6320 inform (input_location
,
6321 " deduced conflicting values for non-type parameter "
6322 "%qE (%qE and %qE)", parm
, first
, second
);
6324 return unify_invalid (explain_p
);
6328 unify_vla_arg (bool explain_p
, tree arg
)
6331 inform (input_location
,
6332 " variable-sized array type %qT is not "
6333 "a valid template argument",
6335 return unify_invalid (explain_p
);
6339 unify_method_type_error (bool explain_p
, tree arg
)
6342 inform (input_location
,
6343 " member function type %qT is not a valid template argument",
6345 return unify_invalid (explain_p
);
6349 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6354 inform_n (input_location
, wanted
,
6355 " candidate expects at least %d argument, %d provided",
6356 " candidate expects at least %d arguments, %d provided",
6359 inform_n (input_location
, wanted
,
6360 " candidate expects %d argument, %d provided",
6361 " candidate expects %d arguments, %d provided",
6364 return unify_invalid (explain_p
);
6368 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6370 return unify_arity (explain_p
, have
, wanted
);
6374 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6375 bool least_p
= false)
6377 return unify_arity (explain_p
, have
, wanted
, least_p
);
6381 unify_arg_conversion (bool explain_p
, tree to_type
,
6382 tree from_type
, tree arg
)
6385 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6386 " cannot convert %qE (type %qT) to type %qT",
6387 arg
, from_type
, to_type
);
6388 return unify_invalid (explain_p
);
6392 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6393 tree parm
, tree arg
)
6398 case tbr_ambiguous_baseclass
:
6399 inform (input_location
, " %qT is an ambiguous base class of %qT",
6403 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6406 return unify_invalid (explain_p
);
6410 unify_inconsistent_template_template_parameters (bool explain_p
)
6413 inform (input_location
,
6414 " template parameters of a template template argument are "
6415 "inconsistent with other deduced template arguments");
6416 return unify_invalid (explain_p
);
6420 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6423 inform (input_location
,
6424 " can't deduce a template for %qT from non-template type %qT",
6426 return unify_invalid (explain_p
);
6430 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6433 inform (input_location
,
6434 " template argument %qE does not match %qE", arg
, parm
);
6435 return unify_invalid (explain_p
);
6438 /* Attempt to convert the non-type template parameter EXPR to the
6439 indicated TYPE. If the conversion is successful, return the
6440 converted value. If the conversion is unsuccessful, return
6441 NULL_TREE if we issued an error message, or error_mark_node if we
6442 did not. We issue error messages for out-and-out bad template
6443 parameters, but not simply because the conversion failed, since we
6444 might be just trying to do argument deduction. Both TYPE and EXPR
6445 must be non-dependent.
6447 The conversion follows the special rules described in
6448 [temp.arg.nontype], and it is much more strict than an implicit
6451 This function is called twice for each template argument (see
6452 lookup_template_class for a more accurate description of this
6453 problem). This means that we need to handle expressions which
6454 are not valid in a C++ source, but can be created from the
6455 first call (for instance, casts to perform conversions). These
6456 hacks can go away after we fix the double coercion problem. */
6459 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6462 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6463 tree orig_expr
= expr
;
6465 /* Detect immediately string literals as invalid non-type argument.
6466 This special-case is not needed for correctness (we would easily
6467 catch this later), but only to provide better diagnostic for this
6468 common user mistake. As suggested by DR 100, we do not mention
6469 linkage issues in the diagnostic as this is not the point. */
6470 /* FIXME we're making this OK. */
6471 if (TREE_CODE (expr
) == STRING_CST
)
6473 if (complain
& tf_error
)
6474 error ("%qE is not a valid template argument for type %qT "
6475 "because string literals can never be used in this context",
6480 /* Add the ADDR_EXPR now for the benefit of
6481 value_dependent_expression_p. */
6482 if (TYPE_PTROBV_P (type
)
6483 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6485 expr
= decay_conversion (expr
, complain
);
6486 if (expr
== error_mark_node
)
6487 return error_mark_node
;
6490 /* If we are in a template, EXPR may be non-dependent, but still
6491 have a syntactic, rather than semantic, form. For example, EXPR
6492 might be a SCOPE_REF, rather than the VAR_DECL to which the
6493 SCOPE_REF refers. Preserving the qualifying scope is necessary
6494 so that access checking can be performed when the template is
6495 instantiated -- but here we need the resolved form so that we can
6496 convert the argument. */
6497 bool non_dep
= false;
6498 if (TYPE_REF_OBJ_P (type
)
6499 && has_value_dependent_address (expr
))
6500 /* If we want the address and it's value-dependent, don't fold. */;
6501 else if (processing_template_decl
6502 && is_nondependent_constant_expression (expr
))
6504 if (error_operand_p (expr
))
6505 return error_mark_node
;
6506 expr_type
= TREE_TYPE (expr
);
6508 /* If the argument is non-dependent, perform any conversions in
6509 non-dependent context as well. */
6510 processing_template_decl_sentinel
s (non_dep
);
6512 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6514 if (value_dependent_expression_p (expr
))
6515 expr
= canonicalize_expr_argument (expr
, complain
);
6517 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6518 to a non-type argument of "nullptr". */
6519 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6520 expr
= fold_simple (convert (type
, expr
));
6522 /* In C++11, integral or enumeration non-type template arguments can be
6523 arbitrary constant expressions. Pointer and pointer to
6524 member arguments can be general constant expressions that evaluate
6525 to a null value, but otherwise still need to be of a specific form. */
6526 if (cxx_dialect
>= cxx11
)
6528 if (TREE_CODE (expr
) == PTRMEM_CST
)
6529 /* A PTRMEM_CST is already constant, and a valid template
6530 argument for a parameter of pointer to member type, we just want
6531 to leave it in that form rather than lower it to a
6533 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6534 || cxx_dialect
>= cxx17
)
6536 /* C++17: A template-argument for a non-type template-parameter shall
6537 be a converted constant expression (8.20) of the type of the
6538 template-parameter. */
6539 expr
= build_converted_constant_expr (type
, expr
, complain
);
6540 if (expr
== error_mark_node
)
6541 return error_mark_node
;
6542 expr
= maybe_constant_value (expr
);
6543 expr
= convert_from_reference (expr
);
6545 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6547 tree folded
= maybe_constant_value (expr
);
6548 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6549 : null_member_pointer_value_p (folded
))
6554 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6555 expr
= mark_lvalue_use (expr
);
6557 expr
= mark_rvalue_use (expr
);
6559 /* HACK: Due to double coercion, we can get a
6560 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6561 which is the tree that we built on the first call (see
6562 below when coercing to reference to object or to reference to
6563 function). We just strip everything and get to the arg.
6564 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6566 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6568 tree probe_type
, probe
= expr
;
6569 if (REFERENCE_REF_P (probe
))
6570 probe
= TREE_OPERAND (probe
, 0);
6571 probe_type
= TREE_TYPE (probe
);
6572 if (TREE_CODE (probe
) == NOP_EXPR
)
6574 /* ??? Maybe we could use convert_from_reference here, but we
6575 would need to relax its constraints because the NOP_EXPR
6576 could actually change the type to something more cv-qualified,
6577 and this is not folded by convert_from_reference. */
6578 tree addr
= TREE_OPERAND (probe
, 0);
6579 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6580 && TREE_CODE (addr
) == ADDR_EXPR
6581 && TYPE_PTR_P (TREE_TYPE (addr
))
6582 && (same_type_ignoring_top_level_qualifiers_p
6583 (TREE_TYPE (probe_type
),
6584 TREE_TYPE (TREE_TYPE (addr
)))))
6586 expr
= TREE_OPERAND (addr
, 0);
6587 expr_type
= TREE_TYPE (probe_type
);
6592 /* [temp.arg.nontype]/5, bullet 1
6594 For a non-type template-parameter of integral or enumeration type,
6595 integral promotions (_conv.prom_) and integral conversions
6596 (_conv.integral_) are applied. */
6597 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6599 if (cxx_dialect
< cxx11
)
6601 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6602 t
= maybe_constant_value (t
);
6603 if (t
!= error_mark_node
)
6607 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6608 return error_mark_node
;
6610 /* Notice that there are constant expressions like '4 % 0' which
6611 do not fold into integer constants. */
6612 if (TREE_CODE (expr
) != INTEGER_CST
6613 && !value_dependent_expression_p (expr
))
6615 if (complain
& tf_error
)
6617 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6618 if (!require_potential_constant_expression (expr
))
6619 expr
= error_mark_node
;
6621 expr
= cxx_constant_value (expr
);
6622 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6623 inform (loc
, "in template argument for type %qT ", type
);
6624 if (expr
== error_mark_node
)
6626 /* else cxx_constant_value complained but gave us
6627 a real constant, so go ahead. */
6628 if (TREE_CODE (expr
) != INTEGER_CST
)
6630 /* Some assemble time constant expressions like
6631 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6632 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6633 as we can emit them into .rodata initializers of
6634 variables, yet they can't fold into an INTEGER_CST at
6635 compile time. Refuse them here. */
6636 gcc_checking_assert (reduced_constant_expression_p (expr
));
6637 error_at (loc
, "template argument %qE for type %qT not "
6638 "a constant integer", expr
, type
);
6646 /* Avoid typedef problems. */
6647 if (TREE_TYPE (expr
) != type
)
6648 expr
= fold_convert (type
, expr
);
6650 /* [temp.arg.nontype]/5, bullet 2
6652 For a non-type template-parameter of type pointer to object,
6653 qualification conversions (_conv.qual_) and the array-to-pointer
6654 conversion (_conv.array_) are applied. */
6655 else if (TYPE_PTROBV_P (type
))
6657 tree decayed
= expr
;
6659 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6660 decay_conversion or an explicit cast. If it's a problematic cast,
6661 we'll complain about it below. */
6662 if (TREE_CODE (expr
) == NOP_EXPR
)
6666 if (TREE_CODE (probe
) == ADDR_EXPR
6667 && TYPE_PTR_P (TREE_TYPE (probe
)))
6670 expr_type
= TREE_TYPE (expr
);
6674 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6676 A template-argument for a non-type, non-template template-parameter
6677 shall be one of: [...]
6679 -- the name of a non-type template-parameter;
6680 -- the address of an object or function with external linkage, [...]
6681 expressed as "& id-expression" where the & is optional if the name
6682 refers to a function or array, or if the corresponding
6683 template-parameter is a reference.
6685 Here, we do not care about functions, as they are invalid anyway
6686 for a parameter of type pointer-to-object. */
6688 if (value_dependent_expression_p (expr
))
6689 /* Non-type template parameters are OK. */
6691 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6692 /* Null pointer values are OK in C++11. */;
6693 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6697 if (complain
& tf_error
)
6698 error ("%qD is not a valid template argument "
6699 "because %qD is a variable, not the address of "
6700 "a variable", orig_expr
, expr
);
6703 if (POINTER_TYPE_P (expr_type
))
6705 if (complain
& tf_error
)
6706 error ("%qE is not a valid template argument for %qT "
6707 "because it is not the address of a variable",
6711 /* Other values, like integer constants, might be valid
6712 non-type arguments of some other type. */
6713 return error_mark_node
;
6717 tree decl
= TREE_OPERAND (expr
, 0);
6721 if (complain
& tf_error
)
6722 error ("%qE is not a valid template argument of type %qT "
6723 "because %qE is not a variable", orig_expr
, type
, decl
);
6726 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6728 if (complain
& tf_error
)
6729 error ("%qE is not a valid template argument of type %qT "
6730 "because %qD does not have external linkage",
6731 orig_expr
, type
, decl
);
6734 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6735 && decl_linkage (decl
) == lk_none
)
6737 if (complain
& tf_error
)
6738 error ("%qE is not a valid template argument of type %qT "
6739 "because %qD has no linkage", orig_expr
, type
, decl
);
6742 /* C++17: For a non-type template-parameter of reference or pointer
6743 type, the value of the constant expression shall not refer to (or
6744 for a pointer type, shall not be the address of):
6745 * a subobject (4.5),
6746 * a temporary object (15.2),
6747 * a string literal (5.13.5),
6748 * the result of a typeid expression (8.2.8), or
6749 * a predefined __func__ variable (11.4.1). */
6750 else if (DECL_ARTIFICIAL (decl
))
6752 if (complain
& tf_error
)
6753 error ("the address of %qD is not a valid template argument",
6757 else if (!same_type_ignoring_top_level_qualifiers_p
6758 (strip_array_types (TREE_TYPE (type
)),
6759 strip_array_types (TREE_TYPE (decl
))))
6761 if (complain
& tf_error
)
6762 error ("the address of the %qT subobject of %qD is not a "
6763 "valid template argument", TREE_TYPE (type
), decl
);
6766 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6768 if (complain
& tf_error
)
6769 error ("the address of %qD is not a valid template argument "
6770 "because it does not have static storage duration",
6778 expr
= perform_qualification_conversions (type
, expr
);
6779 if (expr
== error_mark_node
)
6780 return error_mark_node
;
6782 /* [temp.arg.nontype]/5, bullet 3
6784 For a non-type template-parameter of type reference to object, no
6785 conversions apply. The type referred to by the reference may be more
6786 cv-qualified than the (otherwise identical) type of the
6787 template-argument. The template-parameter is bound directly to the
6788 template-argument, which must be an lvalue. */
6789 else if (TYPE_REF_OBJ_P (type
))
6791 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6793 return error_mark_node
;
6795 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6797 if (complain
& tf_error
)
6798 error ("%qE is not a valid template argument for type %qT "
6799 "because of conflicts in cv-qualification", expr
, type
);
6803 if (!lvalue_p (expr
))
6805 if (complain
& tf_error
)
6806 error ("%qE is not a valid template argument for type %qT "
6807 "because it is not an lvalue", expr
, type
);
6811 /* [temp.arg.nontype]/1
6813 A template-argument for a non-type, non-template template-parameter
6814 shall be one of: [...]
6816 -- the address of an object or function with external linkage. */
6817 if (INDIRECT_REF_P (expr
)
6818 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6820 expr
= TREE_OPERAND (expr
, 0);
6823 if (complain
& tf_error
)
6824 error ("%q#D is not a valid template argument for type %qT "
6825 "because a reference variable does not have a constant "
6826 "address", expr
, type
);
6831 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6832 && value_dependent_expression_p (expr
))
6833 /* OK, dependent reference. We don't want to ask whether a DECL is
6834 itself value-dependent, since what we want here is its address. */;
6839 if (complain
& tf_error
)
6840 error ("%qE is not a valid template argument for type %qT "
6841 "because it is not an object with linkage",
6846 /* DR 1155 allows internal linkage in C++11 and up. */
6847 linkage_kind linkage
= decl_linkage (expr
);
6848 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6850 if (complain
& tf_error
)
6851 error ("%qE is not a valid template argument for type %qT "
6852 "because object %qD does not have linkage",
6857 expr
= build_address (expr
);
6860 if (!same_type_p (type
, TREE_TYPE (expr
)))
6861 expr
= build_nop (type
, expr
);
6863 /* [temp.arg.nontype]/5, bullet 4
6865 For a non-type template-parameter of type pointer to function, only
6866 the function-to-pointer conversion (_conv.func_) is applied. If the
6867 template-argument represents a set of overloaded functions (or a
6868 pointer to such), the matching function is selected from the set
6870 else if (TYPE_PTRFN_P (type
))
6872 /* If the argument is a template-id, we might not have enough
6873 context information to decay the pointer. */
6874 if (!type_unknown_p (expr_type
))
6876 expr
= decay_conversion (expr
, complain
);
6877 if (expr
== error_mark_node
)
6878 return error_mark_node
;
6881 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6882 /* Null pointer values are OK in C++11. */
6883 return perform_qualification_conversions (type
, expr
);
6885 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6886 if (!expr
|| expr
== error_mark_node
)
6889 /* [temp.arg.nontype]/5, bullet 5
6891 For a non-type template-parameter of type reference to function, no
6892 conversions apply. If the template-argument represents a set of
6893 overloaded functions, the matching function is selected from the set
6895 else if (TYPE_REFFN_P (type
))
6897 if (TREE_CODE (expr
) == ADDR_EXPR
)
6899 if (complain
& tf_error
)
6901 error ("%qE is not a valid template argument for type %qT "
6902 "because it is a pointer", expr
, type
);
6903 inform (input_location
, "try using %qE instead",
6904 TREE_OPERAND (expr
, 0));
6909 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6910 if (!expr
|| expr
== error_mark_node
)
6913 /* [temp.arg.nontype]/5, bullet 6
6915 For a non-type template-parameter of type pointer to member function,
6916 no conversions apply. If the template-argument represents a set of
6917 overloaded member functions, the matching member function is selected
6918 from the set (_over.over_). */
6919 else if (TYPE_PTRMEMFUNC_P (type
))
6921 expr
= instantiate_type (type
, expr
, tf_none
);
6922 if (expr
== error_mark_node
)
6923 return error_mark_node
;
6925 /* [temp.arg.nontype] bullet 1 says the pointer to member
6926 expression must be a pointer-to-member constant. */
6927 if (!value_dependent_expression_p (expr
)
6928 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6931 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6932 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6933 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6934 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6936 /* [temp.arg.nontype]/5, bullet 7
6938 For a non-type template-parameter of type pointer to data member,
6939 qualification conversions (_conv.qual_) are applied. */
6940 else if (TYPE_PTRDATAMEM_P (type
))
6942 /* [temp.arg.nontype] bullet 1 says the pointer to member
6943 expression must be a pointer-to-member constant. */
6944 if (!value_dependent_expression_p (expr
)
6945 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6948 expr
= perform_qualification_conversions (type
, expr
);
6949 if (expr
== error_mark_node
)
6952 else if (NULLPTR_TYPE_P (type
))
6954 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6956 if (complain
& tf_error
)
6957 error ("%qE is not a valid template argument for type %qT "
6958 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6963 /* A template non-type parameter must be one of the above. */
6967 /* Sanity check: did we actually convert the argument to the
6969 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6970 (type
, TREE_TYPE (expr
)));
6971 return convert_from_reference (expr
);
6974 /* Subroutine of coerce_template_template_parms, which returns 1 if
6975 PARM_PARM and ARG_PARM match using the rule for the template
6976 parameters of template template parameters. Both PARM and ARG are
6977 template parameters; the rest of the arguments are the same as for
6978 coerce_template_template_parms.
6981 coerce_template_template_parm (tree parm
,
6983 tsubst_flags_t complain
,
6987 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6988 || parm
== NULL_TREE
|| error_operand_p (parm
))
6991 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6994 switch (TREE_CODE (parm
))
6997 /* We encounter instantiations of templates like
6998 template <template <template <class> class> class TT>
7001 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7002 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7004 if (!coerce_template_template_parms
7005 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7011 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7012 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7013 /* Argument is a parameter pack but parameter is not. */
7018 /* The tsubst call is used to handle cases such as
7020 template <int> class C {};
7021 template <class T, template <T> class TT> class D {};
7024 i.e. the parameter list of TT depends on earlier parameters. */
7025 if (!uses_template_parms (TREE_TYPE (arg
)))
7027 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7028 if (!uses_template_parms (t
)
7029 && !same_type_p (t
, TREE_TYPE (arg
)))
7033 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7034 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7035 /* Argument is a parameter pack but parameter is not. */
7047 /* Coerce template argument list ARGLIST for use with template
7048 template-parameter TEMPL. */
7051 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7052 tsubst_flags_t complain
)
7054 /* Consider an example where a template template parameter declared as
7056 template <class T, class U = std::allocator<T> > class TT
7058 The template parameter level of T and U are one level larger than
7059 of TT. To proper process the default argument of U, say when an
7060 instantiation `TT<int>' is seen, we need to build the full
7061 arguments containing {int} as the innermost level. Outer levels,
7062 available when not appearing as default template argument, can be
7063 obtained from the arguments of the enclosing template.
7065 Suppose that TT is later substituted with std::vector. The above
7066 instantiation is `TT<int, std::allocator<T> >' with TT at
7067 level 1, and T at level 2, while the template arguments at level 1
7068 becomes {std::vector} and the inner level 2 is {int}. */
7070 tree outer
= DECL_CONTEXT (templ
);
7073 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7074 /* We want arguments for the partial specialization, not arguments for
7075 the primary template. */
7076 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7078 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7080 else if (current_template_parms
)
7082 /* This is an argument of the current template, so we haven't set
7083 DECL_CONTEXT yet. */
7084 tree relevant_template_parms
;
7086 /* Parameter levels that are greater than the level of the given
7087 template template parm are irrelevant. */
7088 relevant_template_parms
= current_template_parms
;
7089 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7090 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7091 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7093 outer
= template_parms_to_args (relevant_template_parms
);
7097 arglist
= add_to_template_args (outer
, arglist
);
7099 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7100 return coerce_template_parms (parmlist
, arglist
, templ
,
7102 /*require_all_args=*/true,
7103 /*use_default_args=*/true);
7106 /* A cache of template template parameters with match-all default
7108 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7110 store_defaulted_ttp (tree v
, tree t
)
7112 if (!defaulted_ttp_cache
)
7113 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7114 defaulted_ttp_cache
->put (v
, t
);
7117 lookup_defaulted_ttp (tree v
)
7119 if (defaulted_ttp_cache
)
7120 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7125 /* T is a bound template template-parameter. Copy its arguments into default
7126 arguments of the template template-parameter's template parameters. */
7129 add_defaults_to_ttp (tree otmpl
)
7131 if (tree c
= lookup_defaulted_ttp (otmpl
))
7134 tree ntmpl
= copy_node (otmpl
);
7136 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7137 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7138 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7139 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7140 TYPE_NAME (ntype
) = ntmpl
;
7141 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7143 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7144 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7145 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7146 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7148 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7149 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7150 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7151 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7152 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7154 tree o
= TREE_VEC_ELT (vec
, i
);
7155 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7157 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7158 TREE_PURPOSE (n
) = any_targ_node
;
7162 store_defaulted_ttp (otmpl
, ntmpl
);
7166 /* ARG is a bound potential template template-argument, and PARGS is a list
7167 of arguments for the corresponding template template-parameter. Adjust
7168 PARGS as appropriate for application to ARG's template, and if ARG is a
7169 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7170 arguments to the template template parameter. */
7173 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7175 ++processing_template_decl
;
7176 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7177 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7179 /* When comparing two template template-parameters in partial ordering,
7180 rewrite the one currently being used as an argument to have default
7181 arguments for all parameters. */
7182 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7183 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7184 if (pargs
!= error_mark_node
)
7185 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7186 TYPE_TI_ARGS (arg
));
7191 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7192 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7193 /*require_all*/true,
7194 /*use_default*/true);
7196 --processing_template_decl
;
7200 /* Subroutine of unify for the case when PARM is a
7201 BOUND_TEMPLATE_TEMPLATE_PARM. */
7204 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7207 tree parmvec
= TYPE_TI_ARGS (parm
);
7208 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7210 /* The template template parm might be variadic and the argument
7211 not, so flatten both argument lists. */
7212 parmvec
= expand_template_argument_pack (parmvec
);
7213 argvec
= expand_template_argument_pack (argvec
);
7217 /* In keeping with P0522R0, adjust P's template arguments
7218 to apply to A's template; then flatten it again. */
7219 tree nparmvec
= parmvec
;
7220 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7221 nparmvec
= expand_template_argument_pack (nparmvec
);
7223 if (unify (tparms
, targs
, nparmvec
, argvec
,
7224 UNIFY_ALLOW_NONE
, explain_p
))
7227 /* If the P0522 adjustment eliminated a pack expansion, deduce
7230 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7231 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7232 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7237 /* Deduce arguments T, i from TT<T> or TT<i>.
7238 We check each element of PARMVEC and ARGVEC individually
7239 rather than the whole TREE_VEC since they can have
7240 different number of elements, which is allowed under N2555. */
7242 int len
= TREE_VEC_LENGTH (parmvec
);
7244 /* Check if the parameters end in a pack, making them
7246 int parm_variadic_p
= 0;
7248 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7249 parm_variadic_p
= 1;
7251 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7252 /* If the template argument list of P contains a pack
7253 expansion that is not the last template argument, the
7254 entire template argument list is a non-deduced
7256 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7257 return unify_success (explain_p
);
7259 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7260 return unify_too_few_arguments (explain_p
,
7261 TREE_VEC_LENGTH (argvec
), len
);
7263 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7264 if (unify (tparms
, targs
,
7265 TREE_VEC_ELT (parmvec
, i
),
7266 TREE_VEC_ELT (argvec
, i
),
7267 UNIFY_ALLOW_NONE
, explain_p
))
7271 && unify_pack_expansion (tparms
, targs
,
7274 /*subr=*/true, explain_p
))
7281 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7282 template template parameters. Both PARM_PARMS and ARG_PARMS are
7283 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7286 Consider the example:
7287 template <class T> class A;
7288 template<template <class U> class TT> class B;
7290 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7291 the parameters to A, and OUTER_ARGS contains A. */
7294 coerce_template_template_parms (tree parm_parms
,
7296 tsubst_flags_t complain
,
7300 int nparms
, nargs
, i
;
7304 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7305 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7307 nparms
= TREE_VEC_LENGTH (parm_parms
);
7308 nargs
= TREE_VEC_LENGTH (arg_parms
);
7312 /* P0522R0: A template template-parameter P is at least as specialized as
7313 a template template-argument A if, given the following rewrite to two
7314 function templates, the function template corresponding to P is at
7315 least as specialized as the function template corresponding to A
7316 according to the partial ordering rules for function templates
7317 ([temp.func.order]). Given an invented class template X with the
7318 template parameter list of A (including default arguments):
7320 * Each of the two function templates has the same template parameters,
7321 respectively, as P or A.
7323 * Each function template has a single function parameter whose type is
7324 a specialization of X with template arguments corresponding to the
7325 template parameters from the respective function template where, for
7326 each template parameter PP in the template parameter list of the
7327 function template, a corresponding template argument AA is formed. If
7328 PP declares a parameter pack, then AA is the pack expansion
7329 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7331 If the rewrite produces an invalid type, then P is not at least as
7332 specialized as A. */
7334 /* So coerce P's args to apply to A's parms, and then deduce between A's
7335 args and the converted args. If that succeeds, A is at least as
7336 specialized as P, so they match.*/
7337 tree pargs
= template_parms_level_to_args (parm_parms
);
7338 ++processing_template_decl
;
7339 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7340 /*require_all*/true, /*use_default*/true);
7341 --processing_template_decl
;
7342 if (pargs
!= error_mark_node
)
7344 tree targs
= make_tree_vec (nargs
);
7345 tree aargs
= template_parms_level_to_args (arg_parms
);
7346 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7352 /* Determine whether we have a parameter pack at the end of the
7353 template template parameter's template parameter list. */
7354 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7356 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7358 if (error_operand_p (parm
))
7361 switch (TREE_CODE (parm
))
7365 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7370 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7380 && !(variadic_p
&& nargs
>= nparms
- 1))
7383 /* Check all of the template parameters except the parameter pack at
7384 the end (if any). */
7385 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7387 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7388 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7391 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7392 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7394 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7402 /* Check each of the template parameters in the template
7403 argument against the template parameter pack at the end of
7404 the template template parameter. */
7405 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7408 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7410 for (; i
< nargs
; ++i
)
7412 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7415 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7417 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7426 /* Verifies that the deduced template arguments (in TARGS) for the
7427 template template parameters (in TPARMS) represent valid bindings,
7428 by comparing the template parameter list of each template argument
7429 to the template parameter list of its corresponding template
7430 template parameter, in accordance with DR150. This
7431 routine can only be called after all template arguments have been
7432 deduced. It will return TRUE if all of the template template
7433 parameter bindings are okay, FALSE otherwise. */
7435 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7437 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7440 /* We're dealing with template parms in this process. */
7441 ++processing_template_decl
;
7443 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7445 for (i
= 0; i
< ntparms
; ++i
)
7447 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7448 tree targ
= TREE_VEC_ELT (targs
, i
);
7450 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7452 tree packed_args
= NULL_TREE
;
7455 if (ARGUMENT_PACK_P (targ
))
7457 /* Look inside the argument pack. */
7458 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7459 len
= TREE_VEC_LENGTH (packed_args
);
7462 for (idx
= 0; idx
< len
; ++idx
)
7464 tree targ_parms
= NULL_TREE
;
7467 /* Extract the next argument from the argument
7469 targ
= TREE_VEC_ELT (packed_args
, idx
);
7471 if (PACK_EXPANSION_P (targ
))
7472 /* Look at the pattern of the pack expansion. */
7473 targ
= PACK_EXPANSION_PATTERN (targ
);
7475 /* Extract the template parameters from the template
7477 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7478 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7479 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7480 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7482 /* Verify that we can coerce the template template
7483 parameters from the template argument to the template
7484 parameter. This requires an exact match. */
7486 && !coerce_template_template_parms
7487 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7502 --processing_template_decl
;
7506 /* Since type attributes aren't mangled, we need to strip them from
7507 template type arguments. */
7510 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7512 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7514 bool removed_attributes
= false;
7515 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7516 if (removed_attributes
7517 && (complain
& tf_warning
))
7518 warning (OPT_Wignored_attributes
,
7519 "ignoring attributes on template argument %qT", arg
);
7523 /* And from inside dependent non-type arguments like sizeof(Type). */
7526 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7528 if (!arg
|| arg
== error_mark_node
)
7530 bool removed_attributes
= false;
7531 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7532 if (removed_attributes
7533 && (complain
& tf_warning
))
7534 warning (OPT_Wignored_attributes
,
7535 "ignoring attributes in template argument %qE", arg
);
7539 // A template declaration can be substituted for a constrained
7540 // template template parameter only when the argument is more
7541 // constrained than the parameter.
7543 is_compatible_template_arg (tree parm
, tree arg
)
7545 tree parm_cons
= get_constraints (parm
);
7547 /* For now, allow constrained template template arguments
7548 and unconstrained template template parameters. */
7549 if (parm_cons
== NULL_TREE
)
7552 tree arg_cons
= get_constraints (arg
);
7554 // If the template parameter is constrained, we need to rewrite its
7555 // constraints in terms of the ARG's template parameters. This ensures
7556 // that all of the template parameter types will have the same depth.
7558 // Note that this is only valid when coerce_template_template_parm is
7559 // true for the innermost template parameters of PARM and ARG. In other
7560 // words, because coercion is successful, this conversion will be valid.
7563 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7564 parm_cons
= tsubst_constraint_info (parm_cons
,
7565 INNERMOST_TEMPLATE_ARGS (args
),
7566 tf_none
, NULL_TREE
);
7567 if (parm_cons
== error_mark_node
)
7571 return subsumes (parm_cons
, arg_cons
);
7574 // Convert a placeholder argument into a binding to the original
7575 // parameter. The original parameter is saved as the TREE_TYPE of
7578 convert_wildcard_argument (tree parm
, tree arg
)
7580 TREE_TYPE (arg
) = parm
;
7584 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7585 because one of them is dependent. But we need to represent the
7586 conversion for the benefit of cp_tree_equal. */
7589 maybe_convert_nontype_argument (tree type
, tree arg
)
7591 /* Auto parms get no conversion. */
7592 if (type_uses_auto (type
))
7594 /* We don't need or want to add this conversion now if we're going to use the
7595 argument for deduction. */
7596 if (value_dependent_expression_p (arg
))
7599 type
= cv_unqualified (type
);
7600 tree argtype
= TREE_TYPE (arg
);
7601 if (same_type_p (type
, argtype
))
7604 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7605 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7609 /* Convert the indicated template ARG as necessary to match the
7610 indicated template PARM. Returns the converted ARG, or
7611 error_mark_node if the conversion was unsuccessful. Error and
7612 warning messages are issued under control of COMPLAIN. This
7613 conversion is for the Ith parameter in the parameter list. ARGS is
7614 the full set of template arguments deduced so far. */
7617 convert_template_argument (tree parm
,
7620 tsubst_flags_t complain
,
7626 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7628 if (parm
== error_mark_node
)
7629 return error_mark_node
;
7631 /* Trivially convert placeholders. */
7632 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7633 return convert_wildcard_argument (parm
, arg
);
7635 if (arg
== any_targ_node
)
7638 if (TREE_CODE (arg
) == TREE_LIST
7639 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7641 /* The template argument was the name of some
7642 member function. That's usually
7643 invalid, but static members are OK. In any
7644 case, grab the underlying fields/functions
7645 and issue an error later if required. */
7646 orig_arg
= TREE_VALUE (arg
);
7647 TREE_TYPE (arg
) = unknown_type_node
;
7652 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7653 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7654 || requires_tmpl_type
);
7656 /* When determining whether an argument pack expansion is a template,
7657 look at the pattern. */
7658 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7659 arg
= PACK_EXPANSION_PATTERN (arg
);
7661 /* Deal with an injected-class-name used as a template template arg. */
7662 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7664 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7665 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7667 if (cxx_dialect
>= cxx11
)
7668 /* OK under DR 1004. */;
7669 else if (complain
& tf_warning_or_error
)
7670 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7671 " used as template template argument", TYPE_NAME (arg
));
7672 else if (flag_pedantic_errors
)
7680 ((TREE_CODE (arg
) == TEMPLATE_DECL
7681 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7682 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7683 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7684 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7687 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7688 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7689 arg
= TYPE_STUB_DECL (arg
);
7691 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7693 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7694 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7696 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7698 if (complain
& tf_error
)
7699 error ("invalid use of destructor %qE as a type", orig_arg
);
7700 return error_mark_node
;
7703 permerror (input_location
,
7704 "to refer to a type member of a template parameter, "
7705 "use %<typename %E%>", orig_arg
);
7707 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7708 TREE_OPERAND (arg
, 1),
7714 if (is_type
!= requires_type
)
7718 if (complain
& tf_error
)
7720 error ("type/value mismatch at argument %d in template "
7721 "parameter list for %qD",
7724 inform (input_location
,
7725 " expected a constant of type %qT, got %qT",
7727 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7728 else if (requires_tmpl_type
)
7729 inform (input_location
,
7730 " expected a class template, got %qE", orig_arg
);
7732 inform (input_location
,
7733 " expected a type, got %qE", orig_arg
);
7736 return error_mark_node
;
7738 if (is_tmpl_type
^ requires_tmpl_type
)
7740 if (in_decl
&& (complain
& tf_error
))
7742 error ("type/value mismatch at argument %d in template "
7743 "parameter list for %qD",
7746 inform (input_location
,
7747 " expected a type, got %qT", DECL_NAME (arg
));
7749 inform (input_location
,
7750 " expected a class template, got %qT", orig_arg
);
7752 return error_mark_node
;
7755 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7756 /* We already did the appropriate conversion when packing args. */
7760 if (requires_tmpl_type
)
7762 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7763 /* The number of argument required is not known yet.
7764 Just accept it for now. */
7768 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7771 /* Strip alias templates that are equivalent to another
7773 arg
= get_underlying_template (arg
);
7774 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7776 if (coerce_template_template_parms (parmparm
, argparm
,
7782 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7784 if (val
!= error_mark_node
)
7786 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7787 val
= TREE_TYPE (val
);
7788 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7789 val
= make_pack_expansion (val
, complain
);
7794 if (in_decl
&& (complain
& tf_error
))
7796 error ("type/value mismatch at argument %d in "
7797 "template parameter list for %qD",
7799 inform (input_location
,
7800 " expected a template of type %qD, got %qT",
7804 val
= error_mark_node
;
7807 // Check that the constraints are compatible before allowing the
7809 if (val
!= error_mark_node
)
7810 if (!is_compatible_template_arg (parm
, arg
))
7812 if (in_decl
&& (complain
& tf_error
))
7814 error ("constraint mismatch at argument %d in "
7815 "template parameter list for %qD",
7817 inform (input_location
, " expected %qD but got %qD",
7820 val
= error_mark_node
;
7826 /* We only form one instance of each template specialization.
7827 Therefore, if we use a non-canonical variant (i.e., a
7828 typedef), any future messages referring to the type will use
7829 the typedef, which is confusing if those future uses do not
7830 themselves also use the typedef. */
7832 val
= canonicalize_type_argument (val
, complain
);
7836 tree t
= TREE_TYPE (parm
);
7838 if (tree a
= type_uses_auto (t
))
7840 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7841 if (t
== error_mark_node
)
7842 return error_mark_node
;
7845 t
= tsubst (t
, args
, complain
, in_decl
);
7847 if (invalid_nontype_parm_type_p (t
, complain
))
7848 return error_mark_node
;
7850 if (!type_dependent_expression_p (orig_arg
)
7851 && !uses_template_parms (t
))
7852 /* We used to call digest_init here. However, digest_init
7853 will report errors, which we don't want when complain
7854 is zero. More importantly, digest_init will try too
7855 hard to convert things: for example, `0' should not be
7856 converted to pointer type at this point according to
7857 the standard. Accepting this is not merely an
7858 extension, since deciding whether or not these
7859 conversions can occur is part of determining which
7860 function template to call, or whether a given explicit
7861 argument specification is valid. */
7862 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7865 val
= canonicalize_expr_argument (orig_arg
, complain
);
7866 val
= maybe_convert_nontype_argument (t
, val
);
7870 if (val
== NULL_TREE
)
7871 val
= error_mark_node
;
7872 else if (val
== error_mark_node
&& (complain
& tf_error
))
7873 error ("could not convert template argument %qE from %qT to %qT",
7874 orig_arg
, TREE_TYPE (orig_arg
), t
);
7876 if (INDIRECT_REF_P (val
))
7878 /* Reject template arguments that are references to built-in
7879 functions with no library fallbacks. */
7880 const_tree inner
= TREE_OPERAND (val
, 0);
7881 const_tree innertype
= TREE_TYPE (inner
);
7883 && TREE_CODE (innertype
) == REFERENCE_TYPE
7884 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7885 && TREE_OPERAND_LENGTH (inner
) > 0
7886 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7887 return error_mark_node
;
7890 if (TREE_CODE (val
) == SCOPE_REF
)
7892 /* Strip typedefs from the SCOPE_REF. */
7893 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7894 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7896 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7897 QUALIFIED_NAME_IS_TEMPLATE (val
));
7904 /* Coerces the remaining template arguments in INNER_ARGS (from
7905 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7906 Returns the coerced argument pack. PARM_IDX is the position of this
7907 parameter in the template parameter list. ARGS is the original
7908 template argument list. */
7910 coerce_template_parameter_pack (tree parms
,
7918 tsubst_flags_t complain
)
7920 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7921 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7924 tree packed_parms
= NULL_TREE
;
7926 if (arg_idx
> nargs
)
7929 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7931 /* When the template parameter is a non-type template parameter pack
7932 or template template parameter pack whose type or template
7933 parameters use parameter packs, we know exactly how many arguments
7934 we are looking for. Build a vector of the instantiated decls for
7935 these template parameters in PACKED_PARMS. */
7936 /* We can't use make_pack_expansion here because it would interpret a
7937 _DECL as a use rather than a declaration. */
7938 tree decl
= TREE_VALUE (parm
);
7939 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7940 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7941 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7942 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7944 TREE_VEC_LENGTH (args
)--;
7945 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7946 TREE_VEC_LENGTH (args
)++;
7948 if (packed_parms
== error_mark_node
)
7949 return error_mark_node
;
7951 /* If we're doing a partial instantiation of a member template,
7952 verify that all of the types used for the non-type
7953 template parameter pack are, in fact, valid for non-type
7954 template parameters. */
7956 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7958 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7959 for (j
= 0; j
< len
; ++j
)
7961 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7962 if (invalid_nontype_parm_type_p (t
, complain
))
7963 return error_mark_node
;
7965 /* We don't know how many args we have yet, just
7966 use the unconverted ones for now. */
7970 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7972 /* Check if we have a placeholder pack, which indicates we're
7973 in the context of a introduction list. In that case we want
7974 to match this pack to the single placeholder. */
7975 else if (arg_idx
< nargs
7976 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7977 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7979 nargs
= arg_idx
+ 1;
7980 packed_args
= make_tree_vec (1);
7983 packed_args
= make_tree_vec (nargs
- arg_idx
);
7985 /* Convert the remaining arguments, which will be a part of the
7986 parameter pack "parm". */
7987 int first_pack_arg
= arg_idx
;
7988 for (; arg_idx
< nargs
; ++arg_idx
)
7990 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7991 tree actual_parm
= TREE_VALUE (parm
);
7992 int pack_idx
= arg_idx
- first_pack_arg
;
7996 /* Once we've packed as many args as we have types, stop. */
7997 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7999 else if (PACK_EXPANSION_P (arg
))
8000 /* We don't know how many args we have yet, just
8001 use the unconverted ones for now. */
8004 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8007 if (arg
== error_mark_node
)
8009 if (complain
& tf_error
)
8010 error ("template argument %d is invalid", arg_idx
+ 1);
8013 arg
= convert_template_argument (actual_parm
,
8014 arg
, new_args
, complain
, parm_idx
,
8016 if (arg
== error_mark_node
)
8018 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8021 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8022 && TREE_VEC_LENGTH (packed_args
) > 0)
8024 if (complain
& tf_error
)
8025 error ("wrong number of template arguments (%d, should be %d)",
8026 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8027 return error_mark_node
;
8030 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8031 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8032 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8035 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8036 TREE_CONSTANT (argument_pack
) = 1;
8039 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8041 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8042 TREE_VEC_LENGTH (packed_args
));
8043 return argument_pack
;
8046 /* Returns the number of pack expansions in the template argument vector
8050 pack_expansion_args_count (tree args
)
8055 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8057 tree elt
= TREE_VEC_ELT (args
, i
);
8058 if (elt
&& PACK_EXPANSION_P (elt
))
8064 /* Convert all template arguments to their appropriate types, and
8065 return a vector containing the innermost resulting template
8066 arguments. If any error occurs, return error_mark_node. Error and
8067 warning messages are issued under control of COMPLAIN.
8069 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8070 for arguments not specified in ARGS. Otherwise, if
8071 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8072 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8073 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8077 coerce_template_parms (tree parms
,
8080 tsubst_flags_t complain
,
8081 bool require_all_args
,
8082 bool use_default_args
)
8084 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8085 tree orig_inner_args
;
8088 tree new_inner_args
;
8089 int saved_unevaluated_operand
;
8090 int saved_inhibit_evaluation_warnings
;
8092 /* When used as a boolean value, indicates whether this is a
8093 variadic template parameter list. Since it's an int, we can also
8094 subtract it from nparms to get the number of non-variadic
8097 int variadic_args_p
= 0;
8098 int post_variadic_parms
= 0;
8100 /* Likewise for parameters with default arguments. */
8103 if (args
== error_mark_node
)
8104 return error_mark_node
;
8106 nparms
= TREE_VEC_LENGTH (parms
);
8108 /* Determine if there are any parameter packs or default arguments. */
8109 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8111 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8113 ++post_variadic_parms
;
8114 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8116 if (TREE_PURPOSE (parm
))
8120 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8121 /* If there are no parameters that follow a parameter pack, we need to
8122 expand any argument packs so that we can deduce a parameter pack from
8123 some non-packed args followed by an argument pack, as in variadic85.C.
8124 If there are such parameters, we need to leave argument packs intact
8125 so the arguments are assigned properly. This can happen when dealing
8126 with a nested class inside a partial specialization of a class
8127 template, as in variadic92.C, or when deducing a template parameter pack
8128 from a sub-declarator, as in variadic114.C. */
8129 if (!post_variadic_parms
)
8130 inner_args
= expand_template_argument_pack (inner_args
);
8132 /* Count any pack expansion args. */
8133 variadic_args_p
= pack_expansion_args_count (inner_args
);
8135 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8136 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8137 || (nargs
< nparms
- variadic_p
8140 && (!use_default_args
8141 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8142 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8144 if (complain
& tf_error
)
8146 if (variadic_p
|| default_p
)
8148 nparms
-= variadic_p
+ default_p
;
8149 error ("wrong number of template arguments "
8150 "(%d, should be at least %d)", nargs
, nparms
);
8153 error ("wrong number of template arguments "
8154 "(%d, should be %d)", nargs
, nparms
);
8157 inform (DECL_SOURCE_LOCATION (in_decl
),
8158 "provided for %qD", in_decl
);
8161 return error_mark_node
;
8163 /* We can't pass a pack expansion to a non-pack parameter of an alias
8164 template (DR 1430). */
8166 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8167 || concept_template_p (in_decl
))
8169 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8171 if (complain
& tf_error
)
8173 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8175 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8176 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8178 if (PACK_EXPANSION_P (arg
)
8179 && !template_parameter_pack_p (parm
))
8181 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8182 error_at (location_of (arg
),
8183 "pack expansion argument for non-pack parameter "
8184 "%qD of alias template %qD", parm
, in_decl
);
8186 error_at (location_of (arg
),
8187 "pack expansion argument for non-pack parameter "
8188 "%qD of concept %qD", parm
, in_decl
);
8189 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8196 return error_mark_node
;
8199 /* We need to evaluate the template arguments, even though this
8200 template-id may be nested within a "sizeof". */
8201 saved_unevaluated_operand
= cp_unevaluated_operand
;
8202 cp_unevaluated_operand
= 0;
8203 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8204 c_inhibit_evaluation_warnings
= 0;
8205 new_inner_args
= make_tree_vec (nparms
);
8206 new_args
= add_outermost_template_args (args
, new_inner_args
);
8207 int pack_adjust
= 0;
8208 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8213 /* Get the Ith template parameter. */
8214 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8216 if (parm
== error_mark_node
)
8218 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8222 /* Calculate the next argument. */
8223 if (arg_idx
< nargs
)
8224 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8228 if (template_parameter_pack_p (TREE_VALUE (parm
))
8229 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8231 /* Some arguments will be placed in the
8232 template parameter pack PARM. */
8233 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8234 inner_args
, arg_idx
,
8238 if (arg
== NULL_TREE
)
8240 /* We don't know how many args we have yet, just use the
8241 unconverted (and still packed) ones for now. */
8242 new_inner_args
= orig_inner_args
;
8247 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8249 /* Store this argument. */
8250 if (arg
== error_mark_node
)
8253 /* We are done with all of the arguments. */
8258 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8259 arg_idx
+= pack_adjust
;
8266 if (PACK_EXPANSION_P (arg
))
8268 /* "If every valid specialization of a variadic template
8269 requires an empty template parameter pack, the template is
8270 ill-formed, no diagnostic required." So check that the
8271 pattern works with this parameter. */
8272 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8273 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8277 if (conv
== error_mark_node
)
8279 if (complain
& tf_error
)
8280 inform (input_location
, "so any instantiation with a "
8281 "non-empty parameter pack would be ill-formed");
8284 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8285 /* Recover from missing typename. */
8286 TREE_VEC_ELT (inner_args
, arg_idx
)
8287 = make_pack_expansion (conv
, complain
);
8289 /* We don't know how many args we have yet, just
8290 use the unconverted ones for now. */
8291 new_inner_args
= inner_args
;
8296 else if (require_all_args
)
8298 /* There must be a default arg in this case. */
8299 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8301 /* The position of the first default template argument,
8302 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8304 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8305 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8306 arg_idx
- pack_adjust
);
8311 if (arg
== error_mark_node
)
8313 if (complain
& tf_error
)
8314 error ("template argument %d is invalid", arg_idx
+ 1);
8317 /* This only occurs if there was an error in the template
8318 parameter list itself (which we would already have
8319 reported) that we are trying to recover from, e.g., a class
8320 template with a parameter list such as
8321 template<typename..., typename>. */
8324 arg
= convert_template_argument (TREE_VALUE (parm
),
8325 arg
, new_args
, complain
,
8328 if (arg
== error_mark_node
)
8330 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8332 cp_unevaluated_operand
= saved_unevaluated_operand
;
8333 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8335 if (variadic_p
&& arg_idx
< nargs
)
8337 if (complain
& tf_error
)
8339 error ("wrong number of template arguments "
8340 "(%d, should be %d)", nargs
, arg_idx
);
8342 error ("provided for %q+D", in_decl
);
8344 return error_mark_node
;
8348 return error_mark_node
;
8350 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8351 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8352 TREE_VEC_LENGTH (new_inner_args
));
8354 return new_inner_args
;
8357 /* Convert all template arguments to their appropriate types, and
8358 return a vector containing the innermost resulting template
8359 arguments. If any error occurs, return error_mark_node. Error and
8360 warning messages are not issued.
8362 Note that no function argument deduction is performed, and default
8363 arguments are used to fill in unspecified arguments. */
8365 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8367 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8370 /* Convert all template arguments to their appropriate type, and
8371 instantiate default arguments as needed. This returns a vector
8372 containing the innermost resulting template arguments, or
8373 error_mark_node if unsuccessful. */
8375 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8376 tsubst_flags_t complain
)
8378 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8381 /* Like coerce_template_parms. If PARMS represents all template
8382 parameters levels, this function returns a vector of vectors
8383 representing all the resulting argument levels. Note that in this
8384 case, only the innermost arguments are coerced because the
8385 outermost ones are supposed to have been coerced already.
8387 Otherwise, if PARMS represents only (the innermost) vector of
8388 parameters, this function returns a vector containing just the
8389 innermost resulting arguments. */
8392 coerce_innermost_template_parms (tree parms
,
8395 tsubst_flags_t complain
,
8396 bool require_all_args
,
8397 bool use_default_args
)
8399 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8400 int args_depth
= TMPL_ARGS_DEPTH (args
);
8403 if (parms_depth
> 1)
8405 coerced_args
= make_tree_vec (parms_depth
);
8409 for (level
= parms
, cur_depth
= parms_depth
;
8410 parms_depth
> 0 && level
!= NULL_TREE
;
8411 level
= TREE_CHAIN (level
), --cur_depth
)
8414 if (cur_depth
== args_depth
)
8415 l
= coerce_template_parms (TREE_VALUE (level
),
8416 args
, in_decl
, complain
,
8420 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8422 if (l
== error_mark_node
)
8423 return error_mark_node
;
8425 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8429 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8430 args
, in_decl
, complain
,
8433 return coerced_args
;
8436 /* Returns 1 if template args OT and NT are equivalent. */
8439 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8443 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8445 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8448 if (TREE_CODE (nt
) == TREE_VEC
)
8449 /* For member templates */
8450 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8451 else if (PACK_EXPANSION_P (ot
))
8452 return (PACK_EXPANSION_P (nt
)
8453 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8454 PACK_EXPANSION_PATTERN (nt
))
8455 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8456 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8457 else if (ARGUMENT_PACK_P (ot
))
8462 if (!ARGUMENT_PACK_P (nt
))
8465 opack
= ARGUMENT_PACK_ARGS (ot
);
8466 npack
= ARGUMENT_PACK_ARGS (nt
);
8467 len
= TREE_VEC_LENGTH (opack
);
8468 if (TREE_VEC_LENGTH (npack
) != len
)
8470 for (i
= 0; i
< len
; ++i
)
8471 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8472 TREE_VEC_ELT (npack
, i
)))
8476 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8478 else if (TYPE_P (nt
))
8482 /* Don't treat an alias template specialization with dependent
8483 arguments as equivalent to its underlying type when used as a
8484 template argument; we need them to be distinct so that we
8485 substitute into the specialization arguments at instantiation
8486 time. And aliases can't be equivalent without being ==, so
8487 we don't need to look any deeper.
8489 During partial ordering, however, we need to treat them normally so
8490 that we can order uses of the same alias with different
8491 cv-qualification (79960). */
8493 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8496 return same_type_p (ot
, nt
);
8498 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8502 /* Try to treat a template non-type argument that has been converted
8503 to the parameter type as equivalent to one that hasn't yet. */
8504 for (enum tree_code code1
= TREE_CODE (ot
);
8505 CONVERT_EXPR_CODE_P (code1
)
8506 || code1
== NON_LVALUE_EXPR
;
8507 code1
= TREE_CODE (ot
))
8508 ot
= TREE_OPERAND (ot
, 0);
8509 for (enum tree_code code2
= TREE_CODE (nt
);
8510 CONVERT_EXPR_CODE_P (code2
)
8511 || code2
== NON_LVALUE_EXPR
;
8512 code2
= TREE_CODE (nt
))
8513 nt
= TREE_OPERAND (nt
, 0);
8515 return cp_tree_equal (ot
, nt
);
8519 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8520 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8521 NEWARG_PTR with the offending arguments if they are non-NULL. */
8524 comp_template_args (tree oldargs
, tree newargs
,
8525 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8530 if (oldargs
== newargs
)
8533 if (!oldargs
|| !newargs
)
8536 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8539 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8541 tree nt
= TREE_VEC_ELT (newargs
, i
);
8542 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8544 if (! template_args_equal (ot
, nt
, partial_order
))
8546 if (oldarg_ptr
!= NULL
)
8548 if (newarg_ptr
!= NULL
)
8557 comp_template_args_porder (tree oargs
, tree nargs
)
8559 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8563 add_pending_template (tree d
)
8565 tree ti
= (TYPE_P (d
)
8566 ? CLASSTYPE_TEMPLATE_INFO (d
)
8567 : DECL_TEMPLATE_INFO (d
));
8568 struct pending_template
*pt
;
8571 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8574 /* We are called both from instantiate_decl, where we've already had a
8575 tinst_level pushed, and instantiate_template, where we haven't.
8577 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8580 push_tinst_level (d
);
8582 pt
= ggc_alloc
<pending_template
> ();
8584 pt
->tinst
= current_tinst_level
;
8585 if (last_pending_template
)
8586 last_pending_template
->next
= pt
;
8588 pending_templates
= pt
;
8590 last_pending_template
= pt
;
8592 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8599 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8600 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8601 documentation for TEMPLATE_ID_EXPR. */
8604 lookup_template_function (tree fns
, tree arglist
)
8608 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8609 return error_mark_node
;
8611 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8613 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8615 error ("%q#D is not a function template", fns
);
8616 return error_mark_node
;
8619 if (BASELINK_P (fns
))
8621 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8623 BASELINK_FUNCTIONS (fns
),
8628 type
= TREE_TYPE (fns
);
8629 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8630 type
= unknown_type_node
;
8632 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8635 /* Within the scope of a template class S<T>, the name S gets bound
8636 (in build_self_reference) to a TYPE_DECL for the class, not a
8637 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8638 or one of its enclosing classes, and that type is a template,
8639 return the associated TEMPLATE_DECL. Otherwise, the original
8642 Also handle the case when DECL is a TREE_LIST of ambiguous
8643 injected-class-names from different bases. */
8646 maybe_get_template_decl_from_type_decl (tree decl
)
8648 if (decl
== NULL_TREE
)
8651 /* DR 176: A lookup that finds an injected-class-name (10.2
8652 [class.member.lookup]) can result in an ambiguity in certain cases
8653 (for example, if it is found in more than one base class). If all of
8654 the injected-class-names that are found refer to specializations of
8655 the same class template, and if the name is followed by a
8656 template-argument-list, the reference refers to the class template
8657 itself and not a specialization thereof, and is not ambiguous. */
8658 if (TREE_CODE (decl
) == TREE_LIST
)
8660 tree t
, tmpl
= NULL_TREE
;
8661 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8663 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8666 else if (tmpl
!= elt
)
8669 if (tmpl
&& t
== NULL_TREE
)
8675 return (decl
!= NULL_TREE
8676 && DECL_SELF_REFERENCE_P (decl
)
8677 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8678 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8681 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8682 parameters, find the desired type.
8684 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8686 IN_DECL, if non-NULL, is the template declaration we are trying to
8689 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8690 the class we are looking up.
8692 Issue error and warning messages under control of COMPLAIN.
8694 If the template class is really a local class in a template
8695 function, then the FUNCTION_CONTEXT is the function in which it is
8698 ??? Note that this function is currently called *twice* for each
8699 template-id: the first time from the parser, while creating the
8700 incomplete type (finish_template_type), and the second type during the
8701 real instantiation (instantiate_template_class). This is surely something
8702 that we want to avoid. It also causes some problems with argument
8703 coercion (see convert_nontype_argument for more information on this). */
8706 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8707 int entering_scope
, tsubst_flags_t complain
)
8709 tree templ
= NULL_TREE
, parmlist
;
8716 if (identifier_p (d1
))
8718 tree value
= innermost_non_namespace_value (d1
);
8719 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8724 push_decl_namespace (context
);
8725 templ
= lookup_name (d1
);
8726 templ
= maybe_get_template_decl_from_type_decl (templ
);
8728 pop_decl_namespace ();
8731 context
= DECL_CONTEXT (templ
);
8733 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8735 tree type
= TREE_TYPE (d1
);
8737 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8738 an implicit typename for the second A. Deal with it. */
8739 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8740 type
= TREE_TYPE (type
);
8742 if (CLASSTYPE_TEMPLATE_INFO (type
))
8744 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8745 d1
= DECL_NAME (templ
);
8748 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8749 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8751 templ
= TYPE_TI_TEMPLATE (d1
);
8752 d1
= DECL_NAME (templ
);
8754 else if (DECL_TYPE_TEMPLATE_P (d1
))
8757 d1
= DECL_NAME (templ
);
8758 context
= DECL_CONTEXT (templ
);
8760 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8763 d1
= DECL_NAME (templ
);
8766 /* Issue an error message if we didn't find a template. */
8769 if (complain
& tf_error
)
8770 error ("%qT is not a template", d1
);
8771 return error_mark_node
;
8774 if (TREE_CODE (templ
) != TEMPLATE_DECL
8775 /* Make sure it's a user visible template, if it was named by
8777 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8778 && !PRIMARY_TEMPLATE_P (templ
)))
8780 if (complain
& tf_error
)
8782 error ("non-template type %qT used as a template", d1
);
8784 error ("for template declaration %q+D", in_decl
);
8786 return error_mark_node
;
8789 complain
&= ~tf_user
;
8791 /* An alias that just changes the name of a template is equivalent to the
8792 other template, so if any of the arguments are pack expansions, strip
8793 the alias to avoid problems with a pack expansion passed to a non-pack
8794 alias template parameter (DR 1430). */
8795 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8796 templ
= get_underlying_template (templ
);
8798 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8801 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8802 if (arglist2
== error_mark_node
8803 || (!uses_template_parms (arglist2
)
8804 && check_instantiated_args (templ
, arglist2
, complain
)))
8805 return error_mark_node
;
8807 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8812 tree template_type
= TREE_TYPE (templ
);
8815 tree found
= NULL_TREE
;
8818 int is_dependent_type
;
8819 int use_partial_inst_tmpl
= false;
8821 if (template_type
== error_mark_node
)
8822 /* An error occurred while building the template TEMPL, and a
8823 diagnostic has most certainly been emitted for that
8824 already. Let's propagate that error. */
8825 return error_mark_node
;
8827 gen_tmpl
= most_general_template (templ
);
8828 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8829 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8830 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8832 if (arg_depth
== 1 && parm_depth
> 1)
8834 /* We've been given an incomplete set of template arguments.
8837 template <class T> struct S1 {
8838 template <class U> struct S2 {};
8839 template <class U> struct S2<U*> {};
8842 we will be called with an ARGLIST of `U*', but the
8843 TEMPLATE will be `template <class T> template
8844 <class U> struct S1<T>::S2'. We must fill in the missing
8846 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8847 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8848 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8851 /* Now we should have enough arguments. */
8852 gcc_assert (parm_depth
== arg_depth
);
8854 /* From here on, we're only interested in the most general
8857 /* Calculate the BOUND_ARGS. These will be the args that are
8858 actually tsubst'd into the definition to create the
8860 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8862 /*require_all_args=*/true,
8863 /*use_default_args=*/true);
8865 if (arglist
== error_mark_node
)
8866 /* We were unable to bind the arguments. */
8867 return error_mark_node
;
8869 /* In the scope of a template class, explicit references to the
8870 template class refer to the type of the template, not any
8871 instantiation of it. For example, in:
8873 template <class T> class C { void f(C<T>); }
8875 the `C<T>' is just the same as `C'. Outside of the
8876 class, however, such a reference is an instantiation. */
8878 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8879 || currently_open_class (template_type
))
8881 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8883 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8884 return template_type
;
8887 /* If we already have this specialization, return it. */
8888 elt
.tmpl
= gen_tmpl
;
8890 elt
.spec
= NULL_TREE
;
8891 hash
= spec_hasher::hash (&elt
);
8892 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8897 /* If the the template's constraints are not satisfied,
8898 then we cannot form a valid type.
8900 Note that the check is deferred until after the hash
8901 lookup. This prevents redundant checks on previously
8902 instantiated specializations. */
8903 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8905 if (complain
& tf_error
)
8907 error ("template constraint failure");
8908 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8910 return error_mark_node
;
8913 is_dependent_type
= uses_template_parms (arglist
);
8915 /* If the deduced arguments are invalid, then the binding
8917 if (!is_dependent_type
8918 && check_instantiated_args (gen_tmpl
,
8919 INNERMOST_TEMPLATE_ARGS (arglist
),
8921 return error_mark_node
;
8923 if (!is_dependent_type
8924 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8925 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8926 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8928 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8929 DECL_NAME (gen_tmpl
),
8930 /*tag_scope=*/ts_global
);
8934 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8936 if (context
== error_mark_node
)
8937 return error_mark_node
;
8940 context
= global_namespace
;
8942 /* Create the type. */
8943 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8945 /* The user referred to a specialization of an alias
8946 template represented by GEN_TMPL.
8948 [temp.alias]/2 says:
8950 When a template-id refers to the specialization of an
8951 alias template, it is equivalent to the associated
8952 type obtained by substitution of its
8953 template-arguments for the template-parameters in the
8954 type-id of the alias template. */
8956 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8957 /* Note that the call above (by indirectly calling
8958 register_specialization in tsubst_decl) registers the
8959 TYPE_DECL representing the specialization of the alias
8960 template. So next time someone substitutes ARGLIST for
8961 the template parms into the alias template (GEN_TMPL),
8962 she'll get that TYPE_DECL back. */
8964 if (t
== error_mark_node
)
8967 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8969 if (!is_dependent_type
)
8971 set_current_access_from_decl (TYPE_NAME (template_type
));
8972 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8973 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8974 arglist
, complain
, in_decl
),
8975 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8976 arglist
, complain
, in_decl
),
8977 SCOPED_ENUM_P (template_type
), NULL
);
8979 if (t
== error_mark_node
)
8984 /* We don't want to call start_enum for this type, since
8985 the values for the enumeration constants may involve
8986 template parameters. And, no one should be interested
8987 in the enumeration constants for such a type. */
8988 t
= cxx_make_type (ENUMERAL_TYPE
);
8989 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8991 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8992 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8993 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8995 else if (CLASS_TYPE_P (template_type
))
8997 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8998 instantiated here. */
8999 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9001 t
= make_class_type (TREE_CODE (template_type
));
9002 CLASSTYPE_DECLARED_CLASS (t
)
9003 = CLASSTYPE_DECLARED_CLASS (template_type
);
9004 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9006 /* A local class. Make sure the decl gets registered properly. */
9007 if (context
== current_function_decl
)
9008 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
9010 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9011 /* This instantiation is another name for the primary
9012 template type. Set the TYPE_CANONICAL field
9014 TYPE_CANONICAL (t
) = template_type
;
9015 else if (any_template_arguments_need_structural_equality_p (arglist
))
9016 /* Some of the template arguments require structural
9017 equality testing, so this template class requires
9018 structural equality testing. */
9019 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9024 /* If we called start_enum or pushtag above, this information
9025 will already be set up. */
9028 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9030 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9031 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9032 DECL_SOURCE_LOCATION (type_decl
)
9033 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9036 type_decl
= TYPE_NAME (t
);
9038 if (CLASS_TYPE_P (template_type
))
9040 TREE_PRIVATE (type_decl
)
9041 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9042 TREE_PROTECTED (type_decl
)
9043 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9044 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9046 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9047 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9051 if (OVERLOAD_TYPE_P (t
)
9052 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9054 static const char *tags
[] = {"abi_tag", "may_alias"};
9056 for (unsigned ix
= 0; ix
!= 2; ix
++)
9059 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9063 = tree_cons (TREE_PURPOSE (attributes
),
9064 TREE_VALUE (attributes
),
9065 TYPE_ATTRIBUTES (t
));
9069 /* Let's consider the explicit specialization of a member
9070 of a class template specialization that is implicitly instantiated,
9075 template<class U> struct M {}; //#0
9080 struct S<int>::M<char> //#1
9084 [temp.expl.spec]/4 says this is valid.
9086 In this case, when we write:
9089 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9092 When we encounter #1, we want to store the partial instantiation
9093 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9095 For all cases other than this "explicit specialization of member of a
9096 class template", we just want to store the most general template into
9097 the CLASSTYPE_TI_TEMPLATE of M.
9099 This case of "explicit specialization of member of a class template"
9101 1/ the enclosing class is an instantiation of, and therefore not
9102 the same as, the context of the most general template, and
9103 2/ we aren't looking at the partial instantiation itself, i.e.
9104 the innermost arguments are not the same as the innermost parms of
9105 the most general template.
9107 So it's only when 1/ and 2/ happens that we want to use the partial
9108 instantiation of the member template in lieu of its most general
9111 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9112 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9113 /* the enclosing class must be an instantiation... */
9114 && CLASS_TYPE_P (context
)
9115 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9117 TREE_VEC_LENGTH (arglist
)--;
9118 ++processing_template_decl
;
9119 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9120 tree partial_inst_args
=
9121 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9122 arglist
, complain
, NULL_TREE
);
9123 --processing_template_decl
;
9124 TREE_VEC_LENGTH (arglist
)++;
9125 if (partial_inst_args
== error_mark_node
)
9126 return error_mark_node
;
9127 use_partial_inst_tmpl
=
9128 /*...and we must not be looking at the partial instantiation
9130 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9134 if (!use_partial_inst_tmpl
)
9135 /* This case is easy; there are no member templates involved. */
9139 /* This is a full instantiation of a member template. Find
9140 the partial instantiation of which this is an instance. */
9142 /* Temporarily reduce by one the number of levels in the ARGLIST
9143 so as to avoid comparing the last set of arguments. */
9144 TREE_VEC_LENGTH (arglist
)--;
9145 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9146 TREE_VEC_LENGTH (arglist
)++;
9147 /* FOUND is either a proper class type, or an alias
9148 template specialization. In the later case, it's a
9149 TYPE_DECL, resulting from the substituting of arguments
9150 for parameters in the TYPE_DECL of the alias template
9151 done earlier. So be careful while getting the template
9153 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9155 : (TREE_CODE (found
) == TYPE_DECL
9156 ? DECL_TI_TEMPLATE (found
)
9157 : CLASSTYPE_TI_TEMPLATE (found
)));
9160 // Build template info for the new specialization.
9161 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9164 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9165 entry
= ggc_alloc
<spec_entry
> ();
9169 /* Note this use of the partial instantiation so we can check it
9170 later in maybe_process_partial_specialization. */
9171 DECL_TEMPLATE_INSTANTIATIONS (found
)
9172 = tree_cons (arglist
, t
,
9173 DECL_TEMPLATE_INSTANTIATIONS (found
));
9175 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9176 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9177 /* Now that the type has been registered on the instantiations
9178 list, we set up the enumerators. Because the enumeration
9179 constants may involve the enumeration type itself, we make
9180 sure to register the type first, and then create the
9181 constants. That way, doing tsubst_expr for the enumeration
9182 constants won't result in recursive calls here; we'll find
9183 the instantiation and exit above. */
9184 tsubst_enum (template_type
, t
, arglist
);
9186 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9187 /* If the type makes use of template parameters, the
9188 code that generates debugging information will crash. */
9189 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9191 /* Possibly limit visibility based on template args. */
9192 TREE_PUBLIC (type_decl
) = 1;
9193 determine_visibility (type_decl
);
9195 inherit_targ_abi_tags (t
);
9201 /* Wrapper for lookup_template_class_1. */
9204 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9205 int entering_scope
, tsubst_flags_t complain
)
9208 timevar_push (TV_TEMPLATE_INST
);
9209 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9210 entering_scope
, complain
);
9211 timevar_pop (TV_TEMPLATE_INST
);
9215 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9218 lookup_template_variable (tree templ
, tree arglist
)
9220 /* The type of the expression is NULL_TREE since the template-id could refer
9221 to an explicit or partial specialization. */
9222 tree type
= NULL_TREE
;
9223 if (flag_concepts
&& variable_concept_p (templ
))
9224 /* Except that concepts are always bool. */
9225 type
= boolean_type_node
;
9226 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9229 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9232 finish_template_variable (tree var
, tsubst_flags_t complain
)
9234 tree templ
= TREE_OPERAND (var
, 0);
9235 tree arglist
= TREE_OPERAND (var
, 1);
9237 /* We never want to return a VAR_DECL for a variable concept, since they
9238 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9239 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9240 if (concept_p
&& processing_template_decl
)
9243 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9244 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9246 templ
= most_general_template (templ
);
9247 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9248 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9250 /*use_default*/true);
9252 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9254 if (complain
& tf_error
)
9256 error ("use of invalid variable template %qE", var
);
9257 diagnose_constraints (location_of (var
), templ
, arglist
);
9259 return error_mark_node
;
9262 /* If a template-id refers to a specialization of a variable
9263 concept, then the expression is true if and only if the
9264 concept's constraints are satisfied by the given template
9267 NOTE: This is an extension of Concepts Lite TS that
9268 allows constraints to be used in expressions. */
9271 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9272 return evaluate_variable_concept (decl
, arglist
);
9275 return instantiate_template (templ
, arglist
, complain
);
9278 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9279 TARGS template args, and instantiate it if it's not dependent. */
9282 lookup_and_finish_template_variable (tree templ
, tree targs
,
9283 tsubst_flags_t complain
)
9285 templ
= lookup_template_variable (templ
, targs
);
9286 if (!any_dependent_template_arguments_p (targs
))
9288 templ
= finish_template_variable (templ
, complain
);
9292 return convert_from_reference (templ
);
9301 /* True when we should also visit template parameters that occur in
9302 non-deduced contexts. */
9303 bool include_nondeduced_p
;
9304 hash_set
<tree
> *visited
;
9307 /* Called from for_each_template_parm via walk_tree. */
9310 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9313 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9314 tree_fn_t fn
= pfd
->fn
;
9315 void *data
= pfd
->data
;
9316 tree result
= NULL_TREE
;
9318 #define WALK_SUBTREE(NODE) \
9321 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9322 pfd->include_nondeduced_p, \
9324 if (result) goto out; \
9328 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9332 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9333 WALK_SUBTREE (TYPE_CONTEXT (t
));
9335 switch (TREE_CODE (t
))
9338 if (TYPE_PTRMEMFUNC_P (t
))
9344 if (!TYPE_TEMPLATE_INFO (t
))
9347 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9351 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9352 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9356 /* Since we're not going to walk subtrees, we have to do this
9358 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9362 /* Check the return type. */
9363 WALK_SUBTREE (TREE_TYPE (t
));
9365 /* Check the parameter types. Since default arguments are not
9366 instantiated until they are needed, the TYPE_ARG_TYPES may
9367 contain expressions that involve template parameters. But,
9368 no-one should be looking at them yet. And, once they're
9369 instantiated, they don't contain template parameters, so
9370 there's no point in looking at them then, either. */
9374 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9375 WALK_SUBTREE (TREE_VALUE (parm
));
9377 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9378 want walk_tree walking into them itself. */
9382 if (flag_noexcept_type
)
9384 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9386 WALK_SUBTREE (TREE_PURPOSE (spec
));
9391 case UNDERLYING_TYPE
:
9392 if (pfd
->include_nondeduced_p
9393 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9395 pfd
->include_nondeduced_p
,
9397 return error_mark_node
;
9402 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9403 WALK_SUBTREE (DECL_TI_ARGS (t
));
9408 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9409 WALK_SUBTREE (DECL_INITIAL (t
));
9410 if (DECL_CONTEXT (t
)
9411 && pfd
->include_nondeduced_p
)
9412 WALK_SUBTREE (DECL_CONTEXT (t
));
9415 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9416 /* Record template parameters such as `T' inside `TT<T>'. */
9417 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9420 case TEMPLATE_TEMPLATE_PARM
:
9421 case TEMPLATE_TYPE_PARM
:
9422 case TEMPLATE_PARM_INDEX
:
9423 if (fn
&& (*fn
)(t
, data
))
9430 /* A template template parameter is encountered. */
9431 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9432 WALK_SUBTREE (TREE_TYPE (t
));
9434 /* Already substituted template template parameter */
9439 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9440 partial instantiation. */
9441 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9445 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9446 && pfd
->include_nondeduced_p
)
9447 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9452 /* If there's no type, then this thing must be some expression
9453 involving template parameters. */
9454 if (!fn
&& !TREE_TYPE (t
))
9455 return error_mark_node
;
9460 case IMPLICIT_CONV_EXPR
:
9461 case REINTERPRET_CAST_EXPR
:
9462 case CONST_CAST_EXPR
:
9463 case STATIC_CAST_EXPR
:
9464 case DYNAMIC_CAST_EXPR
:
9468 case PSEUDO_DTOR_EXPR
:
9470 return error_mark_node
;
9479 /* We didn't find any template parameters we liked. */
9484 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9485 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9486 call FN with the parameter and the DATA.
9487 If FN returns nonzero, the iteration is terminated, and
9488 for_each_template_parm returns 1. Otherwise, the iteration
9489 continues. If FN never returns a nonzero value, the value
9490 returned by for_each_template_parm is 0. If FN is NULL, it is
9491 considered to be the function which always returns 1.
9493 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9494 parameters that occur in non-deduced contexts. When false, only
9495 visits those template parameters that can be deduced. */
9498 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9499 hash_set
<tree
> *visited
,
9500 bool include_nondeduced_p
,
9503 struct pair_fn_data pfd
;
9508 pfd
.any_fn
= any_fn
;
9510 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9512 /* Walk the tree. (Conceptually, we would like to walk without
9513 duplicates, but for_each_template_parm_r recursively calls
9514 for_each_template_parm, so we would need to reorganize a fair
9515 bit to use walk_tree_without_duplicates, so we keep our own
9518 pfd
.visited
= visited
;
9520 pfd
.visited
= new hash_set
<tree
>;
9521 result
= cp_walk_tree (&t
,
9522 for_each_template_parm_r
,
9536 /* Returns true if T depends on any template parameter. */
9539 uses_template_parms (tree t
)
9545 int saved_processing_template_decl
;
9547 saved_processing_template_decl
= processing_template_decl
;
9548 if (!saved_processing_template_decl
)
9549 processing_template_decl
= 1;
9551 dependent_p
= dependent_type_p (t
);
9552 else if (TREE_CODE (t
) == TREE_VEC
)
9553 dependent_p
= any_dependent_template_arguments_p (t
);
9554 else if (TREE_CODE (t
) == TREE_LIST
)
9555 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9556 || uses_template_parms (TREE_CHAIN (t
)));
9557 else if (TREE_CODE (t
) == TYPE_DECL
)
9558 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9561 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9562 || TREE_CODE (t
) == OVERLOAD
9565 || TREE_CODE (t
) == TRAIT_EXPR
9566 || TREE_CODE (t
) == CONSTRUCTOR
9567 || CONSTANT_CLASS_P (t
))
9568 dependent_p
= (type_dependent_expression_p (t
)
9569 || value_dependent_expression_p (t
));
9572 gcc_assert (t
== error_mark_node
);
9573 dependent_p
= false;
9576 processing_template_decl
= saved_processing_template_decl
;
9581 /* Returns true iff current_function_decl is an incompletely instantiated
9582 template. Useful instead of processing_template_decl because the latter
9583 is set to 0 during instantiate_non_dependent_expr. */
9586 in_template_function (void)
9588 tree fn
= current_function_decl
;
9590 ++processing_template_decl
;
9591 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9592 && DECL_TEMPLATE_INFO (fn
)
9593 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9594 --processing_template_decl
;
9598 /* Returns true if T depends on any template parameter with level LEVEL. */
9601 uses_template_parms_level (tree t
, int level
)
9603 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9604 /*include_nondeduced_p=*/true);
9607 /* Returns true if the signature of DECL depends on any template parameter from
9608 its enclosing class. */
9611 uses_outer_template_parms (tree decl
)
9613 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9616 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9617 &depth
, NULL
, /*include_nondeduced_p=*/true))
9619 if (PRIMARY_TEMPLATE_P (decl
)
9620 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9621 (DECL_TEMPLATE_PARMS (decl
)),
9622 template_parm_outer_level
,
9623 &depth
, NULL
, /*include_nondeduced_p=*/true))
9625 tree ci
= get_constraints (decl
);
9627 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9628 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9629 &depth
, NULL
, /*nondeduced*/true))
9634 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9635 ill-formed translation unit, i.e. a variable or function that isn't
9636 usable in a constant expression. */
9639 neglectable_inst_p (tree d
)
9642 && !undeduced_auto_decl (d
)
9643 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9644 : decl_maybe_constant_var_p (d
)));
9647 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9648 neglectable and instantiated from within an erroneous instantiation. */
9651 limit_bad_template_recursion (tree decl
)
9653 struct tinst_level
*lev
= current_tinst_level
;
9654 int errs
= errorcount
+ sorrycount
;
9655 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9658 for (; lev
; lev
= lev
->next
)
9659 if (neglectable_inst_p (lev
->decl
))
9662 return (lev
&& errs
> lev
->errors
);
9665 static int tinst_depth
;
9666 extern int max_tinst_depth
;
9669 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9671 /* We're starting to instantiate D; record the template instantiation context
9672 for diagnostics and to restore it later. */
9675 push_tinst_level (tree d
)
9677 return push_tinst_level_loc (d
, input_location
);
9680 /* We're starting to instantiate D; record the template instantiation context
9681 at LOC for diagnostics and to restore it later. */
9684 push_tinst_level_loc (tree d
, location_t loc
)
9686 struct tinst_level
*new_level
;
9688 if (tinst_depth
>= max_tinst_depth
)
9690 /* Tell error.c not to try to instantiate any templates. */
9692 fatal_error (input_location
,
9693 "template instantiation depth exceeds maximum of %d"
9694 " (use -ftemplate-depth= to increase the maximum)",
9699 /* If the current instantiation caused problems, don't let it instantiate
9700 anything else. Do allow deduction substitution and decls usable in
9701 constant expressions. */
9702 if (limit_bad_template_recursion (d
))
9705 /* When not -quiet, dump template instantiations other than functions, since
9706 announce_function will take care of those. */
9708 && TREE_CODE (d
) != TREE_LIST
9709 && TREE_CODE (d
) != FUNCTION_DECL
)
9710 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9712 new_level
= ggc_alloc
<tinst_level
> ();
9713 new_level
->decl
= d
;
9714 new_level
->locus
= loc
;
9715 new_level
->errors
= errorcount
+sorrycount
;
9716 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9717 new_level
->next
= current_tinst_level
;
9718 current_tinst_level
= new_level
;
9721 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9722 depth_reached
= tinst_depth
;
9727 /* We're done instantiating this template; return to the instantiation
9731 pop_tinst_level (void)
9733 /* Restore the filename and line number stashed away when we started
9734 this instantiation. */
9735 input_location
= current_tinst_level
->locus
;
9736 current_tinst_level
= current_tinst_level
->next
;
9740 /* We're instantiating a deferred template; restore the template
9741 instantiation context in which the instantiation was requested, which
9742 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9745 reopen_tinst_level (struct tinst_level
*level
)
9747 struct tinst_level
*t
;
9750 for (t
= level
; t
; t
= t
->next
)
9753 current_tinst_level
= level
;
9755 if (current_tinst_level
)
9756 current_tinst_level
->errors
= errorcount
+sorrycount
;
9760 /* Returns the TINST_LEVEL which gives the original instantiation
9763 struct tinst_level
*
9764 outermost_tinst_level (void)
9766 struct tinst_level
*level
= current_tinst_level
;
9769 level
= level
->next
;
9773 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9774 vector of template arguments, as for tsubst.
9776 Returns an appropriate tsubst'd friend declaration. */
9779 tsubst_friend_function (tree decl
, tree args
)
9783 if (TREE_CODE (decl
) == FUNCTION_DECL
9784 && DECL_TEMPLATE_INSTANTIATION (decl
)
9785 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9786 /* This was a friend declared with an explicit template
9787 argument list, e.g.:
9791 to indicate that f was a template instantiation, not a new
9792 function declaration. Now, we have to figure out what
9793 instantiation of what template. */
9795 tree template_id
, arglist
, fns
;
9798 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9800 /* Friend functions are looked up in the containing namespace scope.
9801 We must enter that scope, to avoid finding member functions of the
9802 current class with same name. */
9803 push_nested_namespace (ns
);
9804 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9805 tf_warning_or_error
, NULL_TREE
,
9806 /*integral_constant_expression_p=*/false);
9807 pop_nested_namespace (ns
);
9808 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9809 tf_warning_or_error
, NULL_TREE
);
9810 template_id
= lookup_template_function (fns
, arglist
);
9812 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9813 tmpl
= determine_specialization (template_id
, new_friend
,
9815 /*need_member_template=*/0,
9816 TREE_VEC_LENGTH (args
),
9818 return instantiate_template (tmpl
, new_args
, tf_error
);
9821 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9823 /* The NEW_FRIEND will look like an instantiation, to the
9824 compiler, but is not an instantiation from the point of view of
9825 the language. For example, we might have had:
9827 template <class T> struct S {
9828 template <class U> friend void f(T, U);
9831 Then, in S<int>, template <class U> void f(int, U) is not an
9832 instantiation of anything. */
9833 if (new_friend
== error_mark_node
)
9834 return error_mark_node
;
9836 DECL_USE_TEMPLATE (new_friend
) = 0;
9837 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9839 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9840 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9841 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9844 /* The mangled name for the NEW_FRIEND is incorrect. The function
9845 is not a template instantiation and should not be mangled like
9846 one. Therefore, we forget the mangling here; we'll recompute it
9847 later if we need it. */
9848 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9850 SET_DECL_RTL (new_friend
, NULL
);
9851 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9854 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9857 tree new_friend_template_info
;
9858 tree new_friend_result_template_info
;
9860 int new_friend_is_defn
;
9862 /* We must save some information from NEW_FRIEND before calling
9863 duplicate decls since that function will free NEW_FRIEND if
9865 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9866 new_friend_is_defn
=
9867 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9868 (template_for_substitution (new_friend
)))
9870 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9872 /* This declaration is a `primary' template. */
9873 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9875 new_friend_result_template_info
9876 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9879 new_friend_result_template_info
= NULL_TREE
;
9881 /* Inside pushdecl_namespace_level, we will push into the
9882 current namespace. However, the friend function should go
9883 into the namespace of the template. */
9884 ns
= decl_namespace_context (new_friend
);
9885 push_nested_namespace (ns
);
9886 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9887 pop_nested_namespace (ns
);
9889 if (old_decl
== error_mark_node
)
9890 return error_mark_node
;
9892 if (old_decl
!= new_friend
)
9894 /* This new friend declaration matched an existing
9895 declaration. For example, given:
9897 template <class T> void f(T);
9898 template <class U> class C {
9899 template <class T> friend void f(T) {}
9902 the friend declaration actually provides the definition
9903 of `f', once C has been instantiated for some type. So,
9904 old_decl will be the out-of-class template declaration,
9905 while new_friend is the in-class definition.
9907 But, if `f' was called before this point, the
9908 instantiation of `f' will have DECL_TI_ARGS corresponding
9909 to `T' but not to `U', references to which might appear
9910 in the definition of `f'. Previously, the most general
9911 template for an instantiation of `f' was the out-of-class
9912 version; now it is the in-class version. Therefore, we
9913 run through all specialization of `f', adding to their
9914 DECL_TI_ARGS appropriately. In particular, they need a
9915 new set of outer arguments, corresponding to the
9916 arguments for this class instantiation.
9918 The same situation can arise with something like this:
9921 template <class T> class C {
9925 when `C<int>' is instantiated. Now, `f(int)' is defined
9928 if (!new_friend_is_defn
)
9929 /* On the other hand, if the in-class declaration does
9930 *not* provide a definition, then we don't want to alter
9931 existing definitions. We can just leave everything
9936 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9937 tree new_args
= TI_ARGS (new_friend_template_info
);
9939 /* Overwrite whatever template info was there before, if
9940 any, with the new template information pertaining to
9942 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9944 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9946 /* We should have called reregister_specialization in
9948 gcc_assert (retrieve_specialization (new_template
,
9952 /* Instantiate it if the global has already been used. */
9953 if (DECL_ODR_USED (old_decl
))
9954 instantiate_decl (old_decl
, /*defer_ok=*/true,
9955 /*expl_inst_class_mem_p=*/false);
9961 /* Indicate that the old function template is a partial
9963 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9964 = new_friend_result_template_info
;
9966 gcc_assert (new_template
9967 == most_general_template (new_template
));
9968 gcc_assert (new_template
!= old_decl
);
9970 /* Reassign any specializations already in the hash table
9971 to the new more general template, and add the
9972 additional template args. */
9973 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9977 tree spec
= TREE_VALUE (t
);
9980 elt
.tmpl
= old_decl
;
9981 elt
.args
= DECL_TI_ARGS (spec
);
9982 elt
.spec
= NULL_TREE
;
9984 decl_specializations
->remove_elt (&elt
);
9987 = add_outermost_template_args (new_args
,
9988 DECL_TI_ARGS (spec
));
9990 register_specialization
9991 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9994 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9998 /* The information from NEW_FRIEND has been merged into OLD_DECL
9999 by duplicate_decls. */
10000 new_friend
= old_decl
;
10005 tree context
= DECL_CONTEXT (new_friend
);
10009 template <class T> class C {
10010 template <class U> friend void C1<U>::f (); // case 1
10011 friend void C2<T>::f (); // case 2
10013 we only need to make sure CONTEXT is a complete type for
10014 case 2. To distinguish between the two cases, we note that
10015 CONTEXT of case 1 remains dependent type after tsubst while
10016 this isn't true for case 2. */
10017 ++processing_template_decl
;
10018 dependent_p
= dependent_type_p (context
);
10019 --processing_template_decl
;
10022 && !complete_type_or_else (context
, NULL_TREE
))
10023 return error_mark_node
;
10025 if (COMPLETE_TYPE_P (context
))
10027 tree fn
= new_friend
;
10028 /* do_friend adds the TEMPLATE_DECL for any member friend
10029 template even if it isn't a member template, i.e.
10030 template <class T> friend A<T>::f();
10031 Look through it in that case. */
10032 if (TREE_CODE (fn
) == TEMPLATE_DECL
10033 && !PRIMARY_TEMPLATE_P (fn
))
10034 fn
= DECL_TEMPLATE_RESULT (fn
);
10035 /* Check to see that the declaration is really present, and,
10036 possibly obtain an improved declaration. */
10037 fn
= check_classfn (context
, fn
, NULL_TREE
);
10047 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10048 template arguments, as for tsubst.
10050 Returns an appropriate tsubst'd friend type or error_mark_node on
10054 tsubst_friend_class (tree friend_tmpl
, tree args
)
10058 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10060 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10061 return TREE_TYPE (tmpl
);
10064 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10065 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10066 push_nested_namespace (context
);
10068 push_nested_class (context
);
10070 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10071 /*non_class=*/false, /*block_p=*/false,
10072 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10074 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10076 /* The friend template has already been declared. Just
10077 check to see that the declarations match, and install any new
10078 default parameters. We must tsubst the default parameters,
10079 of course. We only need the innermost template parameters
10080 because that is all that redeclare_class_template will look
10082 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10083 > TMPL_ARGS_DEPTH (args
))
10085 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10086 args
, tf_warning_or_error
);
10087 location_t saved_input_location
= input_location
;
10088 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10089 tree cons
= get_constraints (tmpl
);
10090 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10091 input_location
= saved_input_location
;
10096 /* The friend template has not already been declared. In this
10097 case, the instantiation of the template class will cause the
10098 injection of this template into the namespace scope. */
10099 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10101 if (tmpl
!= error_mark_node
)
10103 /* The new TMPL is not an instantiation of anything, so we
10104 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10105 for the new type because that is supposed to be the
10106 corresponding template decl, i.e., TMPL. */
10107 DECL_USE_TEMPLATE (tmpl
) = 0;
10108 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10109 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10110 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10111 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10113 /* It is hidden. */
10114 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10115 DECL_ANTICIPATED (tmpl
)
10116 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10118 /* Inject this template into the enclosing namspace scope. */
10119 tmpl
= pushdecl_namespace_level (tmpl
, true);
10123 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10124 pop_nested_namespace (context
);
10126 pop_nested_class ();
10128 return TREE_TYPE (tmpl
);
10131 /* Returns zero if TYPE cannot be completed later due to circularity.
10132 Otherwise returns one. */
10135 can_complete_type_without_circularity (tree type
)
10137 if (type
== NULL_TREE
|| type
== error_mark_node
)
10139 else if (COMPLETE_TYPE_P (type
))
10141 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10142 return can_complete_type_without_circularity (TREE_TYPE (type
));
10143 else if (CLASS_TYPE_P (type
)
10144 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10150 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10151 tsubst_flags_t
, tree
);
10153 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10154 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10157 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10158 tsubst_flags_t complain
, tree in_decl
)
10160 gcc_assert (ATTR_IS_DEPENDENT (t
));
10162 tree val
= TREE_VALUE (t
);
10163 if (val
== NULL_TREE
)
10164 /* Nothing to do. */;
10165 else if ((flag_openmp
|| flag_openmp_simd
)
10166 && is_attribute_p ("omp declare simd",
10167 get_attribute_name (t
)))
10169 tree clauses
= TREE_VALUE (val
);
10170 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10171 complain
, in_decl
);
10172 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10173 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10174 tree parms
= DECL_ARGUMENTS (*decl_p
);
10176 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10178 val
= build_tree_list (NULL_TREE
, clauses
);
10182 /* If the first attribute argument is an identifier, don't
10183 pass it through tsubst. Attributes like mode, format,
10184 cleanup and several target specific attributes expect it
10186 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10189 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10190 /*integral_constant_expression_p=*/false);
10191 if (chain
!= TREE_CHAIN (val
))
10192 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10194 else if (PACK_EXPANSION_P (val
))
10196 /* An attribute pack expansion. */
10197 tree purp
= TREE_PURPOSE (t
);
10198 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10199 if (pack
== error_mark_node
)
10200 return error_mark_node
;
10201 int len
= TREE_VEC_LENGTH (pack
);
10202 tree list
= NULL_TREE
;
10204 for (int i
= 0; i
< len
; ++i
)
10206 tree elt
= TREE_VEC_ELT (pack
, i
);
10207 *q
= build_tree_list (purp
, elt
);
10208 q
= &TREE_CHAIN (*q
);
10213 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10214 /*integral_constant_expression_p=*/false);
10216 if (val
!= TREE_VALUE (t
))
10217 return build_tree_list (TREE_PURPOSE (t
), val
);
10221 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10222 unchanged or a new TREE_LIST chain. */
10225 tsubst_attributes (tree attributes
, tree args
,
10226 tsubst_flags_t complain
, tree in_decl
)
10228 tree last_dep
= NULL_TREE
;
10230 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10231 if (ATTR_IS_DEPENDENT (t
))
10234 attributes
= copy_list (attributes
);
10239 for (tree
*p
= &attributes
; *p
; )
10242 if (ATTR_IS_DEPENDENT (t
))
10244 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10249 p
= &TREE_CHAIN (*p
);
10251 *p
= TREE_CHAIN (t
);
10255 p
= &TREE_CHAIN (*p
);
10261 /* Apply any attributes which had to be deferred until instantiation
10262 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10263 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10266 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10267 tree args
, tsubst_flags_t complain
, tree in_decl
)
10269 tree last_dep
= NULL_TREE
;
10273 if (attributes
== NULL_TREE
)
10276 if (DECL_P (*decl_p
))
10278 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10280 p
= &DECL_ATTRIBUTES (*decl_p
);
10281 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10282 to our attributes parameter. */
10283 gcc_assert (*p
== attributes
);
10287 p
= &TYPE_ATTRIBUTES (*decl_p
);
10288 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10289 lookup_template_class_1, and should be preserved. */
10290 gcc_assert (*p
!= attributes
);
10292 p
= &TREE_CHAIN (*p
);
10295 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10296 if (ATTR_IS_DEPENDENT (t
))
10299 attributes
= copy_list (attributes
);
10306 tree late_attrs
= NULL_TREE
;
10307 tree
*q
= &late_attrs
;
10312 if (ATTR_IS_DEPENDENT (t
))
10314 *p
= TREE_CHAIN (t
);
10315 TREE_CHAIN (t
) = NULL_TREE
;
10316 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10318 q
= &TREE_CHAIN (*q
);
10322 p
= &TREE_CHAIN (t
);
10325 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10329 /* Perform (or defer) access check for typedefs that were referenced
10330 from within the template TMPL code.
10331 This is a subroutine of instantiate_decl and instantiate_class_template.
10332 TMPL is the template to consider and TARGS is the list of arguments of
10336 perform_typedefs_access_check (tree tmpl
, tree targs
)
10338 location_t saved_location
;
10340 qualified_typedef_usage_t
*iter
;
10343 || (!CLASS_TYPE_P (tmpl
)
10344 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10347 saved_location
= input_location
;
10348 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10350 tree type_decl
= iter
->typedef_decl
;
10351 tree type_scope
= iter
->context
;
10353 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10356 if (uses_template_parms (type_decl
))
10357 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10358 if (uses_template_parms (type_scope
))
10359 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10361 /* Make access check error messages point to the location
10362 of the use of the typedef. */
10363 input_location
= iter
->locus
;
10364 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10365 type_decl
, type_decl
,
10366 tf_warning_or_error
);
10368 input_location
= saved_location
;
10372 instantiate_class_template_1 (tree type
)
10374 tree templ
, args
, pattern
, t
, member
;
10378 unsigned int saved_maximum_field_alignment
;
10381 if (type
== error_mark_node
)
10382 return error_mark_node
;
10384 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10385 || uses_template_parms (type
))
10388 /* Figure out which template is being instantiated. */
10389 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10390 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10392 /* Mark the type as in the process of being defined. */
10393 TYPE_BEING_DEFINED (type
) = 1;
10395 /* Determine what specialization of the original template to
10397 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10398 if (t
== error_mark_node
)
10399 return error_mark_node
;
10402 /* This TYPE is actually an instantiation of a partial
10403 specialization. We replace the innermost set of ARGS with
10404 the arguments appropriate for substitution. For example,
10407 template <class T> struct S {};
10408 template <class T> struct S<T*> {};
10410 and supposing that we are instantiating S<int*>, ARGS will
10411 presently be {int*} -- but we need {int}. */
10412 pattern
= TREE_TYPE (t
);
10413 args
= TREE_PURPOSE (t
);
10417 pattern
= TREE_TYPE (templ
);
10418 args
= CLASSTYPE_TI_ARGS (type
);
10421 /* If the template we're instantiating is incomplete, then clearly
10422 there's nothing we can do. */
10423 if (!COMPLETE_TYPE_P (pattern
))
10425 /* We can try again later. */
10426 TYPE_BEING_DEFINED (type
) = 0;
10430 /* If we've recursively instantiated too many templates, stop. */
10431 if (! push_tinst_level (type
))
10434 /* We may be in the middle of deferred access check. Disable
10436 push_deferring_access_checks (dk_no_deferred
);
10438 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10439 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10441 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10442 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10443 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10444 fn_context
= error_mark_node
;
10446 push_to_top_level ();
10449 cp_unevaluated_operand
= 0;
10450 c_inhibit_evaluation_warnings
= 0;
10452 /* Use #pragma pack from the template context. */
10453 saved_maximum_field_alignment
= maximum_field_alignment
;
10454 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10456 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10458 /* Set the input location to the most specialized template definition.
10459 This is needed if tsubsting causes an error. */
10460 typedecl
= TYPE_MAIN_DECL (pattern
);
10461 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10462 DECL_SOURCE_LOCATION (typedecl
);
10464 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10465 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10466 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10467 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10468 if (ANON_AGGR_TYPE_P (pattern
))
10469 SET_ANON_AGGR_TYPE_P (type
);
10470 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10472 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10473 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10474 /* Adjust visibility for template arguments. */
10475 determine_visibility (TYPE_MAIN_DECL (type
));
10477 if (CLASS_TYPE_P (type
))
10478 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10480 pbinfo
= TYPE_BINFO (pattern
);
10482 /* We should never instantiate a nested class before its enclosing
10483 class; we need to look up the nested class by name before we can
10484 instantiate it, and that lookup should instantiate the enclosing
10486 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10487 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10489 base_list
= NULL_TREE
;
10490 if (BINFO_N_BASE_BINFOS (pbinfo
))
10496 /* We must enter the scope containing the type, as that is where
10497 the accessibility of types named in dependent bases are
10499 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10501 /* Substitute into each of the bases to determine the actual
10503 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10506 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10507 tree expanded_bases
= NULL_TREE
;
10510 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10513 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10514 args
, tf_error
, NULL_TREE
);
10515 if (expanded_bases
== error_mark_node
)
10518 len
= TREE_VEC_LENGTH (expanded_bases
);
10521 for (idx
= 0; idx
< len
; idx
++)
10523 if (expanded_bases
)
10524 /* Extract the already-expanded base class. */
10525 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10527 /* Substitute to figure out the base class. */
10528 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10531 if (base
== error_mark_node
)
10534 base_list
= tree_cons (access
, base
, base_list
);
10535 if (BINFO_VIRTUAL_P (pbase_binfo
))
10536 TREE_TYPE (base_list
) = integer_type_node
;
10540 /* The list is now in reverse order; correct that. */
10541 base_list
= nreverse (base_list
);
10544 pop_scope (pushed_scope
);
10546 /* Now call xref_basetypes to set up all the base-class
10548 xref_basetypes (type
, base_list
);
10550 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10551 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10552 args
, tf_error
, NULL_TREE
);
10553 fixup_attribute_variants (type
);
10555 /* Now that our base classes are set up, enter the scope of the
10556 class, so that name lookups into base classes, etc. will work
10557 correctly. This is precisely analogous to what we do in
10558 begin_class_definition when defining an ordinary non-template
10559 class, except we also need to push the enclosing classes. */
10560 push_nested_class (type
);
10562 /* Now members are processed in the order of declaration. */
10563 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10564 member
; member
= TREE_CHAIN (member
))
10566 tree t
= TREE_VALUE (member
);
10568 if (TREE_PURPOSE (member
))
10572 if (LAMBDA_TYPE_P (t
))
10573 /* A closure type for a lambda in an NSDMI or default argument.
10574 Ignore it; it will be regenerated when needed. */
10577 /* Build new CLASSTYPE_NESTED_UTDS. */
10580 bool class_template_p
;
10582 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10583 && TYPE_LANG_SPECIFIC (t
)
10584 && CLASSTYPE_IS_TEMPLATE (t
));
10585 /* If the member is a class template, then -- even after
10586 substitution -- there may be dependent types in the
10587 template argument list for the class. We increment
10588 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10589 that function will assume that no types are dependent
10590 when outside of a template. */
10591 if (class_template_p
)
10592 ++processing_template_decl
;
10593 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10594 if (class_template_p
)
10595 --processing_template_decl
;
10596 if (newtag
== error_mark_node
)
10599 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10601 tree name
= TYPE_IDENTIFIER (t
);
10603 if (class_template_p
)
10604 /* Unfortunately, lookup_template_class sets
10605 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10606 instantiation (i.e., for the type of a member
10607 template class nested within a template class.)
10608 This behavior is required for
10609 maybe_process_partial_specialization to work
10610 correctly, but is not accurate in this case;
10611 the TAG is not an instantiation of anything.
10612 (The corresponding TEMPLATE_DECL is an
10613 instantiation, but the TYPE is not.) */
10614 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10616 /* Now, we call pushtag to put this NEWTAG into the scope of
10617 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10618 pushtag calling push_template_decl. We don't have to do
10619 this for enums because it will already have been done in
10622 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10623 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10626 else if (DECL_DECLARES_FUNCTION_P (t
))
10630 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10631 ++processing_template_decl
;
10632 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10633 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10634 --processing_template_decl
;
10635 set_current_access_from_decl (r
);
10636 finish_member_declaration (r
);
10637 /* Instantiate members marked with attribute used. */
10638 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10640 if (TREE_CODE (r
) == FUNCTION_DECL
10641 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10642 cp_check_omp_declare_reduction (r
);
10644 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
10645 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10646 /* A closure type for a lambda in an NSDMI or default argument.
10647 Ignore it; it will be regenerated when needed. */;
10650 /* Build new TYPE_FIELDS. */
10651 if (TREE_CODE (t
) == STATIC_ASSERT
)
10655 ++c_inhibit_evaluation_warnings
;
10657 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10658 tf_warning_or_error
, NULL_TREE
,
10659 /*integral_constant_expression_p=*/true);
10660 --c_inhibit_evaluation_warnings
;
10662 finish_static_assert (condition
,
10663 STATIC_ASSERT_MESSAGE (t
),
10664 STATIC_ASSERT_SOURCE_LOCATION (t
),
10665 /*member_p=*/true);
10667 else if (TREE_CODE (t
) != CONST_DECL
)
10670 tree vec
= NULL_TREE
;
10673 /* The file and line for this declaration, to
10674 assist in error message reporting. Since we
10675 called push_tinst_level above, we don't need to
10677 input_location
= DECL_SOURCE_LOCATION (t
);
10679 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10680 ++processing_template_decl
;
10681 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10682 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10683 --processing_template_decl
;
10685 if (TREE_CODE (r
) == TREE_VEC
)
10687 /* A capture pack became multiple fields. */
10689 len
= TREE_VEC_LENGTH (vec
);
10692 for (int i
= 0; i
< len
; ++i
)
10695 r
= TREE_VEC_ELT (vec
, i
);
10700 [t]he initialization (and any associated
10701 side-effects) of a static data member does
10702 not occur unless the static data member is
10703 itself used in a way that requires the
10704 definition of the static data member to
10707 Therefore, we do not substitute into the
10708 initialized for the static data member here. */
10709 finish_static_data_member_decl
10711 /*init=*/NULL_TREE
,
10712 /*init_const_expr_p=*/false,
10713 /*asmspec_tree=*/NULL_TREE
,
10715 /* Instantiate members marked with attribute used. */
10716 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10719 else if (TREE_CODE (r
) == FIELD_DECL
)
10721 /* Determine whether R has a valid type and can be
10722 completed later. If R is invalid, then its type
10723 is replaced by error_mark_node. */
10724 tree rtype
= TREE_TYPE (r
);
10725 if (can_complete_type_without_circularity (rtype
))
10726 complete_type (rtype
);
10728 if (!complete_or_array_type_p (rtype
))
10730 /* If R's type couldn't be completed and
10731 it isn't a flexible array member (whose
10732 type is incomplete by definition) give
10734 cxx_incomplete_type_error (r
, rtype
);
10735 TREE_TYPE (r
) = error_mark_node
;
10739 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10740 such a thing will already have been added to the field
10741 list by tsubst_enum in finish_member_declaration in the
10742 CLASSTYPE_NESTED_UTDS case above. */
10743 if (!(TREE_CODE (r
) == TYPE_DECL
10744 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10745 && DECL_ARTIFICIAL (r
)))
10747 set_current_access_from_decl (r
);
10748 finish_member_declaration (r
);
10756 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10757 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10759 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10761 tree friend_type
= t
;
10762 bool adjust_processing_template_decl
= false;
10764 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10766 /* template <class T> friend class C; */
10767 friend_type
= tsubst_friend_class (friend_type
, args
);
10768 adjust_processing_template_decl
= true;
10770 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10772 /* template <class T> friend class C::D; */
10773 friend_type
= tsubst (friend_type
, args
,
10774 tf_warning_or_error
, NULL_TREE
);
10775 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10776 friend_type
= TREE_TYPE (friend_type
);
10777 adjust_processing_template_decl
= true;
10779 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10780 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10782 /* This could be either
10786 when dependent_type_p is false or
10788 template <class U> friend class T::C;
10791 /* Bump processing_template_decl in case this is something like
10792 template <class T> friend struct A<T>::B. */
10793 ++processing_template_decl
;
10794 friend_type
= tsubst (friend_type
, args
,
10795 tf_warning_or_error
, NULL_TREE
);
10796 if (dependent_type_p (friend_type
))
10797 adjust_processing_template_decl
= true;
10798 --processing_template_decl
;
10800 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10801 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10802 && TYPE_HIDDEN_P (friend_type
))
10806 where C hasn't been declared yet. Let's lookup name
10807 from namespace scope directly, bypassing any name that
10808 come from dependent base class. */
10809 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10811 /* The call to xref_tag_from_type does injection for friend
10813 push_nested_namespace (ns
);
10815 xref_tag_from_type (friend_type
, NULL_TREE
,
10816 /*tag_scope=*/ts_current
);
10817 pop_nested_namespace (ns
);
10819 else if (uses_template_parms (friend_type
))
10820 /* friend class C<T>; */
10821 friend_type
= tsubst (friend_type
, args
,
10822 tf_warning_or_error
, NULL_TREE
);
10827 where C is already declared or
10829 friend class C<int>;
10831 We don't have to do anything in these cases. */
10833 if (adjust_processing_template_decl
)
10834 /* Trick make_friend_class into realizing that the friend
10835 we're adding is a template, not an ordinary class. It's
10836 important that we use make_friend_class since it will
10837 perform some error-checking and output cross-reference
10839 ++processing_template_decl
;
10841 if (friend_type
!= error_mark_node
)
10842 make_friend_class (type
, friend_type
, /*complain=*/false);
10844 if (adjust_processing_template_decl
)
10845 --processing_template_decl
;
10849 /* Build new DECL_FRIENDLIST. */
10852 /* The file and line for this declaration, to
10853 assist in error message reporting. Since we
10854 called push_tinst_level above, we don't need to
10856 input_location
= DECL_SOURCE_LOCATION (t
);
10858 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10860 ++processing_template_decl
;
10861 push_deferring_access_checks (dk_no_check
);
10864 r
= tsubst_friend_function (t
, args
);
10865 add_friend (type
, r
, /*complain=*/false);
10866 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10868 pop_deferring_access_checks ();
10869 --processing_template_decl
;
10877 /* Restore these before substituting into the lambda capture
10879 cp_unevaluated_operand
= saved_unevaluated_operand
;
10880 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10883 /* Set the file and line number information to whatever is given for
10884 the class itself. This puts error messages involving generated
10885 implicit functions at a predictable point, and the same point
10886 that would be used for non-template classes. */
10887 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10889 unreverse_member_declarations (type
);
10890 finish_struct_1 (type
);
10891 TYPE_BEING_DEFINED (type
) = 0;
10893 /* We don't instantiate default arguments for member functions. 14.7.1:
10895 The implicit instantiation of a class template specialization causes
10896 the implicit instantiation of the declarations, but not of the
10897 definitions or default arguments, of the class member functions,
10898 member classes, static data members and member templates.... */
10900 /* Some typedefs referenced from within the template code need to be access
10901 checked at template instantiation time, i.e now. These types were
10902 added to the template at parsing time. Let's get those and perform
10903 the access checks then. */
10904 perform_typedefs_access_check (pattern
, args
);
10905 perform_deferred_access_checks (tf_warning_or_error
);
10906 pop_nested_class ();
10907 maximum_field_alignment
= saved_maximum_field_alignment
;
10909 pop_from_top_level ();
10910 pop_deferring_access_checks ();
10911 pop_tinst_level ();
10913 /* The vtable for a template class can be emitted in any translation
10914 unit in which the class is instantiated. When there is no key
10915 method, however, finish_struct_1 will already have added TYPE to
10916 the keyed_classes. */
10917 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10918 vec_safe_push (keyed_classes
, type
);
10923 /* Wrapper for instantiate_class_template_1. */
10926 instantiate_class_template (tree type
)
10929 timevar_push (TV_TEMPLATE_INST
);
10930 ret
= instantiate_class_template_1 (type
);
10931 timevar_pop (TV_TEMPLATE_INST
);
10936 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10942 else if (TYPE_P (t
))
10943 r
= tsubst (t
, args
, complain
, in_decl
);
10946 if (!(complain
& tf_warning
))
10947 ++c_inhibit_evaluation_warnings
;
10948 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10949 /*integral_constant_expression_p=*/true);
10950 if (!(complain
& tf_warning
))
10951 --c_inhibit_evaluation_warnings
;
10956 /* Given a function parameter pack TMPL_PARM and some function parameters
10957 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10958 and set *SPEC_P to point at the next point in the list. */
10961 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10963 /* Collect all of the extra "packed" parameters into an
10966 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10967 tree spec_parm
= *spec_p
;
10970 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10972 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10975 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10976 parmvec
= make_tree_vec (len
);
10977 spec_parm
= *spec_p
;
10978 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10980 tree elt
= spec_parm
;
10981 if (DECL_PACK_P (elt
))
10982 elt
= make_pack_expansion (elt
);
10983 TREE_VEC_ELT (parmvec
, i
) = elt
;
10986 /* Build the argument packs. */
10987 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10988 *spec_p
= spec_parm
;
10993 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10994 NONTYPE_ARGUMENT_PACK. */
10997 make_fnparm_pack (tree spec_parm
)
10999 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11002 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11003 pack expansion with no extra args, 2 if it has extra args, or 0
11004 if it is not a pack expansion. */
11007 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11009 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11010 if (i
>= TREE_VEC_LENGTH (vec
))
11012 tree elt
= TREE_VEC_ELT (vec
, i
);
11014 /* A decl pack is itself an expansion. */
11015 elt
= TREE_TYPE (elt
);
11016 if (!PACK_EXPANSION_P (elt
))
11018 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11024 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11027 make_argument_pack_select (tree arg_pack
, unsigned index
)
11029 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11031 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11032 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11037 /* This is a subroutine of tsubst_pack_expansion.
11039 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11040 mechanism to store the (non complete list of) arguments of the
11041 substitution and return a non substituted pack expansion, in order
11042 to wait for when we have enough arguments to really perform the
11046 use_pack_expansion_extra_args_p (tree parm_packs
,
11048 bool has_empty_arg
)
11050 /* If one pack has an expansion and another pack has a normal
11051 argument or if one pack has an empty argument and an another
11052 one hasn't then tsubst_pack_expansion cannot perform the
11053 substitution and need to fall back on the
11054 PACK_EXPANSION_EXTRA mechanism. */
11055 if (parm_packs
== NULL_TREE
)
11057 else if (has_empty_arg
)
11060 bool has_expansion_arg
= false;
11061 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11063 bool has_non_expansion_arg
= false;
11064 for (tree parm_pack
= parm_packs
;
11066 parm_pack
= TREE_CHAIN (parm_pack
))
11068 tree arg
= TREE_VALUE (parm_pack
);
11070 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11072 /* We can't substitute a pack expansion with extra args into
11076 has_expansion_arg
= true;
11078 has_non_expansion_arg
= true;
11081 if (has_expansion_arg
&& has_non_expansion_arg
)
11087 /* [temp.variadic]/6 says that:
11089 The instantiation of a pack expansion [...]
11090 produces a list E1,E2, ..., En, where N is the number of elements
11091 in the pack expansion parameters.
11093 This subroutine of tsubst_pack_expansion produces one of these Ei.
11095 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11096 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11097 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11098 INDEX is the index 'i' of the element Ei to produce. ARGS,
11099 COMPLAIN, and IN_DECL are the same parameters as for the
11100 tsubst_pack_expansion function.
11102 The function returns the resulting Ei upon successful completion,
11103 or error_mark_node.
11105 Note that this function possibly modifies the ARGS parameter, so
11106 it's the responsibility of the caller to restore it. */
11109 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11112 tree args
/* This parm gets
11114 tsubst_flags_t complain
,
11118 bool ith_elem_is_expansion
= false;
11120 /* For each parameter pack, change the substitution of the parameter
11121 pack to the ith argument in its argument pack, then expand the
11123 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11125 tree parm
= TREE_PURPOSE (pack
);
11126 tree arg_pack
= TREE_VALUE (pack
);
11127 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11129 ith_elem_is_expansion
|=
11130 argument_pack_element_is_expansion_p (arg_pack
, index
);
11132 /* Select the Ith argument from the pack. */
11133 if (TREE_CODE (parm
) == PARM_DECL
11135 || TREE_CODE (parm
) == FIELD_DECL
)
11139 aps
= make_argument_pack_select (arg_pack
, index
);
11140 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11141 return error_mark_node
;
11142 register_local_specialization (aps
, parm
);
11145 aps
= retrieve_local_specialization (parm
);
11150 template_parm_level_and_index (parm
, &level
, &idx
);
11154 aps
= make_argument_pack_select (arg_pack
, index
);
11155 /* Update the corresponding argument. */
11156 TMPL_ARG (args
, level
, idx
) = aps
;
11159 /* Re-use the ARGUMENT_PACK_SELECT. */
11160 aps
= TMPL_ARG (args
, level
, idx
);
11162 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11165 /* Substitute into the PATTERN with the (possibly altered)
11167 if (pattern
== in_decl
)
11168 /* Expanding a fixed parameter pack from
11169 coerce_template_parameter_pack. */
11170 t
= tsubst_decl (pattern
, args
, complain
);
11171 else if (pattern
== error_mark_node
)
11172 t
= error_mark_node
;
11173 else if (constraint_p (pattern
))
11175 if (processing_template_decl
)
11176 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11178 t
= (constraints_satisfied_p (pattern
, args
)
11179 ? boolean_true_node
: boolean_false_node
);
11181 else if (!TYPE_P (pattern
))
11182 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11183 /*integral_constant_expression_p=*/false);
11185 t
= tsubst (pattern
, args
, complain
, in_decl
);
11187 /* If the Ith argument pack element is a pack expansion, then
11188 the Ith element resulting from the substituting is going to
11189 be a pack expansion as well. */
11190 if (ith_elem_is_expansion
)
11191 t
= make_pack_expansion (t
, complain
);
11196 /* When the unexpanded parameter pack in a fold expression expands to an empty
11197 sequence, the value of the expression is as follows; the program is
11198 ill-formed if the operator is not listed in this table.
11205 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11207 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11208 if (!FOLD_EXPR_MODIFY_P (t
))
11211 case TRUTH_ANDIF_EXPR
:
11212 return boolean_true_node
;
11213 case TRUTH_ORIF_EXPR
:
11214 return boolean_false_node
;
11215 case COMPOUND_EXPR
:
11221 if (complain
& tf_error
)
11222 error_at (location_of (t
),
11223 "fold of empty expansion over %O", code
);
11224 return error_mark_node
;
11227 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11228 form an expression that combines the two terms using the
11232 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11234 tree op
= FOLD_EXPR_OP (t
);
11235 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11237 // Handle compound assignment operators.
11238 if (FOLD_EXPR_MODIFY_P (t
))
11239 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11243 case COMPOUND_EXPR
:
11244 return build_x_compound_expr (input_location
, left
, right
, complain
);
11246 return build_m_component_ref (left
, right
, complain
);
11248 return build_x_binary_op (input_location
, code
,
11249 left
, TREE_CODE (left
),
11250 right
, TREE_CODE (right
),
11256 /* Substitute ARGS into the pack of a fold expression T. */
11259 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11261 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11264 /* Substitute ARGS into the pack of a fold expression T. */
11267 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11269 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11272 /* Expand a PACK of arguments into a grouped as left fold.
11273 Given a pack containing elements A0, A1, ..., An and an
11274 operator @, this builds the expression:
11276 ((A0 @ A1) @ A2) ... @ An
11278 Note that PACK must not be empty.
11280 The operator is defined by the original fold expression T. */
11283 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11285 tree left
= TREE_VEC_ELT (pack
, 0);
11286 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11288 tree right
= TREE_VEC_ELT (pack
, i
);
11289 left
= fold_expression (t
, left
, right
, complain
);
11294 /* Substitute into a unary left fold expression. */
11297 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11300 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11301 if (pack
== error_mark_node
)
11302 return error_mark_node
;
11303 if (PACK_EXPANSION_P (pack
))
11305 tree r
= copy_node (t
);
11306 FOLD_EXPR_PACK (r
) = pack
;
11309 if (TREE_VEC_LENGTH (pack
) == 0)
11310 return expand_empty_fold (t
, complain
);
11312 return expand_left_fold (t
, pack
, complain
);
11315 /* Substitute into a binary left fold expression.
11317 Do ths by building a single (non-empty) vector of argumnts and
11318 building the expression from those elements. */
11321 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11324 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11325 if (pack
== error_mark_node
)
11326 return error_mark_node
;
11327 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11328 if (init
== error_mark_node
)
11329 return error_mark_node
;
11331 if (PACK_EXPANSION_P (pack
))
11333 tree r
= copy_node (t
);
11334 FOLD_EXPR_PACK (r
) = pack
;
11335 FOLD_EXPR_INIT (r
) = init
;
11339 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11340 TREE_VEC_ELT (vec
, 0) = init
;
11341 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11342 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11344 return expand_left_fold (t
, vec
, complain
);
11347 /* Expand a PACK of arguments into a grouped as right fold.
11348 Given a pack containing elementns A0, A1, ..., and an
11349 operator @, this builds the expression:
11351 A0@ ... (An-2 @ (An-1 @ An))
11353 Note that PACK must not be empty.
11355 The operator is defined by the original fold expression T. */
11358 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11360 // Build the expression.
11361 int n
= TREE_VEC_LENGTH (pack
);
11362 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11363 for (--n
; n
!= 0; --n
)
11365 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11366 right
= fold_expression (t
, left
, right
, complain
);
11371 /* Substitute into a unary right fold expression. */
11374 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11377 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11378 if (pack
== error_mark_node
)
11379 return error_mark_node
;
11380 if (PACK_EXPANSION_P (pack
))
11382 tree r
= copy_node (t
);
11383 FOLD_EXPR_PACK (r
) = pack
;
11386 if (TREE_VEC_LENGTH (pack
) == 0)
11387 return expand_empty_fold (t
, complain
);
11389 return expand_right_fold (t
, pack
, complain
);
11392 /* Substitute into a binary right fold expression.
11394 Do ths by building a single (non-empty) vector of arguments and
11395 building the expression from those elements. */
11398 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11401 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11402 if (pack
== error_mark_node
)
11403 return error_mark_node
;
11404 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11405 if (init
== error_mark_node
)
11406 return error_mark_node
;
11408 if (PACK_EXPANSION_P (pack
))
11410 tree r
= copy_node (t
);
11411 FOLD_EXPR_PACK (r
) = pack
;
11412 FOLD_EXPR_INIT (r
) = init
;
11416 int n
= TREE_VEC_LENGTH (pack
);
11417 tree vec
= make_tree_vec (n
+ 1);
11418 for (int i
= 0; i
< n
; ++i
)
11419 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11420 TREE_VEC_ELT (vec
, n
) = init
;
11422 return expand_right_fold (t
, vec
, complain
);
11425 /* Walk through the pattern of a pack expansion, adding everything in
11426 local_specializations to a list. */
11431 tsubst_flags_t complain
;
11434 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11436 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11437 tree
*extra
= &data
.extra
;
11438 tsubst_flags_t complain
= data
.complain
;
11439 if (tree spec
= retrieve_local_specialization (*tp
))
11441 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11443 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11444 tree args
= ARGUMENT_PACK_ARGS (spec
);
11445 if (TREE_VEC_LENGTH (args
) == 1)
11447 tree elt
= TREE_VEC_ELT (args
, 0);
11448 if (PACK_EXPANSION_P (elt
))
11449 elt
= PACK_EXPANSION_PATTERN (elt
);
11450 if (DECL_PACK_P (elt
))
11453 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11455 /* Handle lambda capture here, since we aren't doing any
11456 substitution now, and so tsubst_copy won't call
11457 process_outer_var_ref. */
11458 tree args
= ARGUMENT_PACK_ARGS (spec
);
11459 int len
= TREE_VEC_LENGTH (args
);
11460 for (int i
= 0; i
< len
; ++i
)
11462 tree arg
= TREE_VEC_ELT (args
, i
);
11464 if (outer_automatic_var_p (arg
))
11465 carg
= process_outer_var_ref (arg
, complain
);
11468 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11472 spec
= copy_node (spec
);
11473 args
= copy_node (args
);
11474 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11475 register_local_specialization (spec
, *tp
);
11477 TREE_VEC_ELT (args
, i
) = carg
;
11482 if (outer_automatic_var_p (spec
))
11483 spec
= process_outer_var_ref (spec
, complain
);
11484 *extra
= tree_cons (*tp
, spec
, *extra
);
11489 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11491 el_data data
= { NULL_TREE
, complain
};
11492 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
11496 /* Substitute ARGS into T, which is an pack expansion
11497 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11498 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11499 (if only a partial substitution could be performed) or
11500 ERROR_MARK_NODE if there was an error. */
11502 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11506 tree pack
, packs
= NULL_TREE
;
11507 bool unsubstituted_packs
= false;
11508 bool unsubstituted_fn_pack
= false;
11511 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11512 bool need_local_specializations
= false;
11515 gcc_assert (PACK_EXPANSION_P (t
));
11516 pattern
= PACK_EXPANSION_PATTERN (t
);
11518 /* Add in any args remembered from an earlier partial instantiation. */
11519 tree extra
= PACK_EXPANSION_EXTRA_ARGS (t
);
11520 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
11522 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
11524 /* The partial instantiation involved local declarations collected in
11525 extract_local_specs; map from the general template to our local
11527 tree gen
= TREE_PURPOSE (elt
);
11528 tree inst
= TREE_VALUE (elt
);
11530 if (tree local
= retrieve_local_specialization (inst
))
11532 /* else inst is already a full instantiation of the pack. */
11533 register_local_specialization (inst
, gen
);
11535 gcc_assert (!TREE_PURPOSE (extra
));
11536 extra
= TREE_VALUE (extra
);
11538 args
= add_to_template_args (extra
, args
);
11540 levels
= TMPL_ARGS_DEPTH (args
);
11542 /* Determine the argument packs that will instantiate the parameter
11543 packs used in the expansion expression. While we're at it,
11544 compute the number of arguments to be expanded and make sure it
11546 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11547 pack
= TREE_CHAIN (pack
))
11549 tree parm_pack
= TREE_VALUE (pack
);
11550 tree arg_pack
= NULL_TREE
;
11551 tree orig_arg
= NULL_TREE
;
11554 if (TREE_CODE (parm_pack
) == BASES
)
11556 gcc_assert (parm_pack
== pattern
);
11557 if (BASES_DIRECT (parm_pack
))
11558 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11559 args
, complain
, in_decl
, false));
11561 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11562 args
, complain
, in_decl
, false));
11564 else if (builtin_pack_call_p (parm_pack
))
11566 /* ??? Support use in other patterns. */
11567 gcc_assert (parm_pack
== pattern
);
11568 return expand_builtin_pack_call (parm_pack
, args
,
11569 complain
, in_decl
);
11571 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11573 /* We know we have correct local_specializations if this
11574 expansion is at function scope, or if we're dealing with a
11575 local parameter in a requires expression; for the latter,
11576 tsubst_requires_expr set it up appropriately. */
11577 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11578 arg_pack
= retrieve_local_specialization (parm_pack
);
11580 /* We can't rely on local_specializations for a parameter
11581 name used later in a function declaration (such as in a
11582 late-specified return type). Even if it exists, it might
11583 have the wrong value for a recursive call. */
11584 need_local_specializations
= true;
11588 /* This parameter pack was used in an unevaluated context. Just
11589 make a dummy decl, since it's only used for its type. */
11590 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11591 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11592 /* Partial instantiation of the parm_pack, we can't build
11593 up an argument pack yet. */
11594 arg_pack
= NULL_TREE
;
11596 arg_pack
= make_fnparm_pack (arg_pack
);
11598 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11599 /* This argument pack isn't fully instantiated yet. We set this
11600 flag rather than clear arg_pack because we do want to do the
11601 optimization below, and we don't want to substitute directly
11602 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11603 where it isn't expected). */
11604 unsubstituted_fn_pack
= true;
11606 else if (is_normal_capture_proxy (parm_pack
))
11608 arg_pack
= retrieve_local_specialization (parm_pack
);
11609 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11610 unsubstituted_fn_pack
= true;
11615 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11617 if (level
<= levels
)
11618 arg_pack
= TMPL_ARG (args
, level
, idx
);
11621 orig_arg
= arg_pack
;
11622 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11623 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11625 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11626 /* This can only happen if we forget to expand an argument
11627 pack somewhere else. Just return an error, silently. */
11629 result
= make_tree_vec (1);
11630 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11637 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11639 /* Don't bother trying to do a partial substitution with
11640 incomplete packs; we'll try again after deduction. */
11641 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11646 else if (len
!= my_len
11647 && !unsubstituted_fn_pack
)
11649 if (!(complain
& tf_error
))
11650 /* Fail quietly. */;
11651 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11652 error ("mismatched argument pack lengths while expanding %qT",
11655 error ("mismatched argument pack lengths while expanding %qE",
11657 return error_mark_node
;
11660 /* Keep track of the parameter packs and their corresponding
11662 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11663 TREE_TYPE (packs
) = orig_arg
;
11667 /* We can't substitute for this parameter pack. We use a flag as
11668 well as the missing_level counter because function parameter
11669 packs don't have a level. */
11670 gcc_assert (processing_template_decl
);
11671 unsubstituted_packs
= true;
11675 /* If the expansion is just T..., return the matching argument pack, unless
11676 we need to call convert_from_reference on all the elements. This is an
11677 important optimization; see c++/68422. */
11678 if (!unsubstituted_packs
11679 && TREE_PURPOSE (packs
) == pattern
)
11681 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11683 /* If the argument pack is a single pack expansion, pull it out. */
11684 if (TREE_VEC_LENGTH (args
) == 1
11685 && pack_expansion_args_count (args
))
11686 return TREE_VEC_ELT (args
, 0);
11688 /* Types need no adjustment, nor does sizeof..., and if we still have
11689 some pack expansion args we won't do anything yet. */
11690 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11691 || PACK_EXPANSION_SIZEOF_P (t
)
11692 || pack_expansion_args_count (args
))
11694 /* Also optimize expression pack expansions if we can tell that the
11695 elements won't have reference type. */
11696 tree type
= TREE_TYPE (pattern
);
11697 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11698 && !PACK_EXPANSION_P (type
)
11699 && !WILDCARD_TYPE_P (type
))
11701 /* Otherwise use the normal path so we get convert_from_reference. */
11704 /* We cannot expand this expansion expression, because we don't have
11705 all of the argument packs we need. */
11706 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11708 /* We got some full packs, but we can't substitute them in until we
11709 have values for all the packs. So remember these until then. */
11711 t
= make_pack_expansion (pattern
, complain
);
11713 if (local_specializations
)
11714 if (tree locals
= extract_local_specs (pattern
, complain
))
11715 extra
= tree_cons (NULL_TREE
, extra
, locals
);
11716 PACK_EXPANSION_EXTRA_ARGS (t
) = extra
;
11719 else if (unsubstituted_packs
)
11721 /* There were no real arguments, we're just replacing a parameter
11722 pack with another version of itself. Substitute into the
11723 pattern and return a PACK_EXPANSION_*. The caller will need to
11725 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11726 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11727 /*integral_constant_expression_p=*/false);
11729 t
= tsubst (pattern
, args
, complain
, in_decl
);
11730 t
= make_pack_expansion (t
, complain
);
11734 gcc_assert (len
>= 0);
11736 if (need_local_specializations
)
11738 /* We're in a late-specified return type, so create our own local
11739 specializations map; the current map is either NULL or (in the
11740 case of recursive unification) might have bindings that we don't
11741 want to use or alter. */
11742 saved_local_specializations
= local_specializations
;
11743 local_specializations
= new hash_map
<tree
, tree
>;
11746 /* For each argument in each argument pack, substitute into the
11748 result
= make_tree_vec (len
);
11749 tree elem_args
= copy_template_args (args
);
11750 for (i
= 0; i
< len
; ++i
)
11752 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11754 elem_args
, complain
,
11756 TREE_VEC_ELT (result
, i
) = t
;
11757 if (t
== error_mark_node
)
11759 result
= error_mark_node
;
11764 /* Update ARGS to restore the substitution from parameter packs to
11765 their argument packs. */
11766 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11768 tree parm
= TREE_PURPOSE (pack
);
11770 if (TREE_CODE (parm
) == PARM_DECL
11772 || TREE_CODE (parm
) == FIELD_DECL
)
11773 register_local_specialization (TREE_TYPE (pack
), parm
);
11778 if (TREE_VALUE (pack
) == NULL_TREE
)
11781 template_parm_level_and_index (parm
, &level
, &idx
);
11783 /* Update the corresponding argument. */
11784 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11785 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11788 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11792 if (need_local_specializations
)
11794 delete local_specializations
;
11795 local_specializations
= saved_local_specializations
;
11798 /* If the dependent pack arguments were such that we end up with only a
11799 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11800 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11801 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11802 return TREE_VEC_ELT (result
, 0);
11807 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11808 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11809 parameter packs; all parms generated from a function parameter pack will
11810 have the same DECL_PARM_INDEX. */
11813 get_pattern_parm (tree parm
, tree tmpl
)
11815 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11818 if (DECL_ARTIFICIAL (parm
))
11820 for (patparm
= DECL_ARGUMENTS (pattern
);
11821 patparm
; patparm
= DECL_CHAIN (patparm
))
11822 if (DECL_ARTIFICIAL (patparm
)
11823 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11828 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11829 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11830 gcc_assert (DECL_PARM_INDEX (patparm
)
11831 == DECL_PARM_INDEX (parm
));
11837 /* Make an argument pack out of the TREE_VEC VEC. */
11840 make_argument_pack (tree vec
)
11843 tree elt
= TREE_VEC_ELT (vec
, 0);
11845 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11848 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11849 TREE_CONSTANT (pack
) = 1;
11851 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11855 /* Return an exact copy of template args T that can be modified
11859 copy_template_args (tree t
)
11861 if (t
== error_mark_node
)
11864 int len
= TREE_VEC_LENGTH (t
);
11865 tree new_vec
= make_tree_vec (len
);
11867 for (int i
= 0; i
< len
; ++i
)
11869 tree elt
= TREE_VEC_ELT (t
, i
);
11870 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11871 elt
= copy_template_args (elt
);
11872 TREE_VEC_ELT (new_vec
, i
) = elt
;
11875 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11876 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11881 /* Substitute ARGS into the vector or list of template arguments T. */
11884 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11887 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11890 if (t
== error_mark_node
)
11891 return error_mark_node
;
11893 len
= TREE_VEC_LENGTH (t
);
11894 elts
= XALLOCAVEC (tree
, len
);
11896 for (i
= 0; i
< len
; i
++)
11898 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11901 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11902 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11903 else if (PACK_EXPANSION_P (orig_arg
))
11905 /* Substitute into an expansion expression. */
11906 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11908 if (TREE_CODE (new_arg
) == TREE_VEC
)
11909 /* Add to the expanded length adjustment the number of
11910 expanded arguments. We subtract one from this
11911 measurement, because the argument pack expression
11912 itself is already counted as 1 in
11913 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11914 the argument pack is empty. */
11915 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11917 else if (ARGUMENT_PACK_P (orig_arg
))
11919 /* Substitute into each of the arguments. */
11920 new_arg
= TYPE_P (orig_arg
)
11921 ? cxx_make_type (TREE_CODE (orig_arg
))
11922 : make_node (TREE_CODE (orig_arg
));
11924 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11925 args
, complain
, in_decl
);
11926 if (pack_args
== error_mark_node
)
11927 new_arg
= error_mark_node
;
11929 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11931 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11932 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11935 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11937 if (new_arg
== error_mark_node
)
11938 return error_mark_node
;
11941 if (new_arg
!= orig_arg
)
11948 /* Make space for the expanded arguments coming from template
11950 t
= make_tree_vec (len
+ expanded_len_adjust
);
11951 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11952 arguments for a member template.
11953 In that case each TREE_VEC in ORIG_T represents a level of template
11954 arguments, and ORIG_T won't carry any non defaulted argument count.
11955 It will rather be the nested TREE_VECs that will carry one.
11956 In other words, ORIG_T carries a non defaulted argument count only
11957 if it doesn't contain any nested TREE_VEC. */
11958 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11960 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11961 count
+= expanded_len_adjust
;
11962 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11964 for (i
= 0, out
= 0; i
< len
; i
++)
11966 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11967 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11968 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11972 /* Now expand the template argument pack "in place". */
11973 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11974 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11978 TREE_VEC_ELT (t
, out
) = elts
[i
];
11986 /* Substitute ARGS into one level PARMS of template parameters. */
11989 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11991 if (parms
== error_mark_node
)
11992 return error_mark_node
;
11994 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11996 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11998 tree tuple
= TREE_VEC_ELT (parms
, i
);
12000 if (tuple
== error_mark_node
)
12003 TREE_VEC_ELT (new_vec
, i
) =
12004 tsubst_template_parm (tuple
, args
, complain
);
12010 /* Return the result of substituting ARGS into the template parameters
12011 given by PARMS. If there are m levels of ARGS and m + n levels of
12012 PARMS, then the result will contain n levels of PARMS. For
12013 example, if PARMS is `template <class T> template <class U>
12014 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12015 result will be `template <int*, double, class V>'. */
12018 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12020 tree r
= NULL_TREE
;
12023 /* When substituting into a template, we must set
12024 PROCESSING_TEMPLATE_DECL as the template parameters may be
12025 dependent if they are based on one-another, and the dependency
12026 predicates are short-circuit outside of templates. */
12027 ++processing_template_decl
;
12029 for (new_parms
= &r
;
12030 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12031 new_parms
= &(TREE_CHAIN (*new_parms
)),
12032 parms
= TREE_CHAIN (parms
))
12034 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12037 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12038 - TMPL_ARGS_DEPTH (args
)),
12039 new_vec
, NULL_TREE
);
12042 --processing_template_decl
;
12047 /* Return the result of substituting ARGS into one template parameter
12048 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12049 parameter and which TREE_PURPOSE is the default argument of the
12050 template parameter. */
12053 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12055 tree default_value
, parm_decl
;
12057 if (args
== NULL_TREE
12059 || t
== error_mark_node
)
12062 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12064 default_value
= TREE_PURPOSE (t
);
12065 parm_decl
= TREE_VALUE (t
);
12067 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12068 if (TREE_CODE (parm_decl
) == PARM_DECL
12069 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12070 parm_decl
= error_mark_node
;
12071 default_value
= tsubst_template_arg (default_value
, args
,
12072 complain
, NULL_TREE
);
12074 return build_tree_list (default_value
, parm_decl
);
12077 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12078 type T. If T is not an aggregate or enumeration type, it is
12079 handled as if by tsubst. IN_DECL is as for tsubst. If
12080 ENTERING_SCOPE is nonzero, T is the context for a template which
12081 we are presently tsubst'ing. Return the substituted value. */
12084 tsubst_aggr_type (tree t
,
12086 tsubst_flags_t complain
,
12088 int entering_scope
)
12090 if (t
== NULL_TREE
)
12093 switch (TREE_CODE (t
))
12096 if (TYPE_PTRMEMFUNC_P (t
))
12097 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12099 /* Fall through. */
12100 case ENUMERAL_TYPE
:
12102 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12107 int saved_unevaluated_operand
;
12108 int saved_inhibit_evaluation_warnings
;
12110 /* In "sizeof(X<I>)" we need to evaluate "I". */
12111 saved_unevaluated_operand
= cp_unevaluated_operand
;
12112 cp_unevaluated_operand
= 0;
12113 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12114 c_inhibit_evaluation_warnings
= 0;
12116 /* First, determine the context for the type we are looking
12118 context
= TYPE_CONTEXT (t
);
12119 if (context
&& TYPE_P (context
))
12121 context
= tsubst_aggr_type (context
, args
, complain
,
12122 in_decl
, /*entering_scope=*/1);
12123 /* If context is a nested class inside a class template,
12124 it may still need to be instantiated (c++/33959). */
12125 context
= complete_type (context
);
12128 /* Then, figure out what arguments are appropriate for the
12129 type we are trying to find. For example, given:
12131 template <class T> struct S;
12132 template <class T, class U> void f(T, U) { S<U> su; }
12134 and supposing that we are instantiating f<int, double>,
12135 then our ARGS will be {int, double}, but, when looking up
12136 S we only want {double}. */
12137 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12138 complain
, in_decl
);
12139 if (argvec
== error_mark_node
)
12140 r
= error_mark_node
;
12143 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12144 entering_scope
, complain
);
12145 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12148 cp_unevaluated_operand
= saved_unevaluated_operand
;
12149 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12154 /* This is not a template type, so there's nothing to do. */
12158 return tsubst (t
, args
, complain
, in_decl
);
12162 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12164 /* Substitute into the default argument ARG (a default argument for
12165 FN), which has the indicated TYPE. */
12168 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12169 tsubst_flags_t complain
)
12171 tree saved_class_ptr
= NULL_TREE
;
12172 tree saved_class_ref
= NULL_TREE
;
12173 int errs
= errorcount
+ sorrycount
;
12175 /* This can happen in invalid code. */
12176 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12179 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12180 parm
= chain_index (parmnum
, parm
);
12181 tree parmtype
= TREE_TYPE (parm
);
12182 if (DECL_BY_REFERENCE (parm
))
12183 parmtype
= TREE_TYPE (parmtype
);
12184 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12187 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12190 /* This default argument came from a template. Instantiate the
12191 default argument here, not in tsubst. In the case of
12200 we must be careful to do name lookup in the scope of S<T>,
12201 rather than in the current class. */
12202 push_access_scope (fn
);
12203 /* The "this" pointer is not valid in a default argument. */
12206 saved_class_ptr
= current_class_ptr
;
12207 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12208 saved_class_ref
= current_class_ref
;
12209 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12212 start_lambda_scope (parm
);
12214 push_deferring_access_checks(dk_no_deferred
);
12215 /* The default argument expression may cause implicitly defined
12216 member functions to be synthesized, which will result in garbage
12217 collection. We must treat this situation as if we were within
12218 the body of function so as to avoid collecting live data on the
12221 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12222 complain
, NULL_TREE
,
12223 /*integral_constant_expression_p=*/false);
12225 pop_deferring_access_checks();
12227 finish_lambda_scope ();
12229 /* Restore the "this" pointer. */
12232 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12233 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12236 if (errorcount
+sorrycount
> errs
12237 && (complain
& tf_warning_or_error
))
12238 inform (input_location
,
12239 " when instantiating default argument for call to %qD", fn
);
12241 /* Make sure the default argument is reasonable. */
12242 arg
= check_default_argument (type
, arg
, complain
);
12244 pop_access_scope (fn
);
12246 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12249 defarg_inst
= tree_cache_map::create_ggc (37);
12250 defarg_inst
->put (parm
, arg
);
12256 /* Substitute into all the default arguments for FN. */
12259 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12264 tmpl_args
= DECL_TI_ARGS (fn
);
12266 /* If this function is not yet instantiated, we certainly don't need
12267 its default arguments. */
12268 if (uses_template_parms (tmpl_args
))
12270 /* Don't do this again for clones. */
12271 if (DECL_CLONED_FUNCTION_P (fn
))
12275 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12277 arg
= TREE_CHAIN (arg
), ++i
)
12278 if (TREE_PURPOSE (arg
))
12279 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12281 TREE_PURPOSE (arg
),
12285 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12288 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12289 tree lambda_fntype
)
12291 tree gen_tmpl
, argvec
;
12292 hashval_t hash
= 0;
12295 /* Nobody should be tsubst'ing into non-template functions. */
12296 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12298 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12300 /* If T is not dependent, just return it. */
12301 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12304 /* Calculate the most general template of which R is a
12306 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12308 /* We're substituting a lambda function under tsubst_lambda_expr but not
12309 directly from it; find the matching function we're already inside.
12310 But don't do this if T is a generic lambda with a single level of
12311 template parms, as in that case we're doing a normal instantiation. */
12312 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12313 && (!generic_lambda_fn_p (t
)
12314 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12315 return enclosing_instantiation_of (t
);
12317 /* Calculate the complete set of arguments used to
12319 argvec
= tsubst_template_args (DECL_TI_ARGS
12320 (DECL_TEMPLATE_RESULT
12321 (DECL_TI_TEMPLATE (t
))),
12322 args
, complain
, in_decl
);
12323 if (argvec
== error_mark_node
)
12324 return error_mark_node
;
12326 /* Check to see if we already have this specialization. */
12327 if (!lambda_fntype
)
12329 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12330 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12334 /* We can see more levels of arguments than parameters if
12335 there was a specialization of a member template, like
12338 template <class T> struct S { template <class U> void f(); }
12339 template <> template <class U> void S<int>::f(U);
12341 Here, we'll be substituting into the specialization,
12342 because that's where we can find the code we actually
12343 want to generate, but we'll have enough arguments for
12344 the most general template.
12346 We also deal with the peculiar case:
12348 template <class T> struct S {
12349 template <class U> friend void f();
12351 template <class U> void f() {}
12353 template void f<double>();
12355 Here, the ARGS for the instantiation of will be {int,
12356 double}. But, we only need as many ARGS as there are
12357 levels of template parameters in CODE_PATTERN. We are
12358 careful not to get fooled into reducing the ARGS in
12361 template <class T> struct S { template <class U> void f(U); }
12362 template <class T> template <> void S<T>::f(int) {}
12364 which we can spot because the pattern will be a
12365 specialization in this case. */
12366 int args_depth
= TMPL_ARGS_DEPTH (args
);
12368 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12370 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12371 args
= get_innermost_template_args (args
, parms_depth
);
12375 /* This special case arises when we have something like this:
12377 template <class T> struct S {
12378 friend void f<int>(int, double);
12381 Here, the DECL_TI_TEMPLATE for the friend declaration
12382 will be an IDENTIFIER_NODE. We are being called from
12383 tsubst_friend_function, and we want only to create a
12384 new decl (R) with appropriate types so that we can call
12385 determine_specialization. */
12386 gen_tmpl
= NULL_TREE
;
12387 argvec
= NULL_TREE
;
12390 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12392 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12393 bool member
= ctx
&& TYPE_P (ctx
);
12395 if (member
&& !closure
)
12396 ctx
= tsubst_aggr_type (ctx
, args
,
12397 complain
, t
, /*entering_scope=*/1);
12399 tree type
= (lambda_fntype
? lambda_fntype
12400 : tsubst (TREE_TYPE (t
), args
,
12401 complain
| tf_fndecl_type
, in_decl
));
12402 if (type
== error_mark_node
)
12403 return error_mark_node
;
12405 /* If we hit excessive deduction depth, the type is bogus even if
12406 it isn't error_mark_node, so don't build a decl. */
12407 if (excessive_deduction_depth
)
12408 return error_mark_node
;
12410 /* We do NOT check for matching decls pushed separately at this
12411 point, as they may not represent instantiations of this
12412 template, and in any case are considered separate under the
12414 tree r
= copy_decl (t
);
12415 DECL_USE_TEMPLATE (r
) = 0;
12416 TREE_TYPE (r
) = type
;
12417 /* Clear out the mangled name and RTL for the instantiation. */
12418 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12419 SET_DECL_RTL (r
, NULL
);
12420 /* Leave DECL_INITIAL set on deleted instantiations. */
12421 if (!DECL_DELETED_FN (r
))
12422 DECL_INITIAL (r
) = NULL_TREE
;
12423 DECL_CONTEXT (r
) = ctx
;
12425 /* OpenMP UDRs have the only argument a reference to the declared
12426 type. We want to diagnose if the declared type is a reference,
12427 which is invalid, but as references to references are usually
12428 quietly merged, diagnose it here. */
12429 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12432 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12433 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12434 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12435 error_at (DECL_SOURCE_LOCATION (t
),
12436 "reference type %qT in "
12437 "%<#pragma omp declare reduction%>", argtype
);
12438 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12439 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12443 if (member
&& DECL_CONV_FN_P (r
))
12444 /* Type-conversion operator. Reconstruct the name, in
12445 case it's the name of one of the template's parameters. */
12446 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12448 tree parms
= DECL_ARGUMENTS (t
);
12450 parms
= DECL_CHAIN (parms
);
12451 parms
= tsubst (parms
, args
, complain
, t
);
12452 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12453 DECL_CONTEXT (parm
) = r
;
12456 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12457 DECL_CHAIN (tparm
) = parms
;
12460 DECL_ARGUMENTS (r
) = parms
;
12461 DECL_RESULT (r
) = NULL_TREE
;
12463 TREE_STATIC (r
) = 0;
12464 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12465 DECL_EXTERNAL (r
) = 1;
12466 /* If this is an instantiation of a function with internal
12467 linkage, we already know what object file linkage will be
12468 assigned to the instantiation. */
12469 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12470 DECL_DEFER_OUTPUT (r
) = 0;
12471 DECL_CHAIN (r
) = NULL_TREE
;
12472 DECL_PENDING_INLINE_INFO (r
) = 0;
12473 DECL_PENDING_INLINE_P (r
) = 0;
12474 DECL_SAVED_TREE (r
) = NULL_TREE
;
12475 DECL_STRUCT_FUNCTION (r
) = NULL
;
12477 /* We'll re-clone as appropriate in instantiate_template. */
12478 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12480 /* If we aren't complaining now, return on error before we register
12481 the specialization so that we'll complain eventually. */
12482 if ((complain
& tf_error
) == 0
12483 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12484 && !grok_op_properties (r
, /*complain=*/false))
12485 return error_mark_node
;
12487 /* When instantiating a constrained member, substitute
12488 into the constraints to create a new constraint. */
12489 if (tree ci
= get_constraints (t
))
12492 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12493 set_constraints (r
, ci
);
12496 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12497 this in the special friend case mentioned above where
12498 GEN_TMPL is NULL. */
12499 if (gen_tmpl
&& !closure
)
12501 DECL_TEMPLATE_INFO (r
)
12502 = build_template_info (gen_tmpl
, argvec
);
12503 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12506 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12508 /* We instantiated this while substituting into
12509 the type earlier (template/friend54.C). */
12512 /* We're not supposed to instantiate default arguments
12513 until they are called, for a template. But, for a
12516 template <class T> void f ()
12517 { extern void g(int i = T()); }
12519 we should do the substitution when the template is
12520 instantiated. We handle the member function case in
12521 instantiate_class_template since the default arguments
12522 might refer to other members of the class. */
12524 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12525 && !uses_template_parms (argvec
))
12526 tsubst_default_arguments (r
, complain
);
12529 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12531 /* Copy the list of befriending classes. */
12532 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12534 friends
= &TREE_CHAIN (*friends
))
12536 *friends
= copy_node (*friends
);
12537 TREE_VALUE (*friends
)
12538 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12541 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12543 maybe_retrofit_in_chrg (r
);
12544 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12545 return error_mark_node
;
12546 /* If this is an instantiation of a member template, clone it.
12547 If it isn't, that'll be handled by
12548 clone_constructors_and_destructors. */
12549 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12550 clone_function_decl (r
, /*update_methods=*/false);
12552 else if ((complain
& tf_error
) != 0
12553 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12554 && !grok_op_properties (r
, /*complain=*/true))
12555 return error_mark_node
;
12557 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12558 SET_DECL_FRIEND_CONTEXT (r
,
12559 tsubst (DECL_FRIEND_CONTEXT (t
),
12560 args
, complain
, in_decl
));
12562 /* Possibly limit visibility based on template args. */
12563 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12564 if (DECL_VISIBILITY_SPECIFIED (t
))
12566 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12567 DECL_ATTRIBUTES (r
)
12568 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12570 determine_visibility (r
);
12571 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12572 && !processing_template_decl
)
12573 defaulted_late_check (r
);
12575 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12576 args
, complain
, in_decl
);
12580 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12583 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12584 tree lambda_fntype
)
12586 /* We can get here when processing a member function template,
12587 member class template, or template template parameter. */
12588 tree decl
= DECL_TEMPLATE_RESULT (t
);
12594 hashval_t hash
= 0;
12596 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12598 /* Template template parameter is treated here. */
12599 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12600 if (new_type
== error_mark_node
)
12601 r
= error_mark_node
;
12602 /* If we get a real template back, return it. This can happen in
12603 the context of most_specialized_partial_spec. */
12604 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12607 /* The new TEMPLATE_DECL was built in
12608 reduce_template_parm_level. */
12609 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12613 if (!lambda_fntype
)
12615 /* We might already have an instance of this template.
12616 The ARGS are for the surrounding class type, so the
12617 full args contain the tsubst'd args for the context,
12618 plus the innermost args from the template decl. */
12619 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12620 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12621 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12622 /* Because this is a template, the arguments will still be
12623 dependent, even after substitution. If
12624 PROCESSING_TEMPLATE_DECL is not set, the dependency
12625 predicates will short-circuit. */
12626 ++processing_template_decl
;
12627 full_args
= tsubst_template_args (tmpl_args
, args
,
12628 complain
, in_decl
);
12629 --processing_template_decl
;
12630 if (full_args
== error_mark_node
)
12631 return error_mark_node
;
12633 /* If this is a default template template argument,
12634 tsubst might not have changed anything. */
12635 if (full_args
== tmpl_args
)
12638 hash
= hash_tmpl_and_args (t
, full_args
);
12639 spec
= retrieve_specialization (t
, full_args
, hash
);
12640 if (spec
!= NULL_TREE
)
12644 /* Make a new template decl. It will be similar to the
12645 original, but will record the current template arguments.
12646 We also create a new function declaration, which is just
12647 like the old one, but points to this new template, rather
12648 than the old one. */
12650 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12651 DECL_CHAIN (r
) = NULL_TREE
;
12653 // Build new template info linking to the original template decl.
12654 if (!lambda_fntype
)
12656 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12657 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12660 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12662 /* The template parameters for this new template are all the
12663 template parameters for the old template, except the
12664 outermost level of parameters. */
12665 DECL_TEMPLATE_PARMS (r
)
12666 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12669 if (TREE_CODE (decl
) == TYPE_DECL
12670 && !TYPE_DECL_ALIAS_P (decl
))
12673 ++processing_template_decl
;
12674 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12675 --processing_template_decl
;
12676 if (new_type
== error_mark_node
)
12677 return error_mark_node
;
12679 TREE_TYPE (r
) = new_type
;
12680 /* For a partial specialization, we need to keep pointing to
12681 the primary template. */
12682 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12683 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12684 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12685 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12686 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12691 ++processing_template_decl
;
12692 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12693 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12695 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12696 --processing_template_decl
;
12697 if (new_decl
== error_mark_node
)
12698 return error_mark_node
;
12700 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12701 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12702 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12705 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12706 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12710 DECL_TI_TEMPLATE (new_decl
) = r
;
12711 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12715 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12716 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12718 if (PRIMARY_TEMPLATE_P (t
))
12719 DECL_PRIMARY_TEMPLATE (r
) = r
;
12721 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12723 /* Record this non-type partial instantiation. */
12724 register_specialization (r
, t
,
12725 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12731 /* True if FN is the op() for a lambda in an uninstantiated template. */
12734 lambda_fn_in_template_p (tree fn
)
12736 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12738 tree closure
= DECL_CONTEXT (fn
);
12739 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12742 /* We're instantiating a variable from template function TCTX. Return the
12743 corresponding current enclosing scope. This gets complicated because lambda
12744 functions in templates are regenerated rather than instantiated, but generic
12745 lambda functions are subsequently instantiated. */
12748 enclosing_instantiation_of (tree otctx
)
12751 tree fn
= current_function_decl
;
12752 int lambda_count
= 0;
12754 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12755 tctx
= decl_function_context (tctx
))
12757 for (; fn
; fn
= decl_function_context (fn
))
12760 int flambda_count
= 0;
12761 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12762 fn
= decl_function_context (fn
))
12764 if (DECL_TEMPLATE_INFO (fn
)
12765 ? most_general_template (fn
) != most_general_template (tctx
)
12768 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12769 || DECL_CONV_FN_P (ofn
));
12772 gcc_unreachable ();
12775 /* Substitute the ARGS into the T, which is a _DECL. Return the
12776 result of the substitution. Issue error and warning messages under
12777 control of COMPLAIN. */
12780 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12782 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12783 location_t saved_loc
;
12784 tree r
= NULL_TREE
;
12786 hashval_t hash
= 0;
12788 /* Set the filename and linenumber to improve error-reporting. */
12789 saved_loc
= input_location
;
12790 input_location
= DECL_SOURCE_LOCATION (t
);
12792 switch (TREE_CODE (t
))
12794 case TEMPLATE_DECL
:
12795 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12798 case FUNCTION_DECL
:
12799 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12804 tree type
= NULL_TREE
;
12806 tree expanded_types
= NULL_TREE
;
12807 tree prev_r
= NULL_TREE
;
12808 tree first_r
= NULL_TREE
;
12810 if (DECL_PACK_P (t
))
12812 /* If there is a local specialization that isn't a
12813 parameter pack, it means that we're doing a "simple"
12814 substitution from inside tsubst_pack_expansion. Just
12815 return the local specialization (which will be a single
12817 tree spec
= retrieve_local_specialization (t
);
12819 && TREE_CODE (spec
) == PARM_DECL
12820 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12823 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12824 the parameters in this function parameter pack. */
12825 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12826 complain
, in_decl
);
12827 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12829 len
= TREE_VEC_LENGTH (expanded_types
);
12831 /* Zero-length parameter packs are boring. Just substitute
12834 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12839 /* All we did was update the type. Make a note of that. */
12840 type
= expanded_types
;
12841 expanded_types
= NULL_TREE
;
12845 /* Loop through all of the parameters we'll build. When T is
12846 a function parameter pack, LEN is the number of expanded
12847 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12849 for (i
= 0; i
< len
; ++i
)
12853 if (DECL_TEMPLATE_PARM_P (t
))
12854 SET_DECL_TEMPLATE_PARM_P (r
);
12856 if (expanded_types
)
12857 /* We're on the Ith parameter of the function parameter
12860 /* Get the Ith type. */
12861 type
= TREE_VEC_ELT (expanded_types
, i
);
12863 /* Rename the parameter to include the index. */
12865 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12868 /* We're dealing with a normal parameter. */
12869 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12871 type
= type_decays_to (type
);
12872 TREE_TYPE (r
) = type
;
12873 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12875 if (DECL_INITIAL (r
))
12877 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12878 DECL_INITIAL (r
) = TREE_TYPE (r
);
12880 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12881 complain
, in_decl
);
12884 DECL_CONTEXT (r
) = NULL_TREE
;
12886 if (!DECL_TEMPLATE_PARM_P (r
))
12887 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12889 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12890 args
, complain
, in_decl
);
12892 /* Keep track of the first new parameter we
12893 generate. That's what will be returned to the
12898 /* Build a proper chain of parameters when substituting
12899 into a function parameter pack. */
12901 DECL_CHAIN (prev_r
) = r
;
12904 /* If cp_unevaluated_operand is set, we're just looking for a
12905 single dummy parameter, so don't keep going. */
12906 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12907 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12908 complain
, DECL_CHAIN (t
));
12910 /* FIRST_R contains the start of the chain we've built. */
12917 tree type
= NULL_TREE
;
12918 tree vec
= NULL_TREE
;
12919 tree expanded_types
= NULL_TREE
;
12922 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12924 /* This field is a lambda capture pack. Return a TREE_VEC of
12925 the expanded fields to instantiate_class_template_1. */
12926 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12927 complain
, in_decl
);
12928 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12930 len
= TREE_VEC_LENGTH (expanded_types
);
12931 vec
= make_tree_vec (len
);
12935 /* All we did was update the type. Make a note of that. */
12936 type
= expanded_types
;
12937 expanded_types
= NULL_TREE
;
12941 for (int i
= 0; i
< len
; ++i
)
12944 if (expanded_types
)
12946 type
= TREE_VEC_ELT (expanded_types
, i
);
12948 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12951 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12953 if (type
== error_mark_node
)
12954 RETURN (error_mark_node
);
12955 TREE_TYPE (r
) = type
;
12956 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12958 if (DECL_C_BIT_FIELD (r
))
12959 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12961 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12962 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12964 /*integral_constant_expression_p=*/true);
12965 if (DECL_INITIAL (t
))
12967 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12968 NSDMI in perform_member_init. Still set DECL_INITIAL
12969 so that we know there is one. */
12970 DECL_INITIAL (r
) = void_node
;
12971 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12972 retrofit_lang_decl (r
);
12973 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12975 /* We don't have to set DECL_CONTEXT here; it is set by
12976 finish_member_declaration. */
12977 DECL_CHAIN (r
) = NULL_TREE
;
12979 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12980 args
, complain
, in_decl
);
12983 TREE_VEC_ELT (vec
, i
) = r
;
12992 /* We reach here only for member using decls. We also need to check
12993 uses_template_parms because DECL_DEPENDENT_P is not set for a
12994 using-declaration that designates a member of the current
12995 instantiation (c++/53549). */
12996 if (DECL_DEPENDENT_P (t
)
12997 || uses_template_parms (USING_DECL_SCOPE (t
)))
12999 tree scope
= USING_DECL_SCOPE (t
);
13000 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13001 if (PACK_EXPANSION_P (scope
))
13003 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13004 int len
= TREE_VEC_LENGTH (vec
);
13005 r
= make_tree_vec (len
);
13006 for (int i
= 0; i
< len
; ++i
)
13008 tree escope
= TREE_VEC_ELT (vec
, i
);
13009 tree elt
= do_class_using_decl (escope
, name
);
13012 r
= error_mark_node
;
13017 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13018 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13020 TREE_VEC_ELT (r
, i
) = elt
;
13025 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13026 complain
, in_decl
);
13027 r
= do_class_using_decl (inst_scope
, name
);
13029 r
= error_mark_node
;
13032 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13033 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13040 DECL_CHAIN (r
) = NULL_TREE
;
13047 tree argvec
= NULL_TREE
;
13048 tree gen_tmpl
= NULL_TREE
;
13050 tree tmpl
= NULL_TREE
;
13052 tree type
= NULL_TREE
;
13055 if (TREE_TYPE (t
) == error_mark_node
)
13056 RETURN (error_mark_node
);
13058 if (TREE_CODE (t
) == TYPE_DECL
13059 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13061 /* If this is the canonical decl, we don't have to
13062 mess with instantiations, and often we can't (for
13063 typename, template type parms and such). Note that
13064 TYPE_NAME is not correct for the above test if
13065 we've copied the type for a typedef. */
13066 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13067 if (type
== error_mark_node
)
13068 RETURN (error_mark_node
);
13069 r
= TYPE_NAME (type
);
13073 /* Check to see if we already have the specialization we
13076 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13078 /* T is a static data member or namespace-scope entity.
13079 We have to substitute into namespace-scope variables
13080 (not just variable templates) because of cases like:
13082 template <class T> void f() { extern T t; }
13084 where the entity referenced is not known until
13085 instantiation time. */
13087 ctx
= DECL_CONTEXT (t
);
13088 if (DECL_CLASS_SCOPE_P (t
))
13090 ctx
= tsubst_aggr_type (ctx
, args
,
13092 in_decl
, /*entering_scope=*/1);
13093 /* If CTX is unchanged, then T is in fact the
13094 specialization we want. That situation occurs when
13095 referencing a static data member within in its own
13096 class. We can use pointer equality, rather than
13097 same_type_p, because DECL_CONTEXT is always
13099 if (ctx
== DECL_CONTEXT (t
)
13100 /* ... unless T is a member template; in which
13101 case our caller can be willing to create a
13102 specialization of that template represented
13104 && !(DECL_TI_TEMPLATE (t
)
13105 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13111 tmpl
= DECL_TI_TEMPLATE (t
);
13112 gen_tmpl
= most_general_template (tmpl
);
13113 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13114 if (argvec
!= error_mark_node
)
13115 argvec
= (coerce_innermost_template_parms
13116 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13117 argvec
, t
, complain
,
13118 /*all*/true, /*defarg*/true));
13119 if (argvec
== error_mark_node
)
13120 RETURN (error_mark_node
);
13121 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13122 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13127 /* A local variable. */
13129 /* Subsequent calls to pushdecl will fill this in. */
13131 /* Unless this is a reference to a static variable from an
13132 enclosing function, in which case we need to fill it in now. */
13133 if (TREE_STATIC (t
))
13135 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13136 if (fn
!= current_function_decl
)
13139 spec
= retrieve_local_specialization (t
);
13141 /* If we already have the specialization we need, there is
13142 nothing more to do. */
13149 /* Create a new node for the specialization we need. */
13151 if (type
== NULL_TREE
)
13153 if (is_typedef_decl (t
))
13154 type
= DECL_ORIGINAL_TYPE (t
);
13156 type
= TREE_TYPE (t
);
13158 && VAR_HAD_UNKNOWN_BOUND (t
)
13159 && type
!= error_mark_node
)
13160 type
= strip_array_domain (type
);
13161 tree sub_args
= args
;
13162 if (tree auto_node
= type_uses_auto (type
))
13164 /* Mask off any template args past the variable's context so we
13165 don't replace the auto with an unrelated argument. */
13166 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13167 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13169 /* This should never happen with the new lambda instantiation
13170 model, but keep the handling just in case. */
13171 gcc_assert (!CHECKING_P
),
13172 sub_args
= strip_innermost_template_args (args
, extra
);
13174 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13178 /* Even if the original location is out of scope, the
13179 newly substituted one is not. */
13180 DECL_DEAD_FOR_LOCAL (r
) = 0;
13181 DECL_INITIALIZED_P (r
) = 0;
13182 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13183 if (type
== error_mark_node
)
13184 RETURN (error_mark_node
);
13185 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13187 /* It may seem that this case cannot occur, since:
13192 declares a function, not a variable. However:
13195 template <typename T> void g() { T t; }
13196 template void g<f>();
13198 is an attempt to declare a variable with function
13200 error ("variable %qD has function type",
13201 /* R is not yet sufficiently initialized, so we
13202 just use its name. */
13204 RETURN (error_mark_node
);
13206 type
= complete_type (type
);
13207 /* Wait until cp_finish_decl to set this again, to handle
13208 circular dependency (template/instantiate6.C). */
13209 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13210 type
= check_var_type (DECL_NAME (r
), type
);
13212 if (DECL_HAS_VALUE_EXPR_P (t
))
13214 tree ve
= DECL_VALUE_EXPR (t
);
13215 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13216 /*constant_expression_p=*/false);
13217 if (REFERENCE_REF_P (ve
))
13219 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13220 ve
= TREE_OPERAND (ve
, 0);
13222 SET_DECL_VALUE_EXPR (r
, ve
);
13224 if (CP_DECL_THREAD_LOCAL_P (r
)
13225 && !processing_template_decl
)
13226 set_decl_tls_model (r
, decl_default_tls_model (r
));
13228 else if (DECL_SELF_REFERENCE_P (t
))
13229 SET_DECL_SELF_REFERENCE_P (r
);
13230 TREE_TYPE (r
) = type
;
13231 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13232 DECL_CONTEXT (r
) = ctx
;
13233 /* Clear out the mangled name and RTL for the instantiation. */
13234 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13235 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13236 SET_DECL_RTL (r
, NULL
);
13237 /* The initializer must not be expanded until it is required;
13238 see [temp.inst]. */
13239 DECL_INITIAL (r
) = NULL_TREE
;
13240 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13243 if (DECL_LANG_SPECIFIC (r
))
13244 SET_DECL_DEPENDENT_INIT_P (r
, false);
13246 SET_DECL_MODE (r
, VOIDmode
);
13248 /* Possibly limit visibility based on template args. */
13249 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13250 if (DECL_VISIBILITY_SPECIFIED (t
))
13252 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13253 DECL_ATTRIBUTES (r
)
13254 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13256 determine_visibility (r
);
13261 /* A static data member declaration is always marked
13262 external when it is declared in-class, even if an
13263 initializer is present. We mimic the non-template
13264 processing here. */
13265 DECL_EXTERNAL (r
) = 1;
13266 if (DECL_NAMESPACE_SCOPE_P (t
))
13267 DECL_NOT_REALLY_EXTERN (r
) = 1;
13269 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13270 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13271 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13275 if (DECL_LANG_SPECIFIC (r
))
13276 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13277 if (!cp_unevaluated_operand
)
13278 register_local_specialization (r
, t
);
13281 DECL_CHAIN (r
) = NULL_TREE
;
13283 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13285 args
, complain
, in_decl
);
13287 /* Preserve a typedef that names a type. */
13288 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13290 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13291 set_underlying_type (r
);
13292 if (TYPE_DECL_ALIAS_P (r
))
13293 /* An alias template specialization can be dependent
13294 even if its underlying type is not. */
13295 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13298 layout_decl (r
, 0);
13303 gcc_unreachable ();
13308 /* Restore the file and line information. */
13309 input_location
= saved_loc
;
13314 /* Substitute into the ARG_TYPES of a function type.
13315 If END is a TREE_CHAIN, leave it and any following types
13319 tsubst_arg_types (tree arg_types
,
13322 tsubst_flags_t complain
,
13325 tree remaining_arg_types
;
13326 tree type
= NULL_TREE
;
13328 tree expanded_args
= NULL_TREE
;
13331 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13334 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13335 args
, end
, complain
, in_decl
);
13336 if (remaining_arg_types
== error_mark_node
)
13337 return error_mark_node
;
13339 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13341 /* For a pack expansion, perform substitution on the
13342 entire expression. Later on, we'll handle the arguments
13344 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13345 args
, complain
, in_decl
);
13347 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13348 /* So that we'll spin through the parameters, one by one. */
13349 i
= TREE_VEC_LENGTH (expanded_args
);
13352 /* We only partially substituted into the parameter
13353 pack. Our type is TYPE_PACK_EXPANSION. */
13354 type
= expanded_args
;
13355 expanded_args
= NULL_TREE
;
13363 type
= TREE_VEC_ELT (expanded_args
, i
);
13365 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13367 if (type
== error_mark_node
)
13368 return error_mark_node
;
13369 if (VOID_TYPE_P (type
))
13371 if (complain
& tf_error
)
13373 error ("invalid parameter type %qT", type
);
13375 error ("in declaration %q+D", in_decl
);
13377 return error_mark_node
;
13380 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13381 return error_mark_node
;
13383 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13384 top-level qualifiers as required. */
13385 type
= cv_unqualified (type_decays_to (type
));
13387 /* We do not substitute into default arguments here. The standard
13388 mandates that they be instantiated only when needed, which is
13389 done in build_over_call. */
13390 default_arg
= TREE_PURPOSE (arg_types
);
13392 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13393 since the new op() won't have any associated template arguments for us
13394 to refer to later. */
13395 if (lambda_fn_in_template_p (in_decl
))
13396 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13397 false/*fn*/, false/*constexpr*/);
13399 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13401 /* We've instantiated a template before its default arguments
13402 have been parsed. This can happen for a nested template
13403 class, and is not an error unless we require the default
13404 argument in a call of this function. */
13405 remaining_arg_types
=
13406 tree_cons (default_arg
, type
, remaining_arg_types
);
13407 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13410 remaining_arg_types
=
13411 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13414 return remaining_arg_types
;
13417 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13418 *not* handle the exception-specification for FNTYPE, because the
13419 initial substitution of explicitly provided template parameters
13420 during argument deduction forbids substitution into the
13421 exception-specification:
13425 All references in the function type of the function template to the
13426 corresponding template parameters are replaced by the specified tem-
13427 plate argument values. If a substitution in a template parameter or
13428 in the function type of the function template results in an invalid
13429 type, type deduction fails. [Note: The equivalent substitution in
13430 exception specifications is done only when the function is instanti-
13431 ated, at which point a program is ill-formed if the substitution
13432 results in an invalid type.] */
13435 tsubst_function_type (tree t
,
13437 tsubst_flags_t complain
,
13441 tree arg_types
= NULL_TREE
;
13444 /* The TYPE_CONTEXT is not used for function/method types. */
13445 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13447 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13449 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13451 if (late_return_type_p
)
13453 /* Substitute the argument types. */
13454 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13455 complain
, in_decl
);
13456 if (arg_types
== error_mark_node
)
13457 return error_mark_node
;
13459 tree save_ccp
= current_class_ptr
;
13460 tree save_ccr
= current_class_ref
;
13461 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13462 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13463 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13466 /* DR 1207: 'this' is in scope in the trailing return type. */
13467 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13470 /* Substitute the return type. */
13471 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13475 current_class_ptr
= save_ccp
;
13476 current_class_ref
= save_ccr
;
13480 /* Substitute the return type. */
13481 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13483 if (return_type
== error_mark_node
)
13484 return error_mark_node
;
13485 /* DR 486 clarifies that creation of a function type with an
13486 invalid return type is a deduction failure. */
13487 if (TREE_CODE (return_type
) == ARRAY_TYPE
13488 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13490 if (complain
& tf_error
)
13492 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13493 error ("function returning an array");
13495 error ("function returning a function");
13497 return error_mark_node
;
13500 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13501 return error_mark_node
;
13503 if (!late_return_type_p
)
13505 /* Substitute the argument types. */
13506 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13507 complain
, in_decl
);
13508 if (arg_types
== error_mark_node
)
13509 return error_mark_node
;
13512 /* Construct a new type node and return it. */
13513 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13515 fntype
= build_function_type (return_type
, arg_types
);
13516 fntype
= apply_memfn_quals (fntype
,
13517 type_memfn_quals (t
),
13518 type_memfn_rqual (t
));
13522 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13523 /* Don't pick up extra function qualifiers from the basetype. */
13524 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13525 if (! MAYBE_CLASS_TYPE_P (r
))
13529 Type deduction may fail for any of the following
13532 -- Attempting to create "pointer to member of T" when T
13533 is not a class type. */
13534 if (complain
& tf_error
)
13535 error ("creating pointer to member function of non-class type %qT",
13537 return error_mark_node
;
13540 fntype
= build_method_type_directly (r
, return_type
,
13541 TREE_CHAIN (arg_types
));
13542 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13544 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13546 if (late_return_type_p
)
13547 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13552 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13553 ARGS into that specification, and return the substituted
13554 specification. If there is no specification, return NULL_TREE. */
13557 tsubst_exception_specification (tree fntype
,
13559 tsubst_flags_t complain
,
13566 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13567 new_specs
= NULL_TREE
;
13568 if (specs
&& TREE_PURPOSE (specs
))
13570 /* A noexcept-specifier. */
13571 tree expr
= TREE_PURPOSE (specs
);
13572 if (TREE_CODE (expr
) == INTEGER_CST
)
13576 /* Defer instantiation of noexcept-specifiers to avoid
13577 excessive instantiations (c++/49107). */
13578 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13579 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13581 /* We already partially instantiated this member template,
13582 so combine the new args with the old. */
13583 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13584 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13585 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13586 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13590 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13591 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13595 new_specs
= tsubst_copy_and_build
13596 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13597 /*integral_constant_expression_p=*/true);
13598 new_specs
= build_noexcept_spec (new_specs
, complain
);
13602 if (! TREE_VALUE (specs
))
13609 tree expanded_specs
= NULL_TREE
;
13611 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13613 /* Expand the pack expansion type. */
13614 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13618 if (expanded_specs
== error_mark_node
)
13619 return error_mark_node
;
13620 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13621 len
= TREE_VEC_LENGTH (expanded_specs
);
13624 /* We're substituting into a member template, so
13625 we got a TYPE_PACK_EXPANSION back. Add that
13626 expansion and move on. */
13627 gcc_assert (TREE_CODE (expanded_specs
)
13628 == TYPE_PACK_EXPANSION
);
13629 new_specs
= add_exception_specifier (new_specs
,
13632 specs
= TREE_CHAIN (specs
);
13637 for (i
= 0; i
< len
; ++i
)
13639 if (expanded_specs
)
13640 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13642 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13643 if (spec
== error_mark_node
)
13645 new_specs
= add_exception_specifier (new_specs
, spec
,
13649 specs
= TREE_CHAIN (specs
);
13655 /* Take the tree structure T and replace template parameters used
13656 therein with the argument vector ARGS. IN_DECL is an associated
13657 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13658 Issue error and warning messages under control of COMPLAIN. Note
13659 that we must be relatively non-tolerant of extensions here, in
13660 order to preserve conformance; if we allow substitutions that
13661 should not be allowed, we may allow argument deductions that should
13662 not succeed, and therefore report ambiguous overload situations
13663 where there are none. In theory, we could allow the substitution,
13664 but indicate that it should have failed, and allow our caller to
13665 make sure that the right thing happens, but we don't try to do this
13668 This function is used for dealing with types, decls and the like;
13669 for expressions, use tsubst_expr or tsubst_copy. */
13672 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13674 enum tree_code code
;
13675 tree type
, r
= NULL_TREE
;
13677 if (t
== NULL_TREE
|| t
== error_mark_node
13678 || t
== integer_type_node
13679 || t
== void_type_node
13680 || t
== char_type_node
13681 || t
== unknown_type_node
13682 || TREE_CODE (t
) == NAMESPACE_DECL
13683 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13687 return tsubst_decl (t
, args
, complain
);
13689 if (args
== NULL_TREE
)
13692 code
= TREE_CODE (t
);
13694 if (code
== IDENTIFIER_NODE
)
13695 type
= IDENTIFIER_TYPE_VALUE (t
);
13697 type
= TREE_TYPE (t
);
13699 gcc_assert (type
!= unknown_type_node
);
13701 /* Reuse typedefs. We need to do this to handle dependent attributes,
13702 such as attribute aligned. */
13704 && typedef_variant_p (t
))
13706 tree decl
= TYPE_NAME (t
);
13708 if (alias_template_specialization_p (t
))
13710 /* DECL represents an alias template and we want to
13712 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13713 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13714 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13716 else if (DECL_CLASS_SCOPE_P (decl
)
13717 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13718 && uses_template_parms (DECL_CONTEXT (decl
)))
13720 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13721 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13722 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13724 else if (DECL_FUNCTION_SCOPE_P (decl
)
13725 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13726 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13727 r
= retrieve_local_specialization (decl
);
13729 /* The typedef is from a non-template context. */
13735 r
= cp_build_qualified_type_real
13736 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13737 complain
| tf_ignore_bad_quals
);
13742 /* We don't have an instantiation yet, so drop the typedef. */
13743 int quals
= cp_type_quals (t
);
13744 t
= DECL_ORIGINAL_TYPE (decl
);
13745 t
= cp_build_qualified_type_real (t
, quals
,
13746 complain
| tf_ignore_bad_quals
);
13750 bool fndecl_type
= (complain
& tf_fndecl_type
);
13751 complain
&= ~tf_fndecl_type
;
13754 && code
!= TYPENAME_TYPE
13755 && code
!= TEMPLATE_TYPE_PARM
13756 && code
!= TEMPLATE_PARM_INDEX
13757 && code
!= IDENTIFIER_NODE
13758 && code
!= FUNCTION_TYPE
13759 && code
!= METHOD_TYPE
)
13760 type
= tsubst (type
, args
, complain
, in_decl
);
13761 if (type
== error_mark_node
)
13762 return error_mark_node
;
13768 case ENUMERAL_TYPE
:
13769 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13770 /*entering_scope=*/0);
13773 case IDENTIFIER_NODE
:
13784 if (t
== integer_type_node
)
13787 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13788 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13792 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13794 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13795 /*integral_constant_expression_p=*/false);
13797 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13799 if (TREE_CODE (max
) == NOP_EXPR
13800 && TREE_SIDE_EFFECTS (omax
)
13801 && !TREE_TYPE (max
))
13802 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13804 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13805 with TREE_SIDE_EFFECTS that indicates this is not an integral
13806 constant expression. */
13807 if (processing_template_decl
13808 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13810 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13811 TREE_SIDE_EFFECTS (max
) = 1;
13814 return compute_array_index_type (NULL_TREE
, max
, complain
);
13817 case TEMPLATE_TYPE_PARM
:
13818 case TEMPLATE_TEMPLATE_PARM
:
13819 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13820 case TEMPLATE_PARM_INDEX
:
13825 tree arg
= NULL_TREE
;
13827 /* Early in template argument deduction substitution, we don't
13828 want to reduce the level of 'auto', or it will be confused
13829 with a normal template parm in subsequent deduction. */
13830 if (is_auto (t
) && (complain
& tf_partial
))
13835 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13836 template_parm_level_and_index (t
, &level
, &idx
);
13838 levels
= TMPL_ARGS_DEPTH (args
);
13839 if (level
<= levels
13840 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13842 arg
= TMPL_ARG (args
, level
, idx
);
13844 /* See through ARGUMENT_PACK_SELECT arguments. */
13845 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13846 arg
= argument_pack_select_arg (arg
);
13849 if (arg
== error_mark_node
)
13850 return error_mark_node
;
13851 else if (arg
!= NULL_TREE
)
13853 if (ARGUMENT_PACK_P (arg
))
13854 /* If ARG is an argument pack, we don't actually want to
13855 perform a substitution here, because substitutions
13856 for argument packs are only done
13857 element-by-element. We can get to this point when
13858 substituting the type of a non-type template
13859 parameter pack, when that type actually contains
13860 template parameter packs from an outer template, e.g.,
13862 template<typename... Types> struct A {
13863 template<Types... Values> struct B { };
13867 if (code
== TEMPLATE_TYPE_PARM
)
13870 gcc_assert (TYPE_P (arg
));
13872 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13874 return cp_build_qualified_type_real
13875 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13877 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13879 /* We are processing a type constructed from a
13880 template template parameter. */
13881 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13882 args
, complain
, in_decl
);
13883 if (argvec
== error_mark_node
)
13884 return error_mark_node
;
13886 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13887 || TREE_CODE (arg
) == TEMPLATE_DECL
13888 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13890 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13891 /* Consider this code:
13893 template <template <class> class Template>
13895 template <class Arg> using Bind = Template<Arg>;
13898 template <template <class> class Template, class Arg>
13899 using Instantiate = Template<Arg>; //#0
13901 template <template <class> class Template,
13904 Instantiate<Internal<Template>::template Bind,
13907 When #1 is parsed, the
13908 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13909 parameter `Template' in #0 matches the
13910 UNBOUND_CLASS_TEMPLATE representing the argument
13911 `Internal<Template>::template Bind'; We then want
13912 to assemble the type `Bind<Argument>' that can't
13913 be fully created right now, because
13914 `Internal<Template>' not being complete, the Bind
13915 template cannot be looked up in that context. So
13916 we need to "store" `Bind<Argument>' for later
13917 when the context of Bind becomes complete. Let's
13918 store that in a TYPENAME_TYPE. */
13919 return make_typename_type (TYPE_CONTEXT (arg
),
13920 build_nt (TEMPLATE_ID_EXPR
,
13921 TYPE_IDENTIFIER (arg
),
13926 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13927 are resolving nested-types in the signature of a
13928 member function templates. Otherwise ARG is a
13929 TEMPLATE_DECL and is the real template to be
13931 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13932 arg
= TYPE_NAME (arg
);
13934 r
= lookup_template_class (arg
,
13936 DECL_CONTEXT (arg
),
13937 /*entering_scope=*/0,
13939 return cp_build_qualified_type_real
13940 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13942 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13945 /* TEMPLATE_PARM_INDEX. */
13946 return convert_from_reference (unshare_expr (arg
));
13950 /* This can happen during the attempted tsubst'ing in
13951 unify. This means that we don't yet have any information
13952 about the template parameter in question. */
13955 /* If we get here, we must have been looking at a parm for a
13956 more deeply nested template. Make a new version of this
13957 template parameter, but with a lower level. */
13960 case TEMPLATE_TYPE_PARM
:
13961 case TEMPLATE_TEMPLATE_PARM
:
13962 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13963 if (cp_type_quals (t
))
13965 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13966 r
= cp_build_qualified_type_real
13967 (r
, cp_type_quals (t
),
13968 complain
| (code
== TEMPLATE_TYPE_PARM
13969 ? tf_ignore_bad_quals
: 0));
13971 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13972 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13973 && (r
= (TEMPLATE_PARM_DESCENDANTS
13974 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13975 && (r
= TREE_TYPE (r
))
13976 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13977 /* Break infinite recursion when substituting the constraints
13978 of a constrained placeholder. */;
13982 TEMPLATE_TYPE_PARM_INDEX (r
)
13983 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13984 r
, levels
, args
, complain
);
13985 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13986 TYPE_MAIN_VARIANT (r
) = r
;
13987 TYPE_POINTER_TO (r
) = NULL_TREE
;
13988 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13990 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13992 /* Propagate constraints on placeholders. */
13993 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13994 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13995 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13996 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13998 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13999 pl
= tsubst (pl
, args
, complain
, in_decl
);
14000 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14004 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14005 /* We have reduced the level of the template
14006 template parameter, but not the levels of its
14007 template parameters, so canonical_type_parameter
14008 will not be able to find the canonical template
14009 template parameter for this level. Thus, we
14010 require structural equality checking to compare
14011 TEMPLATE_TEMPLATE_PARMs. */
14012 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14013 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14014 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14016 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14018 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14020 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14021 /* We might need to substitute into the types of non-type
14022 template parameters. */
14023 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14024 complain
, in_decl
);
14025 if (tmpl
== error_mark_node
)
14026 return error_mark_node
;
14027 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14028 complain
, in_decl
);
14029 if (argvec
== error_mark_node
)
14030 return error_mark_node
;
14032 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14033 = build_template_info (tmpl
, argvec
);
14038 case TEMPLATE_PARM_INDEX
:
14039 /* OK, now substitute the type of the non-type parameter. We
14040 couldn't do it earlier because it might be an auto parameter,
14041 and we wouldn't need to if we had an argument. */
14042 type
= tsubst (type
, args
, complain
, in_decl
);
14043 if (type
== error_mark_node
)
14044 return error_mark_node
;
14045 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14049 gcc_unreachable ();
14057 tree purpose
, value
, chain
;
14059 if (t
== void_list_node
)
14062 purpose
= TREE_PURPOSE (t
);
14065 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14066 if (purpose
== error_mark_node
)
14067 return error_mark_node
;
14069 value
= TREE_VALUE (t
);
14072 value
= tsubst (value
, args
, complain
, in_decl
);
14073 if (value
== error_mark_node
)
14074 return error_mark_node
;
14076 chain
= TREE_CHAIN (t
);
14077 if (chain
&& chain
!= void_type_node
)
14079 chain
= tsubst (chain
, args
, complain
, in_decl
);
14080 if (chain
== error_mark_node
)
14081 return error_mark_node
;
14083 if (purpose
== TREE_PURPOSE (t
)
14084 && value
== TREE_VALUE (t
)
14085 && chain
== TREE_CHAIN (t
))
14087 return hash_tree_cons (purpose
, value
, chain
);
14091 /* We should never be tsubsting a binfo. */
14092 gcc_unreachable ();
14095 /* A vector of template arguments. */
14096 gcc_assert (!type
);
14097 return tsubst_template_args (t
, args
, complain
, in_decl
);
14100 case REFERENCE_TYPE
:
14102 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14107 Type deduction may fail for any of the following
14110 -- Attempting to create a pointer to reference type.
14111 -- Attempting to create a reference to a reference type or
14112 a reference to void.
14114 Core issue 106 says that creating a reference to a reference
14115 during instantiation is no longer a cause for failure. We
14116 only enforce this check in strict C++98 mode. */
14117 if ((TREE_CODE (type
) == REFERENCE_TYPE
14118 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14119 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14121 static location_t last_loc
;
14123 /* We keep track of the last time we issued this error
14124 message to avoid spewing a ton of messages during a
14125 single bad template instantiation. */
14126 if (complain
& tf_error
14127 && last_loc
!= input_location
)
14129 if (VOID_TYPE_P (type
))
14130 error ("forming reference to void");
14131 else if (code
== POINTER_TYPE
)
14132 error ("forming pointer to reference type %qT", type
);
14134 error ("forming reference to reference type %qT", type
);
14135 last_loc
= input_location
;
14138 return error_mark_node
;
14140 else if (TREE_CODE (type
) == FUNCTION_TYPE
14141 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14142 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14144 if (complain
& tf_error
)
14146 if (code
== POINTER_TYPE
)
14147 error ("forming pointer to qualified function type %qT",
14150 error ("forming reference to qualified function type %qT",
14153 return error_mark_node
;
14155 else if (code
== POINTER_TYPE
)
14157 r
= build_pointer_type (type
);
14158 if (TREE_CODE (type
) == METHOD_TYPE
)
14159 r
= build_ptrmemfunc_type (r
);
14161 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14162 /* In C++0x, during template argument substitution, when there is an
14163 attempt to create a reference to a reference type, reference
14164 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14166 "If a template-argument for a template-parameter T names a type
14167 that is a reference to a type A, an attempt to create the type
14168 'lvalue reference to cv T' creates the type 'lvalue reference to
14169 A,' while an attempt to create the type type rvalue reference to
14170 cv T' creates the type T"
14172 r
= cp_build_reference_type
14174 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14176 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14177 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14179 if (r
!= error_mark_node
)
14180 /* Will this ever be needed for TYPE_..._TO values? */
14187 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14188 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14192 Type deduction may fail for any of the following
14195 -- Attempting to create "pointer to member of T" when T
14196 is not a class type. */
14197 if (complain
& tf_error
)
14198 error ("creating pointer to member of non-class type %qT", r
);
14199 return error_mark_node
;
14201 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14203 if (complain
& tf_error
)
14204 error ("creating pointer to member reference type %qT", type
);
14205 return error_mark_node
;
14207 if (VOID_TYPE_P (type
))
14209 if (complain
& tf_error
)
14210 error ("creating pointer to member of type void");
14211 return error_mark_node
;
14213 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14214 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14216 /* The type of the implicit object parameter gets its
14217 cv-qualifiers from the FUNCTION_TYPE. */
14220 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14221 type_memfn_rqual (type
));
14222 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14223 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14227 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14231 case FUNCTION_TYPE
:
14236 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14237 if (fntype
== error_mark_node
)
14238 return error_mark_node
;
14240 /* Substitute the exception specification. */
14241 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14242 /*defer_ok*/fndecl_type
);
14243 if (specs
== error_mark_node
)
14244 return error_mark_node
;
14246 fntype
= build_exception_variant (fntype
, specs
);
14251 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14252 if (domain
== error_mark_node
)
14253 return error_mark_node
;
14255 /* As an optimization, we avoid regenerating the array type if
14256 it will obviously be the same as T. */
14257 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14260 /* These checks should match the ones in create_array_type_for_decl.
14264 The deduction may fail for any of the following reasons:
14266 -- Attempting to create an array with an element type that
14267 is void, a function type, or a reference type, or [DR337]
14268 an abstract class type. */
14269 if (VOID_TYPE_P (type
)
14270 || TREE_CODE (type
) == FUNCTION_TYPE
14271 || (TREE_CODE (type
) == ARRAY_TYPE
14272 && TYPE_DOMAIN (type
) == NULL_TREE
)
14273 || TREE_CODE (type
) == REFERENCE_TYPE
)
14275 if (complain
& tf_error
)
14276 error ("creating array of %qT", type
);
14277 return error_mark_node
;
14280 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14281 return error_mark_node
;
14283 r
= build_cplus_array_type (type
, domain
);
14285 if (TYPE_USER_ALIGN (t
))
14287 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14288 TYPE_USER_ALIGN (r
) = 1;
14294 case TYPENAME_TYPE
:
14296 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14297 in_decl
, /*entering_scope=*/1);
14298 if (ctx
== error_mark_node
)
14299 return error_mark_node
;
14301 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14302 complain
, in_decl
);
14303 if (f
== error_mark_node
)
14304 return error_mark_node
;
14306 if (!MAYBE_CLASS_TYPE_P (ctx
))
14308 if (complain
& tf_error
)
14309 error ("%qT is not a class, struct, or union type", ctx
);
14310 return error_mark_node
;
14312 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14314 /* Normally, make_typename_type does not require that the CTX
14315 have complete type in order to allow things like:
14317 template <class T> struct S { typename S<T>::X Y; };
14319 But, such constructs have already been resolved by this
14320 point, so here CTX really should have complete type, unless
14321 it's a partial instantiation. */
14322 ctx
= complete_type (ctx
);
14323 if (!COMPLETE_TYPE_P (ctx
))
14325 if (complain
& tf_error
)
14326 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14327 return error_mark_node
;
14331 f
= make_typename_type (ctx
, f
, typename_type
,
14332 complain
| tf_keep_type_decl
);
14333 if (f
== error_mark_node
)
14335 if (TREE_CODE (f
) == TYPE_DECL
)
14337 complain
|= tf_ignore_bad_quals
;
14341 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14343 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14345 if (complain
& tf_error
)
14346 error ("%qT resolves to %qT, which is not an enumeration type",
14349 return error_mark_node
;
14351 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14353 if (complain
& tf_error
)
14354 error ("%qT resolves to %qT, which is is not a class type",
14357 return error_mark_node
;
14361 return cp_build_qualified_type_real
14362 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14365 case UNBOUND_CLASS_TEMPLATE
:
14367 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14368 in_decl
, /*entering_scope=*/1);
14369 tree name
= TYPE_IDENTIFIER (t
);
14370 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14372 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14373 return error_mark_node
;
14376 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14377 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14384 ++cp_unevaluated_operand
;
14385 ++c_inhibit_evaluation_warnings
;
14387 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14389 /*integral_constant_expression_p=*/false);
14391 --cp_unevaluated_operand
;
14392 --c_inhibit_evaluation_warnings
;
14394 type
= finish_typeof (type
);
14395 return cp_build_qualified_type_real (type
,
14397 | cp_type_quals (type
),
14401 case DECLTYPE_TYPE
:
14405 ++cp_unevaluated_operand
;
14406 ++c_inhibit_evaluation_warnings
;
14408 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14409 complain
|tf_decltype
, in_decl
,
14410 /*function_p*/false,
14411 /*integral_constant_expression*/false);
14413 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14415 if (type
== NULL_TREE
)
14417 if (complain
& tf_error
)
14418 error ("empty initializer in lambda init-capture");
14419 type
= error_mark_node
;
14421 else if (TREE_CODE (type
) == TREE_LIST
)
14422 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14425 --cp_unevaluated_operand
;
14426 --c_inhibit_evaluation_warnings
;
14428 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14429 type
= lambda_capture_field_type (type
,
14430 DECLTYPE_FOR_INIT_CAPTURE (t
),
14431 DECLTYPE_FOR_REF_CAPTURE (t
));
14432 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14433 type
= lambda_proxy_type (type
);
14436 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14437 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14439 /* In a template ~id could be either a complement expression
14440 or an unqualified-id naming a destructor; if instantiating
14441 it produces an expression, it's not an id-expression or
14444 type
= finish_decltype_type (type
, id
, complain
);
14446 return cp_build_qualified_type_real (type
,
14448 | cp_type_quals (type
),
14449 complain
| tf_ignore_bad_quals
);
14452 case UNDERLYING_TYPE
:
14454 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14455 complain
, in_decl
);
14456 return finish_underlying_type (type
);
14459 case TYPE_ARGUMENT_PACK
:
14460 case NONTYPE_ARGUMENT_PACK
:
14464 if (code
== NONTYPE_ARGUMENT_PACK
)
14465 r
= make_node (code
);
14467 r
= cxx_make_type (code
);
14469 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14470 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14471 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14489 /* We should use one of the expression tsubsts for these codes. */
14490 gcc_unreachable ();
14493 sorry ("use of %qs in template", get_tree_code_name (code
));
14494 return error_mark_node
;
14498 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14499 expression on the left-hand side of the "." or "->" operator. We
14500 only do the lookup if we had a dependent BASELINK. Otherwise we
14501 adjust it onto the instantiated heirarchy. */
14504 tsubst_baselink (tree baselink
, tree object_type
,
14505 tree args
, tsubst_flags_t complain
, tree in_decl
)
14507 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14508 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14509 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14511 tree optype
= BASELINK_OPTYPE (baselink
);
14512 optype
= tsubst (optype
, args
, complain
, in_decl
);
14514 tree template_args
= NULL_TREE
;
14515 bool template_id_p
= false;
14516 tree fns
= BASELINK_FUNCTIONS (baselink
);
14517 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14519 template_id_p
= true;
14520 template_args
= TREE_OPERAND (fns
, 1);
14521 fns
= TREE_OPERAND (fns
, 0);
14523 template_args
= tsubst_template_args (template_args
, args
,
14524 complain
, in_decl
);
14527 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14528 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14529 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14533 tree name
= OVL_NAME (fns
);
14534 if (IDENTIFIER_CONV_OP_P (name
))
14535 name
= make_conv_op_name (optype
);
14537 if (name
== complete_dtor_identifier
)
14538 /* Treat as-if non-dependent below. */
14539 dependent_p
= false;
14541 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14544 if ((complain
& tf_error
)
14545 && constructor_name_p (name
, qualifying_scope
))
14546 error ("cannot call constructor %<%T::%D%> directly",
14547 qualifying_scope
, name
);
14548 return error_mark_node
;
14551 if (BASELINK_P (baselink
))
14552 fns
= BASELINK_FUNCTIONS (baselink
);
14555 /* We're going to overwrite pieces below, make a duplicate. */
14556 baselink
= copy_node (baselink
);
14558 /* If lookup found a single function, mark it as used at this point.
14559 (If lookup found multiple functions the one selected later by
14560 overload resolution will be marked as used at that point.) */
14561 if (!template_id_p
&& !really_overloaded_fn (fns
)
14562 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14563 return error_mark_node
;
14565 if (BASELINK_P (baselink
))
14567 /* Add back the template arguments, if present. */
14569 BASELINK_FUNCTIONS (baselink
)
14570 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14572 /* Update the conversion operator type. */
14573 BASELINK_OPTYPE (baselink
) = optype
;
14577 object_type
= current_class_type
;
14579 if (qualified_p
|| !dependent_p
)
14581 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14585 /* We need to call adjust_result_of_qualified_name_lookup in case the
14586 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14587 so that we still get virtual function binding. */
14588 BASELINK_QUALIFIED_P (baselink
) = false;
14594 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14595 true if the qualified-id will be a postfix-expression in-and-of
14596 itself; false if more of the postfix-expression follows the
14597 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14601 tsubst_qualified_id (tree qualified_id
, tree args
,
14602 tsubst_flags_t complain
, tree in_decl
,
14603 bool done
, bool address_p
)
14609 tree template_args
;
14610 location_t loc
= UNKNOWN_LOCATION
;
14612 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14614 /* Figure out what name to look up. */
14615 name
= TREE_OPERAND (qualified_id
, 1);
14616 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14618 is_template
= true;
14619 loc
= EXPR_LOCATION (name
);
14620 template_args
= TREE_OPERAND (name
, 1);
14622 template_args
= tsubst_template_args (template_args
, args
,
14623 complain
, in_decl
);
14624 if (template_args
== error_mark_node
)
14625 return error_mark_node
;
14626 name
= TREE_OPERAND (name
, 0);
14630 is_template
= false;
14631 template_args
= NULL_TREE
;
14634 /* Substitute into the qualifying scope. When there are no ARGS, we
14635 are just trying to simplify a non-dependent expression. In that
14636 case the qualifying scope may be dependent, and, in any case,
14637 substituting will not help. */
14638 scope
= TREE_OPERAND (qualified_id
, 0);
14641 scope
= tsubst (scope
, args
, complain
, in_decl
);
14642 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14647 if (dependent_scope_p (scope
))
14650 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14651 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14652 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14653 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14657 if (!BASELINK_P (name
) && !DECL_P (expr
))
14659 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14661 /* A BIT_NOT_EXPR is used to represent a destructor. */
14662 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14664 error ("qualifying type %qT does not match destructor name ~%qT",
14665 scope
, TREE_OPERAND (expr
, 0));
14666 expr
= error_mark_node
;
14669 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14670 /*is_type_p=*/0, false);
14673 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14674 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14675 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14677 if (complain
& tf_error
)
14679 error ("dependent-name %qE is parsed as a non-type, but "
14680 "instantiation yields a type", qualified_id
);
14681 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14683 return error_mark_node
;
14689 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14691 /* Remember that there was a reference to this entity. */
14692 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14693 return error_mark_node
;
14696 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14698 if (complain
& tf_error
)
14699 qualified_name_lookup_error (scope
,
14700 TREE_OPERAND (qualified_id
, 1),
14701 expr
, input_location
);
14702 return error_mark_node
;
14707 if (variable_template_p (expr
))
14708 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14711 expr
= lookup_template_function (expr
, template_args
);
14714 if (expr
== error_mark_node
&& complain
& tf_error
)
14715 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14716 expr
, input_location
);
14717 else if (TYPE_P (scope
))
14719 expr
= (adjust_result_of_qualified_name_lookup
14720 (expr
, scope
, current_nonlambda_class_type ()));
14721 expr
= (finish_qualified_id_expr
14722 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14723 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14724 /*template_arg_p=*/false, complain
));
14727 /* Expressions do not generally have reference type. */
14728 if (TREE_CODE (expr
) != SCOPE_REF
14729 /* However, if we're about to form a pointer-to-member, we just
14730 want the referenced member referenced. */
14731 && TREE_CODE (expr
) != OFFSET_REF
)
14732 expr
= convert_from_reference (expr
);
14734 if (REF_PARENTHESIZED_P (qualified_id
))
14735 expr
= force_paren_expr (expr
);
14740 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14741 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14745 tsubst_init (tree init
, tree decl
, tree args
,
14746 tsubst_flags_t complain
, tree in_decl
)
14751 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14753 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14755 /* If we had an initializer but it
14756 instantiated to nothing,
14757 value-initialize the object. This will
14758 only occur when the initializer was a
14759 pack expansion where the parameter packs
14760 used in that expansion were of length
14762 init
= build_value_init (TREE_TYPE (decl
),
14764 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14765 init
= get_target_expr_sfinae (init
, complain
);
14766 if (TREE_CODE (init
) == TARGET_EXPR
)
14767 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14773 /* Like tsubst, but deals with expressions. This function just replaces
14774 template parms; to finish processing the resultant expression, use
14775 tsubst_copy_and_build or tsubst_expr. */
14778 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14780 enum tree_code code
;
14783 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14786 code
= TREE_CODE (t
);
14791 r
= retrieve_local_specialization (t
);
14793 if (r
== NULL_TREE
)
14795 /* We get here for a use of 'this' in an NSDMI. */
14796 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14797 return current_class_ptr
;
14799 /* This can happen for a parameter name used later in a function
14800 declaration (such as in a late-specified return type). Just
14801 make a dummy decl, since it's only used for its type. */
14802 gcc_assert (cp_unevaluated_operand
!= 0);
14803 r
= tsubst_decl (t
, args
, complain
);
14804 /* Give it the template pattern as its context; its true context
14805 hasn't been instantiated yet and this is good enough for
14807 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14810 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14811 r
= argument_pack_select_arg (r
);
14812 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14813 return error_mark_node
;
14821 if (DECL_TEMPLATE_PARM_P (t
))
14822 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14823 /* There is no need to substitute into namespace-scope
14825 if (DECL_NAMESPACE_SCOPE_P (t
))
14827 /* If ARGS is NULL, then T is known to be non-dependent. */
14828 if (args
== NULL_TREE
)
14829 return scalar_constant_value (t
);
14831 /* Unfortunately, we cannot just call lookup_name here.
14834 template <int I> int f() {
14836 struct S { void g() { E e = a; } };
14839 When we instantiate f<7>::S::g(), say, lookup_name is not
14840 clever enough to find f<7>::a. */
14842 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14843 /*entering_scope=*/0);
14845 for (v
= TYPE_VALUES (enum_type
);
14847 v
= TREE_CHAIN (v
))
14848 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14849 return TREE_VALUE (v
);
14851 /* We didn't find the name. That should never happen; if
14852 name-lookup found it during preliminary parsing, we
14853 should find it again here during instantiation. */
14854 gcc_unreachable ();
14859 if (DECL_CONTEXT (t
))
14863 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14864 /*entering_scope=*/1);
14865 if (ctx
!= DECL_CONTEXT (t
))
14867 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14870 if (complain
& tf_error
)
14871 error ("using invalid field %qD", t
);
14872 return error_mark_node
;
14881 case FUNCTION_DECL
:
14882 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14883 r
= tsubst (t
, args
, complain
, in_decl
);
14884 else if (local_variable_p (t
)
14885 && uses_template_parms (DECL_CONTEXT (t
)))
14887 r
= retrieve_local_specialization (t
);
14888 if (r
== NULL_TREE
)
14890 /* First try name lookup to find the instantiation. */
14891 r
= lookup_name (DECL_NAME (t
));
14892 if (r
&& !is_capture_proxy (r
))
14894 /* Make sure that the one we found is the one we want. */
14895 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14896 if (ctx
!= DECL_CONTEXT (r
))
14904 /* This can happen for a variable used in a
14905 late-specified return type of a local lambda, or for a
14906 local static or constant. Building a new VAR_DECL
14907 should be OK in all those cases. */
14908 r
= tsubst_decl (t
, args
, complain
);
14909 if (local_specializations
)
14910 /* Avoid infinite recursion (79640). */
14911 register_local_specialization (r
, t
);
14912 if (decl_maybe_constant_var_p (r
))
14914 /* We can't call cp_finish_decl, so handle the
14915 initializer by hand. */
14916 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14917 complain
, in_decl
);
14918 if (!processing_template_decl
)
14919 init
= maybe_constant_init (init
);
14920 if (processing_template_decl
14921 ? potential_constant_expression (init
)
14922 : reduced_constant_expression_p (init
))
14923 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14924 = TREE_CONSTANT (r
) = true;
14925 DECL_INITIAL (r
) = init
;
14926 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14928 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14929 complain
, adc_variable_type
);
14931 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14932 || decl_constant_var_p (r
)
14933 || errorcount
|| sorrycount
);
14934 if (!processing_template_decl
14935 && !TREE_STATIC (r
))
14936 r
= process_outer_var_ref (r
, complain
);
14938 /* Remember this for subsequent uses. */
14939 if (local_specializations
)
14940 register_local_specialization (r
, t
);
14942 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14943 r
= argument_pack_select_arg (r
);
14947 if (!mark_used (r
, complain
))
14948 return error_mark_node
;
14951 case NAMESPACE_DECL
:
14955 /* An OVERLOAD will always be a non-dependent overload set; an
14956 overload set from function scope will just be represented with an
14957 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14958 gcc_assert (!uses_template_parms (t
));
14959 /* We must have marked any lookups as persistent. */
14960 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14964 return tsubst_baselink (t
, current_nonlambda_class_type (),
14965 args
, complain
, in_decl
);
14967 case TEMPLATE_DECL
:
14968 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14969 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14970 args
, complain
, in_decl
);
14971 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14972 return tsubst (t
, args
, complain
, in_decl
);
14973 else if (DECL_CLASS_SCOPE_P (t
)
14974 && uses_template_parms (DECL_CONTEXT (t
)))
14976 /* Template template argument like the following example need
14979 template <template <class> class TT> struct C {};
14980 template <class T> struct D {
14981 template <class U> struct E {};
14986 We are processing the template argument `E' in #1 for
14987 the template instantiation #2. Originally, `E' is a
14988 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14989 have to substitute this with one having context `D<int>'. */
14991 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14992 if (dependent_scope_p (context
))
14994 /* When rewriting a constructor into a deduction guide, a
14995 non-dependent name can become dependent, so memtmpl<args>
14996 becomes context::template memtmpl<args>. */
14997 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14998 return build_qualified_name (type
, context
, DECL_NAME (t
),
15001 return lookup_field (context
, DECL_NAME(t
), 0, false);
15004 /* Ordinary template template argument. */
15007 case NON_LVALUE_EXPR
:
15008 case VIEW_CONVERT_EXPR
:
15010 /* Handle location wrappers by substituting the wrapped node
15011 first, *then* reusing the resulting type. Doing the type
15012 first ensures that we handle template parameters and
15013 parameter pack expansions. */
15014 gcc_assert (location_wrapper_p (t
));
15015 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15016 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15020 case REINTERPRET_CAST_EXPR
:
15021 case CONST_CAST_EXPR
:
15022 case STATIC_CAST_EXPR
:
15023 case DYNAMIC_CAST_EXPR
:
15024 case IMPLICIT_CONV_EXPR
:
15028 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15029 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15030 return build1 (code
, type
, op0
);
15034 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15035 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15037 tree expanded
, op
= TREE_OPERAND (t
, 0);
15040 if (SIZEOF_EXPR_TYPE_P (t
))
15041 op
= TREE_TYPE (op
);
15043 ++cp_unevaluated_operand
;
15044 ++c_inhibit_evaluation_warnings
;
15045 /* We only want to compute the number of arguments. */
15046 if (PACK_EXPANSION_P (op
))
15047 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15049 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15050 args
, complain
, in_decl
);
15051 --cp_unevaluated_operand
;
15052 --c_inhibit_evaluation_warnings
;
15054 if (TREE_CODE (expanded
) == TREE_VEC
)
15056 len
= TREE_VEC_LENGTH (expanded
);
15057 /* Set TREE_USED for the benefit of -Wunused. */
15058 for (int i
= 0; i
< len
; i
++)
15059 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15060 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15063 if (expanded
== error_mark_node
)
15064 return error_mark_node
;
15065 else if (PACK_EXPANSION_P (expanded
)
15066 || (TREE_CODE (expanded
) == TREE_VEC
15067 && pack_expansion_args_count (expanded
)))
15070 if (PACK_EXPANSION_P (expanded
))
15072 else if (TREE_VEC_LENGTH (expanded
) == 1)
15073 expanded
= TREE_VEC_ELT (expanded
, 0);
15075 expanded
= make_argument_pack (expanded
);
15077 if (TYPE_P (expanded
))
15078 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15079 complain
& tf_error
);
15081 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15082 complain
& tf_error
);
15085 return build_int_cst (size_type_node
, len
);
15087 if (SIZEOF_EXPR_TYPE_P (t
))
15089 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15090 args
, complain
, in_decl
);
15091 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15092 r
= build1 (SIZEOF_EXPR
,
15093 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15094 SIZEOF_EXPR_TYPE_P (r
) = 1;
15101 case TRUTH_NOT_EXPR
:
15104 case UNARY_PLUS_EXPR
: /* Unary + */
15106 case AT_ENCODE_EXPR
:
15110 case REALPART_EXPR
:
15111 case IMAGPART_EXPR
:
15114 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15115 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15116 return build1 (code
, type
, op0
);
15119 case COMPONENT_REF
:
15124 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15125 name
= TREE_OPERAND (t
, 1);
15126 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15128 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15129 complain
, in_decl
);
15130 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15132 else if (TREE_CODE (name
) == SCOPE_REF
15133 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15135 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15136 complain
, in_decl
);
15137 name
= TREE_OPERAND (name
, 1);
15138 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15139 complain
, in_decl
);
15140 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15141 name
= build_qualified_name (/*type=*/NULL_TREE
,
15143 /*template_p=*/false);
15145 else if (BASELINK_P (name
))
15146 name
= tsubst_baselink (name
,
15147 non_reference (TREE_TYPE (object
)),
15151 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15152 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15158 case TRUNC_DIV_EXPR
:
15159 case CEIL_DIV_EXPR
:
15160 case FLOOR_DIV_EXPR
:
15161 case ROUND_DIV_EXPR
:
15162 case EXACT_DIV_EXPR
:
15166 case TRUNC_MOD_EXPR
:
15167 case FLOOR_MOD_EXPR
:
15168 case TRUTH_ANDIF_EXPR
:
15169 case TRUTH_ORIF_EXPR
:
15170 case TRUTH_AND_EXPR
:
15171 case TRUTH_OR_EXPR
:
15184 case COMPOUND_EXPR
:
15187 case PREDECREMENT_EXPR
:
15188 case PREINCREMENT_EXPR
:
15189 case POSTDECREMENT_EXPR
:
15190 case POSTINCREMENT_EXPR
:
15192 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15193 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15194 return build_nt (code
, op0
, op1
);
15199 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15200 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15201 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15202 QUALIFIED_NAME_IS_TEMPLATE (t
));
15207 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15208 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15209 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15214 int n
= VL_EXP_OPERAND_LENGTH (t
);
15215 tree result
= build_vl_exp (CALL_EXPR
, n
);
15217 for (i
= 0; i
< n
; i
++)
15218 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15219 complain
, in_decl
);
15225 case PSEUDO_DTOR_EXPR
:
15226 case VEC_PERM_EXPR
:
15228 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15229 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15230 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15231 r
= build_nt (code
, op0
, op1
, op2
);
15232 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15238 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15239 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15240 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15241 r
= build_nt (code
, op0
, op1
, op2
);
15242 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15248 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15249 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15250 r
= build_nt (code
, op0
, op1
);
15251 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15252 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15256 case TEMPLATE_ID_EXPR
:
15258 /* Substituted template arguments */
15259 tree fn
= TREE_OPERAND (t
, 0);
15260 tree targs
= TREE_OPERAND (t
, 1);
15262 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15264 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15266 return lookup_template_function (fn
, targs
);
15271 tree purpose
, value
, chain
;
15273 if (t
== void_list_node
)
15276 purpose
= TREE_PURPOSE (t
);
15278 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15279 value
= TREE_VALUE (t
);
15281 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15282 chain
= TREE_CHAIN (t
);
15283 if (chain
&& chain
!= void_type_node
)
15284 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15285 if (purpose
== TREE_PURPOSE (t
)
15286 && value
== TREE_VALUE (t
)
15287 && chain
== TREE_CHAIN (t
))
15289 return tree_cons (purpose
, value
, chain
);
15294 case ENUMERAL_TYPE
:
15296 case TEMPLATE_TYPE_PARM
:
15297 case TEMPLATE_TEMPLATE_PARM
:
15298 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15299 case TEMPLATE_PARM_INDEX
:
15301 case REFERENCE_TYPE
:
15303 case FUNCTION_TYPE
:
15306 case TYPENAME_TYPE
:
15307 case UNBOUND_CLASS_TEMPLATE
:
15309 case DECLTYPE_TYPE
:
15311 return tsubst (t
, args
, complain
, in_decl
);
15315 /* Fall through. */
15316 case IDENTIFIER_NODE
:
15317 if (IDENTIFIER_CONV_OP_P (t
))
15319 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15320 return make_conv_op_name (new_type
);
15326 /* This is handled by tsubst_copy_and_build. */
15327 gcc_unreachable ();
15331 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15332 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15333 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15336 case CLEANUP_POINT_EXPR
:
15337 /* We shouldn't have built any of these during initial template
15338 generation. Instead, they should be built during instantiation
15339 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15340 gcc_unreachable ();
15344 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15345 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15346 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15347 r
= build2 (code
, type
, op0
, op1
);
15348 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15349 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15350 && !(complain
& tf_error
))
15351 return error_mark_node
;
15355 case EXPR_PACK_EXPANSION
:
15356 error ("invalid use of pack expansion expression");
15357 return error_mark_node
;
15359 case NONTYPE_ARGUMENT_PACK
:
15360 error ("use %<...%> to expand argument pack");
15361 return error_mark_node
;
15364 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15372 /* Instantiate any typedefs in the type. */
15373 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15374 r
= fold_convert (type
, t
);
15375 gcc_assert (TREE_CODE (r
) == code
);
15380 /* These can sometimes show up in a partial instantiation, but never
15381 involve template parms. */
15382 gcc_assert (!uses_template_parms (t
));
15385 case UNARY_LEFT_FOLD_EXPR
:
15386 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15387 case UNARY_RIGHT_FOLD_EXPR
:
15388 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15389 case BINARY_LEFT_FOLD_EXPR
:
15390 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15391 case BINARY_RIGHT_FOLD_EXPR
:
15392 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15396 case DEBUG_BEGIN_STMT
:
15397 /* ??? There's no point in copying it for now, but maybe some
15398 day it will contain more information, such as a pointer back
15399 to the containing function, inlined copy or so. */
15403 /* We shouldn't get here, but keep going if !flag_checking. */
15405 gcc_unreachable ();
15410 /* Helper function for tsubst_omp_clauses, used for instantiation of
15411 OMP_CLAUSE_DECL of clauses. */
15414 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15417 if (decl
== NULL_TREE
)
15420 /* Handle an OpenMP array section represented as a TREE_LIST (or
15421 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15422 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15423 TREE_LIST. We can handle it exactly the same as an array section
15424 (purpose, value, and a chain), even though the nomenclature
15425 (low_bound, length, etc) is different. */
15426 if (TREE_CODE (decl
) == TREE_LIST
)
15429 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15430 /*integral_constant_expression_p=*/false);
15431 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15432 /*integral_constant_expression_p=*/false);
15433 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15435 if (TREE_PURPOSE (decl
) == low_bound
15436 && TREE_VALUE (decl
) == length
15437 && TREE_CHAIN (decl
) == chain
)
15439 tree ret
= tree_cons (low_bound
, length
, chain
);
15440 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15441 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15444 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15445 /*integral_constant_expression_p=*/false);
15446 /* Undo convert_from_reference tsubst_expr could have called. */
15448 && REFERENCE_REF_P (ret
)
15449 && !REFERENCE_REF_P (decl
))
15450 ret
= TREE_OPERAND (ret
, 0);
15454 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15457 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15458 tree args
, tsubst_flags_t complain
, tree in_decl
)
15460 tree new_clauses
= NULL_TREE
, nc
, oc
;
15461 tree linear_no_step
= NULL_TREE
;
15463 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15465 nc
= copy_node (oc
);
15466 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15469 switch (OMP_CLAUSE_CODE (nc
))
15471 case OMP_CLAUSE_LASTPRIVATE
:
15472 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15474 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15475 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15476 in_decl
, /*integral_constant_expression_p=*/false);
15477 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15478 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15481 case OMP_CLAUSE_PRIVATE
:
15482 case OMP_CLAUSE_SHARED
:
15483 case OMP_CLAUSE_FIRSTPRIVATE
:
15484 case OMP_CLAUSE_COPYIN
:
15485 case OMP_CLAUSE_COPYPRIVATE
:
15486 case OMP_CLAUSE_UNIFORM
:
15487 case OMP_CLAUSE_DEPEND
:
15488 case OMP_CLAUSE_FROM
:
15489 case OMP_CLAUSE_TO
:
15490 case OMP_CLAUSE_MAP
:
15491 case OMP_CLAUSE_USE_DEVICE_PTR
:
15492 case OMP_CLAUSE_IS_DEVICE_PTR
:
15493 OMP_CLAUSE_DECL (nc
)
15494 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15497 case OMP_CLAUSE_TILE
:
15498 case OMP_CLAUSE_IF
:
15499 case OMP_CLAUSE_NUM_THREADS
:
15500 case OMP_CLAUSE_SCHEDULE
:
15501 case OMP_CLAUSE_COLLAPSE
:
15502 case OMP_CLAUSE_FINAL
:
15503 case OMP_CLAUSE_DEVICE
:
15504 case OMP_CLAUSE_DIST_SCHEDULE
:
15505 case OMP_CLAUSE_NUM_TEAMS
:
15506 case OMP_CLAUSE_THREAD_LIMIT
:
15507 case OMP_CLAUSE_SAFELEN
:
15508 case OMP_CLAUSE_SIMDLEN
:
15509 case OMP_CLAUSE_NUM_TASKS
:
15510 case OMP_CLAUSE_GRAINSIZE
:
15511 case OMP_CLAUSE_PRIORITY
:
15512 case OMP_CLAUSE_ORDERED
:
15513 case OMP_CLAUSE_HINT
:
15514 case OMP_CLAUSE_NUM_GANGS
:
15515 case OMP_CLAUSE_NUM_WORKERS
:
15516 case OMP_CLAUSE_VECTOR_LENGTH
:
15517 case OMP_CLAUSE_WORKER
:
15518 case OMP_CLAUSE_VECTOR
:
15519 case OMP_CLAUSE_ASYNC
:
15520 case OMP_CLAUSE_WAIT
:
15521 OMP_CLAUSE_OPERAND (nc
, 0)
15522 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15523 in_decl
, /*integral_constant_expression_p=*/false);
15525 case OMP_CLAUSE_REDUCTION
:
15526 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15528 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15529 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15531 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15532 complain
, in_decl
);
15533 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15534 = build_qualified_name (NULL_TREE
, scope
,
15535 TREE_OPERAND (placeholder
, 1),
15539 gcc_assert (identifier_p (placeholder
));
15541 OMP_CLAUSE_DECL (nc
)
15542 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15545 case OMP_CLAUSE_GANG
:
15546 case OMP_CLAUSE_ALIGNED
:
15547 OMP_CLAUSE_DECL (nc
)
15548 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15550 OMP_CLAUSE_OPERAND (nc
, 1)
15551 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15552 in_decl
, /*integral_constant_expression_p=*/false);
15554 case OMP_CLAUSE_LINEAR
:
15555 OMP_CLAUSE_DECL (nc
)
15556 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15558 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15560 gcc_assert (!linear_no_step
);
15561 linear_no_step
= nc
;
15563 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15564 OMP_CLAUSE_LINEAR_STEP (nc
)
15565 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15566 complain
, in_decl
);
15568 OMP_CLAUSE_LINEAR_STEP (nc
)
15569 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15571 /*integral_constant_expression_p=*/false);
15573 case OMP_CLAUSE_NOWAIT
:
15574 case OMP_CLAUSE_DEFAULT
:
15575 case OMP_CLAUSE_UNTIED
:
15576 case OMP_CLAUSE_MERGEABLE
:
15577 case OMP_CLAUSE_INBRANCH
:
15578 case OMP_CLAUSE_NOTINBRANCH
:
15579 case OMP_CLAUSE_PROC_BIND
:
15580 case OMP_CLAUSE_FOR
:
15581 case OMP_CLAUSE_PARALLEL
:
15582 case OMP_CLAUSE_SECTIONS
:
15583 case OMP_CLAUSE_TASKGROUP
:
15584 case OMP_CLAUSE_NOGROUP
:
15585 case OMP_CLAUSE_THREADS
:
15586 case OMP_CLAUSE_SIMD
:
15587 case OMP_CLAUSE_DEFAULTMAP
:
15588 case OMP_CLAUSE_INDEPENDENT
:
15589 case OMP_CLAUSE_AUTO
:
15590 case OMP_CLAUSE_SEQ
:
15593 gcc_unreachable ();
15595 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15596 switch (OMP_CLAUSE_CODE (nc
))
15598 case OMP_CLAUSE_SHARED
:
15599 case OMP_CLAUSE_PRIVATE
:
15600 case OMP_CLAUSE_FIRSTPRIVATE
:
15601 case OMP_CLAUSE_LASTPRIVATE
:
15602 case OMP_CLAUSE_COPYPRIVATE
:
15603 case OMP_CLAUSE_LINEAR
:
15604 case OMP_CLAUSE_REDUCTION
:
15605 case OMP_CLAUSE_USE_DEVICE_PTR
:
15606 case OMP_CLAUSE_IS_DEVICE_PTR
:
15607 /* tsubst_expr on SCOPE_REF results in returning
15608 finish_non_static_data_member result. Undo that here. */
15609 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15610 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15611 == IDENTIFIER_NODE
))
15613 tree t
= OMP_CLAUSE_DECL (nc
);
15616 switch (TREE_CODE (v
))
15618 case COMPONENT_REF
:
15622 case POINTER_PLUS_EXPR
:
15623 v
= TREE_OPERAND (v
, 0);
15626 if (DECL_CONTEXT (v
) == current_function_decl
15627 && DECL_ARTIFICIAL (v
)
15628 && DECL_NAME (v
) == this_identifier
)
15629 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15636 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15637 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15638 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15639 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15640 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15642 tree decl
= OMP_CLAUSE_DECL (nc
);
15645 retrofit_lang_decl (decl
);
15646 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15655 new_clauses
= nreverse (new_clauses
);
15656 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15658 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15659 if (linear_no_step
)
15660 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15661 if (nc
== linear_no_step
)
15663 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15667 return new_clauses
;
15670 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15673 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15676 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15678 tree purpose
, value
, chain
;
15683 if (TREE_CODE (t
) != TREE_LIST
)
15684 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15685 /*function_p=*/false,
15686 /*integral_constant_expression_p=*/false);
15688 if (t
== void_list_node
)
15691 purpose
= TREE_PURPOSE (t
);
15693 purpose
= RECUR (purpose
);
15694 value
= TREE_VALUE (t
);
15697 if (TREE_CODE (value
) != LABEL_DECL
)
15698 value
= RECUR (value
);
15701 value
= lookup_label (DECL_NAME (value
));
15702 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15703 TREE_USED (value
) = 1;
15706 chain
= TREE_CHAIN (t
);
15707 if (chain
&& chain
!= void_type_node
)
15708 chain
= RECUR (chain
);
15709 return tree_cons (purpose
, value
, chain
);
15713 /* Used to temporarily communicate the list of #pragma omp parallel
15714 clauses to #pragma omp for instantiation if they are combined
15717 static tree
*omp_parallel_combined_clauses
;
15719 /* Substitute one OMP_FOR iterator. */
15722 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15723 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15724 tree args
, tsubst_flags_t complain
, tree in_decl
,
15725 bool integral_constant_expression_p
)
15727 #define RECUR(NODE) \
15728 tsubst_expr ((NODE), args, complain, in_decl, \
15729 integral_constant_expression_p)
15730 tree decl
, init
, cond
, incr
;
15732 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15733 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15735 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15737 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15738 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15741 decl
= TREE_OPERAND (init
, 0);
15742 init
= TREE_OPERAND (init
, 1);
15743 tree decl_expr
= NULL_TREE
;
15744 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15746 /* We need to jump through some hoops to handle declarations in the
15747 init-statement, since we might need to handle auto deduction,
15748 but we need to keep control of initialization. */
15750 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15751 decl
= tsubst_decl (decl
, args
, complain
);
15755 if (TREE_CODE (decl
) == SCOPE_REF
)
15757 decl
= RECUR (decl
);
15758 if (TREE_CODE (decl
) == COMPONENT_REF
)
15762 switch (TREE_CODE (v
))
15764 case COMPONENT_REF
:
15768 case POINTER_PLUS_EXPR
:
15769 v
= TREE_OPERAND (v
, 0);
15772 if (DECL_CONTEXT (v
) == current_function_decl
15773 && DECL_ARTIFICIAL (v
)
15774 && DECL_NAME (v
) == this_identifier
)
15776 decl
= TREE_OPERAND (decl
, 1);
15777 decl
= omp_privatize_field (decl
, false);
15787 decl
= RECUR (decl
);
15789 init
= RECUR (init
);
15791 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15792 if (auto_node
&& init
)
15794 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
15796 gcc_assert (!type_dependent_expression_p (decl
));
15798 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15802 /* Declare the variable, but don't let that initialize it. */
15803 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15804 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15806 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15809 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15810 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15811 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15813 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15814 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15815 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15816 NOP_EXPR
, rhs
, complain
);
15819 incr
= RECUR (incr
);
15820 TREE_VEC_ELT (declv
, i
) = decl
;
15821 TREE_VEC_ELT (initv
, i
) = init
;
15822 TREE_VEC_ELT (condv
, i
) = cond
;
15823 TREE_VEC_ELT (incrv
, i
) = incr
;
15829 /* Declare and initialize the variable. */
15837 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15839 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15841 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15842 && OMP_CLAUSE_DECL (*pc
) == decl
)
15844 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15845 && OMP_CLAUSE_DECL (*pc
) == decl
)
15849 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15851 *pc
= OMP_CLAUSE_CHAIN (c
);
15852 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15855 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15856 && OMP_CLAUSE_DECL (*pc
) == decl
)
15858 error ("iteration variable %qD should not be firstprivate",
15860 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15862 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15863 && OMP_CLAUSE_DECL (*pc
) == decl
)
15865 error ("iteration variable %qD should not be reduction",
15867 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15870 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15875 if (*pc
== NULL_TREE
)
15877 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15878 OMP_CLAUSE_DECL (c
) = decl
;
15879 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15882 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15887 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15888 if (COMPARISON_CLASS_P (cond
))
15890 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15891 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15892 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15895 cond
= RECUR (cond
);
15896 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15897 switch (TREE_CODE (incr
))
15899 case PREINCREMENT_EXPR
:
15900 case PREDECREMENT_EXPR
:
15901 case POSTINCREMENT_EXPR
:
15902 case POSTDECREMENT_EXPR
:
15903 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15904 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15907 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15908 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15910 tree rhs
= TREE_OPERAND (incr
, 1);
15911 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15912 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15913 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15914 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15915 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15919 incr
= RECUR (incr
);
15922 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15923 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15925 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15926 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15927 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15928 TREE_TYPE (decl
), lhs
,
15929 RECUR (TREE_OPERAND (incr
, 2))));
15931 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15932 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15933 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15935 tree rhs
= TREE_OPERAND (incr
, 2);
15936 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15937 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15938 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15939 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15940 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15944 incr
= RECUR (incr
);
15947 incr
= RECUR (incr
);
15951 TREE_VEC_ELT (declv
, i
) = decl
;
15952 TREE_VEC_ELT (initv
, i
) = init
;
15953 TREE_VEC_ELT (condv
, i
) = cond
;
15954 TREE_VEC_ELT (incrv
, i
) = incr
;
15958 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15959 of OMP_TARGET's body. */
15962 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15964 *walk_subtrees
= 0;
15965 switch (TREE_CODE (*tp
))
15970 case STATEMENT_LIST
:
15971 *walk_subtrees
= 1;
15979 /* Helper function for tsubst_expr. For decomposition declaration
15980 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15981 also the corresponding decls representing the identifiers
15982 of the decomposition declaration. Return DECL if successful
15983 or error_mark_node otherwise, set *FIRST to the first decl
15984 in the list chained through DECL_CHAIN and *CNT to the number
15988 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15989 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15992 tree decl2
, decl3
, prev
= decl
;
15994 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15995 for (decl2
= DECL_CHAIN (pattern_decl
);
15998 && DECL_DECOMPOSITION_P (decl2
)
15999 && DECL_NAME (decl2
);
16000 decl2
= DECL_CHAIN (decl2
))
16002 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16004 gcc_assert (errorcount
);
16005 return error_mark_node
;
16008 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16009 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16010 tree v
= DECL_VALUE_EXPR (decl2
);
16011 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16012 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16013 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16014 SET_DECL_VALUE_EXPR (decl2
, v
);
16015 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16017 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16018 maybe_push_decl (decl3
);
16019 if (error_operand_p (decl3
))
16020 decl
= error_mark_node
;
16021 else if (decl
!= error_mark_node
16022 && DECL_CHAIN (decl3
) != prev
)
16024 gcc_assert (errorcount
);
16025 decl
= error_mark_node
;
16034 /* Like tsubst_copy for expressions, etc. but also does semantic
16038 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16039 bool integral_constant_expression_p
)
16041 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16042 #define RECUR(NODE) \
16043 tsubst_expr ((NODE), args, complain, in_decl, \
16044 integral_constant_expression_p)
16050 if (t
== NULL_TREE
|| t
== error_mark_node
)
16053 loc
= input_location
;
16054 if (EXPR_HAS_LOCATION (t
))
16055 input_location
= EXPR_LOCATION (t
);
16056 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16057 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16059 switch (TREE_CODE (t
))
16061 case STATEMENT_LIST
:
16063 tree_stmt_iterator i
;
16064 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16065 RECUR (tsi_stmt (i
));
16069 case CTOR_INITIALIZER
:
16070 finish_mem_initializers (tsubst_initializer_list
16071 (TREE_OPERAND (t
, 0), args
));
16075 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16079 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16080 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16081 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16083 finish_expr_stmt (tmp
);
16087 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16088 /*attribs=*/NULL_TREE
);
16093 tree decl
, pattern_decl
;
16096 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16097 if (TREE_CODE (decl
) == LABEL_DECL
)
16098 finish_label_decl (DECL_NAME (decl
));
16099 else if (TREE_CODE (decl
) == USING_DECL
)
16101 tree scope
= USING_DECL_SCOPE (decl
);
16102 tree name
= DECL_NAME (decl
);
16104 scope
= tsubst (scope
, args
, complain
, in_decl
);
16105 decl
= lookup_qualified_name (scope
, name
,
16106 /*is_type_p=*/false,
16107 /*complain=*/false);
16108 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16109 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16111 finish_local_using_decl (decl
, scope
, name
);
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. */
16119 if (DECL_PACK_P (decl
))
16121 inst
= (retrieve_local_specialization
16122 (DECL_CAPTURED_VARIABLE (decl
)));
16123 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16127 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16128 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16129 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16131 register_local_specialization (inst
, decl
);
16134 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16135 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16136 /* Don't copy the old closure; we'll create a new one in
16137 tsubst_lambda_expr. */
16141 init
= DECL_INITIAL (decl
);
16142 decl
= tsubst (decl
, args
, complain
, in_decl
);
16143 if (decl
!= error_mark_node
)
16145 /* By marking the declaration as instantiated, we avoid
16146 trying to instantiate it. Since instantiate_decl can't
16147 handle local variables, and since we've already done
16148 all that needs to be done, that's the right thing to
16151 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16153 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16154 /* Anonymous aggregates are a special case. */
16155 finish_anon_union (decl
);
16156 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16158 DECL_CONTEXT (decl
) = current_function_decl
;
16159 if (DECL_NAME (decl
) == this_identifier
)
16161 tree lam
= DECL_CONTEXT (current_function_decl
);
16162 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16163 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16165 insert_capture_proxy (decl
);
16167 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16168 /* We already did a pushtag. */;
16169 else if (TREE_CODE (decl
) == FUNCTION_DECL
16170 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16171 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16173 DECL_CONTEXT (decl
) = NULL_TREE
;
16175 DECL_CONTEXT (decl
) = current_function_decl
;
16176 cp_check_omp_declare_reduction (decl
);
16180 int const_init
= false;
16181 maybe_push_decl (decl
);
16183 && DECL_PRETTY_FUNCTION_P (decl
))
16185 /* For __PRETTY_FUNCTION__ we have to adjust the
16187 const char *const name
16188 = cxx_printable_name (current_function_decl
, 2);
16189 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16192 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16195 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16198 && DECL_DECOMPOSITION_P (decl
)
16199 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16204 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16205 complain
, in_decl
, &first
, &cnt
);
16206 if (ndecl
!= error_mark_node
)
16207 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16208 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16209 if (ndecl
!= error_mark_node
)
16210 cp_finish_decomp (ndecl
, first
, cnt
);
16213 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16222 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16223 RECUR (FOR_INIT_STMT (t
));
16224 finish_init_stmt (stmt
);
16225 tmp
= RECUR (FOR_COND (t
));
16226 finish_for_cond (tmp
, stmt
, false, 0);
16227 tmp
= RECUR (FOR_EXPR (t
));
16228 finish_for_expr (tmp
, stmt
);
16230 bool prev
= note_iteration_stmt_body_start ();
16231 RECUR (FOR_BODY (t
));
16232 note_iteration_stmt_body_end (prev
);
16234 finish_for_stmt (stmt
);
16237 case RANGE_FOR_STMT
:
16239 /* Construct another range_for, if this is not a final
16240 substitution (for inside inside a generic lambda of a
16241 template). Otherwise convert to a regular for. */
16243 stmt
= (processing_template_decl
16244 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16245 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16246 decl
= RANGE_FOR_DECL (t
);
16247 decl
= tsubst (decl
, args
, complain
, in_decl
);
16248 maybe_push_decl (decl
);
16249 expr
= RECUR (RANGE_FOR_EXPR (t
));
16251 tree decomp_first
= NULL_TREE
;
16252 unsigned decomp_cnt
= 0;
16253 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16254 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16256 &decomp_first
, &decomp_cnt
);
16258 if (processing_template_decl
)
16260 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16261 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16262 finish_range_for_decl (stmt
, decl
, expr
);
16266 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16267 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16268 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16269 decomp_first
, decomp_cnt
,
16270 RANGE_FOR_IVDEP (t
), unroll
);
16273 bool prev
= note_iteration_stmt_body_start ();
16274 RECUR (RANGE_FOR_BODY (t
));
16275 note_iteration_stmt_body_end (prev
);
16276 finish_for_stmt (stmt
);
16281 stmt
= begin_while_stmt ();
16282 tmp
= RECUR (WHILE_COND (t
));
16283 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16285 bool prev
= note_iteration_stmt_body_start ();
16286 RECUR (WHILE_BODY (t
));
16287 note_iteration_stmt_body_end (prev
);
16289 finish_while_stmt (stmt
);
16293 stmt
= begin_do_stmt ();
16295 bool prev
= note_iteration_stmt_body_start ();
16296 RECUR (DO_BODY (t
));
16297 note_iteration_stmt_body_end (prev
);
16299 finish_do_body (stmt
);
16300 tmp
= RECUR (DO_COND (t
));
16301 finish_do_stmt (tmp
, stmt
, false, 0);
16305 stmt
= begin_if_stmt ();
16306 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16307 tmp
= RECUR (IF_COND (t
));
16308 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16309 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16310 /* Don't instantiate the THEN_CLAUSE. */;
16313 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16315 ++c_inhibit_evaluation_warnings
;
16316 RECUR (THEN_CLAUSE (t
));
16318 --c_inhibit_evaluation_warnings
;
16320 finish_then_clause (stmt
);
16322 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16323 /* Don't instantiate the ELSE_CLAUSE. */;
16324 else if (ELSE_CLAUSE (t
))
16326 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16327 begin_else_clause (stmt
);
16329 ++c_inhibit_evaluation_warnings
;
16330 RECUR (ELSE_CLAUSE (t
));
16332 --c_inhibit_evaluation_warnings
;
16333 finish_else_clause (stmt
);
16336 finish_if_stmt (stmt
);
16340 if (BIND_EXPR_BODY_BLOCK (t
))
16341 stmt
= begin_function_body ();
16343 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16344 ? BCS_TRY_BLOCK
: 0);
16346 RECUR (BIND_EXPR_BODY (t
));
16348 if (BIND_EXPR_BODY_BLOCK (t
))
16349 finish_function_body (stmt
);
16351 finish_compound_stmt (stmt
);
16355 finish_break_stmt ();
16358 case CONTINUE_STMT
:
16359 finish_continue_stmt ();
16363 stmt
= begin_switch_stmt ();
16364 tmp
= RECUR (SWITCH_STMT_COND (t
));
16365 finish_switch_cond (tmp
, stmt
);
16366 RECUR (SWITCH_STMT_BODY (t
));
16367 finish_switch_stmt (stmt
);
16370 case CASE_LABEL_EXPR
:
16372 tree low
= RECUR (CASE_LOW (t
));
16373 tree high
= RECUR (CASE_HIGH (t
));
16374 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16375 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16376 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16377 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16383 tree decl
= LABEL_EXPR_LABEL (t
);
16386 label
= finish_label_stmt (DECL_NAME (decl
));
16387 if (TREE_CODE (label
) == LABEL_DECL
)
16388 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16389 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16390 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16395 tmp
= GOTO_DESTINATION (t
);
16396 if (TREE_CODE (tmp
) != LABEL_DECL
)
16397 /* Computed goto's must be tsubst'd into. On the other hand,
16398 non-computed gotos must not be; the identifier in question
16399 will have no binding. */
16402 tmp
= DECL_NAME (tmp
);
16403 finish_goto_stmt (tmp
);
16408 tree string
= RECUR (ASM_STRING (t
));
16409 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16410 complain
, in_decl
);
16411 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16412 complain
, in_decl
);
16413 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16414 complain
, in_decl
);
16415 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16416 complain
, in_decl
);
16417 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16419 tree asm_expr
= tmp
;
16420 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16421 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16422 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16429 stmt
= begin_try_block ();
16430 RECUR (TRY_STMTS (t
));
16431 finish_cleanup_try_block (stmt
);
16432 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16436 tree compound_stmt
= NULL_TREE
;
16438 if (FN_TRY_BLOCK_P (t
))
16439 stmt
= begin_function_try_block (&compound_stmt
);
16441 stmt
= begin_try_block ();
16443 RECUR (TRY_STMTS (t
));
16445 if (FN_TRY_BLOCK_P (t
))
16446 finish_function_try_block (stmt
);
16448 finish_try_block (stmt
);
16450 RECUR (TRY_HANDLERS (t
));
16451 if (FN_TRY_BLOCK_P (t
))
16452 finish_function_handler_sequence (stmt
, compound_stmt
);
16454 finish_handler_sequence (stmt
);
16460 tree decl
= HANDLER_PARMS (t
);
16464 decl
= tsubst (decl
, args
, complain
, in_decl
);
16465 /* Prevent instantiate_decl from trying to instantiate
16466 this variable. We've already done all that needs to be
16468 if (decl
!= error_mark_node
)
16469 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16471 stmt
= begin_handler ();
16472 finish_handler_parms (decl
, stmt
);
16473 RECUR (HANDLER_BODY (t
));
16474 finish_handler (stmt
);
16479 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16480 if (CLASS_TYPE_P (tmp
))
16482 /* Local classes are not independent templates; they are
16483 instantiated along with their containing function. And this
16484 way we don't have to deal with pushing out of one local class
16485 to instantiate a member of another local class. */
16486 /* Closures are handled by the LAMBDA_EXPR. */
16487 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16488 complete_type (tmp
);
16489 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16491 || (TREE_CODE (fld
) == FUNCTION_DECL
16492 && !DECL_ARTIFICIAL (fld
)))
16493 && DECL_TEMPLATE_INSTANTIATION (fld
))
16494 instantiate_decl (fld
, /*defer_ok=*/false,
16495 /*expl_inst_class=*/false);
16499 case STATIC_ASSERT
:
16503 ++c_inhibit_evaluation_warnings
;
16505 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16508 /*integral_constant_expression_p=*/true);
16509 --c_inhibit_evaluation_warnings
;
16511 finish_static_assert (condition
,
16512 STATIC_ASSERT_MESSAGE (t
),
16513 STATIC_ASSERT_SOURCE_LOCATION (t
),
16514 /*member_p=*/false);
16519 case OACC_PARALLEL
:
16520 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16522 stmt
= begin_omp_parallel ();
16523 RECUR (OMP_BODY (t
));
16524 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16528 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16529 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16530 complain
, in_decl
);
16531 if (OMP_PARALLEL_COMBINED (t
))
16532 omp_parallel_combined_clauses
= &tmp
;
16533 stmt
= begin_omp_parallel ();
16534 RECUR (OMP_PARALLEL_BODY (t
));
16535 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16536 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16537 = OMP_PARALLEL_COMBINED (t
);
16538 pop_omp_privatization_clauses (r
);
16542 r
= push_omp_privatization_clauses (false);
16543 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16544 complain
, in_decl
);
16545 stmt
= begin_omp_task ();
16546 RECUR (OMP_TASK_BODY (t
));
16547 finish_omp_task (tmp
, stmt
);
16548 pop_omp_privatization_clauses (r
);
16553 case OMP_DISTRIBUTE
:
16557 tree clauses
, body
, pre_body
;
16558 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16559 tree orig_declv
= NULL_TREE
;
16560 tree incrv
= NULL_TREE
;
16561 enum c_omp_region_type ort
= C_ORT_OMP
;
16564 if (TREE_CODE (t
) == OACC_LOOP
)
16567 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16568 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16570 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16572 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16573 if (OMP_FOR_ORIG_DECLS (t
))
16574 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16575 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16576 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16577 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16580 stmt
= begin_omp_structured_block ();
16582 pre_body
= push_stmt_list ();
16583 RECUR (OMP_FOR_PRE_BODY (t
));
16584 pre_body
= pop_stmt_list (pre_body
);
16586 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16587 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16588 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16589 incrv
, &clauses
, args
, complain
, in_decl
,
16590 integral_constant_expression_p
);
16591 omp_parallel_combined_clauses
= NULL
;
16593 body
= push_stmt_list ();
16594 RECUR (OMP_FOR_BODY (t
));
16595 body
= pop_stmt_list (body
);
16597 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16598 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16599 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16603 t
= make_node (TREE_CODE (t
));
16604 TREE_TYPE (t
) = void_type_node
;
16605 OMP_FOR_BODY (t
) = body
;
16606 OMP_FOR_PRE_BODY (t
) = pre_body
;
16607 OMP_FOR_CLAUSES (t
) = clauses
;
16608 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16612 add_stmt (finish_omp_structured_block (stmt
));
16613 pop_omp_privatization_clauses (r
);
16618 omp_parallel_combined_clauses
= NULL
;
16623 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16624 && OMP_TEAMS_COMBINED (t
));
16625 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16627 stmt
= push_stmt_list ();
16628 RECUR (OMP_BODY (t
));
16629 stmt
= pop_stmt_list (stmt
);
16632 OMP_BODY (t
) = stmt
;
16633 OMP_CLAUSES (t
) = tmp
;
16635 pop_omp_privatization_clauses (r
);
16639 case OMP_TARGET_DATA
:
16641 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16642 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16644 keep_next_level (true);
16645 stmt
= begin_omp_structured_block ();
16647 RECUR (OMP_BODY (t
));
16648 stmt
= finish_omp_structured_block (stmt
);
16651 OMP_BODY (t
) = stmt
;
16652 OMP_CLAUSES (t
) = tmp
;
16653 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16655 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16658 /* For combined target teams, ensure the num_teams and
16659 thread_limit clause expressions are evaluated on the host,
16660 before entering the target construct. */
16662 for (c
= OMP_TEAMS_CLAUSES (teams
);
16663 c
; c
= OMP_CLAUSE_CHAIN (c
))
16664 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16665 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16666 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16668 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16669 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16670 if (expr
== error_mark_node
)
16672 tmp
= TARGET_EXPR_SLOT (expr
);
16674 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16675 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16676 OMP_CLAUSE_FIRSTPRIVATE
);
16677 OMP_CLAUSE_DECL (tc
) = tmp
;
16678 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16679 OMP_TARGET_CLAUSES (t
) = tc
;
16688 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16689 complain
, in_decl
);
16690 OACC_DECLARE_CLAUSES (t
) = tmp
;
16694 case OMP_TARGET_UPDATE
:
16695 case OMP_TARGET_ENTER_DATA
:
16696 case OMP_TARGET_EXIT_DATA
:
16697 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16698 complain
, in_decl
);
16700 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16704 case OACC_ENTER_DATA
:
16705 case OACC_EXIT_DATA
:
16707 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16708 complain
, in_decl
);
16710 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16715 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16716 complain
, in_decl
);
16717 stmt
= push_stmt_list ();
16718 RECUR (OMP_BODY (t
));
16719 stmt
= pop_stmt_list (stmt
);
16722 OMP_BODY (t
) = stmt
;
16723 OMP_ORDERED_CLAUSES (t
) = tmp
;
16729 case OMP_TASKGROUP
:
16730 stmt
= push_stmt_list ();
16731 RECUR (OMP_BODY (t
));
16732 stmt
= pop_stmt_list (stmt
);
16735 OMP_BODY (t
) = stmt
;
16740 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16741 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16743 tree op1
= TREE_OPERAND (t
, 1);
16744 tree rhs1
= NULL_TREE
;
16746 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16748 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16749 op1
= TREE_OPERAND (op1
, 1);
16751 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16752 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16753 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16754 NULL_TREE
, NULL_TREE
, rhs1
,
16755 OMP_ATOMIC_SEQ_CST (t
));
16759 tree op1
= TREE_OPERAND (t
, 1);
16760 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16761 tree rhs1
= NULL_TREE
;
16762 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16763 enum tree_code opcode
= NOP_EXPR
;
16764 if (code
== OMP_ATOMIC_READ
)
16766 v
= RECUR (TREE_OPERAND (op1
, 0));
16767 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16769 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16770 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16772 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16773 v
= RECUR (TREE_OPERAND (op1
, 0));
16774 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16775 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16777 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16778 op11
= TREE_OPERAND (op11
, 1);
16780 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16781 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16782 opcode
= TREE_CODE (op11
);
16783 if (opcode
== MODIFY_EXPR
)
16789 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16790 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16792 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16793 OMP_ATOMIC_SEQ_CST (t
));
16797 case TRANSACTION_EXPR
:
16800 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16801 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16803 if (TRANSACTION_EXPR_IS_STMT (t
))
16805 tree body
= TRANSACTION_EXPR_BODY (t
);
16806 tree noex
= NULL_TREE
;
16807 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16809 noex
= MUST_NOT_THROW_COND (body
);
16810 if (noex
== NULL_TREE
)
16811 noex
= boolean_true_node
;
16812 body
= TREE_OPERAND (body
, 0);
16814 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16816 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16820 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16821 RECUR (TRANSACTION_EXPR_BODY (t
)),
16828 case MUST_NOT_THROW_EXPR
:
16830 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16831 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16832 RETURN (build_must_not_throw_expr (op0
, cond
));
16835 case EXPR_PACK_EXPANSION
:
16836 error ("invalid use of pack expansion expression");
16837 RETURN (error_mark_node
);
16839 case NONTYPE_ARGUMENT_PACK
:
16840 error ("use %<...%> to expand argument pack");
16841 RETURN (error_mark_node
);
16843 case COMPOUND_EXPR
:
16844 tmp
= RECUR (TREE_OPERAND (t
, 0));
16845 if (tmp
== NULL_TREE
)
16846 /* If the first operand was a statement, we're done with it. */
16847 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16848 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16849 RECUR (TREE_OPERAND (t
, 1)),
16852 case ANNOTATE_EXPR
:
16853 tmp
= RECUR (TREE_OPERAND (t
, 0));
16854 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16855 TREE_TYPE (tmp
), tmp
,
16856 RECUR (TREE_OPERAND (t
, 1)),
16857 RECUR (TREE_OPERAND (t
, 2))));
16860 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16862 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16863 /*function_p=*/false,
16864 integral_constant_expression_p
));
16867 RETURN (NULL_TREE
);
16869 input_location
= loc
;
16875 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16876 function. For description of the body see comment above
16877 cp_parser_omp_declare_reduction_exprs. */
16880 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16882 if (t
== NULL_TREE
|| t
== error_mark_node
)
16885 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16887 tree_stmt_iterator tsi
;
16890 memset (stmts
, 0, sizeof stmts
);
16891 for (i
= 0, tsi
= tsi_start (t
);
16892 i
< 7 && !tsi_end_p (tsi
);
16893 i
++, tsi_next (&tsi
))
16894 stmts
[i
] = tsi_stmt (tsi
);
16895 gcc_assert (tsi_end_p (tsi
));
16899 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16900 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16901 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16902 args
, complain
, in_decl
);
16903 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16904 args
, complain
, in_decl
);
16905 DECL_CONTEXT (omp_out
) = current_function_decl
;
16906 DECL_CONTEXT (omp_in
) = current_function_decl
;
16907 keep_next_level (true);
16908 tree block
= begin_omp_structured_block ();
16909 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16910 block
= finish_omp_structured_block (block
);
16911 block
= maybe_cleanup_point_expr_void (block
);
16912 add_decl_expr (omp_out
);
16913 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16914 TREE_NO_WARNING (omp_out
) = 1;
16915 add_decl_expr (omp_in
);
16916 finish_expr_stmt (block
);
16920 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16921 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16922 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16923 args
, complain
, in_decl
);
16924 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16925 args
, complain
, in_decl
);
16926 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16927 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16928 keep_next_level (true);
16929 tree block
= begin_omp_structured_block ();
16930 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16931 block
= finish_omp_structured_block (block
);
16932 block
= maybe_cleanup_point_expr_void (block
);
16933 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16934 add_decl_expr (omp_priv
);
16935 add_decl_expr (omp_orig
);
16936 finish_expr_stmt (block
);
16938 add_decl_expr (omp_orig
);
16942 /* T is a postfix-expression that is not being used in a function
16943 call. Return the substituted version of T. */
16946 tsubst_non_call_postfix_expression (tree t
, tree args
,
16947 tsubst_flags_t complain
,
16950 if (TREE_CODE (t
) == SCOPE_REF
)
16951 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16952 /*done=*/false, /*address_p=*/false);
16954 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16955 /*function_p=*/false,
16956 /*integral_constant_expression_p=*/false);
16961 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16962 instantiation context. Instantiating a pack expansion containing a lambda
16963 might result in multiple lambdas all based on the same lambda in the
16967 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16969 tree oldfn
= lambda_function (t
);
16972 tree r
= build_lambda_expr ();
16974 LAMBDA_EXPR_LOCATION (r
)
16975 = LAMBDA_EXPR_LOCATION (t
);
16976 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16977 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16978 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16980 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16981 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16983 record_lambda_scope (r
);
16985 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16986 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16988 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16989 cap
= TREE_CHAIN (cap
))
16991 tree field
= TREE_PURPOSE (cap
);
16992 if (PACK_EXPANSION_P (field
))
16993 field
= PACK_EXPANSION_PATTERN (field
);
16994 field
= tsubst_decl (field
, args
, complain
);
16996 if (field
== error_mark_node
)
16997 return error_mark_node
;
16999 tree init
= TREE_VALUE (cap
);
17000 if (PACK_EXPANSION_P (init
))
17001 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17003 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17004 /*fn*/false, /*constexpr*/false);
17006 if (TREE_CODE (field
) == TREE_VEC
)
17008 int len
= TREE_VEC_LENGTH (field
);
17009 gcc_assert (TREE_CODE (init
) == TREE_VEC
17010 && TREE_VEC_LENGTH (init
) == len
);
17011 for (int i
= 0; i
< len
; ++i
)
17012 LAMBDA_EXPR_CAPTURE_LIST (r
)
17013 = tree_cons (TREE_VEC_ELT (field
, i
),
17014 TREE_VEC_ELT (init
, i
),
17015 LAMBDA_EXPR_CAPTURE_LIST (r
));
17019 LAMBDA_EXPR_CAPTURE_LIST (r
)
17020 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17022 if (id_equal (DECL_NAME (field
), "__this"))
17023 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17027 tree type
= begin_lambda_type (r
);
17029 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17030 determine_visibility (TYPE_NAME (type
));
17032 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17034 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17035 ? DECL_TI_TEMPLATE (oldfn
)
17038 tree fntype
= static_fn_type (oldfn
);
17040 ++processing_template_decl
;
17041 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17043 --processing_template_decl
;
17045 if (fntype
== error_mark_node
)
17046 r
= error_mark_node
;
17049 /* Fix the type of 'this'. */
17050 fntype
= build_memfn_type (fntype
, type
,
17051 type_memfn_quals (fntype
),
17052 type_memfn_rqual (fntype
));
17056 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17057 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17058 finish_member_declaration (tmpl
);
17063 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17064 finish_member_declaration (fn
);
17067 /* Let finish_function set this. */
17068 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17070 bool nested
= cfun
;
17072 push_function_context ();
17074 /* Still increment function_depth so that we don't GC in the
17075 middle of an expression. */
17078 local_specialization_stack
s (lss_copy
);
17080 tree body
= start_lambda_function (fn
, r
);
17082 register_parameter_specializations (oldfn
, fn
);
17084 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17085 /*constexpr*/false);
17087 finish_lambda_function (body
);
17090 pop_function_context ();
17094 /* The capture list was built up in reverse order; fix that now. */
17095 LAMBDA_EXPR_CAPTURE_LIST (r
)
17096 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17098 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17100 maybe_add_lambda_conv_op (type
);
17103 finish_struct (type
, /*attr*/NULL_TREE
);
17105 insert_pending_capture_proxies ();
17110 /* Like tsubst but deals with expressions and performs semantic
17111 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17114 tsubst_copy_and_build (tree t
,
17116 tsubst_flags_t complain
,
17119 bool integral_constant_expression_p
)
17121 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17122 #define RECUR(NODE) \
17123 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17124 /*function_p=*/false, \
17125 integral_constant_expression_p)
17130 if (t
== NULL_TREE
|| t
== error_mark_node
)
17133 loc
= input_location
;
17134 if (EXPR_HAS_LOCATION (t
))
17135 input_location
= EXPR_LOCATION (t
);
17137 /* N3276 decltype magic only applies to calls at the top level or on the
17138 right side of a comma. */
17139 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17140 complain
&= ~tf_decltype
;
17142 switch (TREE_CODE (t
))
17146 /* Fall through. */
17147 case IDENTIFIER_NODE
:
17151 bool non_integral_constant_expression_p
;
17152 const char *error_msg
;
17154 if (IDENTIFIER_CONV_OP_P (t
))
17156 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17157 t
= make_conv_op_name (new_type
);
17160 /* Look up the name. */
17161 decl
= lookup_name (t
);
17163 /* By convention, expressions use ERROR_MARK_NODE to indicate
17164 failure, not NULL_TREE. */
17165 if (decl
== NULL_TREE
)
17166 decl
= error_mark_node
;
17168 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17170 integral_constant_expression_p
,
17171 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17172 &non_integral_constant_expression_p
,
17173 /*template_p=*/false,
17175 /*address_p=*/false,
17176 /*template_arg_p=*/false,
17181 if (!function_p
&& identifier_p (decl
))
17183 if (complain
& tf_error
)
17184 unqualified_name_lookup_error (decl
);
17185 decl
= error_mark_node
;
17190 case TEMPLATE_ID_EXPR
:
17193 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17194 tree targs
= TREE_OPERAND (t
, 1);
17197 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17198 if (targs
== error_mark_node
)
17199 return error_mark_node
;
17201 if (TREE_CODE (templ
) == SCOPE_REF
)
17203 tree name
= TREE_OPERAND (templ
, 1);
17204 tree tid
= lookup_template_function (name
, targs
);
17205 TREE_OPERAND (templ
, 1) = tid
;
17209 if (variable_template_p (templ
))
17210 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17212 if (TREE_CODE (templ
) == COMPONENT_REF
)
17214 object
= TREE_OPERAND (templ
, 0);
17215 templ
= TREE_OPERAND (templ
, 1);
17218 object
= NULL_TREE
;
17219 templ
= lookup_template_function (templ
, targs
);
17222 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17223 object
, templ
, NULL_TREE
));
17225 RETURN (baselink_for_fns (templ
));
17230 tree r
= RECUR (TREE_OPERAND (t
, 0));
17232 if (REFERENCE_REF_P (t
))
17234 /* A type conversion to reference type will be enclosed in
17235 such an indirect ref, but the substitution of the cast
17236 will have also added such an indirect ref. */
17237 r
= convert_from_reference (r
);
17240 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17241 complain
|decltype_flag
);
17243 if (REF_PARENTHESIZED_P (t
))
17244 r
= force_paren_expr (r
);
17251 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17252 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17253 RETURN (build_nop (type
, op0
));
17256 case IMPLICIT_CONV_EXPR
:
17258 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17259 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17260 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17262 retval
= copy_node (t
);
17263 TREE_TYPE (retval
) = type
;
17264 TREE_OPERAND (retval
, 0) = expr
;
17267 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17268 /* We'll pass this to convert_nontype_argument again, we don't need
17269 to actually perform any conversion here. */
17271 int flags
= LOOKUP_IMPLICIT
;
17272 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17273 flags
= LOOKUP_NORMAL
;
17274 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17280 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17281 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17282 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17286 case REINTERPRET_CAST_EXPR
:
17287 case CONST_CAST_EXPR
:
17288 case DYNAMIC_CAST_EXPR
:
17289 case STATIC_CAST_EXPR
:
17292 tree op
, r
= NULL_TREE
;
17294 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17295 if (integral_constant_expression_p
17296 && !cast_valid_in_integral_constant_expression_p (type
))
17298 if (complain
& tf_error
)
17299 error ("a cast to a type other than an integral or "
17300 "enumeration type cannot appear in a constant-expression");
17301 RETURN (error_mark_node
);
17304 op
= RECUR (TREE_OPERAND (t
, 0));
17306 warning_sentinel
s(warn_useless_cast
);
17307 warning_sentinel
s2(warn_ignored_qualifiers
);
17308 switch (TREE_CODE (t
))
17311 r
= build_functional_cast (type
, op
, complain
);
17313 case REINTERPRET_CAST_EXPR
:
17314 r
= build_reinterpret_cast (type
, op
, complain
);
17316 case CONST_CAST_EXPR
:
17317 r
= build_const_cast (type
, op
, complain
);
17319 case DYNAMIC_CAST_EXPR
:
17320 r
= build_dynamic_cast (type
, op
, complain
);
17322 case STATIC_CAST_EXPR
:
17323 r
= build_static_cast (type
, op
, complain
);
17326 gcc_unreachable ();
17332 case POSTDECREMENT_EXPR
:
17333 case POSTINCREMENT_EXPR
:
17334 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17335 args
, complain
, in_decl
);
17336 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17337 complain
|decltype_flag
));
17339 case PREDECREMENT_EXPR
:
17340 case PREINCREMENT_EXPR
:
17344 case TRUTH_NOT_EXPR
:
17345 case UNARY_PLUS_EXPR
: /* Unary + */
17346 case REALPART_EXPR
:
17347 case IMAGPART_EXPR
:
17348 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17349 RECUR (TREE_OPERAND (t
, 0)),
17350 complain
|decltype_flag
));
17352 case FIX_TRUNC_EXPR
:
17353 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17357 op1
= TREE_OPERAND (t
, 0);
17358 if (TREE_CODE (op1
) == LABEL_DECL
)
17359 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17360 EXPR_LOCATION (op1
)));
17361 if (TREE_CODE (op1
) == SCOPE_REF
)
17362 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17363 /*done=*/true, /*address_p=*/true);
17365 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17367 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17368 complain
|decltype_flag
));
17373 case TRUNC_DIV_EXPR
:
17374 case CEIL_DIV_EXPR
:
17375 case FLOOR_DIV_EXPR
:
17376 case ROUND_DIV_EXPR
:
17377 case EXACT_DIV_EXPR
:
17381 case TRUNC_MOD_EXPR
:
17382 case FLOOR_MOD_EXPR
:
17383 case TRUTH_ANDIF_EXPR
:
17384 case TRUTH_ORIF_EXPR
:
17385 case TRUTH_AND_EXPR
:
17386 case TRUTH_OR_EXPR
:
17402 warning_sentinel
s1(warn_type_limits
);
17403 warning_sentinel
s2(warn_div_by_zero
);
17404 warning_sentinel
s3(warn_logical_op
);
17405 warning_sentinel
s4(warn_tautological_compare
);
17406 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17407 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17408 tree r
= build_x_binary_op
17409 (input_location
, TREE_CODE (t
),
17411 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17413 : TREE_CODE (TREE_OPERAND (t
, 0))),
17415 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17417 : TREE_CODE (TREE_OPERAND (t
, 1))),
17419 complain
|decltype_flag
);
17420 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17421 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17426 case POINTER_PLUS_EXPR
:
17428 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17429 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17430 return fold_build_pointer_plus (op0
, op1
);
17434 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17435 /*address_p=*/false));
17437 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17438 args
, complain
, in_decl
);
17439 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17440 RECUR (TREE_OPERAND (t
, 1)),
17441 complain
|decltype_flag
));
17444 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17445 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17446 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17453 op1
= TREE_OPERAND (t
, 0);
17454 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17455 op1
= TREE_TYPE (op1
);
17458 /* When there are no ARGS, we are trying to evaluate a
17459 non-dependent expression from the parser. Trying to do
17460 the substitutions may not work. */
17462 op1
= TREE_TYPE (op1
);
17466 ++cp_unevaluated_operand
;
17467 ++c_inhibit_evaluation_warnings
;
17469 op1
= tsubst (op1
, args
, complain
, in_decl
);
17471 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17472 /*function_p=*/false,
17473 /*integral_constant_expression_p=*/
17475 --cp_unevaluated_operand
;
17476 --c_inhibit_evaluation_warnings
;
17479 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17480 complain
& tf_error
);
17482 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17483 complain
& tf_error
);
17484 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17486 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17488 if (!processing_template_decl
&& TYPE_P (op1
))
17490 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17491 build1 (NOP_EXPR
, op1
, error_mark_node
));
17492 SIZEOF_EXPR_TYPE_P (r
) = 1;
17495 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17496 TREE_SIDE_EFFECTS (r
) = 0;
17497 TREE_READONLY (r
) = 1;
17499 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17504 case AT_ENCODE_EXPR
:
17506 op1
= TREE_OPERAND (t
, 0);
17507 ++cp_unevaluated_operand
;
17508 ++c_inhibit_evaluation_warnings
;
17509 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17510 /*function_p=*/false,
17511 /*integral_constant_expression_p=*/false);
17512 --cp_unevaluated_operand
;
17513 --c_inhibit_evaluation_warnings
;
17514 RETURN (objc_build_encode_expr (op1
));
17517 case NOEXCEPT_EXPR
:
17518 op1
= TREE_OPERAND (t
, 0);
17519 ++cp_unevaluated_operand
;
17520 ++c_inhibit_evaluation_warnings
;
17521 ++cp_noexcept_operand
;
17522 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17523 /*function_p=*/false,
17524 /*integral_constant_expression_p=*/false);
17525 --cp_unevaluated_operand
;
17526 --c_inhibit_evaluation_warnings
;
17527 --cp_noexcept_operand
;
17528 RETURN (finish_noexcept_expr (op1
, complain
));
17532 warning_sentinel
s(warn_div_by_zero
);
17533 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17534 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17535 tree r
= build_x_modify_expr
17536 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17537 complain
|decltype_flag
);
17538 /* TREE_NO_WARNING must be set if either the expression was
17539 parenthesized or it uses an operator such as >>= rather
17540 than plain assignment. In the former case, it was already
17541 set and must be copied. In the latter case,
17542 build_x_modify_expr sets it and it must not be reset
17544 if (TREE_NO_WARNING (t
))
17545 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17551 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17552 args
, complain
, in_decl
);
17553 /* Remember that there was a reference to this entity. */
17555 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17556 RETURN (error_mark_node
);
17557 RETURN (build_x_arrow (input_location
, op1
, complain
));
17561 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17562 tree init
= RECUR (TREE_OPERAND (t
, 3));
17563 vec
<tree
, va_gc
> *placement_vec
;
17564 vec
<tree
, va_gc
> *init_vec
;
17567 if (placement
== NULL_TREE
)
17568 placement_vec
= NULL
;
17571 placement_vec
= make_tree_vector ();
17572 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17573 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17576 /* If there was an initializer in the original tree, but it
17577 instantiated to an empty list, then we should pass a
17578 non-NULL empty vector to tell build_new that it was an
17579 empty initializer() rather than no initializer. This can
17580 only happen when the initializer is a pack expansion whose
17581 parameter packs are of length zero. */
17582 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17586 init_vec
= make_tree_vector ();
17587 if (init
== void_node
)
17588 gcc_assert (init_vec
!= NULL
);
17591 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17592 vec_safe_push (init_vec
, TREE_VALUE (init
));
17596 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17597 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17598 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17599 NEW_EXPR_USE_GLOBAL (t
),
17602 if (placement_vec
!= NULL
)
17603 release_tree_vector (placement_vec
);
17604 if (init_vec
!= NULL
)
17605 release_tree_vector (init_vec
);
17612 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17613 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17614 RETURN (delete_sanity (op0
, op1
,
17615 DELETE_EXPR_USE_VEC (t
),
17616 DELETE_EXPR_USE_GLOBAL (t
),
17620 case COMPOUND_EXPR
:
17622 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17623 complain
& ~tf_decltype
, in_decl
,
17624 /*function_p=*/false,
17625 integral_constant_expression_p
);
17626 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17628 RECUR (TREE_OPERAND (t
, 1)),
17629 complain
|decltype_flag
));
17635 vec
<tree
, va_gc
> *call_args
;
17636 unsigned int nargs
, i
;
17641 function
= CALL_EXPR_FN (t
);
17642 /* Internal function with no arguments. */
17643 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17646 /* When we parsed the expression, we determined whether or
17647 not Koenig lookup should be performed. */
17648 koenig_p
= KOENIG_LOOKUP_P (t
);
17649 if (function
== NULL_TREE
)
17652 qualified_p
= false;
17654 else if (TREE_CODE (function
) == SCOPE_REF
)
17656 qualified_p
= true;
17657 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17659 /*address_p=*/false);
17661 else if (koenig_p
&& identifier_p (function
))
17663 /* Do nothing; calling tsubst_copy_and_build on an identifier
17664 would incorrectly perform unqualified lookup again.
17666 Note that we can also have an IDENTIFIER_NODE if the earlier
17667 unqualified lookup found a member function; in that case
17668 koenig_p will be false and we do want to do the lookup
17669 again to find the instantiated member function.
17671 FIXME but doing that causes c++/15272, so we need to stop
17672 using IDENTIFIER_NODE in that situation. */
17673 qualified_p
= false;
17677 if (TREE_CODE (function
) == COMPONENT_REF
)
17679 tree op
= TREE_OPERAND (function
, 1);
17681 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17682 || (BASELINK_P (op
)
17683 && BASELINK_QUALIFIED_P (op
)));
17686 qualified_p
= false;
17688 if (TREE_CODE (function
) == ADDR_EXPR
17689 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17690 /* Avoid error about taking the address of a constructor. */
17691 function
= TREE_OPERAND (function
, 0);
17693 function
= tsubst_copy_and_build (function
, args
, complain
,
17696 integral_constant_expression_p
);
17698 if (BASELINK_P (function
))
17699 qualified_p
= true;
17702 nargs
= call_expr_nargs (t
);
17703 call_args
= make_tree_vector ();
17704 for (i
= 0; i
< nargs
; ++i
)
17706 tree arg
= CALL_EXPR_ARG (t
, i
);
17708 if (!PACK_EXPANSION_P (arg
))
17709 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17712 /* Expand the pack expansion and push each entry onto
17714 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17715 if (TREE_CODE (arg
) == TREE_VEC
)
17717 unsigned int len
, j
;
17719 len
= TREE_VEC_LENGTH (arg
);
17720 for (j
= 0; j
< len
; ++j
)
17722 tree value
= TREE_VEC_ELT (arg
, j
);
17723 if (value
!= NULL_TREE
)
17724 value
= convert_from_reference (value
);
17725 vec_safe_push (call_args
, value
);
17730 /* A partial substitution. Add one entry. */
17731 vec_safe_push (call_args
, arg
);
17736 /* We do not perform argument-dependent lookup if normal
17737 lookup finds a non-function, in accordance with the
17738 expected resolution of DR 218. */
17740 && ((is_overloaded_fn (function
)
17741 /* If lookup found a member function, the Koenig lookup is
17742 not appropriate, even if an unqualified-name was used
17743 to denote the function. */
17744 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17745 || identifier_p (function
))
17746 /* Only do this when substitution turns a dependent call
17747 into a non-dependent call. */
17748 && type_dependent_expression_p_push (t
)
17749 && !any_type_dependent_arguments_p (call_args
))
17750 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17752 if (function
!= NULL_TREE
17753 && identifier_p (function
)
17754 && !any_type_dependent_arguments_p (call_args
))
17756 if (koenig_p
&& (complain
& tf_warning_or_error
))
17758 /* For backwards compatibility and good diagnostics, try
17759 the unqualified lookup again if we aren't in SFINAE
17761 tree unq
= (tsubst_copy_and_build
17762 (function
, args
, complain
, in_decl
, true,
17763 integral_constant_expression_p
));
17764 if (unq
== error_mark_node
)
17766 release_tree_vector (call_args
);
17767 RETURN (error_mark_node
);
17770 if (unq
!= function
)
17772 /* In a lambda fn, we have to be careful to not
17773 introduce new this captures. Legacy code can't
17774 be using lambdas anyway, so it's ok to be
17776 bool in_lambda
= (current_class_type
17777 && LAMBDA_TYPE_P (current_class_type
));
17778 char const *const msg
17779 = G_("%qD was not declared in this scope, "
17780 "and no declarations were found by "
17781 "argument-dependent lookup at the point "
17782 "of instantiation");
17786 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17789 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17795 if (INDIRECT_REF_P (fn
))
17796 fn
= TREE_OPERAND (fn
, 0);
17797 if (is_overloaded_fn (fn
))
17798 fn
= get_first_fn (fn
);
17801 /* Can't say anything more. */;
17802 else if (DECL_CLASS_SCOPE_P (fn
))
17804 location_t loc
= EXPR_LOC_OR_LOC (t
,
17807 "declarations in dependent base %qT are "
17808 "not found by unqualified lookup",
17809 DECL_CLASS_CONTEXT (fn
));
17810 if (current_class_ptr
)
17812 "use %<this->%D%> instead", function
);
17815 "use %<%T::%D%> instead",
17816 current_class_name
, function
);
17819 inform (DECL_SOURCE_LOCATION (fn
),
17820 "%qD declared here, later in the "
17821 "translation unit", fn
);
17824 release_tree_vector (call_args
);
17825 RETURN (error_mark_node
);
17832 if (identifier_p (function
))
17834 if (complain
& tf_error
)
17835 unqualified_name_lookup_error (function
);
17836 release_tree_vector (call_args
);
17837 RETURN (error_mark_node
);
17841 /* Remember that there was a reference to this entity. */
17842 if (function
!= NULL_TREE
17843 && DECL_P (function
)
17844 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17846 release_tree_vector (call_args
);
17847 RETURN (error_mark_node
);
17850 /* Put back tf_decltype for the actual call. */
17851 complain
|= decltype_flag
;
17853 if (function
== NULL_TREE
)
17854 switch (CALL_EXPR_IFN (t
))
17857 gcc_assert (nargs
== 1);
17858 if (vec_safe_length (call_args
) != 1)
17860 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17861 "wrong number of arguments to "
17862 "%<__builtin_launder%>");
17863 ret
= error_mark_node
;
17866 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17868 (*call_args
)[0], complain
);
17872 /* Unsupported internal function with arguments. */
17873 gcc_unreachable ();
17875 else if (TREE_CODE (function
) == OFFSET_REF
)
17876 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17878 else if (TREE_CODE (function
) == COMPONENT_REF
)
17880 tree instance
= TREE_OPERAND (function
, 0);
17881 tree fn
= TREE_OPERAND (function
, 1);
17883 if (processing_template_decl
17884 && (type_dependent_expression_p (instance
)
17885 || (!BASELINK_P (fn
)
17886 && TREE_CODE (fn
) != FIELD_DECL
)
17887 || type_dependent_expression_p (fn
)
17888 || any_type_dependent_arguments_p (call_args
)))
17889 ret
= build_min_nt_call_vec (function
, call_args
);
17890 else if (!BASELINK_P (fn
))
17891 ret
= finish_call_expr (function
, &call_args
,
17892 /*disallow_virtual=*/false,
17893 /*koenig_p=*/false,
17896 ret
= (build_new_method_call
17898 &call_args
, NULL_TREE
,
17899 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17904 ret
= finish_call_expr (function
, &call_args
,
17905 /*disallow_virtual=*/qualified_p
,
17909 release_tree_vector (call_args
);
17911 if (ret
!= error_mark_node
)
17913 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17914 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17915 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17916 bool thk
= CALL_FROM_THUNK_P (t
);
17917 if (op
|| ord
|| rev
|| thk
)
17919 function
= extract_call_expr (ret
);
17920 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17921 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17922 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17925 if (TREE_CODE (function
) == CALL_EXPR
)
17926 CALL_FROM_THUNK_P (function
) = true;
17928 AGGR_INIT_FROM_THUNK_P (function
) = true;
17929 /* The thunk location is not interesting. */
17930 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17940 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17941 tree folded_cond
= fold_non_dependent_expr (cond
);
17944 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17946 if (integer_zerop (folded_cond
))
17948 ++c_inhibit_evaluation_warnings
;
17949 exp1
= RECUR (TREE_OPERAND (t
, 1));
17950 --c_inhibit_evaluation_warnings
;
17951 exp2
= RECUR (TREE_OPERAND (t
, 2));
17955 exp1
= RECUR (TREE_OPERAND (t
, 1));
17956 ++c_inhibit_evaluation_warnings
;
17957 exp2
= RECUR (TREE_OPERAND (t
, 2));
17958 --c_inhibit_evaluation_warnings
;
17960 cond
= folded_cond
;
17964 exp1
= RECUR (TREE_OPERAND (t
, 1));
17965 exp2
= RECUR (TREE_OPERAND (t
, 2));
17968 warning_sentinel
s(warn_duplicated_branches
);
17969 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17970 cond
, exp1
, exp2
, complain
));
17973 case PSEUDO_DTOR_EXPR
:
17975 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17976 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17977 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17978 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17984 tree purpose
, value
, chain
;
17986 if (t
== void_list_node
)
17989 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17990 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17992 /* We have pack expansions, so expand those and
17993 create a new list out of it. */
17994 tree purposevec
= NULL_TREE
;
17995 tree valuevec
= NULL_TREE
;
17999 /* Expand the argument expressions. */
18000 if (TREE_PURPOSE (t
))
18001 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18002 complain
, in_decl
);
18003 if (TREE_VALUE (t
))
18004 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18005 complain
, in_decl
);
18007 /* Build the rest of the list. */
18008 chain
= TREE_CHAIN (t
);
18009 if (chain
&& chain
!= void_type_node
)
18010 chain
= RECUR (chain
);
18012 /* Determine the number of arguments. */
18013 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18015 len
= TREE_VEC_LENGTH (purposevec
);
18016 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18018 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18019 len
= TREE_VEC_LENGTH (valuevec
);
18022 /* Since we only performed a partial substitution into
18023 the argument pack, we only RETURN (a single list
18025 if (purposevec
== TREE_PURPOSE (t
)
18026 && valuevec
== TREE_VALUE (t
)
18027 && chain
== TREE_CHAIN (t
))
18030 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18033 /* Convert the argument vectors into a TREE_LIST */
18037 /* Grab the Ith values. */
18039 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18042 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18045 /* Build the list (backwards). */
18046 chain
= tree_cons (purpose
, value
, chain
);
18052 purpose
= TREE_PURPOSE (t
);
18054 purpose
= RECUR (purpose
);
18055 value
= TREE_VALUE (t
);
18057 value
= RECUR (value
);
18058 chain
= TREE_CHAIN (t
);
18059 if (chain
&& chain
!= void_type_node
)
18060 chain
= RECUR (chain
);
18061 if (purpose
== TREE_PURPOSE (t
)
18062 && value
== TREE_VALUE (t
)
18063 && chain
== TREE_CHAIN (t
))
18065 RETURN (tree_cons (purpose
, value
, chain
));
18068 case COMPONENT_REF
:
18075 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18076 args
, complain
, in_decl
);
18077 /* Remember that there was a reference to this entity. */
18078 if (DECL_P (object
)
18079 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18080 RETURN (error_mark_node
);
18081 object_type
= TREE_TYPE (object
);
18083 member
= TREE_OPERAND (t
, 1);
18084 if (BASELINK_P (member
))
18085 member
= tsubst_baselink (member
,
18086 non_reference (TREE_TYPE (object
)),
18087 args
, complain
, in_decl
);
18089 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18090 if (member
== error_mark_node
)
18091 RETURN (error_mark_node
);
18093 if (TREE_CODE (member
) == FIELD_DECL
)
18095 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18096 if (TREE_CODE (r
) == COMPONENT_REF
)
18097 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18100 else if (type_dependent_expression_p (object
))
18101 /* We can't do much here. */;
18102 else if (!CLASS_TYPE_P (object_type
))
18104 if (scalarish_type_p (object_type
))
18106 tree s
= NULL_TREE
;
18107 tree dtor
= member
;
18109 if (TREE_CODE (dtor
) == SCOPE_REF
)
18111 s
= TREE_OPERAND (dtor
, 0);
18112 dtor
= TREE_OPERAND (dtor
, 1);
18114 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18116 dtor
= TREE_OPERAND (dtor
, 0);
18118 RETURN (finish_pseudo_destructor_expr
18119 (object
, s
, dtor
, input_location
));
18123 else if (TREE_CODE (member
) == SCOPE_REF
18124 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18126 /* Lookup the template functions now that we know what the
18128 tree scope
= TREE_OPERAND (member
, 0);
18129 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18130 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18131 member
= lookup_qualified_name (scope
, tmpl
,
18132 /*is_type_p=*/false,
18133 /*complain=*/false);
18134 if (BASELINK_P (member
))
18136 BASELINK_FUNCTIONS (member
)
18137 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18139 member
= (adjust_result_of_qualified_name_lookup
18140 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18145 qualified_name_lookup_error (scope
, tmpl
, member
,
18147 RETURN (error_mark_node
);
18150 else if (TREE_CODE (member
) == SCOPE_REF
18151 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18152 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18154 if (complain
& tf_error
)
18156 if (TYPE_P (TREE_OPERAND (member
, 0)))
18157 error ("%qT is not a class or namespace",
18158 TREE_OPERAND (member
, 0));
18160 error ("%qD is not a class or namespace",
18161 TREE_OPERAND (member
, 0));
18163 RETURN (error_mark_node
);
18166 r
= finish_class_member_access_expr (object
, member
,
18167 /*template_p=*/false,
18169 if (TREE_CODE (r
) == COMPONENT_REF
)
18170 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18175 RETURN (build_throw
18176 (RECUR (TREE_OPERAND (t
, 0))));
18180 vec
<constructor_elt
, va_gc
> *n
;
18181 constructor_elt
*ce
;
18182 unsigned HOST_WIDE_INT idx
;
18183 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18184 bool process_index_p
;
18186 bool need_copy_p
= false;
18189 if (type
== error_mark_node
)
18190 RETURN (error_mark_node
);
18192 /* We do not want to process the index of aggregate
18193 initializers as they are identifier nodes which will be
18194 looked up by digest_init. */
18195 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18197 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18198 newlen
= vec_safe_length (n
);
18199 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18201 if (ce
->index
&& process_index_p
18202 /* An identifier index is looked up in the type
18203 being initialized, not the current scope. */
18204 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18205 ce
->index
= RECUR (ce
->index
);
18207 if (PACK_EXPANSION_P (ce
->value
))
18209 /* Substitute into the pack expansion. */
18210 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18213 if (ce
->value
== error_mark_node
18214 || PACK_EXPANSION_P (ce
->value
))
18216 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18217 /* Just move the argument into place. */
18218 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18221 /* Update the length of the final CONSTRUCTOR
18222 arguments vector, and note that we will need to
18224 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18225 need_copy_p
= true;
18229 ce
->value
= RECUR (ce
->value
);
18234 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18236 vec_alloc (n
, newlen
);
18237 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18239 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18241 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18242 for (i
= 0; i
< len
; ++i
)
18243 CONSTRUCTOR_APPEND_ELT (n
, 0,
18244 TREE_VEC_ELT (ce
->value
, i
));
18247 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18251 r
= build_constructor (init_list_type_node
, n
);
18252 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18254 if (TREE_HAS_CONSTRUCTOR (t
))
18256 fcl_t cl
= fcl_functional
;
18257 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18259 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18262 TREE_TYPE (r
) = type
;
18268 tree operand_0
= TREE_OPERAND (t
, 0);
18269 if (TYPE_P (operand_0
))
18271 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18272 RETURN (get_typeid (operand_0
, complain
));
18276 operand_0
= RECUR (operand_0
);
18277 RETURN (build_typeid (operand_0
, complain
));
18288 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18289 /* ??? We're doing a subset of finish_id_expression here. */
18291 && !processing_template_decl
18292 && !cp_unevaluated_operand
18293 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18294 && CP_DECL_THREAD_LOCAL_P (r
))
18296 if (tree wrap
= get_tls_wrapper_fn (r
))
18297 /* Replace an evaluated use of the thread_local variable with
18298 a call to its wrapper. */
18299 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18301 else if (outer_automatic_var_p (r
))
18302 r
= process_outer_var_ref (r
, complain
);
18304 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18305 /* If the original type was a reference, we'll be wrapped in
18306 the appropriate INDIRECT_REF. */
18307 r
= convert_from_reference (r
);
18313 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18314 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18315 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18318 case OFFSETOF_EXPR
:
18321 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18322 in_decl
, /*function_p=*/false,
18323 /*integral_constant_expression_p=*/false);
18324 RETURN (finish_offsetof (object_ptr
,
18325 RECUR (TREE_OPERAND (t
, 0)),
18326 EXPR_LOCATION (t
)));
18329 case ADDRESSOF_EXPR
:
18330 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18331 RECUR (TREE_OPERAND (t
, 0)), complain
));
18335 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18336 complain
, in_decl
);
18338 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18339 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18340 type2
= RECUR (type2
);
18342 type2
= tsubst (type2
, args
, complain
, in_decl
);
18344 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18349 tree old_stmt_expr
= cur_stmt_expr
;
18350 tree stmt_expr
= begin_stmt_expr ();
18352 cur_stmt_expr
= stmt_expr
;
18353 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18354 integral_constant_expression_p
);
18355 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18356 cur_stmt_expr
= old_stmt_expr
;
18358 /* If the resulting list of expression statement is empty,
18359 fold it further into void_node. */
18360 if (empty_expr_stmt_p (stmt_expr
))
18361 stmt_expr
= void_node
;
18363 RETURN (stmt_expr
);
18368 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18370 RETURN (build_lambda_object (r
));
18374 /* We can get here for a constant initializer of non-dependent type.
18375 FIXME stop folding in cp_parser_initializer_clause. */
18377 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18382 case TRANSACTION_EXPR
:
18383 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18384 integral_constant_expression_p
));
18387 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18389 case VEC_PERM_EXPR
:
18391 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18392 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18393 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18394 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18398 case REQUIRES_EXPR
:
18399 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18401 case NON_LVALUE_EXPR
:
18402 case VIEW_CONVERT_EXPR
:
18403 /* We should only see these for location wrapper nodes, or within
18404 instantiate_non_dependent_expr (when args is NULL_TREE). */
18405 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18406 if (location_wrapper_p (t
))
18407 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18408 EXPR_LOCATION (t
)));
18412 /* Handle Objective-C++ constructs, if appropriate. */
18415 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18416 in_decl
, /*function_p=*/false);
18420 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18426 input_location
= loc
;
18430 /* Verify that the instantiated ARGS are valid. For type arguments,
18431 make sure that the type's linkage is ok. For non-type arguments,
18432 make sure they are constants if they are integral or enumerations.
18433 Emit an error under control of COMPLAIN, and return TRUE on error. */
18436 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18438 if (dependent_template_arg_p (t
))
18440 if (ARGUMENT_PACK_P (t
))
18442 tree vec
= ARGUMENT_PACK_ARGS (t
);
18443 int len
= TREE_VEC_LENGTH (vec
);
18444 bool result
= false;
18447 for (i
= 0; i
< len
; ++i
)
18448 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18452 else if (TYPE_P (t
))
18454 /* [basic.link]: A name with no linkage (notably, the name
18455 of a class or enumeration declared in a local scope)
18456 shall not be used to declare an entity with linkage.
18457 This implies that names with no linkage cannot be used as
18460 DR 757 relaxes this restriction for C++0x. */
18461 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18462 : no_linkage_check (t
, /*relaxed_p=*/false));
18466 /* DR 488 makes use of a type with no linkage cause
18467 type deduction to fail. */
18468 if (complain
& tf_error
)
18470 if (TYPE_UNNAMED_P (nt
))
18471 error ("%qT is/uses unnamed type", t
);
18473 error ("template argument for %qD uses local type %qT",
18478 /* In order to avoid all sorts of complications, we do not
18479 allow variably-modified types as template arguments. */
18480 else if (variably_modified_type_p (t
, NULL_TREE
))
18482 if (complain
& tf_error
)
18483 error ("%qT is a variably modified type", t
);
18487 /* Class template and alias template arguments should be OK. */
18488 else if (DECL_TYPE_TEMPLATE_P (t
))
18490 /* A non-type argument of integral or enumerated type must be a
18492 else if (TREE_TYPE (t
)
18493 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18494 && !REFERENCE_REF_P (t
)
18495 && !TREE_CONSTANT (t
))
18497 if (complain
& tf_error
)
18498 error ("integral expression %qE is not constant", t
);
18505 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18507 int ix
, len
= DECL_NTPARMS (tmpl
);
18508 bool result
= false;
18510 for (ix
= 0; ix
!= len
; ix
++)
18512 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18515 if (result
&& (complain
& tf_error
))
18516 error (" trying to instantiate %qD", tmpl
);
18520 /* We're out of SFINAE context now, so generate diagnostics for the access
18521 errors we saw earlier when instantiating D from TMPL and ARGS. */
18524 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18526 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18527 tree type
= TREE_TYPE (pattern
);
18528 location_t loc
= input_location
;
18530 push_access_scope (d
);
18531 push_deferring_access_checks (dk_no_deferred
);
18532 input_location
= DECL_SOURCE_LOCATION (pattern
);
18533 tsubst (type
, args
, tf_warning_or_error
, d
);
18534 input_location
= loc
;
18535 pop_deferring_access_checks ();
18536 pop_access_scope (d
);
18539 /* Instantiate the indicated variable, function, or alias template TMPL with
18540 the template arguments in TARG_PTR. */
18543 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18545 tree targ_ptr
= orig_args
;
18549 bool access_ok
= true;
18551 if (tmpl
== error_mark_node
)
18552 return error_mark_node
;
18554 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18556 /* If this function is a clone, handle it specially. */
18557 if (DECL_CLONED_FUNCTION_P (tmpl
))
18562 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18563 DECL_CLONED_FUNCTION. */
18564 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18565 targ_ptr
, complain
);
18566 if (spec
== error_mark_node
)
18567 return error_mark_node
;
18569 /* Look for the clone. */
18570 FOR_EACH_CLONE (clone
, spec
)
18571 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18573 /* We should always have found the clone by now. */
18574 gcc_unreachable ();
18578 if (targ_ptr
== error_mark_node
)
18579 return error_mark_node
;
18581 /* Check to see if we already have this specialization. */
18582 gen_tmpl
= most_general_template (tmpl
);
18583 if (TMPL_ARGS_DEPTH (targ_ptr
)
18584 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18585 /* targ_ptr only has the innermost template args, so add the outer ones
18586 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18587 the case of a non-dependent call within a template definition). */
18588 targ_ptr
= (add_outermost_template_args
18589 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18592 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18593 but it doesn't seem to be on the hot path. */
18594 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18596 gcc_assert (tmpl
== gen_tmpl
18597 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18599 || fndecl
== NULL_TREE
);
18601 if (spec
!= NULL_TREE
)
18603 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18605 if (complain
& tf_error
)
18606 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18607 return error_mark_node
;
18612 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18614 return error_mark_node
;
18616 /* We are building a FUNCTION_DECL, during which the access of its
18617 parameters and return types have to be checked. However this
18618 FUNCTION_DECL which is the desired context for access checking
18619 is not built yet. We solve this chicken-and-egg problem by
18620 deferring all checks until we have the FUNCTION_DECL. */
18621 push_deferring_access_checks (dk_deferred
);
18623 /* Instantiation of the function happens in the context of the function
18624 template, not the context of the overload resolution we're doing. */
18625 push_to_top_level ();
18626 /* If there are dependent arguments, e.g. because we're doing partial
18627 ordering, make sure processing_template_decl stays set. */
18628 if (uses_template_parms (targ_ptr
))
18629 ++processing_template_decl
;
18630 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18632 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18633 complain
, gen_tmpl
, true);
18634 push_nested_class (ctx
);
18637 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18639 fndecl
= NULL_TREE
;
18640 if (VAR_P (pattern
))
18642 /* We need to determine if we're using a partial or explicit
18643 specialization now, because the type of the variable could be
18645 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18646 tree elt
= most_specialized_partial_spec (tid
, complain
);
18647 if (elt
== error_mark_node
)
18648 pattern
= error_mark_node
;
18651 tree partial_tmpl
= TREE_VALUE (elt
);
18652 tree partial_args
= TREE_PURPOSE (elt
);
18653 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18654 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18658 /* Substitute template parameters to obtain the specialization. */
18659 if (fndecl
== NULL_TREE
)
18660 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18661 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18662 pop_nested_class ();
18663 pop_from_top_level ();
18665 if (fndecl
== error_mark_node
)
18667 pop_deferring_access_checks ();
18668 return error_mark_node
;
18671 /* The DECL_TI_TEMPLATE should always be the immediate parent
18672 template, not the most general template. */
18673 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18674 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18676 /* Now we know the specialization, compute access previously
18677 deferred. Do no access control for inheriting constructors,
18678 as we already checked access for the inherited constructor. */
18679 if (!(flag_new_inheriting_ctors
18680 && DECL_INHERITED_CTOR (fndecl
)))
18682 push_access_scope (fndecl
);
18683 if (!perform_deferred_access_checks (complain
))
18685 pop_access_scope (fndecl
);
18687 pop_deferring_access_checks ();
18689 /* If we've just instantiated the main entry point for a function,
18690 instantiate all the alternate entry points as well. We do this
18691 by cloning the instantiation of the main entry point, not by
18692 instantiating the template clones. */
18693 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18694 clone_function_decl (fndecl
, /*update_methods=*/false);
18698 if (!(complain
& tf_error
))
18700 /* Remember to reinstantiate when we're out of SFINAE so the user
18701 can see the errors. */
18702 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18704 return error_mark_node
;
18709 /* Wrapper for instantiate_template_1. */
18712 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18715 timevar_push (TV_TEMPLATE_INST
);
18716 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18717 timevar_pop (TV_TEMPLATE_INST
);
18721 /* Instantiate the alias template TMPL with ARGS. Also push a template
18722 instantiation level, which instantiate_template doesn't do because
18723 functions and variables have sufficient context established by the
18727 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18729 struct pending_template
*old_last_pend
= last_pending_template
;
18730 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18731 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18732 return error_mark_node
;
18733 tree tinst
= build_tree_list (tmpl
, args
);
18734 if (!push_tinst_level (tinst
))
18737 return error_mark_node
;
18741 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18742 args
, tmpl
, complain
,
18743 /*require_all_args=*/true,
18744 /*use_default_args=*/true);
18746 tree r
= instantiate_template (tmpl
, args
, complain
);
18747 pop_tinst_level ();
18748 /* We can't free this if a pending_template entry or last_error_tinst_level
18749 is pointing at it. */
18750 if (last_pending_template
== old_last_pend
18751 && last_error_tinst_level
== old_error_tinst
)
18757 /* PARM is a template parameter pack for FN. Returns true iff
18758 PARM is used in a deducible way in the argument list of FN. */
18761 pack_deducible_p (tree parm
, tree fn
)
18763 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18764 for (; t
; t
= TREE_CHAIN (t
))
18766 tree type
= TREE_VALUE (t
);
18768 if (!PACK_EXPANSION_P (type
))
18770 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18771 packs
; packs
= TREE_CHAIN (packs
))
18772 if (template_args_equal (TREE_VALUE (packs
), parm
))
18774 /* The template parameter pack is used in a function parameter
18775 pack. If this is the end of the parameter list, the
18776 template parameter pack is deducible. */
18777 if (TREE_CHAIN (t
) == void_list_node
)
18780 /* Otherwise, not. Well, it could be deduced from
18781 a non-pack parameter, but doing so would end up with
18782 a deduction mismatch, so don't bother. */
18786 /* The template parameter pack isn't used in any function parameter
18787 packs, but it might be used deeper, e.g. tuple<Args...>. */
18791 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18792 NARGS elements of the arguments that are being used when calling
18793 it. TARGS is a vector into which the deduced template arguments
18796 Returns either a FUNCTION_DECL for the matching specialization of FN or
18797 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18798 true, diagnostics will be printed to explain why it failed.
18800 If FN is a conversion operator, or we are trying to produce a specific
18801 specialization, RETURN_TYPE is the return type desired.
18803 The EXPLICIT_TARGS are explicit template arguments provided via a
18806 The parameter STRICT is one of:
18809 We are deducing arguments for a function call, as in
18810 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18811 deducing arguments for a call to the result of a conversion
18812 function template, as in [over.call.object].
18815 We are deducing arguments for a conversion function, as in
18816 [temp.deduct.conv].
18819 We are deducing arguments when doing an explicit instantiation
18820 as in [temp.explicit], when determining an explicit specialization
18821 as in [temp.expl.spec], or when taking the address of a function
18822 template, as in [temp.deduct.funcaddr]. */
18825 fn_type_unification (tree fn
,
18826 tree explicit_targs
,
18829 unsigned int nargs
,
18831 unification_kind_t strict
,
18838 tree decl
= NULL_TREE
;
18839 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18841 static int deduction_depth
;
18842 struct pending_template
*old_last_pend
= last_pending_template
;
18843 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18846 if (flag_new_inheriting_ctors
)
18847 fn
= strip_inheriting_ctors (fn
);
18849 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18851 tree r
= error_mark_node
;
18853 tree full_targs
= targs
;
18854 if (TMPL_ARGS_DEPTH (targs
)
18855 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18856 full_targs
= (add_outermost_template_args
18857 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18861 complain
|= tf_decltype
;
18863 /* In C++0x, it's possible to have a function template whose type depends
18864 on itself recursively. This is most obvious with decltype, but can also
18865 occur with enumeration scope (c++/48969). So we need to catch infinite
18866 recursion and reject the substitution at deduction time; this function
18867 will return error_mark_node for any repeated substitution.
18869 This also catches excessive recursion such as when f<N> depends on
18870 f<N-1> across all integers, and returns error_mark_node for all the
18871 substitutions back up to the initial one.
18873 This is, of course, not reentrant. */
18874 if (excessive_deduction_depth
)
18875 return error_mark_node
;
18876 tinst
= build_tree_list (fn
, NULL_TREE
);
18879 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18881 fntype
= TREE_TYPE (fn
);
18882 if (explicit_targs
)
18886 The specified template arguments must match the template
18887 parameters in kind (i.e., type, nontype, template), and there
18888 must not be more arguments than there are parameters;
18889 otherwise type deduction fails.
18891 Nontype arguments must match the types of the corresponding
18892 nontype template parameters, or must be convertible to the
18893 types of the corresponding nontype parameters as specified in
18894 _temp.arg.nontype_, otherwise type deduction fails.
18896 All references in the function type of the function template
18897 to the corresponding template parameters are replaced by the
18898 specified template argument values. If a substitution in a
18899 template parameter or in the function type of the function
18900 template results in an invalid type, type deduction fails. */
18901 int i
, len
= TREE_VEC_LENGTH (tparms
);
18902 location_t loc
= input_location
;
18903 bool incomplete
= false;
18905 if (explicit_targs
== error_mark_node
)
18908 if (TMPL_ARGS_DEPTH (explicit_targs
)
18909 < TMPL_ARGS_DEPTH (full_targs
))
18910 explicit_targs
= add_outermost_template_args (full_targs
,
18913 /* Adjust any explicit template arguments before entering the
18914 substitution context. */
18916 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18918 /*require_all_args=*/false,
18919 /*use_default_args=*/false));
18920 if (explicit_targs
== error_mark_node
)
18923 /* Substitute the explicit args into the function type. This is
18924 necessary so that, for instance, explicitly declared function
18925 arguments can match null pointed constants. If we were given
18926 an incomplete set of explicit args, we must not do semantic
18927 processing during substitution as we could create partial
18929 for (i
= 0; i
< len
; i
++)
18931 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18932 bool parameter_pack
= false;
18933 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18935 /* Dig out the actual parm. */
18936 if (TREE_CODE (parm
) == TYPE_DECL
18937 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18939 parm
= TREE_TYPE (parm
);
18940 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18942 else if (TREE_CODE (parm
) == PARM_DECL
)
18944 parm
= DECL_INITIAL (parm
);
18945 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18948 if (!parameter_pack
&& targ
== NULL_TREE
)
18949 /* No explicit argument for this template parameter. */
18952 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18954 /* Mark the argument pack as "incomplete". We could
18955 still deduce more arguments during unification.
18956 We remove this mark in type_unification_real. */
18959 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18960 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18961 = ARGUMENT_PACK_ARGS (targ
);
18964 /* We have some incomplete argument packs. */
18969 TREE_VALUE (tinst
) = explicit_targs
;
18970 if (!push_tinst_level (tinst
))
18972 excessive_deduction_depth
= true;
18975 processing_template_decl
+= incomplete
;
18976 input_location
= DECL_SOURCE_LOCATION (fn
);
18977 /* Ignore any access checks; we'll see them again in
18978 instantiate_template and they might have the wrong
18979 access path at this point. */
18980 push_deferring_access_checks (dk_deferred
);
18981 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18982 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18983 pop_deferring_access_checks ();
18984 input_location
= loc
;
18985 processing_template_decl
-= incomplete
;
18986 pop_tinst_level ();
18988 if (fntype
== error_mark_node
)
18991 /* Place the explicitly specified arguments in TARGS. */
18992 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18993 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18994 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18997 /* Never do unification on the 'this' parameter. */
18998 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
19000 if (return_type
&& strict
== DEDUCE_CALL
)
19002 /* We're deducing for a call to the result of a template conversion
19003 function. The parms we really want are in return_type. */
19004 if (POINTER_TYPE_P (return_type
))
19005 return_type
= TREE_TYPE (return_type
);
19006 parms
= TYPE_ARG_TYPES (return_type
);
19008 else if (return_type
)
19012 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19013 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19014 new_args
[0] = return_type
;
19015 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19020 /* We allow incomplete unification without an error message here
19021 because the standard doesn't seem to explicitly prohibit it. Our
19022 callers must be ready to deal with unification failures in any
19025 TREE_VALUE (tinst
) = targs
;
19026 /* If we aren't explaining yet, push tinst context so we can see where
19027 any errors (e.g. from class instantiations triggered by instantiation
19028 of default template arguments) come from. If we are explaining, this
19029 context is redundant. */
19030 if (!explain_p
&& !push_tinst_level (tinst
))
19032 excessive_deduction_depth
= true;
19036 /* type_unification_real will pass back any access checks from default
19037 template argument substitution. */
19038 vec
<deferred_access_check
, va_gc
> *checks
;
19041 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19042 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19043 strict
, flags
, &checks
, explain_p
);
19045 pop_tinst_level ();
19049 /* Now that we have bindings for all of the template arguments,
19050 ensure that the arguments deduced for the template template
19051 parameters have compatible template parameter lists. We cannot
19052 check this property before we have deduced all template
19053 arguments, because the template parameter types of a template
19054 template parameter might depend on prior template parameters
19055 deduced after the template template parameter. The following
19056 ill-formed example illustrates this issue:
19058 template<typename T, template<T> class C> void f(C<5>, T);
19060 template<int N> struct X {};
19063 f(X<5>(), 5l); // error: template argument deduction fails
19066 The template parameter list of 'C' depends on the template type
19067 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19068 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19069 time that we deduce 'C'. */
19070 if (!template_template_parm_bindings_ok_p
19071 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19073 unify_inconsistent_template_template_parameters (explain_p
);
19077 /* All is well so far. Now, check:
19081 When all template arguments have been deduced, all uses of
19082 template parameters in nondeduced contexts are replaced with
19083 the corresponding deduced argument values. If the
19084 substitution results in an invalid type, as described above,
19085 type deduction fails. */
19086 TREE_VALUE (tinst
) = targs
;
19087 if (!push_tinst_level (tinst
))
19089 excessive_deduction_depth
= true;
19093 /* Also collect access checks from the instantiation. */
19094 reopen_deferring_access_checks (checks
);
19096 decl
= instantiate_template (fn
, targs
, complain
);
19098 checks
= get_deferred_access_checks ();
19099 pop_deferring_access_checks ();
19101 pop_tinst_level ();
19103 if (decl
== error_mark_node
)
19106 /* Now perform any access checks encountered during substitution. */
19107 push_access_scope (decl
);
19108 ok
= perform_access_checks (checks
, complain
);
19109 pop_access_scope (decl
);
19113 /* If we're looking for an exact match, check that what we got
19114 is indeed an exact match. It might not be if some template
19115 parameters are used in non-deduced contexts. But don't check
19116 for an exact match if we have dependent template arguments;
19117 in that case we're doing partial ordering, and we already know
19118 that we have two candidates that will provide the actual type. */
19119 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19121 tree substed
= TREE_TYPE (decl
);
19125 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19127 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19128 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19129 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19131 unify_type_mismatch (explain_p
, args
[i
],
19132 TREE_VALUE (sarg
));
19137 /* After doing deduction with the inherited constructor, actually return an
19138 instantiation of the inheriting constructor. */
19140 decl
= instantiate_template (orig_fn
, targs
, complain
);
19146 if (excessive_deduction_depth
)
19148 if (deduction_depth
== 0)
19149 /* Reset once we're all the way out. */
19150 excessive_deduction_depth
= false;
19153 /* We can't free this if a pending_template entry or last_error_tinst_level
19154 is pointing at it. */
19155 if (last_pending_template
== old_last_pend
19156 && last_error_tinst_level
== old_error_tinst
)
19162 /* Adjust types before performing type deduction, as described in
19163 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19164 sections are symmetric. PARM is the type of a function parameter
19165 or the return type of the conversion function. ARG is the type of
19166 the argument passed to the call, or the type of the value
19167 initialized with the result of the conversion function.
19168 ARG_EXPR is the original argument expression, which may be null. */
19171 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19184 /* Swap PARM and ARG throughout the remainder of this
19185 function; the handling is precisely symmetric since PARM
19186 will initialize ARG rather than vice versa. */
19187 std::swap (parm
, arg
);
19191 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19192 too, but here handle it by stripping the reference from PARM
19193 rather than by adding it to ARG. */
19194 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19195 && TYPE_REF_IS_RVALUE (*parm
)
19196 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19197 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19198 && TREE_CODE (*arg
) == REFERENCE_TYPE
19199 && !TYPE_REF_IS_RVALUE (*arg
))
19200 *parm
= TREE_TYPE (*parm
);
19201 /* Nothing else to do in this case. */
19205 gcc_unreachable ();
19208 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19210 /* [temp.deduct.call]
19212 If P is not a reference type:
19214 --If A is an array type, the pointer type produced by the
19215 array-to-pointer standard conversion (_conv.array_) is
19216 used in place of A for type deduction; otherwise,
19218 --If A is a function type, the pointer type produced by
19219 the function-to-pointer standard conversion
19220 (_conv.func_) is used in place of A for type deduction;
19223 --If A is a cv-qualified type, the top level
19224 cv-qualifiers of A's type are ignored for type
19226 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19227 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19228 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19229 *arg
= build_pointer_type (*arg
);
19231 *arg
= TYPE_MAIN_VARIANT (*arg
);
19234 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19235 reference to a cv-unqualified template parameter that does not represent a
19236 template parameter of a class template (during class template argument
19237 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19238 an lvalue, the type "lvalue reference to A" is used in place of A for type
19240 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19241 && TYPE_REF_IS_RVALUE (*parm
)
19242 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19243 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19244 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19245 && (arg_expr
? lvalue_p (arg_expr
)
19246 /* try_one_overload doesn't provide an arg_expr, but
19247 functions are always lvalues. */
19248 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19249 *arg
= build_reference_type (*arg
);
19251 /* [temp.deduct.call]
19253 If P is a cv-qualified type, the top level cv-qualifiers
19254 of P's type are ignored for type deduction. If P is a
19255 reference type, the type referred to by P is used for
19257 *parm
= TYPE_MAIN_VARIANT (*parm
);
19258 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19260 *parm
= TREE_TYPE (*parm
);
19261 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19264 /* DR 322. For conversion deduction, remove a reference type on parm
19265 too (which has been swapped into ARG). */
19266 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19267 *arg
= TREE_TYPE (*arg
);
19272 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19273 template which does contain any deducible template parameters; check if
19274 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19275 unify_one_argument. */
19278 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19279 int flags
, bool explain_p
)
19284 type
= TREE_TYPE (arg
);
19288 if (same_type_p (parm
, type
))
19289 return unify_success (explain_p
);
19291 if (strict
== DEDUCE_CONV
)
19293 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19294 explain_p
? tf_warning_or_error
: tf_none
))
19295 return unify_success (explain_p
);
19297 else if (strict
!= DEDUCE_EXACT
)
19299 if (can_convert_arg (parm
, type
,
19300 TYPE_P (arg
) ? NULL_TREE
: arg
,
19301 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19302 return unify_success (explain_p
);
19305 if (strict
== DEDUCE_EXACT
)
19306 return unify_type_mismatch (explain_p
, parm
, arg
);
19308 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19311 static bool uses_deducible_template_parms (tree type
);
19313 /* Returns true iff the expression EXPR is one from which a template
19314 argument can be deduced. In other words, if it's an undecorated
19315 use of a template non-type parameter. */
19318 deducible_expression (tree expr
)
19320 /* Strip implicit conversions. */
19321 while (CONVERT_EXPR_P (expr
))
19322 expr
= TREE_OPERAND (expr
, 0);
19323 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19326 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19327 deducible way; that is, if it has a max value of <PARM> - 1. */
19330 deducible_array_bound (tree domain
)
19332 if (domain
== NULL_TREE
)
19335 tree max
= TYPE_MAX_VALUE (domain
);
19336 if (TREE_CODE (max
) != MINUS_EXPR
)
19339 return deducible_expression (TREE_OPERAND (max
, 0));
19342 /* Returns true iff the template arguments ARGS use a template parameter
19343 in a deducible way. */
19346 deducible_template_args (tree args
)
19348 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19351 tree elt
= TREE_VEC_ELT (args
, i
);
19352 if (ARGUMENT_PACK_P (elt
))
19353 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19356 if (PACK_EXPANSION_P (elt
))
19357 elt
= PACK_EXPANSION_PATTERN (elt
);
19358 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19360 else if (TYPE_P (elt
))
19361 deducible
= uses_deducible_template_parms (elt
);
19363 deducible
= deducible_expression (elt
);
19371 /* Returns true iff TYPE contains any deducible references to template
19372 parameters, as per 14.8.2.5. */
19375 uses_deducible_template_parms (tree type
)
19377 if (PACK_EXPANSION_P (type
))
19378 type
= PACK_EXPANSION_PATTERN (type
);
19385 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19386 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19392 if (POINTER_TYPE_P (type
))
19393 return uses_deducible_template_parms (TREE_TYPE (type
));
19395 /* T[integer-constant ]
19397 if (TREE_CODE (type
) == ARRAY_TYPE
)
19398 return (uses_deducible_template_parms (TREE_TYPE (type
))
19399 || deducible_array_bound (TYPE_DOMAIN (type
)));
19411 if (TYPE_PTRMEM_P (type
))
19412 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19413 || (uses_deducible_template_parms
19414 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19416 /* template-name <T> (where template-name refers to a class template)
19417 template-name <i> (where template-name refers to a class template) */
19418 if (CLASS_TYPE_P (type
)
19419 && CLASSTYPE_TEMPLATE_INFO (type
)
19420 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19421 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19422 (CLASSTYPE_TI_ARGS (type
)));
19427 if (TREE_CODE (type
) == FUNCTION_TYPE
19428 || TREE_CODE (type
) == METHOD_TYPE
)
19430 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19432 tree parm
= TYPE_ARG_TYPES (type
);
19433 if (TREE_CODE (type
) == METHOD_TYPE
)
19434 parm
= TREE_CHAIN (parm
);
19435 for (; parm
; parm
= TREE_CHAIN (parm
))
19436 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19443 /* Subroutine of type_unification_real and unify_pack_expansion to
19444 handle unification of a single P/A pair. Parameters are as
19445 for those functions. */
19448 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19449 int subr
, unification_kind_t strict
,
19452 tree arg_expr
= NULL_TREE
;
19455 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19456 return unify_invalid (explain_p
);
19457 if (arg
== unknown_type_node
)
19458 /* We can't deduce anything from this, but we might get all the
19459 template args from other function args. */
19460 return unify_success (explain_p
);
19462 /* Implicit conversions (Clause 4) will be performed on a function
19463 argument to convert it to the type of the corresponding function
19464 parameter if the parameter type contains no template-parameters that
19465 participate in template argument deduction. */
19466 if (strict
!= DEDUCE_EXACT
19467 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19468 /* For function parameters with no deducible template parameters,
19469 just return. We'll check non-dependent conversions later. */
19470 return unify_success (explain_p
);
19475 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19476 | UNIFY_ALLOW_MORE_CV_QUAL
19477 | UNIFY_ALLOW_DERIVED
);
19481 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19485 arg_strict
= UNIFY_ALLOW_NONE
;
19489 gcc_unreachable ();
19492 /* We only do these transformations if this is the top-level
19493 parameter_type_list in a call or declaration matching; in other
19494 situations (nested function declarators, template argument lists) we
19495 won't be comparing a type to an expression, and we don't do any type
19501 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19502 if (type_unknown_p (arg
))
19504 /* [temp.deduct.type] A template-argument can be
19505 deduced from a pointer to function or pointer
19506 to member function argument if the set of
19507 overloaded functions does not contain function
19508 templates and at most one of a set of
19509 overloaded functions provides a unique
19511 resolve_overloaded_unification (tparms
, targs
, parm
,
19513 arg_strict
, explain_p
);
19514 /* If a unique match was not found, this is a
19515 non-deduced context, so we still succeed. */
19516 return unify_success (explain_p
);
19520 arg
= unlowered_expr_type (arg
);
19521 if (arg
== error_mark_node
)
19522 return unify_invalid (explain_p
);
19526 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19529 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19530 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19531 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19533 /* For deduction from an init-list we need the actual list. */
19534 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19536 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19539 /* for_each_template_parm callback that always returns 0. */
19542 zero_r (tree
, void *)
19547 /* for_each_template_parm any_fn callback to handle deduction of a template
19548 type argument from the type of an array bound. */
19551 array_deduction_r (tree t
, void *data
)
19553 tree_pair_p d
= (tree_pair_p
)data
;
19554 tree
&tparms
= d
->purpose
;
19555 tree
&targs
= d
->value
;
19557 if (TREE_CODE (t
) == ARRAY_TYPE
)
19558 if (tree dom
= TYPE_DOMAIN (t
))
19559 if (tree max
= TYPE_MAX_VALUE (dom
))
19561 if (TREE_CODE (max
) == MINUS_EXPR
)
19562 max
= TREE_OPERAND (max
, 0);
19563 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19564 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19565 UNIFY_ALLOW_NONE
, /*explain*/false);
19568 /* Keep walking. */
19572 /* Try to deduce any not-yet-deduced template type arguments from the type of
19573 an array bound. This is handled separately from unify because 14.8.2.5 says
19574 "The type of a type parameter is only deduced from an array bound if it is
19575 not otherwise deduced." */
19578 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19580 tree_pair_s data
= { tparms
, targs
};
19581 hash_set
<tree
> visited
;
19582 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19583 /*nondeduced*/false, array_deduction_r
);
19586 /* Most parms like fn_type_unification.
19588 If SUBR is 1, we're being called recursively (to unify the
19589 arguments of a function or method parameter of a function
19592 CHECKS is a pointer to a vector of access checks encountered while
19593 substituting default template arguments. */
19596 type_unification_real (tree tparms
,
19600 unsigned int xnargs
,
19602 unification_kind_t strict
,
19604 vec
<deferred_access_check
, va_gc
> **checks
,
19609 int ntparms
= TREE_VEC_LENGTH (tparms
);
19610 int saw_undeduced
= 0;
19613 unsigned int nargs
;
19616 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19617 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19618 gcc_assert (ntparms
> 0);
19620 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19622 /* Reset the number of non-defaulted template arguments contained
19624 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19632 while (parms
&& parms
!= void_list_node
19635 parm
= TREE_VALUE (parms
);
19637 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19638 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19639 /* For a function parameter pack that occurs at the end of the
19640 parameter-declaration-list, the type A of each remaining
19641 argument of the call is compared with the type P of the
19642 declarator-id of the function parameter pack. */
19645 parms
= TREE_CHAIN (parms
);
19647 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19648 /* For a function parameter pack that does not occur at the
19649 end of the parameter-declaration-list, the type of the
19650 parameter pack is a non-deduced context. */
19656 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19662 && parms
!= void_list_node
19663 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19665 /* Unify the remaining arguments with the pack expansion type. */
19667 tree parmvec
= make_tree_vec (1);
19669 /* Allocate a TREE_VEC and copy in all of the arguments */
19670 argvec
= make_tree_vec (nargs
- ia
);
19671 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19672 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19674 /* Copy the parameter into parmvec. */
19675 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19676 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19677 /*subr=*/subr
, explain_p
))
19680 /* Advance to the end of the list of parameters. */
19681 parms
= TREE_CHAIN (parms
);
19684 /* Fail if we've reached the end of the parm list, and more args
19685 are present, and the parm list isn't variadic. */
19686 if (ia
< nargs
&& parms
== void_list_node
)
19687 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19688 /* Fail if parms are left and they don't have default values and
19689 they aren't all deduced as empty packs (c++/57397). This is
19690 consistent with sufficient_parms_p. */
19691 if (parms
&& parms
!= void_list_node
19692 && TREE_PURPOSE (parms
) == NULL_TREE
)
19694 unsigned int count
= nargs
;
19699 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19702 p
= TREE_CHAIN (p
);
19704 while (p
&& p
!= void_list_node
);
19705 if (count
!= nargs
)
19706 return unify_too_few_arguments (explain_p
, ia
, count
,
19712 tsubst_flags_t complain
= (explain_p
19713 ? tf_warning_or_error
19715 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19717 for (i
= 0; i
< ntparms
; i
++)
19719 tree targ
= TREE_VEC_ELT (targs
, i
);
19720 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19722 /* Clear the "incomplete" flags on all argument packs now so that
19723 substituting them into later default arguments works. */
19724 if (targ
&& ARGUMENT_PACK_P (targ
))
19726 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19727 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19730 if (targ
|| tparm
== error_mark_node
)
19732 tparm
= TREE_VALUE (tparm
);
19734 if (TREE_CODE (tparm
) == TYPE_DECL
19735 && !tried_array_deduction
)
19737 try_array_deduction (tparms
, targs
, xparms
);
19738 tried_array_deduction
= true;
19739 if (TREE_VEC_ELT (targs
, i
))
19743 /* If this is an undeduced nontype parameter that depends on
19744 a type parameter, try another pass; its type may have been
19745 deduced from a later argument than the one from which
19746 this parameter can be deduced. */
19747 if (TREE_CODE (tparm
) == PARM_DECL
19748 && uses_template_parms (TREE_TYPE (tparm
))
19749 && saw_undeduced
< 2)
19755 /* Core issue #226 (C++0x) [temp.deduct]:
19757 If a template argument has not been deduced, its
19758 default template argument, if any, is used.
19760 When we are in C++98 mode, TREE_PURPOSE will either
19761 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19762 to explicitly check cxx_dialect here. */
19763 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19764 /* OK, there is a default argument. Wait until after the
19765 conversion check to do substitution. */
19768 /* If the type parameter is a parameter pack, then it will
19769 be deduced to an empty parameter pack. */
19770 if (template_parameter_pack_p (tparm
))
19774 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19776 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19777 TREE_CONSTANT (arg
) = 1;
19780 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19782 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19784 TREE_VEC_ELT (targs
, i
) = arg
;
19788 return unify_parameter_deduction_failure (explain_p
, tparm
);
19791 /* DR 1391: All parameters have args, now check non-dependent parms for
19793 if (saw_undeduced
< 2)
19794 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19795 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19797 parm
= TREE_VALUE (parms
);
19799 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19800 && (!TREE_CHAIN (parms
)
19801 || TREE_CHAIN (parms
) == void_list_node
))
19802 /* For a function parameter pack that occurs at the end of the
19803 parameter-declaration-list, the type A of each remaining
19804 argument of the call is compared with the type P of the
19805 declarator-id of the function parameter pack. */
19808 parms
= TREE_CHAIN (parms
);
19810 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19811 /* For a function parameter pack that does not occur at the
19812 end of the parameter-declaration-list, the type of the
19813 parameter pack is a non-deduced context. */
19819 if (uses_template_parms (parm
))
19821 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19826 /* Now substitute into the default template arguments. */
19827 for (i
= 0; i
< ntparms
; i
++)
19829 tree targ
= TREE_VEC_ELT (targs
, i
);
19830 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19832 if (targ
|| tparm
== error_mark_node
)
19834 tree parm
= TREE_VALUE (tparm
);
19836 if (TREE_CODE (parm
) == PARM_DECL
19837 && uses_template_parms (TREE_TYPE (parm
))
19838 && saw_undeduced
< 2)
19841 tree arg
= TREE_PURPOSE (tparm
);
19842 reopen_deferring_access_checks (*checks
);
19843 location_t save_loc
= input_location
;
19845 input_location
= DECL_SOURCE_LOCATION (parm
);
19846 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19847 if (!uses_template_parms (arg
))
19848 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19850 else if (saw_undeduced
< 2)
19853 arg
= error_mark_node
;
19854 input_location
= save_loc
;
19855 *checks
= get_deferred_access_checks ();
19856 pop_deferring_access_checks ();
19857 if (arg
== error_mark_node
)
19861 TREE_VEC_ELT (targs
, i
) = arg
;
19862 /* The position of the first default template argument,
19863 is also the number of non-defaulted arguments in TARGS.
19865 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19866 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19870 if (saw_undeduced
++ == 1)
19874 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19875 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19877 return unify_success (explain_p
);
19880 /* Subroutine of type_unification_real. Args are like the variables
19881 at the call site. ARG is an overloaded function (or template-id);
19882 we try deducing template args from each of the overloads, and if
19883 only one succeeds, we go with that. Modifies TARGS and returns
19884 true on success. */
19887 resolve_overloaded_unification (tree tparms
,
19891 unification_kind_t strict
,
19895 tree tempargs
= copy_node (targs
);
19897 tree goodfn
= NULL_TREE
;
19900 if (TREE_CODE (arg
) == ADDR_EXPR
)
19902 arg
= TREE_OPERAND (arg
, 0);
19908 if (TREE_CODE (arg
) == COMPONENT_REF
)
19909 /* Handle `&x' where `x' is some static or non-static member
19911 arg
= TREE_OPERAND (arg
, 1);
19913 if (TREE_CODE (arg
) == OFFSET_REF
)
19914 arg
= TREE_OPERAND (arg
, 1);
19916 /* Strip baselink information. */
19917 if (BASELINK_P (arg
))
19918 arg
= BASELINK_FUNCTIONS (arg
);
19920 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19922 /* If we got some explicit template args, we need to plug them into
19923 the affected templates before we try to unify, in case the
19924 explicit args will completely resolve the templates in question. */
19927 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19928 arg
= TREE_OPERAND (arg
, 0);
19930 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19933 tree subargs
, elem
;
19935 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19938 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19939 expl_subargs
, NULL_TREE
, tf_none
,
19940 /*require_all_args=*/true,
19941 /*use_default_args=*/true);
19942 if (subargs
!= error_mark_node
19943 && !any_dependent_template_arguments_p (subargs
))
19945 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19946 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19947 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19948 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19957 /* If no templates (or more than one) are fully resolved by the
19958 explicit arguments, this template-id is a non-deduced context; it
19959 could still be OK if we deduce all template arguments for the
19960 enclosing call through other arguments. */
19964 else if (TREE_CODE (arg
) != OVERLOAD
19965 && TREE_CODE (arg
) != FUNCTION_DECL
)
19966 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19967 -- but the deduction does not succeed because the expression is
19968 not just the function on its own. */
19971 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19974 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19975 strict
, sub_strict
, addr_p
, explain_p
)
19976 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19983 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19984 to function or pointer to member function argument if the set of
19985 overloaded functions does not contain function templates and at most
19986 one of a set of overloaded functions provides a unique match.
19988 So if we found multiple possibilities, we return success but don't
19989 deduce anything. */
19993 int i
= TREE_VEC_LENGTH (targs
);
19995 if (TREE_VEC_ELT (tempargs
, i
))
19997 tree old
= TREE_VEC_ELT (targs
, i
);
19998 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19999 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
20000 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
20001 /* Don't forget explicit template arguments in a pack. */
20002 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
20003 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
20004 TREE_VEC_ELT (targs
, i
) = new_
;
20013 /* Core DR 115: In contexts where deduction is done and fails, or in
20014 contexts where deduction is not done, if a template argument list is
20015 specified and it, along with any default template arguments, identifies
20016 a single function template specialization, then the template-id is an
20017 lvalue for the function template specialization. */
20020 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20022 tree expr
, offset
, baselink
;
20025 if (!type_unknown_p (orig_expr
))
20030 offset
= NULL_TREE
;
20031 baselink
= NULL_TREE
;
20033 if (TREE_CODE (expr
) == ADDR_EXPR
)
20035 expr
= TREE_OPERAND (expr
, 0);
20038 if (TREE_CODE (expr
) == OFFSET_REF
)
20041 expr
= TREE_OPERAND (expr
, 1);
20043 if (BASELINK_P (expr
))
20046 expr
= BASELINK_FUNCTIONS (expr
);
20049 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20052 tree goodfn
= NULL_TREE
;
20054 /* If we got some explicit template args, we need to plug them into
20055 the affected templates before we try to unify, in case the
20056 explicit args will completely resolve the templates in question. */
20058 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20059 tree arg
= TREE_OPERAND (expr
, 0);
20060 tree badfn
= NULL_TREE
;
20061 tree badargs
= NULL_TREE
;
20063 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20066 tree subargs
, elem
;
20068 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20071 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20072 expl_subargs
, NULL_TREE
, tf_none
,
20073 /*require_all_args=*/true,
20074 /*use_default_args=*/true);
20075 if (subargs
!= error_mark_node
20076 && !any_dependent_template_arguments_p (subargs
))
20078 elem
= instantiate_template (fn
, subargs
, tf_none
);
20079 if (elem
== error_mark_node
)
20084 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20093 mark_used (goodfn
);
20096 expr
= build_baselink (BASELINK_BINFO (baselink
),
20097 BASELINK_ACCESS_BINFO (baselink
),
20098 expr
, BASELINK_OPTYPE (baselink
));
20102 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20103 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20106 expr
= cp_build_addr_expr (expr
, complain
);
20109 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20110 /* There were no good options and at least one bad one, so let the
20111 user know what the problem is. */
20112 instantiate_template (badfn
, badargs
, complain
);
20117 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20118 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20119 different overloads deduce different arguments for a given parm.
20120 ADDR_P is true if the expression for which deduction is being
20121 performed was of the form "& fn" rather than simply "fn".
20123 Returns 1 on success. */
20126 try_one_overload (tree tparms
,
20131 unification_kind_t strict
,
20140 if (arg
== error_mark_node
)
20143 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20144 to function or pointer to member function argument if the set of
20145 overloaded functions does not contain function templates and at most
20146 one of a set of overloaded functions provides a unique match.
20148 So if this is a template, just return success. */
20150 if (uses_template_parms (arg
))
20153 if (TREE_CODE (arg
) == METHOD_TYPE
)
20154 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20156 arg
= build_pointer_type (arg
);
20158 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20160 /* We don't copy orig_targs for this because if we have already deduced
20161 some template args from previous args, unify would complain when we
20162 try to deduce a template parameter for the same argument, even though
20163 there isn't really a conflict. */
20164 nargs
= TREE_VEC_LENGTH (targs
);
20165 tempargs
= make_tree_vec (nargs
);
20167 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20170 /* First make sure we didn't deduce anything that conflicts with
20171 explicitly specified args. */
20172 for (i
= nargs
; i
--; )
20174 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20175 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20179 else if (uses_template_parms (elt
))
20180 /* Since we're unifying against ourselves, we will fill in
20181 template args used in the function parm list with our own
20182 template parms. Discard them. */
20183 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20184 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20186 /* Check that the argument at each index of the deduced argument pack
20187 is equivalent to the corresponding explicitly specified argument.
20188 We may have deduced more arguments than were explicitly specified,
20191 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20192 that's wrong if we deduce the same argument pack from multiple
20193 function arguments: it's only incomplete the first time. */
20195 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20196 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20198 if (TREE_VEC_LENGTH (deduced_pack
)
20199 < TREE_VEC_LENGTH (explicit_pack
))
20202 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20203 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20204 TREE_VEC_ELT (deduced_pack
, j
)))
20207 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20211 for (i
= nargs
; i
--; )
20213 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20216 TREE_VEC_ELT (targs
, i
) = elt
;
20222 /* PARM is a template class (perhaps with unbound template
20223 parameters). ARG is a fully instantiated type. If ARG can be
20224 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20225 TARGS are as for unify. */
20228 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20231 tree copy_of_targs
;
20233 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20235 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20236 /* Matches anything. */;
20237 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20238 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20241 /* We need to make a new template argument vector for the call to
20242 unify. If we used TARGS, we'd clutter it up with the result of
20243 the attempted unification, even if this class didn't work out.
20244 We also don't want to commit ourselves to all the unifications
20245 we've already done, since unification is supposed to be done on
20246 an argument-by-argument basis. In other words, consider the
20247 following pathological case:
20249 template <int I, int J, int K>
20252 template <int I, int J>
20253 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20255 template <int I, int J, int K>
20256 void f(S<I, J, K>, S<I, I, I>);
20265 Now, by the time we consider the unification involving `s2', we
20266 already know that we must have `f<0, 0, 0>'. But, even though
20267 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20268 because there are two ways to unify base classes of S<0, 1, 2>
20269 with S<I, I, I>. If we kept the already deduced knowledge, we
20270 would reject the possibility I=1. */
20271 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20273 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20275 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20280 /* If unification failed, we're done. */
20281 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20282 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20288 /* Given a template type PARM and a class type ARG, find the unique
20289 base type in ARG that is an instance of PARM. We do not examine
20290 ARG itself; only its base-classes. If there is not exactly one
20291 appropriate base class, return NULL_TREE. PARM may be the type of
20292 a partial specialization, as well as a plain template type. Used
20295 static enum template_base_result
20296 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20297 bool explain_p
, tree
*result
)
20299 tree rval
= NULL_TREE
;
20302 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20304 binfo
= TYPE_BINFO (complete_type (arg
));
20307 /* The type could not be completed. */
20308 *result
= NULL_TREE
;
20309 return tbr_incomplete_type
;
20312 /* Walk in inheritance graph order. The search order is not
20313 important, and this avoids multiple walks of virtual bases. */
20314 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20316 tree r
= try_class_unification (tparms
, targs
, parm
,
20317 BINFO_TYPE (binfo
), explain_p
);
20321 /* If there is more than one satisfactory baseclass, then:
20325 If they yield more than one possible deduced A, the type
20329 if (rval
&& !same_type_p (r
, rval
))
20331 *result
= NULL_TREE
;
20332 return tbr_ambiguous_baseclass
;
20340 return tbr_success
;
20343 /* Returns the level of DECL, which declares a template parameter. */
20346 template_decl_level (tree decl
)
20348 switch (TREE_CODE (decl
))
20351 case TEMPLATE_DECL
:
20352 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20355 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20358 gcc_unreachable ();
20363 /* Decide whether ARG can be unified with PARM, considering only the
20364 cv-qualifiers of each type, given STRICT as documented for unify.
20365 Returns nonzero iff the unification is OK on that basis. */
20368 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20370 int arg_quals
= cp_type_quals (arg
);
20371 int parm_quals
= cp_type_quals (parm
);
20373 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20374 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20376 /* Although a CVR qualifier is ignored when being applied to a
20377 substituted template parameter ([8.3.2]/1 for example), that
20378 does not allow us to unify "const T" with "int&" because both
20379 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20380 It is ok when we're allowing additional CV qualifiers
20381 at the outer level [14.8.2.1]/3,1st bullet. */
20382 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20383 || TREE_CODE (arg
) == FUNCTION_TYPE
20384 || TREE_CODE (arg
) == METHOD_TYPE
)
20385 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20388 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20389 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20393 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20394 && (arg_quals
& parm_quals
) != parm_quals
)
20397 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20398 && (parm_quals
& arg_quals
) != arg_quals
)
20404 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20406 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20408 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20409 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20410 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20412 *index
= TEMPLATE_TYPE_IDX (parm
);
20413 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20417 *index
= TEMPLATE_PARM_IDX (parm
);
20418 *level
= TEMPLATE_PARM_LEVEL (parm
);
20422 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20424 if (unify (TP, TA, P, A, S, EP)) \
20428 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20429 expansion at the end of PACKED_PARMS. Returns 0 if the type
20430 deduction succeeds, 1 otherwise. STRICT is the same as in
20431 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20432 function call argument list. We'll need to adjust the arguments to make them
20433 types. SUBR tells us if this is from a recursive call to
20434 type_unification_real, or for comparing two template argument
20438 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20439 tree packed_args
, unification_kind_t strict
,
20440 bool subr
, bool explain_p
)
20443 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20444 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20445 tree pack
, packs
= NULL_TREE
;
20446 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20448 /* Add in any args remembered from an earlier partial instantiation. */
20449 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20450 int levels
= TMPL_ARGS_DEPTH (targs
);
20452 packed_args
= expand_template_argument_pack (packed_args
);
20454 int len
= TREE_VEC_LENGTH (packed_args
);
20456 /* Determine the parameter packs we will be deducing from the
20457 pattern, and record their current deductions. */
20458 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20459 pack
; pack
= TREE_CHAIN (pack
))
20461 tree parm_pack
= TREE_VALUE (pack
);
20464 /* Determine the index and level of this parameter pack. */
20465 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20466 if (level
< levels
)
20469 /* Keep track of the parameter packs and their corresponding
20471 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20472 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20475 /* Loop through all of the arguments that have not yet been
20476 unified and unify each with the pattern. */
20477 for (i
= start
; i
< len
; i
++)
20480 bool any_explicit
= false;
20481 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20483 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20484 or the element of its argument pack at the current index if
20485 this argument was explicitly specified. */
20486 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20490 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20493 if (TREE_VALUE (pack
)
20494 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20495 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20497 any_explicit
= true;
20498 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20500 TMPL_ARG (targs
, level
, idx
) = arg
;
20503 /* If we had explicit template arguments, substitute them into the
20504 pattern before deduction. */
20507 /* Some arguments might still be unspecified or dependent. */
20509 ++processing_template_decl
;
20510 dependent
= any_dependent_template_arguments_p (targs
);
20512 --processing_template_decl
;
20513 parm
= tsubst (pattern
, targs
,
20514 explain_p
? tf_warning_or_error
: tf_none
,
20517 --processing_template_decl
;
20518 if (parm
== error_mark_node
)
20524 /* Unify the pattern with the current argument. */
20525 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20529 /* For each parameter pack, collect the deduced value. */
20530 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20533 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20535 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20536 TMPL_ARG (targs
, level
, idx
);
20540 /* Verify that the results of unification with the parameter packs
20541 produce results consistent with what we've seen before, and make
20542 the deduced argument packs available. */
20543 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20545 tree old_pack
= TREE_VALUE (pack
);
20546 tree new_args
= TREE_TYPE (pack
);
20547 int i
, len
= TREE_VEC_LENGTH (new_args
);
20549 bool nondeduced_p
= false;
20551 /* By default keep the original deduced argument pack.
20552 If necessary, more specific code is going to update the
20553 resulting deduced argument later down in this function. */
20554 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20555 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20557 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20558 actually deduce anything. */
20559 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20560 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20561 nondeduced_p
= true;
20565 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20567 /* If we had fewer function args than explicit template args,
20568 just use the explicits. */
20569 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20570 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20571 if (len
< explicit_len
)
20572 new_args
= explicit_args
;
20578 /* Build the deduced *_ARGUMENT_PACK. */
20579 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20581 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20582 TREE_CONSTANT (result
) = 1;
20585 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20587 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20589 /* Note the deduced argument packs for this parameter
20591 TMPL_ARG (targs
, level
, idx
) = result
;
20593 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20594 && (ARGUMENT_PACK_ARGS (old_pack
)
20595 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20597 /* We only had the explicitly-provided arguments before, but
20598 now we have a complete set of arguments. */
20599 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20601 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20602 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20603 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20607 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20608 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20610 if (!comp_template_args (old_args
, new_args
,
20611 &bad_old_arg
, &bad_new_arg
))
20612 /* Inconsistent unification of this parameter pack. */
20613 return unify_parameter_pack_inconsistent (explain_p
,
20619 return unify_success (explain_p
);
20622 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20623 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20624 parameters and return value are as for unify. */
20627 unify_array_domain (tree tparms
, tree targs
,
20628 tree parm_dom
, tree arg_dom
,
20636 /* Our representation of array types uses "N - 1" as the
20637 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20638 not an integer constant. We cannot unify arbitrarily
20639 complex expressions, so we eliminate the MINUS_EXPRs
20641 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20642 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20645 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20646 parm_max
= TREE_OPERAND (parm_max
, 0);
20648 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20649 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20652 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20653 trying to unify the type of a variable with the type
20654 of a template parameter. For example:
20656 template <unsigned int N>
20657 void f (char (&) [N]);
20664 Here, the type of the ARG will be "int [g(i)]", and
20665 may be a SAVE_EXPR, etc. */
20666 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20667 return unify_vla_arg (explain_p
, arg_dom
);
20668 arg_max
= TREE_OPERAND (arg_max
, 0);
20671 /* If only one of the bounds used a MINUS_EXPR, compensate
20672 by adding one to the other bound. */
20673 if (parm_cst
&& !arg_cst
)
20674 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20678 else if (arg_cst
&& !parm_cst
)
20679 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20684 return unify (tparms
, targs
, parm_max
, arg_max
,
20685 UNIFY_ALLOW_INTEGER
, explain_p
);
20688 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20690 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20695 if (PACK_EXPANSION_P (t
))
20696 t
= PACK_EXPANSION_PATTERN (t
);
20697 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20698 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20699 || DECL_TYPE_TEMPLATE_P (t
))
20701 else if (TYPE_P (t
))
20707 /* Deduce the value of template parameters. TPARMS is the (innermost)
20708 set of template parameters to a template. TARGS is the bindings
20709 for those template parameters, as determined thus far; TARGS may
20710 include template arguments for outer levels of template parameters
20711 as well. PARM is a parameter to a template function, or a
20712 subcomponent of that parameter; ARG is the corresponding argument.
20713 This function attempts to match PARM with ARG in a manner
20714 consistent with the existing assignments in TARGS. If more values
20715 are deduced, then TARGS is updated.
20717 Returns 0 if the type deduction succeeds, 1 otherwise. The
20718 parameter STRICT is a bitwise or of the following flags:
20721 Require an exact match between PARM and ARG.
20722 UNIFY_ALLOW_MORE_CV_QUAL:
20723 Allow the deduced ARG to be more cv-qualified (by qualification
20724 conversion) than ARG.
20725 UNIFY_ALLOW_LESS_CV_QUAL:
20726 Allow the deduced ARG to be less cv-qualified than ARG.
20727 UNIFY_ALLOW_DERIVED:
20728 Allow the deduced ARG to be a template base class of ARG,
20729 or a pointer to a template base class of the type pointed to by
20731 UNIFY_ALLOW_INTEGER:
20732 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20733 case for more information.
20734 UNIFY_ALLOW_OUTER_LEVEL:
20735 This is the outermost level of a deduction. Used to determine validity
20736 of qualification conversions. A valid qualification conversion must
20737 have const qualified pointers leading up to the inner type which
20738 requires additional CV quals, except at the outer level, where const
20739 is not required [conv.qual]. It would be normal to set this flag in
20740 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20741 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20742 This is the outermost level of a deduction, and PARM can be more CV
20743 qualified at this point.
20744 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20745 This is the outermost level of a deduction, and PARM can be less CV
20746 qualified at this point. */
20749 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20755 int strict_in
= strict
;
20756 tsubst_flags_t complain
= (explain_p
20757 ? tf_warning_or_error
20760 /* I don't think this will do the right thing with respect to types.
20761 But the only case I've seen it in so far has been array bounds, where
20762 signedness is the only information lost, and I think that will be
20764 while (CONVERT_EXPR_P (parm
))
20765 parm
= TREE_OPERAND (parm
, 0);
20767 if (arg
== error_mark_node
)
20768 return unify_invalid (explain_p
);
20769 if (arg
== unknown_type_node
20770 || arg
== init_list_type_node
)
20771 /* We can't deduce anything from this, but we might get all the
20772 template args from other function args. */
20773 return unify_success (explain_p
);
20775 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20776 return unify_success (explain_p
);
20778 /* If PARM uses template parameters, then we can't bail out here,
20779 even if ARG == PARM, since we won't record unifications for the
20780 template parameters. We might need them if we're trying to
20781 figure out which of two things is more specialized. */
20782 if (arg
== parm
&& !uses_template_parms (parm
))
20783 return unify_success (explain_p
);
20785 /* Handle init lists early, so the rest of the function can assume
20786 we're dealing with a type. */
20787 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20791 tree orig_parm
= parm
;
20793 /* Replace T with std::initializer_list<T> for deduction. */
20794 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20795 && flag_deduce_init_list
)
20796 parm
= listify (parm
);
20798 if (!is_std_init_list (parm
)
20799 && TREE_CODE (parm
) != ARRAY_TYPE
)
20800 /* We can only deduce from an initializer list argument if the
20801 parameter is std::initializer_list or an array; otherwise this
20802 is a non-deduced context. */
20803 return unify_success (explain_p
);
20805 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20806 elttype
= TREE_TYPE (parm
);
20809 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20810 /* Deduction is defined in terms of a single type, so just punt
20811 on the (bizarre) std::initializer_list<T...>. */
20812 if (PACK_EXPANSION_P (elttype
))
20813 return unify_success (explain_p
);
20816 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20818 int elt_strict
= strict
;
20820 if (elt
== error_mark_node
)
20821 return unify_invalid (explain_p
);
20823 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20825 tree type
= TREE_TYPE (elt
);
20826 if (type
== error_mark_node
)
20827 return unify_invalid (explain_p
);
20828 /* It should only be possible to get here for a call. */
20829 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20830 elt_strict
|= maybe_adjust_types_for_deduction
20831 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20835 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20839 if (TREE_CODE (parm
) == ARRAY_TYPE
20840 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20842 /* Also deduce from the length of the initializer list. */
20843 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20844 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20845 if (idx
== error_mark_node
)
20846 return unify_invalid (explain_p
);
20847 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20851 /* If the std::initializer_list<T> deduction worked, replace the
20852 deduced A with std::initializer_list<A>. */
20853 if (orig_parm
!= parm
)
20855 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20856 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20857 targ
= listify (targ
);
20858 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20860 return unify_success (explain_p
);
20863 /* If parm and arg aren't the same kind of thing (template, type, or
20864 expression), fail early. */
20865 if (pa_kind (parm
) != pa_kind (arg
))
20866 return unify_invalid (explain_p
);
20868 /* Immediately reject some pairs that won't unify because of
20869 cv-qualification mismatches. */
20870 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20872 /* It is the elements of the array which hold the cv quals of an array
20873 type, and the elements might be template type parms. We'll check
20874 when we recurse. */
20875 && TREE_CODE (arg
) != ARRAY_TYPE
20876 /* We check the cv-qualifiers when unifying with template type
20877 parameters below. We want to allow ARG `const T' to unify with
20878 PARM `T' for example, when computing which of two templates
20879 is more specialized, for example. */
20880 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20881 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20882 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20884 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20885 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20886 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20887 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20888 strict
&= ~UNIFY_ALLOW_DERIVED
;
20889 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20890 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20892 switch (TREE_CODE (parm
))
20894 case TYPENAME_TYPE
:
20896 case UNBOUND_CLASS_TEMPLATE
:
20897 /* In a type which contains a nested-name-specifier, template
20898 argument values cannot be deduced for template parameters used
20899 within the nested-name-specifier. */
20900 return unify_success (explain_p
);
20902 case TEMPLATE_TYPE_PARM
:
20903 case TEMPLATE_TEMPLATE_PARM
:
20904 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20905 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20906 if (error_operand_p (tparm
))
20907 return unify_invalid (explain_p
);
20909 if (TEMPLATE_TYPE_LEVEL (parm
)
20910 != template_decl_level (tparm
))
20911 /* The PARM is not one we're trying to unify. Just check
20912 to see if it matches ARG. */
20914 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20915 && (is_auto (parm
) ? is_auto (arg
)
20916 : same_type_p (parm
, arg
)))
20917 return unify_success (explain_p
);
20919 return unify_type_mismatch (explain_p
, parm
, arg
);
20921 idx
= TEMPLATE_TYPE_IDX (parm
);
20922 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20923 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20924 if (error_operand_p (tparm
))
20925 return unify_invalid (explain_p
);
20927 /* Check for mixed types and values. */
20928 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20929 && TREE_CODE (tparm
) != TYPE_DECL
)
20930 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20931 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20932 gcc_unreachable ();
20934 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20936 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20937 && CLASS_TYPE_P (arg
))
20939 /* First try to match ARG directly. */
20940 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20944 /* Otherwise, look for a suitable base of ARG, as below. */
20945 enum template_base_result r
;
20946 r
= get_template_base (tparms
, targs
, parm
, arg
,
20949 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20953 /* ARG must be constructed from a template class or a template
20954 template parameter. */
20955 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20956 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20957 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20959 /* Deduce arguments T, i from TT<T> or TT<i>. */
20960 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20963 arg
= TYPE_TI_TEMPLATE (arg
);
20965 /* Fall through to deduce template name. */
20968 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20969 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20971 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20973 /* Simple cases: Value already set, does match or doesn't. */
20974 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20975 return unify_success (explain_p
);
20977 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20981 /* If PARM is `const T' and ARG is only `int', we don't have
20982 a match unless we are allowing additional qualification.
20983 If ARG is `const int' and PARM is just `T' that's OK;
20984 that binds `const int' to `T'. */
20985 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20987 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20989 /* Consider the case where ARG is `const volatile int' and
20990 PARM is `const T'. Then, T should be `volatile int'. */
20991 arg
= cp_build_qualified_type_real
20992 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20993 if (arg
== error_mark_node
)
20994 return unify_invalid (explain_p
);
20996 /* Simple cases: Value already set, does match or doesn't. */
20997 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20998 return unify_success (explain_p
);
21000 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21002 /* Make sure that ARG is not a variable-sized array. (Note
21003 that were talking about variable-sized arrays (like
21004 `int[n]'), rather than arrays of unknown size (like
21005 `int[]').) We'll get very confused by such a type since
21006 the bound of the array is not constant, and therefore
21007 not mangleable. Besides, such types are not allowed in
21008 ISO C++, so we can do as we please here. We do allow
21009 them for 'auto' deduction, since that isn't ABI-exposed. */
21010 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21011 return unify_vla_arg (explain_p
, arg
);
21013 /* Strip typedefs as in convert_template_argument. */
21014 arg
= canonicalize_type_argument (arg
, tf_none
);
21017 /* If ARG is a parameter pack or an expansion, we cannot unify
21018 against it unless PARM is also a parameter pack. */
21019 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21020 && !template_parameter_pack_p (parm
))
21021 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21023 /* If the argument deduction results is a METHOD_TYPE,
21024 then there is a problem.
21025 METHOD_TYPE doesn't map to any real C++ type the result of
21026 the deduction can not be of that type. */
21027 if (TREE_CODE (arg
) == METHOD_TYPE
)
21028 return unify_method_type_error (explain_p
, arg
);
21030 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21031 return unify_success (explain_p
);
21033 case TEMPLATE_PARM_INDEX
:
21034 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21035 if (error_operand_p (tparm
))
21036 return unify_invalid (explain_p
);
21038 if (TEMPLATE_PARM_LEVEL (parm
)
21039 != template_decl_level (tparm
))
21041 /* The PARM is not one we're trying to unify. Just check
21042 to see if it matches ARG. */
21043 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21044 && cp_tree_equal (parm
, arg
));
21046 unify_expression_unequal (explain_p
, parm
, arg
);
21050 idx
= TEMPLATE_PARM_IDX (parm
);
21051 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21055 if ((strict
& UNIFY_ALLOW_INTEGER
)
21056 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21057 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21058 /* We're deducing from an array bound, the type doesn't matter. */
21059 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21060 int x
= !cp_tree_equal (targ
, arg
);
21062 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21066 /* [temp.deduct.type] If, in the declaration of a function template
21067 with a non-type template-parameter, the non-type
21068 template-parameter is used in an expression in the function
21069 parameter-list and, if the corresponding template-argument is
21070 deduced, the template-argument type shall match the type of the
21071 template-parameter exactly, except that a template-argument
21072 deduced from an array bound may be of any integral type.
21073 The non-type parameter might use already deduced type parameters. */
21074 ++processing_template_decl
;
21075 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
21076 --processing_template_decl
;
21077 if (tree a
= type_uses_auto (tparm
))
21079 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21080 if (tparm
== error_mark_node
)
21084 if (!TREE_TYPE (arg
))
21085 /* Template-parameter dependent expression. Just accept it for now.
21086 It will later be processed in convert_template_argument. */
21088 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21089 non_reference (tparm
)))
21091 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21092 && CP_INTEGRAL_TYPE_P (tparm
))
21093 /* Convert the ARG to the type of PARM; the deduced non-type
21094 template argument must exactly match the types of the
21095 corresponding parameter. */
21096 arg
= fold (build_nop (tparm
, arg
));
21097 else if (uses_template_parms (tparm
))
21099 /* We haven't deduced the type of this parameter yet. */
21100 if (cxx_dialect
>= cxx17
21101 /* We deduce from array bounds in try_array_deduction. */
21102 && !(strict
& UNIFY_ALLOW_INTEGER
))
21104 /* Deduce it from the non-type argument. */
21105 tree atype
= TREE_TYPE (arg
);
21106 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21108 UNIFY_ALLOW_NONE
, explain_p
);
21111 /* Try again later. */
21112 return unify_success (explain_p
);
21115 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21117 /* If ARG is a parameter pack or an expansion, we cannot unify
21118 against it unless PARM is also a parameter pack. */
21119 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21120 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21121 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21124 bool removed_attr
= false;
21125 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21127 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21128 return unify_success (explain_p
);
21132 /* A pointer-to-member constant can be unified only with
21133 another constant. */
21134 if (TREE_CODE (arg
) != PTRMEM_CST
)
21135 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21137 /* Just unify the class member. It would be useless (and possibly
21138 wrong, depending on the strict flags) to unify also
21139 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21140 arg refer to the same variable, even if through different
21141 classes. For instance:
21143 struct A { int x; };
21146 Unification of &A::x and &B::x must succeed. */
21147 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21148 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21153 if (!TYPE_PTR_P (arg
))
21154 return unify_type_mismatch (explain_p
, parm
, arg
);
21156 /* [temp.deduct.call]
21158 A can be another pointer or pointer to member type that can
21159 be converted to the deduced A via a qualification
21160 conversion (_conv.qual_).
21162 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21163 This will allow for additional cv-qualification of the
21164 pointed-to types if appropriate. */
21166 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21167 /* The derived-to-base conversion only persists through one
21168 level of pointers. */
21169 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21171 return unify (tparms
, targs
, TREE_TYPE (parm
),
21172 TREE_TYPE (arg
), strict
, explain_p
);
21175 case REFERENCE_TYPE
:
21176 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21177 return unify_type_mismatch (explain_p
, parm
, arg
);
21178 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21179 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21182 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21183 return unify_type_mismatch (explain_p
, parm
, arg
);
21184 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21185 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21186 return unify_type_mismatch (explain_p
, parm
, arg
);
21187 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21188 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21189 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21190 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21191 TYPE_DOMAIN (arg
), explain_p
);
21192 return unify_success (explain_p
);
21199 case ENUMERAL_TYPE
:
21202 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21203 return unify_type_mismatch (explain_p
, parm
, arg
);
21205 /* We have already checked cv-qualification at the top of the
21207 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21208 return unify_type_mismatch (explain_p
, parm
, arg
);
21210 /* As far as unification is concerned, this wins. Later checks
21211 will invalidate it if necessary. */
21212 return unify_success (explain_p
);
21214 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21215 /* Type INTEGER_CST can come from ordinary constant template args. */
21217 while (CONVERT_EXPR_P (arg
))
21218 arg
= TREE_OPERAND (arg
, 0);
21220 if (TREE_CODE (arg
) != INTEGER_CST
)
21221 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21222 return (tree_int_cst_equal (parm
, arg
)
21223 ? unify_success (explain_p
)
21224 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21228 int i
, len
, argslen
;
21229 int parm_variadic_p
= 0;
21231 if (TREE_CODE (arg
) != TREE_VEC
)
21232 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21234 len
= TREE_VEC_LENGTH (parm
);
21235 argslen
= TREE_VEC_LENGTH (arg
);
21237 /* Check for pack expansions in the parameters. */
21238 for (i
= 0; i
< len
; ++i
)
21240 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21243 /* We can unify against something with a trailing
21245 parm_variadic_p
= 1;
21247 /* [temp.deduct.type]/9: If the template argument list of
21248 P contains a pack expansion that is not the last
21249 template argument, the entire template argument list
21250 is a non-deduced context. */
21251 return unify_success (explain_p
);
21255 /* If we don't have enough arguments to satisfy the parameters
21256 (not counting the pack expression at the end), or we have
21257 too many arguments for a parameter list that doesn't end in
21258 a pack expression, we can't unify. */
21259 if (parm_variadic_p
21260 ? argslen
< len
- parm_variadic_p
21262 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21264 /* Unify all of the parameters that precede the (optional)
21265 pack expression. */
21266 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21268 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21269 TREE_VEC_ELT (parm
, i
),
21270 TREE_VEC_ELT (arg
, i
),
21271 UNIFY_ALLOW_NONE
, explain_p
);
21273 if (parm_variadic_p
)
21274 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21276 /*subr=*/true, explain_p
);
21277 return unify_success (explain_p
);
21282 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21283 return unify_type_mismatch (explain_p
, parm
, arg
);
21285 if (TYPE_PTRMEMFUNC_P (parm
))
21287 if (!TYPE_PTRMEMFUNC_P (arg
))
21288 return unify_type_mismatch (explain_p
, parm
, arg
);
21290 return unify (tparms
, targs
,
21291 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21292 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21293 strict
, explain_p
);
21295 else if (TYPE_PTRMEMFUNC_P (arg
))
21296 return unify_type_mismatch (explain_p
, parm
, arg
);
21298 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21300 tree t
= NULL_TREE
;
21302 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21304 /* First, we try to unify the PARM and ARG directly. */
21305 t
= try_class_unification (tparms
, targs
,
21306 parm
, arg
, explain_p
);
21310 /* Fallback to the special case allowed in
21311 [temp.deduct.call]:
21313 If P is a class, and P has the form
21314 template-id, then A can be a derived class of
21315 the deduced A. Likewise, if P is a pointer to
21316 a class of the form template-id, A can be a
21317 pointer to a derived class pointed to by the
21319 enum template_base_result r
;
21320 r
= get_template_base (tparms
, targs
, parm
, arg
,
21325 /* Don't give the derived diagnostic if we're
21326 already dealing with the same template. */
21328 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21329 && (CLASSTYPE_TI_TEMPLATE (parm
)
21330 == CLASSTYPE_TI_TEMPLATE (arg
)));
21331 return unify_no_common_base (explain_p
&& !same_template
,
21336 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21337 && (CLASSTYPE_TI_TEMPLATE (parm
)
21338 == CLASSTYPE_TI_TEMPLATE (arg
)))
21339 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21340 Then, we should unify `int' and `U'. */
21343 /* There's no chance of unification succeeding. */
21344 return unify_type_mismatch (explain_p
, parm
, arg
);
21346 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21347 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21349 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21350 return unify_type_mismatch (explain_p
, parm
, arg
);
21351 return unify_success (explain_p
);
21354 case FUNCTION_TYPE
:
21356 unsigned int nargs
;
21361 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21362 return unify_type_mismatch (explain_p
, parm
, arg
);
21364 /* CV qualifications for methods can never be deduced, they must
21365 match exactly. We need to check them explicitly here,
21366 because type_unification_real treats them as any other
21367 cv-qualified parameter. */
21368 if (TREE_CODE (parm
) == METHOD_TYPE
21369 && (!check_cv_quals_for_unify
21371 class_of_this_parm (arg
),
21372 class_of_this_parm (parm
))))
21373 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21374 if (TREE_CODE (arg
) == FUNCTION_TYPE
21375 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21376 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21377 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21378 return unify_type_mismatch (explain_p
, parm
, arg
);
21380 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21381 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21383 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21384 args
= XALLOCAVEC (tree
, nargs
);
21385 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21386 a
!= NULL_TREE
&& a
!= void_list_node
;
21387 a
= TREE_CHAIN (a
), ++i
)
21388 args
[i
] = TREE_VALUE (a
);
21391 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21392 args
, nargs
, 1, DEDUCE_EXACT
,
21393 LOOKUP_NORMAL
, NULL
, explain_p
))
21396 if (flag_noexcept_type
)
21398 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21399 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21400 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21401 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21402 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21403 && uses_template_parms (TREE_PURPOSE (pspec
)))
21404 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21405 TREE_PURPOSE (aspec
),
21406 UNIFY_ALLOW_NONE
, explain_p
);
21407 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21408 return unify_type_mismatch (explain_p
, parm
, arg
);
21415 /* Unify a pointer to member with a pointer to member function, which
21416 deduces the type of the member as a function type. */
21417 if (TYPE_PTRMEMFUNC_P (arg
))
21419 /* Check top-level cv qualifiers */
21420 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21421 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21423 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21424 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21425 UNIFY_ALLOW_NONE
, explain_p
);
21427 /* Determine the type of the function we are unifying against. */
21428 tree fntype
= static_fn_type (arg
);
21430 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21433 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21434 return unify_type_mismatch (explain_p
, parm
, arg
);
21435 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21436 TYPE_OFFSET_BASETYPE (arg
),
21437 UNIFY_ALLOW_NONE
, explain_p
);
21438 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21439 strict
, explain_p
);
21442 if (DECL_TEMPLATE_PARM_P (parm
))
21443 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21444 if (arg
!= scalar_constant_value (parm
))
21445 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21446 return unify_success (explain_p
);
21449 case TEMPLATE_DECL
:
21450 /* Matched cases are handled by the ARG == PARM test above. */
21451 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21454 /* We might get a variable as a non-type template argument in parm if the
21455 corresponding parameter is type-dependent. Make any necessary
21456 adjustments based on whether arg is a reference. */
21457 if (CONSTANT_CLASS_P (arg
))
21458 parm
= fold_non_dependent_expr (parm
);
21459 else if (REFERENCE_REF_P (arg
))
21461 tree sub
= TREE_OPERAND (arg
, 0);
21463 if (TREE_CODE (sub
) == ADDR_EXPR
)
21464 arg
= TREE_OPERAND (sub
, 0);
21466 /* Now use the normal expression code to check whether they match. */
21469 case TYPE_ARGUMENT_PACK
:
21470 case NONTYPE_ARGUMENT_PACK
:
21471 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21472 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21475 case DECLTYPE_TYPE
:
21476 case UNDERLYING_TYPE
:
21477 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21478 or UNDERLYING_TYPE nodes. */
21479 return unify_success (explain_p
);
21482 /* Unification fails if we hit an error node. */
21483 return unify_invalid (explain_p
);
21486 if (REFERENCE_REF_P (parm
))
21488 bool pexp
= PACK_EXPANSION_P (arg
);
21490 arg
= PACK_EXPANSION_PATTERN (arg
);
21491 if (REFERENCE_REF_P (arg
))
21492 arg
= TREE_OPERAND (arg
, 0);
21494 arg
= make_pack_expansion (arg
, complain
);
21495 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21496 strict
, explain_p
);
21501 /* An unresolved overload is a nondeduced context. */
21502 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21503 return unify_success (explain_p
);
21504 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21506 /* We must be looking at an expression. This can happen with
21510 void foo(S<I>, S<I + 2>);
21512 This is a "nondeduced context":
21516 The nondeduced contexts are:
21518 --A type that is a template-id in which one or more of
21519 the template-arguments is an expression that references
21520 a template-parameter.
21522 In these cases, we assume deduction succeeded, but don't
21523 actually infer any unifications. */
21525 if (!uses_template_parms (parm
)
21526 && !template_args_equal (parm
, arg
))
21527 return unify_expression_unequal (explain_p
, parm
, arg
);
21529 return unify_success (explain_p
);
21532 #undef RECUR_AND_CHECK_FAILURE
21534 /* Note that DECL can be defined in this translation unit, if
21538 mark_definable (tree decl
)
21541 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21542 FOR_EACH_CLONE (clone
, decl
)
21543 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21546 /* Called if RESULT is explicitly instantiated, or is a member of an
21547 explicitly instantiated class. */
21550 mark_decl_instantiated (tree result
, int extern_p
)
21552 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21554 /* If this entity has already been written out, it's too late to
21555 make any modifications. */
21556 if (TREE_ASM_WRITTEN (result
))
21559 /* For anonymous namespace we don't need to do anything. */
21560 if (decl_anon_ns_mem_p (result
))
21562 gcc_assert (!TREE_PUBLIC (result
));
21566 if (TREE_CODE (result
) != FUNCTION_DECL
)
21567 /* The TREE_PUBLIC flag for function declarations will have been
21568 set correctly by tsubst. */
21569 TREE_PUBLIC (result
) = 1;
21571 /* This might have been set by an earlier implicit instantiation. */
21572 DECL_COMDAT (result
) = 0;
21575 DECL_NOT_REALLY_EXTERN (result
) = 0;
21578 mark_definable (result
);
21579 mark_needed (result
);
21580 /* Always make artificials weak. */
21581 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21582 comdat_linkage (result
);
21583 /* For WIN32 we also want to put explicit instantiations in
21584 linkonce sections. */
21585 else if (TREE_PUBLIC (result
))
21586 maybe_make_one_only (result
);
21589 /* If EXTERN_P, then this function will not be emitted -- unless
21590 followed by an explicit instantiation, at which point its linkage
21591 will be adjusted. If !EXTERN_P, then this function will be
21592 emitted here. In neither circumstance do we want
21593 import_export_decl to adjust the linkage. */
21594 DECL_INTERFACE_KNOWN (result
) = 1;
21597 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21598 important template arguments. If any are missing, we check whether
21599 they're important by using error_mark_node for substituting into any
21600 args that were used for partial ordering (the ones between ARGS and END)
21601 and seeing if it bubbles up. */
21604 check_undeduced_parms (tree targs
, tree args
, tree end
)
21606 bool found
= false;
21608 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21609 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21612 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21616 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21617 if (substed
== error_mark_node
)
21623 /* Given two function templates PAT1 and PAT2, return:
21625 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21626 -1 if PAT2 is more specialized than PAT1.
21627 0 if neither is more specialized.
21629 LEN indicates the number of parameters we should consider
21630 (defaulted parameters should not be considered).
21632 The 1998 std underspecified function template partial ordering, and
21633 DR214 addresses the issue. We take pairs of arguments, one from
21634 each of the templates, and deduce them against each other. One of
21635 the templates will be more specialized if all the *other*
21636 template's arguments deduce against its arguments and at least one
21637 of its arguments *does* *not* deduce against the other template's
21638 corresponding argument. Deduction is done as for class templates.
21639 The arguments used in deduction have reference and top level cv
21640 qualifiers removed. Iff both arguments were originally reference
21641 types *and* deduction succeeds in both directions, an lvalue reference
21642 wins against an rvalue reference and otherwise the template
21643 with the more cv-qualified argument wins for that pairing (if
21644 neither is more cv-qualified, they both are equal). Unlike regular
21645 deduction, after all the arguments have been deduced in this way,
21646 we do *not* verify the deduced template argument values can be
21647 substituted into non-deduced contexts.
21649 The logic can be a bit confusing here, because we look at deduce1 and
21650 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21651 can find template arguments for pat1 to make arg1 look like arg2, that
21652 means that arg2 is at least as specialized as arg1. */
21655 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21657 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21658 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21659 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21660 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21661 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21662 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21663 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21664 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21665 tree origs1
, origs2
;
21666 bool lose1
= false;
21667 bool lose2
= false;
21669 /* Remove the this parameter from non-static member functions. If
21670 one is a non-static member function and the other is not a static
21671 member function, remove the first parameter from that function
21672 also. This situation occurs for operator functions where we
21673 locate both a member function (with this pointer) and non-member
21674 operator (with explicit first operand). */
21675 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21677 len
--; /* LEN is the number of significant arguments for DECL1 */
21678 args1
= TREE_CHAIN (args1
);
21679 if (!DECL_STATIC_FUNCTION_P (decl2
))
21680 args2
= TREE_CHAIN (args2
);
21682 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21684 args2
= TREE_CHAIN (args2
);
21685 if (!DECL_STATIC_FUNCTION_P (decl1
))
21688 args1
= TREE_CHAIN (args1
);
21692 /* If only one is a conversion operator, they are unordered. */
21693 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21696 /* Consider the return type for a conversion function */
21697 if (DECL_CONV_FN_P (decl1
))
21699 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21700 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21704 processing_template_decl
++;
21710 /* Stop when an ellipsis is seen. */
21711 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21713 tree arg1
= TREE_VALUE (args1
);
21714 tree arg2
= TREE_VALUE (args2
);
21715 int deduce1
, deduce2
;
21721 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21722 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21724 /* When both arguments are pack expansions, we need only
21725 unify the patterns themselves. */
21726 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21727 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21729 /* This is the last comparison we need to do. */
21733 /* DR 1847: If a particular P contains no template-parameters that
21734 participate in template argument deduction, that P is not used to
21735 determine the ordering. */
21736 if (!uses_deducible_template_parms (arg1
)
21737 && !uses_deducible_template_parms (arg2
))
21740 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21742 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21743 arg1
= TREE_TYPE (arg1
);
21744 quals1
= cp_type_quals (arg1
);
21747 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21749 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21750 arg2
= TREE_TYPE (arg2
);
21751 quals2
= cp_type_quals (arg2
);
21754 arg1
= TYPE_MAIN_VARIANT (arg1
);
21755 arg2
= TYPE_MAIN_VARIANT (arg2
);
21757 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21759 int i
, len2
= remaining_arguments (args2
);
21760 tree parmvec
= make_tree_vec (1);
21761 tree argvec
= make_tree_vec (len2
);
21764 /* Setup the parameter vector, which contains only ARG1. */
21765 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21767 /* Setup the argument vector, which contains the remaining
21769 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21770 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21772 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21773 argvec
, DEDUCE_EXACT
,
21774 /*subr=*/true, /*explain_p=*/false)
21777 /* We cannot deduce in the other direction, because ARG1 is
21778 a pack expansion but ARG2 is not. */
21781 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21783 int i
, len1
= remaining_arguments (args1
);
21784 tree parmvec
= make_tree_vec (1);
21785 tree argvec
= make_tree_vec (len1
);
21788 /* Setup the parameter vector, which contains only ARG1. */
21789 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21791 /* Setup the argument vector, which contains the remaining
21793 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21794 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21796 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21797 argvec
, DEDUCE_EXACT
,
21798 /*subr=*/true, /*explain_p=*/false)
21801 /* We cannot deduce in the other direction, because ARG2 is
21802 a pack expansion but ARG1 is not.*/
21808 /* The normal case, where neither argument is a pack
21810 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21811 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21813 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21814 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21818 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21819 arg2, then arg2 is not as specialized as arg1. */
21825 /* "If, for a given type, deduction succeeds in both directions
21826 (i.e., the types are identical after the transformations above)
21827 and both P and A were reference types (before being replaced with
21828 the type referred to above):
21829 - if the type from the argument template was an lvalue reference and
21830 the type from the parameter template was not, the argument type is
21831 considered to be more specialized than the other; otherwise,
21832 - if the type from the argument template is more cv-qualified
21833 than the type from the parameter template (as described above),
21834 the argument type is considered to be more specialized than the other;
21836 - neither type is more specialized than the other." */
21838 if (deduce1
&& deduce2
)
21840 if (ref1
&& ref2
&& ref1
!= ref2
)
21847 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21849 if ((quals1
& quals2
) == quals2
)
21851 if ((quals1
& quals2
) == quals1
)
21856 if (lose1
&& lose2
)
21857 /* We've failed to deduce something in either direction.
21858 These must be unordered. */
21863 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21864 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21865 /* We have already processed all of the arguments in our
21866 handing of the pack expansion type. */
21869 args1
= TREE_CHAIN (args1
);
21870 args2
= TREE_CHAIN (args2
);
21873 /* "In most cases, all template parameters must have values in order for
21874 deduction to succeed, but for partial ordering purposes a template
21875 parameter may remain without a value provided it is not used in the
21876 types being used for partial ordering."
21878 Thus, if we are missing any of the targs1 we need to substitute into
21879 origs1, then pat2 is not as specialized as pat1. This can happen when
21880 there is a nondeduced context. */
21881 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21883 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21886 processing_template_decl
--;
21888 /* If both deductions succeed, the partial ordering selects the more
21889 constrained template. */
21890 if (!lose1
&& !lose2
)
21892 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21893 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21894 lose1
= !subsumes_constraints (c1
, c2
);
21895 lose2
= !subsumes_constraints (c2
, c1
);
21898 /* All things being equal, if the next argument is a pack expansion
21899 for one function but not for the other, prefer the
21900 non-variadic function. FIXME this is bogus; see c++/41958. */
21902 && args1
&& TREE_VALUE (args1
)
21903 && args2
&& TREE_VALUE (args2
))
21905 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21906 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21909 if (lose1
== lose2
)
21917 /* Determine which of two partial specializations of TMPL is more
21920 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21921 to the first partial specialization. The TREE_PURPOSE is the
21922 innermost set of template parameters for the partial
21923 specialization. PAT2 is similar, but for the second template.
21925 Return 1 if the first partial specialization is more specialized;
21926 -1 if the second is more specialized; 0 if neither is more
21929 See [temp.class.order] for information about determining which of
21930 two templates is more specialized. */
21933 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21937 bool any_deductions
= false;
21939 tree tmpl1
= TREE_VALUE (pat1
);
21940 tree tmpl2
= TREE_VALUE (pat2
);
21941 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21942 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21944 /* Just like what happens for functions, if we are ordering between
21945 different template specializations, we may encounter dependent
21946 types in the arguments, and we need our dependency check functions
21947 to behave correctly. */
21948 ++processing_template_decl
;
21949 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21953 any_deductions
= true;
21956 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21960 any_deductions
= true;
21962 --processing_template_decl
;
21964 /* If both deductions succeed, the partial ordering selects the more
21965 constrained template. */
21966 if (!winner
&& any_deductions
)
21967 return more_constrained (tmpl1
, tmpl2
);
21969 /* In the case of a tie where at least one of the templates
21970 has a parameter pack at the end, the template with the most
21971 non-packed parameters wins. */
21974 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21975 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21977 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21978 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21979 int len1
= TREE_VEC_LENGTH (args1
);
21980 int len2
= TREE_VEC_LENGTH (args2
);
21982 /* We don't count the pack expansion at the end. */
21983 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21985 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21990 else if (len1
< len2
)
21997 /* Return the template arguments that will produce the function signature
21998 DECL from the function template FN, with the explicit template
21999 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22000 also match. Return NULL_TREE if no satisfactory arguments could be
22004 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
22006 int ntparms
= DECL_NTPARMS (fn
);
22007 tree targs
= make_tree_vec (ntparms
);
22008 tree decl_type
= TREE_TYPE (decl
);
22009 tree decl_arg_types
;
22011 unsigned int nargs
, ix
;
22014 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22016 /* Never do unification on the 'this' parameter. */
22017 decl_arg_types
= skip_artificial_parms_for (decl
,
22018 TYPE_ARG_TYPES (decl_type
));
22020 nargs
= list_length (decl_arg_types
);
22021 args
= XALLOCAVEC (tree
, nargs
);
22022 for (arg
= decl_arg_types
, ix
= 0;
22023 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22024 arg
= TREE_CHAIN (arg
), ++ix
)
22025 args
[ix
] = TREE_VALUE (arg
);
22027 if (fn_type_unification (fn
, explicit_args
, targs
,
22029 (check_rettype
|| DECL_CONV_FN_P (fn
)
22030 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22031 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
22033 == error_mark_node
)
22039 /* Return the innermost template arguments that, when applied to a partial
22040 specialization SPEC_TMPL of TMPL, yield the ARGS.
22042 For example, suppose we have:
22044 template <class T, class U> struct S {};
22045 template <class T> struct S<T*, int> {};
22047 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22048 partial specialization and the ARGS will be {double*, int}. The resulting
22049 vector will be {double}, indicating that `T' is bound to `double'. */
22052 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22054 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22056 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22057 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22059 tree innermost_deduced_args
;
22061 innermost_deduced_args
= make_tree_vec (ntparms
);
22062 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22064 deduced_args
= copy_node (args
);
22065 SET_TMPL_ARGS_LEVEL (deduced_args
,
22066 TMPL_ARGS_DEPTH (deduced_args
),
22067 innermost_deduced_args
);
22070 deduced_args
= innermost_deduced_args
;
22072 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22074 if (unify (tparms
, deduced_args
,
22075 INNERMOST_TEMPLATE_ARGS (spec_args
),
22076 INNERMOST_TEMPLATE_ARGS (args
),
22077 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22080 for (i
= 0; i
< ntparms
; ++i
)
22081 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22083 if (!tried_array_deduction
)
22085 try_array_deduction (tparms
, innermost_deduced_args
,
22086 INNERMOST_TEMPLATE_ARGS (spec_args
));
22087 tried_array_deduction
= true;
22088 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22094 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22095 if (!push_tinst_level (tinst
))
22097 excessive_deduction_depth
= true;
22101 /* Verify that nondeduced template arguments agree with the type
22102 obtained from argument deduction.
22106 struct A { typedef int X; };
22107 template <class T, class U> struct C {};
22108 template <class T> struct C<T, typename T::X> {};
22110 Then with the instantiation `C<A, int>', we can deduce that
22111 `T' is `A' but unify () does not check whether `typename T::X'
22113 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22115 if (spec_args
!= error_mark_node
)
22116 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22117 INNERMOST_TEMPLATE_ARGS (spec_args
),
22118 tmpl
, tf_none
, false, false);
22120 pop_tinst_level ();
22122 if (spec_args
== error_mark_node
22123 /* We only need to check the innermost arguments; the other
22124 arguments will always agree. */
22125 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22126 INNERMOST_TEMPLATE_ARGS (args
)))
22129 /* Now that we have bindings for all of the template arguments,
22130 ensure that the arguments deduced for the template template
22131 parameters have compatible template parameter lists. See the use
22132 of template_template_parm_bindings_ok_p in fn_type_unification
22133 for more information. */
22134 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22137 return deduced_args
;
22140 // Compare two function templates T1 and T2 by deducing bindings
22141 // from one against the other. If both deductions succeed, compare
22142 // constraints to see which is more constrained.
22144 more_specialized_inst (tree t1
, tree t2
)
22149 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22155 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22161 // If both deductions succeed, then one may be more constrained.
22162 if (count
== 2 && fate
== 0)
22163 fate
= more_constrained (t1
, t2
);
22168 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22169 Return the TREE_LIST node with the most specialized template, if
22170 any. If there is no most specialized template, the error_mark_node
22173 Note that this function does not look at, or modify, the
22174 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22175 returned is one of the elements of INSTANTIATIONS, callers may
22176 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22177 and retrieve it from the value returned. */
22180 most_specialized_instantiation (tree templates
)
22184 ++processing_template_decl
;
22187 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22189 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22190 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22195 /* Equally specialized, move to next function. If there
22196 is no next function, nothing's most specialized. */
22197 fn
= TREE_CHAIN (fn
);
22205 /* Now verify that champ is better than everything earlier in the
22206 instantiation list. */
22207 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22208 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22215 processing_template_decl
--;
22218 return error_mark_node
;
22223 /* If DECL is a specialization of some template, return the most
22224 general such template. Otherwise, returns NULL_TREE.
22226 For example, given:
22228 template <class T> struct S { template <class U> void f(U); };
22230 if TMPL is `template <class U> void S<int>::f(U)' this will return
22231 the full template. This function will not trace past partial
22232 specializations, however. For example, given in addition:
22234 template <class T> struct S<T*> { template <class U> void f(U); };
22236 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22237 `template <class T> template <class U> S<T*>::f(U)'. */
22240 most_general_template (tree decl
)
22242 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22244 if (tree tinfo
= get_template_info (decl
))
22245 decl
= TI_TEMPLATE (tinfo
);
22246 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22247 template friend, or a FIELD_DECL for a capture pack. */
22248 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22252 /* Look for more and more general templates. */
22253 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22255 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22256 (See cp-tree.h for details.) */
22257 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22260 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22261 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22262 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22265 /* Stop if we run into an explicitly specialized class template. */
22266 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22267 && DECL_CONTEXT (decl
)
22268 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22271 decl
= DECL_TI_TEMPLATE (decl
);
22277 /* Return the most specialized of the template partial specializations
22278 which can produce TARGET, a specialization of some class or variable
22279 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22280 a TEMPLATE_DECL node corresponding to the partial specialization, while
22281 the TREE_PURPOSE is the set of template arguments that must be
22282 substituted into the template pattern in order to generate TARGET.
22284 If the choice of partial specialization is ambiguous, a diagnostic
22285 is issued, and the error_mark_node is returned. If there are no
22286 partial specializations matching TARGET, then NULL_TREE is
22287 returned, indicating that the primary template should be used. */
22290 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22292 tree list
= NULL_TREE
;
22297 tree outer_args
= NULL_TREE
;
22300 if (TYPE_P (target
))
22302 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22303 tmpl
= TI_TEMPLATE (tinfo
);
22304 args
= TI_ARGS (tinfo
);
22306 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22308 tmpl
= TREE_OPERAND (target
, 0);
22309 args
= TREE_OPERAND (target
, 1);
22311 else if (VAR_P (target
))
22313 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22314 tmpl
= TI_TEMPLATE (tinfo
);
22315 args
= TI_ARGS (tinfo
);
22318 gcc_unreachable ();
22320 tree main_tmpl
= most_general_template (tmpl
);
22322 /* For determining which partial specialization to use, only the
22323 innermost args are interesting. */
22324 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22326 outer_args
= strip_innermost_template_args (args
, 1);
22327 args
= INNERMOST_TEMPLATE_ARGS (args
);
22330 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22333 tree spec_tmpl
= TREE_VALUE (t
);
22337 /* Substitute in the template args from the enclosing class. */
22338 ++processing_template_decl
;
22339 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22340 --processing_template_decl
;
22343 if (spec_tmpl
== error_mark_node
)
22344 return error_mark_node
;
22346 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22350 spec_args
= add_to_template_args (outer_args
, spec_args
);
22352 /* Keep the candidate only if the constraints are satisfied,
22353 or if we're not compiling with concepts. */
22355 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22357 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22358 TREE_TYPE (list
) = TREE_TYPE (t
);
22366 ambiguous_p
= false;
22369 t
= TREE_CHAIN (t
);
22370 for (; t
; t
= TREE_CHAIN (t
))
22372 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22379 t
= TREE_CHAIN (t
);
22382 ambiguous_p
= true;
22391 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22393 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22396 ambiguous_p
= true;
22404 char *spaces
= NULL
;
22405 if (!(complain
& tf_error
))
22406 return error_mark_node
;
22407 if (TYPE_P (target
))
22408 error ("ambiguous template instantiation for %q#T", target
);
22410 error ("ambiguous template instantiation for %q#D", target
);
22411 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22412 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22414 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22415 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22416 "%s %#qS", spaces
? spaces
: str
, subst
);
22417 spaces
= spaces
? spaces
: get_spaces (str
);
22420 return error_mark_node
;
22426 /* Explicitly instantiate DECL. */
22429 do_decl_instantiation (tree decl
, tree storage
)
22431 tree result
= NULL_TREE
;
22434 if (!decl
|| decl
== error_mark_node
)
22435 /* An error occurred, for which grokdeclarator has already issued
22436 an appropriate message. */
22438 else if (! DECL_LANG_SPECIFIC (decl
))
22440 error ("explicit instantiation of non-template %q#D", decl
);
22444 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22445 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22447 if (VAR_P (decl
) && !var_templ
)
22449 /* There is an asymmetry here in the way VAR_DECLs and
22450 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22451 the latter, the DECL we get back will be marked as a
22452 template instantiation, and the appropriate
22453 DECL_TEMPLATE_INFO will be set up. This does not happen for
22454 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22455 should handle VAR_DECLs as it currently handles
22457 if (!DECL_CLASS_SCOPE_P (decl
))
22459 error ("%qD is not a static data member of a class template", decl
);
22462 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22463 if (!result
|| !VAR_P (result
))
22465 error ("no matching template for %qD found", decl
);
22468 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22470 error ("type %qT for explicit instantiation %qD does not match "
22471 "declared type %qT", TREE_TYPE (result
), decl
,
22476 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22478 error ("explicit instantiation of %q#D", decl
);
22484 /* Check for various error cases. Note that if the explicit
22485 instantiation is valid the RESULT will currently be marked as an
22486 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22487 until we get here. */
22489 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22491 /* DR 259 [temp.spec].
22493 Both an explicit instantiation and a declaration of an explicit
22494 specialization shall not appear in a program unless the explicit
22495 instantiation follows a declaration of the explicit specialization.
22497 For a given set of template parameters, if an explicit
22498 instantiation of a template appears after a declaration of an
22499 explicit specialization for that template, the explicit
22500 instantiation has no effect. */
22503 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22507 No program shall explicitly instantiate any template more
22510 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22511 the first instantiation was `extern' and the second is not,
22512 and EXTERN_P for the opposite case. */
22513 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22514 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22515 /* If an "extern" explicit instantiation follows an ordinary
22516 explicit instantiation, the template is instantiated. */
22520 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22522 error ("no matching template for %qD found", result
);
22525 else if (!DECL_TEMPLATE_INFO (result
))
22527 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22531 if (storage
== NULL_TREE
)
22533 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22535 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22536 pedwarn (input_location
, OPT_Wpedantic
,
22537 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22542 error ("storage class %qD applied to template instantiation", storage
);
22544 check_explicit_instantiation_namespace (result
);
22545 mark_decl_instantiated (result
, extern_p
);
22547 instantiate_decl (result
, /*defer_ok=*/true,
22548 /*expl_inst_class_mem_p=*/false);
22552 mark_class_instantiated (tree t
, int extern_p
)
22554 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22555 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22556 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22557 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22560 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22561 rest_of_type_compilation (t
, 1);
22565 /* Called from do_type_instantiation through binding_table_foreach to
22566 do recursive instantiation for the type bound in ENTRY. */
22568 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22570 tree storage
= *(tree
*) data
;
22572 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22573 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22574 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22577 /* Perform an explicit instantiation of template class T. STORAGE, if
22578 non-null, is the RID for extern, inline or static. COMPLAIN is
22579 nonzero if this is called from the parser, zero if called recursively,
22580 since the standard is unclear (as detailed below). */
22583 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22588 int previous_instantiation_extern_p
= 0;
22590 if (TREE_CODE (t
) == TYPE_DECL
)
22593 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22596 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22598 error ("explicit instantiation of non-class template %qD", tmpl
);
22600 error ("explicit instantiation of non-template type %qT", t
);
22606 if (!COMPLETE_TYPE_P (t
))
22608 if (complain
& tf_error
)
22609 error ("explicit instantiation of %q#T before definition of template",
22614 if (storage
!= NULL_TREE
)
22616 if (!in_system_header_at (input_location
))
22618 if (storage
== ridpointers
[(int) RID_EXTERN
])
22620 if (cxx_dialect
== cxx98
)
22621 pedwarn (input_location
, OPT_Wpedantic
,
22622 "ISO C++ 1998 forbids the use of %<extern%> on "
22623 "explicit instantiations");
22626 pedwarn (input_location
, OPT_Wpedantic
,
22627 "ISO C++ forbids the use of %qE"
22628 " on explicit instantiations", storage
);
22631 if (storage
== ridpointers
[(int) RID_INLINE
])
22633 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22635 else if (storage
== ridpointers
[(int) RID_STATIC
])
22639 error ("storage class %qD applied to template instantiation",
22645 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22647 /* DR 259 [temp.spec].
22649 Both an explicit instantiation and a declaration of an explicit
22650 specialization shall not appear in a program unless the explicit
22651 instantiation follows a declaration of the explicit specialization.
22653 For a given set of template parameters, if an explicit
22654 instantiation of a template appears after a declaration of an
22655 explicit specialization for that template, the explicit
22656 instantiation has no effect. */
22659 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22663 No program shall explicitly instantiate any template more
22666 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22667 instantiation was `extern'. If EXTERN_P then the second is.
22668 These cases are OK. */
22669 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22671 if (!previous_instantiation_extern_p
&& !extern_p
22672 && (complain
& tf_error
))
22673 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22675 /* If we've already instantiated the template, just return now. */
22676 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22680 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22681 mark_class_instantiated (t
, extern_p
);
22686 /* In contrast to implicit instantiation, where only the
22687 declarations, and not the definitions, of members are
22688 instantiated, we have here:
22692 The explicit instantiation of a class template specialization
22693 implies the instantiation of all of its members not
22694 previously explicitly specialized in the translation unit
22695 containing the explicit instantiation.
22697 Of course, we can't instantiate member template classes, since we
22698 don't have any arguments for them. Note that the standard is
22699 unclear on whether the instantiation of the members are
22700 *explicit* instantiations or not. However, the most natural
22701 interpretation is that it should be an explicit
22703 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22705 || (TREE_CODE (fld
) == FUNCTION_DECL
22707 && user_provided_p (fld
)))
22708 && DECL_TEMPLATE_INSTANTIATION (fld
))
22710 mark_decl_instantiated (fld
, extern_p
);
22712 instantiate_decl (fld
, /*defer_ok=*/true,
22713 /*expl_inst_class_mem_p=*/true);
22716 if (CLASSTYPE_NESTED_UTDS (t
))
22717 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22718 bt_instantiate_type_proc
, &storage
);
22721 /* Given a function DECL, which is a specialization of TMPL, modify
22722 DECL to be a re-instantiation of TMPL with the same template
22723 arguments. TMPL should be the template into which tsubst'ing
22724 should occur for DECL, not the most general template.
22726 One reason for doing this is a scenario like this:
22729 void f(const T&, int i);
22731 void g() { f(3, 7); }
22734 void f(const T& t, const int i) { }
22736 Note that when the template is first instantiated, with
22737 instantiate_template, the resulting DECL will have no name for the
22738 first parameter, and the wrong type for the second. So, when we go
22739 to instantiate the DECL, we regenerate it. */
22742 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22744 /* The arguments used to instantiate DECL, from the most general
22748 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22750 /* Make sure that we can see identifiers, and compute access
22752 push_access_scope (decl
);
22754 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22762 args_depth
= TMPL_ARGS_DEPTH (args
);
22763 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22764 if (args_depth
> parms_depth
)
22765 args
= get_innermost_template_args (args
, parms_depth
);
22767 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22768 args
, tf_error
, NULL_TREE
,
22769 /*defer_ok*/false);
22770 if (specs
&& specs
!= error_mark_node
)
22771 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22774 /* Merge parameter declarations. */
22775 decl_parm
= skip_artificial_parms_for (decl
,
22776 DECL_ARGUMENTS (decl
));
22778 = skip_artificial_parms_for (code_pattern
,
22779 DECL_ARGUMENTS (code_pattern
));
22780 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22785 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22786 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22787 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22789 parm_type
= type_decays_to (parm_type
);
22790 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22791 TREE_TYPE (decl_parm
) = parm_type
;
22792 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22793 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22795 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22796 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22798 decl_parm
= DECL_CHAIN (decl_parm
);
22799 pattern_parm
= DECL_CHAIN (pattern_parm
);
22801 /* Merge any parameters that match with the function parameter
22803 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22806 tree expanded_types
;
22807 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22808 the parameters in this function parameter pack. */
22809 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22810 args
, tf_error
, NULL_TREE
);
22811 len
= TREE_VEC_LENGTH (expanded_types
);
22812 for (i
= 0; i
< len
; i
++)
22817 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22818 /* Rename the parameter to include the index. */
22819 DECL_NAME (decl_parm
) =
22820 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22821 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22822 parm_type
= type_decays_to (parm_type
);
22823 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22824 TREE_TYPE (decl_parm
) = parm_type
;
22825 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22826 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22828 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22829 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22831 decl_parm
= DECL_CHAIN (decl_parm
);
22834 /* Merge additional specifiers from the CODE_PATTERN. */
22835 if (DECL_DECLARED_INLINE_P (code_pattern
)
22836 && !DECL_DECLARED_INLINE_P (decl
))
22837 DECL_DECLARED_INLINE_P (decl
) = 1;
22839 else if (VAR_P (decl
))
22841 start_lambda_scope (decl
);
22842 DECL_INITIAL (decl
) =
22843 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22844 tf_error
, DECL_TI_TEMPLATE (decl
),
22845 /*integral_constant_expression_p=*/false);
22846 finish_lambda_scope ();
22847 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22848 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22849 tf_error
, DECL_TI_TEMPLATE (decl
));
22852 gcc_unreachable ();
22854 pop_access_scope (decl
);
22857 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22858 substituted to get DECL. */
22861 template_for_substitution (tree decl
)
22863 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22865 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22866 for the instantiation. This is not always the most general
22867 template. Consider, for example:
22870 struct S { template <class U> void f();
22871 template <> void f<int>(); };
22873 and an instantiation of S<double>::f<int>. We want TD to be the
22874 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22875 while (/* An instantiation cannot have a definition, so we need a
22876 more general template. */
22877 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22878 /* We must also deal with friend templates. Given:
22880 template <class T> struct S {
22881 template <class U> friend void f() {};
22884 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22885 so far as the language is concerned, but that's still
22886 where we get the pattern for the instantiation from. On
22887 other hand, if the definition comes outside the class, say:
22889 template <class T> struct S {
22890 template <class U> friend void f();
22892 template <class U> friend void f() {}
22894 we don't need to look any further. That's what the check for
22895 DECL_INITIAL is for. */
22896 || (TREE_CODE (decl
) == FUNCTION_DECL
22897 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22898 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22900 /* The present template, TD, should not be a definition. If it
22901 were a definition, we should be using it! Note that we
22902 cannot restructure the loop to just keep going until we find
22903 a template with a definition, since that might go too far if
22904 a specialization was declared, but not defined. */
22906 /* Fetch the more general template. */
22907 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22913 /* Returns true if we need to instantiate this template instance even if we
22914 know we aren't going to emit it. */
22917 always_instantiate_p (tree decl
)
22919 /* We always instantiate inline functions so that we can inline them. An
22920 explicit instantiation declaration prohibits implicit instantiation of
22921 non-inline functions. With high levels of optimization, we would
22922 normally inline non-inline functions -- but we're not allowed to do
22923 that for "extern template" functions. Therefore, we check
22924 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22925 return ((TREE_CODE (decl
) == FUNCTION_DECL
22926 && (DECL_DECLARED_INLINE_P (decl
)
22927 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22928 /* And we need to instantiate static data members so that
22929 their initializers are available in integral constant
22932 && decl_maybe_constant_var_p (decl
)));
22935 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22936 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22937 error, true otherwise. */
22940 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22942 tree fntype
, spec
, noex
, clone
;
22944 /* Don't instantiate a noexcept-specification from template context. */
22945 if (processing_template_decl
)
22948 if (DECL_CLONED_FUNCTION_P (fn
))
22949 fn
= DECL_CLONED_FUNCTION (fn
);
22950 fntype
= TREE_TYPE (fn
);
22951 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22953 if (!spec
|| !TREE_PURPOSE (spec
))
22956 noex
= TREE_PURPOSE (spec
);
22958 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22960 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22961 bool added
= false;
22962 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22963 spec
= get_defaulted_eh_spec (fn
, complain
);
22964 else if (!(added
= !fns
->add (fn
)))
22966 /* If hash_set::add returns true, the element was already there. */
22967 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22968 DECL_SOURCE_LOCATION (fn
));
22970 "exception specification of %qD depends on itself",
22972 spec
= noexcept_false_spec
;
22974 else if (push_tinst_level (fn
))
22976 push_access_scope (fn
);
22977 push_deferring_access_checks (dk_no_deferred
);
22978 input_location
= DECL_SOURCE_LOCATION (fn
);
22979 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22980 DEFERRED_NOEXCEPT_ARGS (noex
),
22981 tf_warning_or_error
, fn
,
22982 /*function_p=*/false,
22983 /*integral_constant_expression_p=*/true);
22984 pop_deferring_access_checks ();
22985 pop_access_scope (fn
);
22986 pop_tinst_level ();
22987 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22988 if (spec
== error_mark_node
)
22989 spec
= noexcept_false_spec
;
22992 spec
= noexcept_false_spec
;
22997 if (spec
== error_mark_node
)
23000 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
23003 FOR_EACH_CLONE (clone
, fn
)
23005 if (TREE_TYPE (clone
) == fntype
)
23006 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23008 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23014 /* We're starting to process the function INST, an instantiation of PATTERN;
23015 add their parameters to local_specializations. */
23018 register_parameter_specializations (tree pattern
, tree inst
)
23020 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23021 tree spec_parm
= DECL_ARGUMENTS (inst
);
23022 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23024 register_local_specialization (spec_parm
, tmpl_parm
);
23025 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23026 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23028 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23030 if (!DECL_PACK_P (tmpl_parm
))
23032 register_local_specialization (spec_parm
, tmpl_parm
);
23033 spec_parm
= DECL_CHAIN (spec_parm
);
23037 /* Register the (value) argument pack as a specialization of
23038 TMPL_PARM, then move on. */
23039 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23040 register_local_specialization (argpack
, tmpl_parm
);
23043 gcc_assert (!spec_parm
);
23046 /* Produce the definition of D, a _DECL generated from a template. If
23047 DEFER_OK is true, then we don't have to actually do the
23048 instantiation now; we just have to do it sometime. Normally it is
23049 an error if this is an explicit instantiation but D is undefined.
23050 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23051 instantiated class template. */
23054 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23056 tree tmpl
= DECL_TI_TEMPLATE (d
);
23063 bool pattern_defined
;
23064 location_t saved_loc
= input_location
;
23065 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23066 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23070 /* This function should only be used to instantiate templates for
23071 functions and static member variables. */
23072 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23074 /* A concept is never instantiated. */
23075 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23077 /* Variables are never deferred; if instantiation is required, they
23078 are instantiated right away. That allows for better code in the
23079 case that an expression refers to the value of the variable --
23080 if the variable has a constant value the referring expression can
23081 take advantage of that fact. */
23085 /* Don't instantiate cloned functions. Instead, instantiate the
23086 functions they cloned. */
23087 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23088 d
= DECL_CLONED_FUNCTION (d
);
23090 if (DECL_TEMPLATE_INSTANTIATED (d
)
23091 || (TREE_CODE (d
) == FUNCTION_DECL
23092 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23093 || DECL_TEMPLATE_SPECIALIZATION (d
))
23094 /* D has already been instantiated or explicitly specialized, so
23095 there's nothing for us to do here.
23097 It might seem reasonable to check whether or not D is an explicit
23098 instantiation, and, if so, stop here. But when an explicit
23099 instantiation is deferred until the end of the compilation,
23100 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23101 the instantiation. */
23104 /* Check to see whether we know that this template will be
23105 instantiated in some other file, as with "extern template"
23107 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23109 /* In general, we do not instantiate such templates. */
23110 if (external_p
&& !always_instantiate_p (d
))
23113 gen_tmpl
= most_general_template (tmpl
);
23114 gen_args
= DECL_TI_ARGS (d
);
23116 if (tmpl
!= gen_tmpl
)
23117 /* We should already have the extra args. */
23118 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23119 == TMPL_ARGS_DEPTH (gen_args
));
23120 /* And what's in the hash table should match D. */
23121 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23122 || spec
== NULL_TREE
);
23124 /* This needs to happen before any tsubsting. */
23125 if (! push_tinst_level (d
))
23128 timevar_push (TV_TEMPLATE_INST
);
23130 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23131 for the instantiation. */
23132 td
= template_for_substitution (d
);
23137 /* Look up an explicit specialization, if any. */
23138 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23139 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23140 if (elt
&& elt
!= error_mark_node
)
23142 td
= TREE_VALUE (elt
);
23143 args
= TREE_PURPOSE (elt
);
23147 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23149 /* We should never be trying to instantiate a member of a class
23150 template or partial specialization. */
23151 gcc_assert (d
!= code_pattern
);
23153 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23154 || DECL_TEMPLATE_SPECIALIZATION (td
))
23155 /* In the case of a friend template whose definition is provided
23156 outside the class, we may have too many arguments. Drop the
23157 ones we don't need. The same is true for specializations. */
23158 args
= get_innermost_template_args
23159 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23161 if (TREE_CODE (d
) == FUNCTION_DECL
)
23163 deleted_p
= DECL_DELETED_FN (code_pattern
);
23164 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23165 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23166 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23172 if (DECL_CLASS_SCOPE_P (code_pattern
))
23173 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23174 || DECL_INLINE_VAR_P (code_pattern
));
23176 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23179 /* We may be in the middle of deferred access check. Disable it now. */
23180 push_deferring_access_checks (dk_no_deferred
);
23182 /* Unless an explicit instantiation directive has already determined
23183 the linkage of D, remember that a definition is available for
23185 if (pattern_defined
23186 && !DECL_INTERFACE_KNOWN (d
)
23187 && !DECL_NOT_REALLY_EXTERN (d
))
23188 mark_definable (d
);
23190 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23191 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23192 input_location
= DECL_SOURCE_LOCATION (d
);
23194 /* If D is a member of an explicitly instantiated class template,
23195 and no definition is available, treat it like an implicit
23197 if (!pattern_defined
&& expl_inst_class_mem_p
23198 && DECL_EXPLICIT_INSTANTIATION (d
))
23200 /* Leave linkage flags alone on instantiations with anonymous
23202 if (TREE_PUBLIC (d
))
23204 DECL_NOT_REALLY_EXTERN (d
) = 0;
23205 DECL_INTERFACE_KNOWN (d
) = 0;
23207 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23210 /* Defer all other templates, unless we have been explicitly
23211 forbidden from doing so. */
23212 if (/* If there is no definition, we cannot instantiate the
23215 /* If it's OK to postpone instantiation, do so. */
23217 /* If this is a static data member that will be defined
23218 elsewhere, we don't want to instantiate the entire data
23219 member, but we do want to instantiate the initializer so that
23220 we can substitute that elsewhere. */
23221 || (external_p
&& VAR_P (d
))
23222 /* Handle here a deleted function too, avoid generating
23223 its body (c++/61080). */
23226 /* The definition of the static data member is now required so
23227 we must substitute the initializer. */
23229 && !DECL_INITIAL (d
)
23230 && DECL_INITIAL (code_pattern
))
23234 bool const_init
= false;
23235 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23237 ns
= decl_namespace_context (d
);
23238 push_nested_namespace (ns
);
23240 push_nested_class (DECL_CONTEXT (d
));
23241 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23243 tf_warning_or_error
, NULL_TREE
,
23244 /*integral_constant_expression_p=*/false);
23245 /* If instantiating the initializer involved instantiating this
23246 again, don't call cp_finish_decl twice. */
23247 if (!DECL_INITIAL (d
))
23249 /* Make sure the initializer is still constant, in case of
23250 circular dependency (template/instantiate6.C). */
23252 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23253 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23254 /*asmspec_tree=*/NULL_TREE
,
23255 LOOKUP_ONLYCONVERTING
);
23258 pop_nested_class ();
23259 pop_nested_namespace (ns
);
23262 /* We restore the source position here because it's used by
23263 add_pending_template. */
23264 input_location
= saved_loc
;
23266 if (at_eof
&& !pattern_defined
23267 && DECL_EXPLICIT_INSTANTIATION (d
)
23268 && DECL_NOT_REALLY_EXTERN (d
))
23271 The definition of a non-exported function template, a
23272 non-exported member function template, or a non-exported
23273 member function or static data member of a class template
23274 shall be present in every translation unit in which it is
23275 explicitly instantiated. */
23276 permerror (input_location
, "explicit instantiation of %qD "
23277 "but no definition available", d
);
23279 /* If we're in unevaluated context, we just wanted to get the
23280 constant value; this isn't an odr use, so don't queue
23281 a full instantiation. */
23282 if (cp_unevaluated_operand
!= 0)
23284 /* ??? Historically, we have instantiated inline functions, even
23285 when marked as "extern template". */
23286 if (!(external_p
&& VAR_P (d
)))
23287 add_pending_template (d
);
23290 /* Tell the repository that D is available in this translation unit
23291 -- and see if it is supposed to be instantiated here. */
23292 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23294 /* In a PCH file, despite the fact that the repository hasn't
23295 requested instantiation in the PCH it is still possible that
23296 an instantiation will be required in a file that includes the
23299 add_pending_template (d
);
23300 /* Instantiate inline functions so that the inliner can do its
23301 job, even though we'll not be emitting a copy of this
23303 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23307 bool push_to_top
, nested
;
23309 fn_context
= decl_function_context (d
);
23310 nested
= current_function_decl
!= NULL_TREE
;
23311 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23313 vec
<tree
> omp_privatization_save
;
23315 save_omp_privatization_clauses (omp_privatization_save
);
23318 push_to_top_level ();
23321 push_function_context ();
23322 cp_unevaluated_operand
= 0;
23323 c_inhibit_evaluation_warnings
= 0;
23326 /* Mark D as instantiated so that recursive calls to
23327 instantiate_decl do not try to instantiate it again. */
23328 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23330 /* Regenerate the declaration in case the template has been modified
23331 by a subsequent redeclaration. */
23332 regenerate_decl_from_template (d
, td
, args
);
23334 /* We already set the file and line above. Reset them now in case
23335 they changed as a result of calling regenerate_decl_from_template. */
23336 input_location
= DECL_SOURCE_LOCATION (d
);
23341 bool const_init
= false;
23343 /* Clear out DECL_RTL; whatever was there before may not be right
23344 since we've reset the type of the declaration. */
23345 SET_DECL_RTL (d
, NULL
);
23346 DECL_IN_AGGR_P (d
) = 0;
23348 /* The initializer is placed in DECL_INITIAL by
23349 regenerate_decl_from_template so we don't need to
23350 push/pop_access_scope again here. Pull it out so that
23351 cp_finish_decl can process it. */
23352 init
= DECL_INITIAL (d
);
23353 DECL_INITIAL (d
) = NULL_TREE
;
23354 DECL_INITIALIZED_P (d
) = 0;
23356 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23357 initializer. That function will defer actual emission until
23358 we have a chance to determine linkage. */
23359 DECL_EXTERNAL (d
) = 0;
23361 /* Enter the scope of D so that access-checking works correctly. */
23362 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23364 push_nested_class (DECL_CONTEXT (d
));
23366 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23367 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23370 pop_nested_class ();
23372 if (variable_template_p (gen_tmpl
))
23373 note_variable_template_instantiation (d
);
23375 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23376 synthesize_method (d
);
23377 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23379 /* Set up the list of local specializations. */
23380 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23381 tree block
= NULL_TREE
;
23383 /* Set up context. */
23384 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23385 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23386 block
= push_stmt_list ();
23388 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23390 /* Some typedefs referenced from within the template code need to be
23391 access checked at template instantiation time, i.e now. These
23392 types were added to the template at parsing time. Let's get those
23393 and perform the access checks then. */
23394 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23397 /* Create substitution entries for the parameters. */
23398 register_parameter_specializations (code_pattern
, d
);
23400 /* Substitute into the body of the function. */
23401 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23402 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23403 tf_warning_or_error
, tmpl
);
23406 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23407 tf_warning_or_error
, tmpl
,
23408 /*integral_constant_expression_p=*/false);
23410 /* Set the current input_location to the end of the function
23411 so that finish_function knows where we are. */
23413 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23415 /* Remember if we saw an infinite loop in the template. */
23416 current_function_infinite_loop
23417 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23420 /* Finish the function. */
23421 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23422 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23423 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23426 d
= finish_function (/*inline_p=*/false);
23427 expand_or_defer_fn (d
);
23430 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23431 cp_check_omp_declare_reduction (d
);
23434 /* We're not deferring instantiation any more. */
23435 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23438 pop_from_top_level ();
23440 pop_function_context ();
23443 restore_omp_privatization_clauses (omp_privatization_save
);
23446 pop_deferring_access_checks ();
23447 timevar_pop (TV_TEMPLATE_INST
);
23448 pop_tinst_level ();
23449 input_location
= saved_loc
;
23450 cp_unevaluated_operand
= saved_unevaluated_operand
;
23451 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23456 /* Run through the list of templates that we wish we could
23457 instantiate, and instantiate any we can. RETRIES is the
23458 number of times we retry pending template instantiation. */
23461 instantiate_pending_templates (int retries
)
23464 location_t saved_loc
= input_location
;
23466 /* Instantiating templates may trigger vtable generation. This in turn
23467 may require further template instantiations. We place a limit here
23468 to avoid infinite loop. */
23469 if (pending_templates
&& retries
>= max_tinst_depth
)
23471 tree decl
= pending_templates
->tinst
->decl
;
23473 fatal_error (input_location
,
23474 "template instantiation depth exceeds maximum of %d"
23475 " instantiating %q+D, possibly from virtual table generation"
23476 " (use -ftemplate-depth= to increase the maximum)",
23477 max_tinst_depth
, decl
);
23478 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23479 /* Pretend that we defined it. */
23480 DECL_INITIAL (decl
) = error_mark_node
;
23486 struct pending_template
**t
= &pending_templates
;
23487 struct pending_template
*last
= NULL
;
23491 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23492 bool complete
= false;
23494 if (TYPE_P (instantiation
))
23496 if (!COMPLETE_TYPE_P (instantiation
))
23498 instantiate_class_template (instantiation
);
23499 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23500 for (tree fld
= TYPE_FIELDS (instantiation
);
23501 fld
; fld
= TREE_CHAIN (fld
))
23503 || (TREE_CODE (fld
) == FUNCTION_DECL
23504 && !DECL_ARTIFICIAL (fld
)))
23505 && DECL_TEMPLATE_INSTANTIATION (fld
))
23506 instantiate_decl (fld
,
23507 /*defer_ok=*/false,
23508 /*expl_inst_class_mem_p=*/false);
23510 if (COMPLETE_TYPE_P (instantiation
))
23514 complete
= COMPLETE_TYPE_P (instantiation
);
23518 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23519 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23522 = instantiate_decl (instantiation
,
23523 /*defer_ok=*/false,
23524 /*expl_inst_class_mem_p=*/false);
23525 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23529 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23530 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23534 /* If INSTANTIATION has been instantiated, then we don't
23535 need to consider it again in the future. */
23543 current_tinst_level
= NULL
;
23545 last_pending_template
= last
;
23547 while (reconsider
);
23549 input_location
= saved_loc
;
23552 /* Substitute ARGVEC into T, which is a list of initializers for
23553 either base class or a non-static data member. The TREE_PURPOSEs
23554 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23555 instantiate_decl. */
23558 tsubst_initializer_list (tree t
, tree argvec
)
23560 tree inits
= NULL_TREE
;
23562 for (; t
; t
= TREE_CHAIN (t
))
23566 tree expanded_bases
= NULL_TREE
;
23567 tree expanded_arguments
= NULL_TREE
;
23570 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23575 /* Expand the base class expansion type into separate base
23577 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23578 tf_warning_or_error
,
23580 if (expanded_bases
== error_mark_node
)
23583 /* We'll be building separate TREE_LISTs of arguments for
23585 len
= TREE_VEC_LENGTH (expanded_bases
);
23586 expanded_arguments
= make_tree_vec (len
);
23587 for (i
= 0; i
< len
; i
++)
23588 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23590 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23591 expand each argument in the TREE_VALUE of t. */
23592 expr
= make_node (EXPR_PACK_EXPANSION
);
23593 PACK_EXPANSION_LOCAL_P (expr
) = true;
23594 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23595 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23597 if (TREE_VALUE (t
) == void_type_node
)
23598 /* VOID_TYPE_NODE is used to indicate
23599 value-initialization. */
23601 for (i
= 0; i
< len
; i
++)
23602 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23606 /* Substitute parameter packs into each argument in the
23608 in_base_initializer
= 1;
23609 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23611 tree expanded_exprs
;
23613 /* Expand the argument. */
23614 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23616 = tsubst_pack_expansion (expr
, argvec
,
23617 tf_warning_or_error
,
23619 if (expanded_exprs
== error_mark_node
)
23622 /* Prepend each of the expanded expressions to the
23623 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23624 for (i
= 0; i
< len
; i
++)
23626 TREE_VEC_ELT (expanded_arguments
, i
) =
23627 tree_cons (NULL_TREE
,
23628 TREE_VEC_ELT (expanded_exprs
, i
),
23629 TREE_VEC_ELT (expanded_arguments
, i
));
23632 in_base_initializer
= 0;
23634 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23635 since we built them backwards. */
23636 for (i
= 0; i
< len
; i
++)
23638 TREE_VEC_ELT (expanded_arguments
, i
) =
23639 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23644 for (i
= 0; i
< len
; ++i
)
23646 if (expanded_bases
)
23648 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23649 decl
= expand_member_init (decl
);
23650 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23655 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23656 tf_warning_or_error
, NULL_TREE
);
23658 decl
= expand_member_init (decl
);
23659 if (decl
&& !DECL_P (decl
))
23660 in_base_initializer
= 1;
23662 init
= TREE_VALUE (t
);
23664 if (init
!= void_type_node
)
23665 init
= tsubst_expr (init
, argvec
,
23666 tf_warning_or_error
, NULL_TREE
,
23667 /*integral_constant_expression_p=*/false);
23668 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23669 /* If we had an initializer but it instantiated to nothing,
23670 value-initialize the object. This will only occur when
23671 the initializer was a pack expansion where the parameter
23672 packs used in that expansion were of length zero. */
23673 init
= void_type_node
;
23674 in_base_initializer
= 0;
23679 init
= build_tree_list (decl
, init
);
23680 TREE_CHAIN (init
) = inits
;
23688 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23691 set_current_access_from_decl (tree decl
)
23693 if (TREE_PRIVATE (decl
))
23694 current_access_specifier
= access_private_node
;
23695 else if (TREE_PROTECTED (decl
))
23696 current_access_specifier
= access_protected_node
;
23698 current_access_specifier
= access_public_node
;
23701 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23702 is the instantiation (which should have been created with
23703 start_enum) and ARGS are the template arguments to use. */
23706 tsubst_enum (tree tag
, tree newtag
, tree args
)
23710 if (SCOPED_ENUM_P (newtag
))
23711 begin_scope (sk_scoped_enum
, newtag
);
23713 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23718 decl
= TREE_VALUE (e
);
23719 /* Note that in a template enum, the TREE_VALUE is the
23720 CONST_DECL, not the corresponding INTEGER_CST. */
23721 value
= tsubst_expr (DECL_INITIAL (decl
),
23722 args
, tf_warning_or_error
, NULL_TREE
,
23723 /*integral_constant_expression_p=*/true);
23725 /* Give this enumeration constant the correct access. */
23726 set_current_access_from_decl (decl
);
23728 /* Actually build the enumerator itself. Here we're assuming that
23729 enumerators can't have dependent attributes. */
23730 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23731 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23734 if (SCOPED_ENUM_P (newtag
))
23737 finish_enum_value_list (newtag
);
23738 finish_enum (newtag
);
23740 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23741 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23744 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23745 its type -- but without substituting the innermost set of template
23746 arguments. So, innermost set of template parameters will appear in
23750 get_mostly_instantiated_function_type (tree decl
)
23752 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23753 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23756 /* Return truthvalue if we're processing a template different from
23757 the last one involved in diagnostics. */
23759 problematic_instantiation_changed (void)
23761 return current_tinst_level
!= last_error_tinst_level
;
23764 /* Remember current template involved in diagnostics. */
23766 record_last_problematic_instantiation (void)
23768 last_error_tinst_level
= current_tinst_level
;
23771 struct tinst_level
*
23772 current_instantiation (void)
23774 return current_tinst_level
;
23777 /* Return TRUE if current_function_decl is being instantiated, false
23781 instantiating_current_function_p (void)
23783 return (current_instantiation ()
23784 && current_instantiation ()->decl
== current_function_decl
);
23787 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23788 type. Return false for ok, true for disallowed. Issue error and
23789 inform messages under control of COMPLAIN. */
23792 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23794 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23796 else if (POINTER_TYPE_P (type
))
23798 else if (TYPE_PTRMEM_P (type
))
23800 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23802 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23804 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23806 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23808 /* A bound template template parm could later be instantiated to have a valid
23809 nontype parm type via an alias template. */
23810 else if (cxx_dialect
>= cxx11
23811 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23814 if (complain
& tf_error
)
23816 if (type
== error_mark_node
)
23817 inform (input_location
, "invalid template non-type parameter");
23819 error ("%q#T is not a valid type for a template non-type parameter",
23825 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23826 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23829 dependent_type_p_r (tree type
)
23835 A type is dependent if it is:
23837 -- a template parameter. Template template parameters are types
23838 for us (since TYPE_P holds true for them) so we handle
23840 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23841 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23843 /* -- a qualified-id with a nested-name-specifier which contains a
23844 class-name that names a dependent type or whose unqualified-id
23845 names a dependent type. */
23846 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23849 /* An alias template specialization can be dependent even if the
23850 resulting type is not. */
23851 if (dependent_alias_template_spec_p (type
))
23854 /* -- a cv-qualified type where the cv-unqualified type is
23856 No code is necessary for this bullet; the code below handles
23857 cv-qualified types, and we don't want to strip aliases with
23858 TYPE_MAIN_VARIANT because of DR 1558. */
23859 /* -- a compound type constructed from any dependent type. */
23860 if (TYPE_PTRMEM_P (type
))
23861 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23862 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23864 else if (TYPE_PTR_P (type
)
23865 || TREE_CODE (type
) == REFERENCE_TYPE
)
23866 return dependent_type_p (TREE_TYPE (type
));
23867 else if (TREE_CODE (type
) == FUNCTION_TYPE
23868 || TREE_CODE (type
) == METHOD_TYPE
)
23872 if (dependent_type_p (TREE_TYPE (type
)))
23874 for (arg_type
= TYPE_ARG_TYPES (type
);
23876 arg_type
= TREE_CHAIN (arg_type
))
23877 if (dependent_type_p (TREE_VALUE (arg_type
)))
23879 if (cxx_dialect
>= cxx17
)
23880 /* A value-dependent noexcept-specifier makes the type dependent. */
23881 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
23882 if (tree noex
= TREE_PURPOSE (spec
))
23883 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23884 affect overload resolution and treating it as dependent breaks
23886 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
23887 && value_dependent_expression_p (noex
))
23891 /* -- an array type constructed from any dependent type or whose
23892 size is specified by a constant expression that is
23895 We checked for type- and value-dependence of the bounds in
23896 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23897 if (TREE_CODE (type
) == ARRAY_TYPE
)
23899 if (TYPE_DOMAIN (type
)
23900 && dependent_type_p (TYPE_DOMAIN (type
)))
23902 return dependent_type_p (TREE_TYPE (type
));
23905 /* -- a template-id in which either the template name is a template
23907 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23909 /* ... or any of the template arguments is a dependent type or
23910 an expression that is type-dependent or value-dependent. */
23911 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23912 && (any_dependent_template_arguments_p
23913 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23916 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23917 dependent; if the argument of the `typeof' expression is not
23918 type-dependent, then it should already been have resolved. */
23919 if (TREE_CODE (type
) == TYPEOF_TYPE
23920 || TREE_CODE (type
) == DECLTYPE_TYPE
23921 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23924 /* A template argument pack is dependent if any of its packed
23926 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23928 tree args
= ARGUMENT_PACK_ARGS (type
);
23929 int i
, len
= TREE_VEC_LENGTH (args
);
23930 for (i
= 0; i
< len
; ++i
)
23931 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23935 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23936 be template parameters. */
23937 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23940 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23943 /* The standard does not specifically mention types that are local
23944 to template functions or local classes, but they should be
23945 considered dependent too. For example:
23947 template <int I> void f() {
23952 The size of `E' cannot be known until the value of `I' has been
23953 determined. Therefore, `E' must be considered dependent. */
23954 scope
= TYPE_CONTEXT (type
);
23955 if (scope
&& TYPE_P (scope
))
23956 return dependent_type_p (scope
);
23957 /* Don't use type_dependent_expression_p here, as it can lead
23958 to infinite recursion trying to determine whether a lambda
23959 nested in a lambda is dependent (c++/47687). */
23960 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23961 && DECL_LANG_SPECIFIC (scope
)
23962 && DECL_TEMPLATE_INFO (scope
)
23963 && (any_dependent_template_arguments_p
23964 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23967 /* Other types are non-dependent. */
23971 /* Returns TRUE if TYPE is dependent, in the sense of
23972 [temp.dep.type]. Note that a NULL type is considered dependent. */
23975 dependent_type_p (tree type
)
23977 /* If there are no template parameters in scope, then there can't be
23978 any dependent types. */
23979 if (!processing_template_decl
)
23981 /* If we are not processing a template, then nobody should be
23982 providing us with a dependent type. */
23984 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23988 /* If the type is NULL, we have not computed a type for the entity
23989 in question; in that case, the type is dependent. */
23993 /* Erroneous types can be considered non-dependent. */
23994 if (type
== error_mark_node
)
23997 /* Getting here with global_type_node means we improperly called this
23998 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23999 gcc_checking_assert (type
!= global_type_node
);
24001 /* If we have not already computed the appropriate value for TYPE,
24003 if (!TYPE_DEPENDENT_P_VALID (type
))
24005 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
24006 TYPE_DEPENDENT_P_VALID (type
) = 1;
24009 return TYPE_DEPENDENT_P (type
);
24012 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24013 lookup. In other words, a dependent type that is not the current
24017 dependent_scope_p (tree scope
)
24019 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24020 && !currently_open_class (scope
));
24023 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24024 an unknown base of 'this' (and is therefore instantiation-dependent). */
24027 unknown_base_ref_p (tree t
)
24029 if (!current_class_ptr
)
24032 tree mem
= TREE_OPERAND (t
, 1);
24033 if (shared_member_p (mem
))
24036 tree cur
= current_nonlambda_class_type ();
24037 if (!any_dependent_bases_p (cur
))
24040 tree ctx
= TREE_OPERAND (t
, 0);
24041 if (DERIVED_FROM_P (ctx
, cur
))
24047 /* T is a SCOPE_REF; return whether we need to consider it
24048 instantiation-dependent so that we can check access at instantiation
24049 time even though we know which member it resolves to. */
24052 instantiation_dependent_scope_ref_p (tree t
)
24054 if (DECL_P (TREE_OPERAND (t
, 1))
24055 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24056 && !unknown_base_ref_p (t
)
24057 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24058 TREE_OPERAND (t
, 1)))
24064 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24065 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24068 /* Note that this predicate is not appropriate for general expressions;
24069 only constant expressions (that satisfy potential_constant_expression)
24070 can be tested for value dependence. */
24073 value_dependent_expression_p (tree expression
)
24075 if (!processing_template_decl
|| expression
== NULL_TREE
)
24078 /* A type-dependent expression is also value-dependent. */
24079 if (type_dependent_expression_p (expression
))
24082 switch (TREE_CODE (expression
))
24085 /* A dependent member function of the current instantiation. */
24086 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24088 case FUNCTION_DECL
:
24089 /* A dependent member function of the current instantiation. */
24090 if (DECL_CLASS_SCOPE_P (expression
)
24091 && dependent_type_p (DECL_CONTEXT (expression
)))
24095 case IDENTIFIER_NODE
:
24096 /* A name that has not been looked up -- must be dependent. */
24099 case TEMPLATE_PARM_INDEX
:
24100 /* A non-type template parm. */
24104 /* A non-type template parm. */
24105 if (DECL_TEMPLATE_PARM_P (expression
))
24107 return value_dependent_expression_p (DECL_INITIAL (expression
));
24110 /* A constant with literal type and is initialized
24111 with an expression that is value-dependent. */
24112 if (DECL_DEPENDENT_INIT_P (expression
)
24113 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24114 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24116 if (DECL_HAS_VALUE_EXPR_P (expression
))
24118 tree value_expr
= DECL_VALUE_EXPR (expression
);
24119 if (value_dependent_expression_p (value_expr
))
24124 case DYNAMIC_CAST_EXPR
:
24125 case STATIC_CAST_EXPR
:
24126 case CONST_CAST_EXPR
:
24127 case REINTERPRET_CAST_EXPR
:
24129 case IMPLICIT_CONV_EXPR
:
24130 /* These expressions are value-dependent if the type to which
24131 the cast occurs is dependent or the expression being casted
24132 is value-dependent. */
24134 tree type
= TREE_TYPE (expression
);
24136 if (dependent_type_p (type
))
24139 /* A functional cast has a list of operands. */
24140 expression
= TREE_OPERAND (expression
, 0);
24143 /* If there are no operands, it must be an expression such
24144 as "int()". This should not happen for aggregate types
24145 because it would form non-constant expressions. */
24146 gcc_assert (cxx_dialect
>= cxx11
24147 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24152 if (TREE_CODE (expression
) == TREE_LIST
)
24153 return any_value_dependent_elements_p (expression
);
24155 return value_dependent_expression_p (expression
);
24159 if (SIZEOF_EXPR_TYPE_P (expression
))
24160 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24164 /* A `sizeof' expression is value-dependent if the operand is
24165 type-dependent or is a pack expansion. */
24166 expression
= TREE_OPERAND (expression
, 0);
24167 if (PACK_EXPANSION_P (expression
))
24169 else if (TYPE_P (expression
))
24170 return dependent_type_p (expression
);
24171 return instantiation_dependent_uneval_expression_p (expression
);
24173 case AT_ENCODE_EXPR
:
24174 /* An 'encode' expression is value-dependent if the operand is
24176 expression
= TREE_OPERAND (expression
, 0);
24177 return dependent_type_p (expression
);
24179 case NOEXCEPT_EXPR
:
24180 expression
= TREE_OPERAND (expression
, 0);
24181 return instantiation_dependent_uneval_expression_p (expression
);
24184 /* All instantiation-dependent expressions should also be considered
24185 value-dependent. */
24186 return instantiation_dependent_scope_ref_p (expression
);
24188 case COMPONENT_REF
:
24189 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24190 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24192 case NONTYPE_ARGUMENT_PACK
:
24193 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24194 is value-dependent. */
24196 tree values
= ARGUMENT_PACK_ARGS (expression
);
24197 int i
, len
= TREE_VEC_LENGTH (values
);
24199 for (i
= 0; i
< len
; ++i
)
24200 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24208 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24210 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24216 if (TREE_CODE (type2
) != TREE_LIST
)
24217 return dependent_type_p (type2
);
24219 for (; type2
; type2
= TREE_CHAIN (type2
))
24220 if (dependent_type_p (TREE_VALUE (type2
)))
24227 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24228 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24231 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24232 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24236 tree op
= TREE_OPERAND (expression
, 0);
24237 return (value_dependent_expression_p (op
)
24238 || has_value_dependent_address (op
));
24241 case REQUIRES_EXPR
:
24242 /* Treat all requires-expressions as value-dependent so
24243 we don't try to fold them. */
24247 return dependent_type_p (TREE_OPERAND (expression
, 0));
24251 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24253 tree fn
= get_callee_fndecl (expression
);
24255 nargs
= call_expr_nargs (expression
);
24256 for (i
= 0; i
< nargs
; ++i
)
24258 tree op
= CALL_EXPR_ARG (expression
, i
);
24259 /* In a call to a constexpr member function, look through the
24260 implicit ADDR_EXPR on the object argument so that it doesn't
24261 cause the call to be considered value-dependent. We also
24262 look through it in potential_constant_expression. */
24263 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24264 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24265 && TREE_CODE (op
) == ADDR_EXPR
)
24266 op
= TREE_OPERAND (op
, 0);
24267 if (value_dependent_expression_p (op
))
24273 case TEMPLATE_ID_EXPR
:
24274 return variable_concept_p (TREE_OPERAND (expression
, 0));
24280 if (dependent_type_p (TREE_TYPE (expression
)))
24282 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24283 if (value_dependent_expression_p (val
))
24289 /* Treat a GNU statement expression as dependent to avoid crashing
24290 under instantiate_non_dependent_expr; it can't be constant. */
24294 /* A constant expression is value-dependent if any subexpression is
24295 value-dependent. */
24296 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24298 case tcc_reference
:
24300 case tcc_comparison
:
24302 case tcc_expression
:
24305 int i
, len
= cp_tree_operand_length (expression
);
24307 for (i
= 0; i
< len
; i
++)
24309 tree t
= TREE_OPERAND (expression
, i
);
24311 /* In some cases, some of the operands may be missing.
24312 (For example, in the case of PREDECREMENT_EXPR, the
24313 amount to increment by may be missing.) That doesn't
24314 make the expression dependent. */
24315 if (t
&& value_dependent_expression_p (t
))
24326 /* The expression is not value-dependent. */
24330 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24331 [temp.dep.expr]. Note that an expression with no type is
24332 considered dependent. Other parts of the compiler arrange for an
24333 expression with type-dependent subexpressions to have no type, so
24334 this function doesn't have to be fully recursive. */
24337 type_dependent_expression_p (tree expression
)
24339 if (!processing_template_decl
)
24342 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24345 STRIP_ANY_LOCATION_WRAPPER (expression
);
24347 /* An unresolved name is always dependent. */
24348 if (identifier_p (expression
)
24349 || TREE_CODE (expression
) == USING_DECL
24350 || TREE_CODE (expression
) == WILDCARD_DECL
)
24353 /* A fold expression is type-dependent. */
24354 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24355 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24356 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24357 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24360 /* Some expression forms are never type-dependent. */
24361 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24362 || TREE_CODE (expression
) == SIZEOF_EXPR
24363 || TREE_CODE (expression
) == ALIGNOF_EXPR
24364 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24365 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24366 || TREE_CODE (expression
) == TRAIT_EXPR
24367 || TREE_CODE (expression
) == TYPEID_EXPR
24368 || TREE_CODE (expression
) == DELETE_EXPR
24369 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24370 || TREE_CODE (expression
) == THROW_EXPR
24371 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24374 /* The types of these expressions depends only on the type to which
24375 the cast occurs. */
24376 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24377 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24378 || TREE_CODE (expression
) == CONST_CAST_EXPR
24379 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24380 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24381 || TREE_CODE (expression
) == CAST_EXPR
)
24382 return dependent_type_p (TREE_TYPE (expression
));
24384 /* The types of these expressions depends only on the type created
24385 by the expression. */
24386 if (TREE_CODE (expression
) == NEW_EXPR
24387 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24389 /* For NEW_EXPR tree nodes created inside a template, either
24390 the object type itself or a TREE_LIST may appear as the
24392 tree type
= TREE_OPERAND (expression
, 1);
24393 if (TREE_CODE (type
) == TREE_LIST
)
24394 /* This is an array type. We need to check array dimensions
24396 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24397 || value_dependent_expression_p
24398 (TREE_OPERAND (TREE_VALUE (type
), 1));
24400 return dependent_type_p (type
);
24403 if (TREE_CODE (expression
) == SCOPE_REF
)
24405 tree scope
= TREE_OPERAND (expression
, 0);
24406 tree name
= TREE_OPERAND (expression
, 1);
24408 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24409 contains an identifier associated by name lookup with one or more
24410 declarations declared with a dependent type, or...a
24411 nested-name-specifier or qualified-id that names a member of an
24412 unknown specialization. */
24413 return (type_dependent_expression_p (name
)
24414 || dependent_scope_p (scope
));
24417 if (TREE_CODE (expression
) == TEMPLATE_DECL
24418 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24419 return uses_outer_template_parms (expression
);
24421 if (TREE_CODE (expression
) == STMT_EXPR
)
24422 expression
= stmt_expr_value_expr (expression
);
24424 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24429 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24431 if (type_dependent_expression_p (elt
))
24437 /* A static data member of the current instantiation with incomplete
24438 array type is type-dependent, as the definition and specializations
24439 can have different bounds. */
24440 if (VAR_P (expression
)
24441 && DECL_CLASS_SCOPE_P (expression
)
24442 && dependent_type_p (DECL_CONTEXT (expression
))
24443 && VAR_HAD_UNKNOWN_BOUND (expression
))
24446 /* An array of unknown bound depending on a variadic parameter, eg:
24448 template<typename... Args>
24449 void foo (Args... args)
24451 int arr[] = { args... };
24454 template<int... vals>
24457 int arr[] = { vals... };
24460 If the array has no length and has an initializer, it must be that
24461 we couldn't determine its length in cp_complete_array_type because
24462 it is dependent. */
24463 if (VAR_P (expression
)
24464 && TREE_TYPE (expression
) != NULL_TREE
24465 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24466 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24467 && DECL_INITIAL (expression
))
24470 /* A function or variable template-id is type-dependent if it has any
24471 dependent template arguments. */
24472 if (VAR_OR_FUNCTION_DECL_P (expression
)
24473 && DECL_LANG_SPECIFIC (expression
)
24474 && DECL_TEMPLATE_INFO (expression
))
24476 /* Consider the innermost template arguments, since those are the ones
24477 that come from the template-id; the template arguments for the
24478 enclosing class do not make it type-dependent unless they are used in
24479 the type of the decl. */
24480 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24481 && (any_dependent_template_arguments_p
24482 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24486 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24487 type-dependent. Checking this is important for functions with auto return
24488 type, which looks like a dependent type. */
24489 if (TREE_CODE (expression
) == FUNCTION_DECL
24490 && !(DECL_CLASS_SCOPE_P (expression
)
24491 && dependent_type_p (DECL_CONTEXT (expression
)))
24492 && !(DECL_FRIEND_P (expression
)
24493 && (!DECL_FRIEND_CONTEXT (expression
)
24494 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24495 && !DECL_LOCAL_FUNCTION_P (expression
))
24497 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24498 || undeduced_auto_decl (expression
));
24502 /* Always dependent, on the number of arguments if nothing else. */
24503 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24506 if (TREE_TYPE (expression
) == unknown_type_node
)
24508 if (TREE_CODE (expression
) == ADDR_EXPR
)
24509 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24510 if (TREE_CODE (expression
) == COMPONENT_REF
24511 || TREE_CODE (expression
) == OFFSET_REF
)
24513 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24515 expression
= TREE_OPERAND (expression
, 1);
24516 if (identifier_p (expression
))
24519 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24520 if (TREE_CODE (expression
) == SCOPE_REF
)
24523 if (BASELINK_P (expression
))
24525 if (BASELINK_OPTYPE (expression
)
24526 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24528 expression
= BASELINK_FUNCTIONS (expression
);
24531 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24533 if (any_dependent_template_arguments_p
24534 (TREE_OPERAND (expression
, 1)))
24536 expression
= TREE_OPERAND (expression
, 0);
24537 if (identifier_p (expression
))
24541 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24542 || TREE_CODE (expression
) == FUNCTION_DECL
);
24544 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24545 if (type_dependent_expression_p (*iter
))
24551 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24553 /* Dependent type attributes might not have made it from the decl to
24555 if (DECL_P (expression
)
24556 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24559 return (dependent_type_p (TREE_TYPE (expression
)));
24562 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24563 type-dependent if the expression refers to a member of the current
24564 instantiation and the type of the referenced member is dependent, or the
24565 class member access expression refers to a member of an unknown
24568 This function returns true if the OBJECT in such a class member access
24569 expression is of an unknown specialization. */
24572 type_dependent_object_expression_p (tree object
)
24574 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24576 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24578 tree scope
= TREE_TYPE (object
);
24579 return (!scope
|| dependent_scope_p (scope
));
24582 /* walk_tree callback function for instantiation_dependent_expression_p,
24583 below. Returns non-zero if a dependent subexpression is found. */
24586 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24591 /* We don't have to worry about decltype currently because decltype
24592 of an instantiation-dependent expr is a dependent type. This
24593 might change depending on the resolution of DR 1172. */
24594 *walk_subtrees
= false;
24597 enum tree_code code
= TREE_CODE (*tp
);
24600 /* Don't treat an argument list as dependent just because it has no
24606 case TEMPLATE_PARM_INDEX
:
24609 /* Handle expressions with type operands. */
24613 case AT_ENCODE_EXPR
:
24615 tree op
= TREE_OPERAND (*tp
, 0);
24616 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24617 op
= TREE_TYPE (op
);
24620 if (dependent_type_p (op
))
24624 *walk_subtrees
= false;
24631 case COMPONENT_REF
:
24632 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24633 /* In a template, finish_class_member_access_expr creates a
24634 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24635 type-dependent, so that we can check access control at
24636 instantiation time (PR 42277). See also Core issue 1273. */
24641 if (instantiation_dependent_scope_ref_p (*tp
))
24646 /* Treat statement-expressions as dependent. */
24650 /* Treat requires-expressions as dependent. */
24651 case REQUIRES_EXPR
:
24655 /* Treat calls to function concepts as dependent. */
24656 if (function_concept_check_p (*tp
))
24660 case TEMPLATE_ID_EXPR
:
24661 /* And variable concepts. */
24662 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24670 if (type_dependent_expression_p (*tp
))
24676 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24677 sense defined by the ABI:
24679 "An expression is instantiation-dependent if it is type-dependent
24680 or value-dependent, or it has a subexpression that is type-dependent
24681 or value-dependent."
24683 Except don't actually check value-dependence for unevaluated expressions,
24684 because in sizeof(i) we don't care about the value of i. Checking
24685 type-dependence will in turn check value-dependence of array bounds/template
24686 arguments as needed. */
24689 instantiation_dependent_uneval_expression_p (tree expression
)
24693 if (!processing_template_decl
)
24696 if (expression
== error_mark_node
)
24699 result
= cp_walk_tree_without_duplicates (&expression
,
24700 instantiation_dependent_r
, NULL
);
24701 return result
!= NULL_TREE
;
24704 /* As above, but also check value-dependence of the expression as a whole. */
24707 instantiation_dependent_expression_p (tree expression
)
24709 return (instantiation_dependent_uneval_expression_p (expression
)
24710 || value_dependent_expression_p (expression
));
24713 /* Like type_dependent_expression_p, but it also works while not processing
24714 a template definition, i.e. during substitution or mangling. */
24717 type_dependent_expression_p_push (tree expr
)
24720 ++processing_template_decl
;
24721 b
= type_dependent_expression_p (expr
);
24722 --processing_template_decl
;
24726 /* Returns TRUE if ARGS contains a type-dependent expression. */
24729 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24734 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24736 if (type_dependent_expression_p (arg
))
24742 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24743 expressions) contains any type-dependent expressions. */
24746 any_type_dependent_elements_p (const_tree list
)
24748 for (; list
; list
= TREE_CHAIN (list
))
24749 if (type_dependent_expression_p (TREE_VALUE (list
)))
24755 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24756 expressions) contains any value-dependent expressions. */
24759 any_value_dependent_elements_p (const_tree list
)
24761 for (; list
; list
= TREE_CHAIN (list
))
24762 if (value_dependent_expression_p (TREE_VALUE (list
)))
24768 /* Returns TRUE if the ARG (a template argument) is dependent. */
24771 dependent_template_arg_p (tree arg
)
24773 if (!processing_template_decl
)
24776 /* Assume a template argument that was wrongly written by the user
24777 is dependent. This is consistent with what
24778 any_dependent_template_arguments_p [that calls this function]
24780 if (!arg
|| arg
== error_mark_node
)
24783 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24784 arg
= argument_pack_select_arg (arg
);
24786 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24788 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24790 if (DECL_TEMPLATE_PARM_P (arg
))
24792 /* A member template of a dependent class is not necessarily
24793 type-dependent, but it is a dependent template argument because it
24794 will be a member of an unknown specialization to that template. */
24795 tree scope
= CP_DECL_CONTEXT (arg
);
24796 return TYPE_P (scope
) && dependent_type_p (scope
);
24798 else if (ARGUMENT_PACK_P (arg
))
24800 tree args
= ARGUMENT_PACK_ARGS (arg
);
24801 int i
, len
= TREE_VEC_LENGTH (args
);
24802 for (i
= 0; i
< len
; ++i
)
24804 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24810 else if (TYPE_P (arg
))
24811 return dependent_type_p (arg
);
24813 return (type_dependent_expression_p (arg
)
24814 || value_dependent_expression_p (arg
));
24817 /* Returns true if ARGS (a collection of template arguments) contains
24818 any types that require structural equality testing. */
24821 any_template_arguments_need_structural_equality_p (tree args
)
24828 if (args
== error_mark_node
)
24831 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24833 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24834 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24836 tree arg
= TREE_VEC_ELT (level
, j
);
24837 tree packed_args
= NULL_TREE
;
24840 if (ARGUMENT_PACK_P (arg
))
24842 /* Look inside the argument pack. */
24843 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24844 len
= TREE_VEC_LENGTH (packed_args
);
24847 for (k
= 0; k
< len
; ++k
)
24850 arg
= TREE_VEC_ELT (packed_args
, k
);
24852 if (error_operand_p (arg
))
24854 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24856 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24858 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24859 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24868 /* Returns true if ARGS (a collection of template arguments) contains
24869 any dependent arguments. */
24872 any_dependent_template_arguments_p (const_tree args
)
24879 if (args
== error_mark_node
)
24882 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24884 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24885 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24886 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24893 /* Returns TRUE if the template TMPL is type-dependent. */
24896 dependent_template_p (tree tmpl
)
24898 if (TREE_CODE (tmpl
) == OVERLOAD
)
24900 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24901 if (dependent_template_p (*iter
))
24906 /* Template template parameters are dependent. */
24907 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24908 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24910 /* So are names that have not been looked up. */
24911 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24916 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24919 dependent_template_id_p (tree tmpl
, tree args
)
24921 return (dependent_template_p (tmpl
)
24922 || any_dependent_template_arguments_p (args
));
24925 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24929 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24933 if (!processing_template_decl
)
24936 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24938 tree decl
= TREE_VEC_ELT (declv
, i
);
24939 tree init
= TREE_VEC_ELT (initv
, i
);
24940 tree cond
= TREE_VEC_ELT (condv
, i
);
24941 tree incr
= TREE_VEC_ELT (incrv
, i
);
24943 if (type_dependent_expression_p (decl
)
24944 || TREE_CODE (decl
) == SCOPE_REF
)
24947 if (init
&& type_dependent_expression_p (init
))
24950 if (type_dependent_expression_p (cond
))
24953 if (COMPARISON_CLASS_P (cond
)
24954 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24955 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24958 if (TREE_CODE (incr
) == MODOP_EXPR
)
24960 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24961 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24964 else if (type_dependent_expression_p (incr
))
24966 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24968 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24970 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24972 tree t
= TREE_OPERAND (incr
, 1);
24973 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24974 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24983 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24984 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24985 no such TYPE can be found. Note that this function peers inside
24986 uninstantiated templates and therefore should be used only in
24987 extremely limited situations. ONLY_CURRENT_P restricts this
24988 peering to the currently open classes hierarchy (which is required
24989 when comparing types). */
24992 resolve_typename_type (tree type
, bool only_current_p
)
25001 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
25003 scope
= TYPE_CONTEXT (type
);
25004 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25005 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25006 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25007 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25008 identifier of the TYPENAME_TYPE anymore.
25009 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25010 TYPENAME_TYPE instead, we avoid messing up with a possible
25011 typedef variant case. */
25012 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25014 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25015 it first before we can figure out what NAME refers to. */
25016 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25018 if (TYPENAME_IS_RESOLVING_P (scope
))
25019 /* Given a class template A with a dependent base with nested type C,
25020 typedef typename A::C::C C will land us here, as trying to resolve
25021 the initial A::C leads to the local C typedef, which leads back to
25022 A::C::C. So we break the recursion now. */
25025 scope
= resolve_typename_type (scope
, only_current_p
);
25027 /* If we don't know what SCOPE refers to, then we cannot resolve the
25029 if (!CLASS_TYPE_P (scope
))
25031 /* If this is a typedef, we don't want to look inside (c++/11987). */
25032 if (typedef_variant_p (type
))
25034 /* If SCOPE isn't the template itself, it will not have a valid
25035 TYPE_FIELDS list. */
25036 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25037 /* scope is either the template itself or a compatible instantiation
25038 like X<T>, so look up the name in the original template. */
25039 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25040 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25041 gcc_checking_assert (uses_template_parms (scope
));
25042 /* If scope has no fields, it can't be a current instantiation. Check this
25043 before currently_open_class to avoid infinite recursion (71515). */
25044 if (!TYPE_FIELDS (scope
))
25046 /* If the SCOPE is not the current instantiation, there's no reason
25047 to look inside it. */
25048 if (only_current_p
&& !currently_open_class (scope
))
25050 /* Enter the SCOPE so that name lookup will be resolved as if we
25051 were in the class definition. In particular, SCOPE will no
25052 longer be considered a dependent type. */
25053 pushed_scope
= push_scope (scope
);
25054 /* Look up the declaration. */
25055 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25056 tf_warning_or_error
);
25058 result
= NULL_TREE
;
25060 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25061 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25062 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25065 else if (identifier_p (fullname
)
25066 && TREE_CODE (decl
) == TYPE_DECL
)
25068 result
= TREE_TYPE (decl
);
25069 if (result
== error_mark_node
)
25070 result
= NULL_TREE
;
25072 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25073 && DECL_CLASS_TEMPLATE_P (decl
))
25075 /* Obtain the template and the arguments. */
25076 tree tmpl
= TREE_OPERAND (fullname
, 0);
25077 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25079 /* We get here with a plain identifier because a previous tentative
25080 parse of the nested-name-specifier as part of a ptr-operator saw
25081 ::template X<A>. The use of ::template is necessary in a
25082 ptr-operator, but wrong in a declarator-id.
25084 [temp.names]: In a qualified-id of a declarator-id, the keyword
25085 template shall not appear at the top level. */
25086 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25087 "keyword %<template%> not allowed in declarator-id");
25090 tree args
= TREE_OPERAND (fullname
, 1);
25091 /* Instantiate the template. */
25092 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25093 /*entering_scope=*/true,
25094 tf_error
| tf_user
);
25095 if (result
== error_mark_node
)
25096 result
= NULL_TREE
;
25099 /* Leave the SCOPE. */
25101 pop_scope (pushed_scope
);
25103 /* If we failed to resolve it, return the original typename. */
25107 /* If lookup found a typename type, resolve that too. */
25108 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25110 /* Ill-formed programs can cause infinite recursion here, so we
25111 must catch that. */
25112 TYPENAME_IS_RESOLVING_P (result
) = 1;
25113 result
= resolve_typename_type (result
, only_current_p
);
25114 TYPENAME_IS_RESOLVING_P (result
) = 0;
25117 /* Qualify the resulting type. */
25118 quals
= cp_type_quals (type
);
25120 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25125 /* EXPR is an expression which is not type-dependent. Return a proxy
25126 for EXPR that can be used to compute the types of larger
25127 expressions containing EXPR. */
25130 build_non_dependent_expr (tree expr
)
25132 tree orig_expr
= expr
;
25135 /* When checking, try to get a constant value for all non-dependent
25136 expressions in order to expose bugs in *_dependent_expression_p
25137 and constexpr. This can affect code generation, see PR70704, so
25138 only do this for -fchecking=2. */
25139 if (flag_checking
> 1
25140 && cxx_dialect
>= cxx11
25141 /* Don't do this during nsdmi parsing as it can lead to
25142 unexpected recursive instantiations. */
25143 && !parsing_nsdmi ()
25144 /* Don't do this during concept expansion either and for
25145 the same reason. */
25146 && !expanding_concept ())
25147 fold_non_dependent_expr (expr
);
25149 STRIP_ANY_LOCATION_WRAPPER (expr
);
25151 /* Preserve OVERLOADs; the functions must be available to resolve
25154 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25155 inner_expr
= stmt_expr_value_expr (inner_expr
);
25156 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25157 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25158 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25159 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25160 if (is_overloaded_fn (inner_expr
)
25161 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25163 /* There is no need to return a proxy for a variable. */
25166 /* Preserve string constants; conversions from string constants to
25167 "char *" are allowed, even though normally a "const char *"
25168 cannot be used to initialize a "char *". */
25169 if (TREE_CODE (expr
) == STRING_CST
)
25171 /* Preserve void and arithmetic constants, as an optimization -- there is no
25172 reason to create a new node. */
25173 if (TREE_CODE (expr
) == VOID_CST
25174 || TREE_CODE (expr
) == INTEGER_CST
25175 || TREE_CODE (expr
) == REAL_CST
)
25177 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25178 There is at least one place where we want to know that a
25179 particular expression is a throw-expression: when checking a ?:
25180 expression, there are special rules if the second or third
25181 argument is a throw-expression. */
25182 if (TREE_CODE (expr
) == THROW_EXPR
)
25185 /* Don't wrap an initializer list, we need to be able to look inside. */
25186 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25189 /* Don't wrap a dummy object, we need to be able to test for it. */
25190 if (is_dummy_object (expr
))
25193 if (TREE_CODE (expr
) == COND_EXPR
)
25194 return build3 (COND_EXPR
,
25196 TREE_OPERAND (expr
, 0),
25197 (TREE_OPERAND (expr
, 1)
25198 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25199 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25200 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25201 if (TREE_CODE (expr
) == COMPOUND_EXPR
25202 && !COMPOUND_EXPR_OVERLOADED (expr
))
25203 return build2 (COMPOUND_EXPR
,
25205 TREE_OPERAND (expr
, 0),
25206 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25208 /* If the type is unknown, it can't really be non-dependent */
25209 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25211 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25212 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25215 /* ARGS is a vector of expressions as arguments to a function call.
25216 Replace the arguments with equivalent non-dependent expressions.
25217 This modifies ARGS in place. */
25220 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25225 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25227 tree newarg
= build_non_dependent_expr (arg
);
25229 (*args
)[ix
] = newarg
;
25233 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25234 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25235 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25238 make_auto_1 (tree name
, bool set_canonical
)
25240 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25241 TYPE_NAME (au
) = build_decl (input_location
,
25242 TYPE_DECL
, name
, au
);
25243 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25244 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25245 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25246 TYPE_NAME (au
), NULL_TREE
);
25248 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25249 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25250 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25256 make_decltype_auto (void)
25258 return make_auto_1 (decltype_auto_identifier
, true);
25264 return make_auto_1 (auto_identifier
, true);
25267 /* Return a C++17 deduction placeholder for class template TMPL. */
25270 make_template_placeholder (tree tmpl
)
25272 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25273 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25277 /* True iff T is a C++17 class template deduction placeholder. */
25280 template_placeholder_p (tree t
)
25282 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25285 /* Make a "constrained auto" type-specifier. This is an
25286 auto type with constraints that must be associated after
25287 deduction. The constraint is formed from the given
25288 CONC and its optional sequence of arguments, which are
25289 non-null if written as partial-concept-id. */
25292 make_constrained_auto (tree con
, tree args
)
25294 tree type
= make_auto_1 (auto_identifier
, false);
25296 /* Build the constraint. */
25297 tree tmpl
= DECL_TI_TEMPLATE (con
);
25298 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25299 expr
= build_concept_check (expr
, type
, args
);
25301 tree constr
= normalize_expression (expr
);
25302 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25304 /* Our canonical type depends on the constraint. */
25305 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25307 /* Attach the constraint to the type declaration. */
25308 tree decl
= TYPE_NAME (type
);
25312 /* Given type ARG, return std::initializer_list<ARG>. */
25317 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25319 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25321 gcc_rich_location
richloc (input_location
);
25322 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25323 error_at (&richloc
,
25324 "deducing from brace-enclosed initializer list"
25325 " requires %<#include <initializer_list>%>");
25327 return error_mark_node
;
25329 tree argvec
= make_tree_vec (1);
25330 TREE_VEC_ELT (argvec
, 0) = arg
;
25332 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25333 NULL_TREE
, 0, tf_warning_or_error
);
25336 /* Replace auto in TYPE with std::initializer_list<auto>. */
25339 listify_autos (tree type
, tree auto_node
)
25341 tree init_auto
= listify (auto_node
);
25342 tree argvec
= make_tree_vec (1);
25343 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25344 if (processing_template_decl
)
25345 argvec
= add_to_template_args (current_template_args (), argvec
);
25346 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25349 /* Hash traits for hashing possibly constrained 'auto'
25350 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25352 struct auto_hash
: default_hash_traits
<tree
>
25354 static inline hashval_t
hash (tree
);
25355 static inline bool equal (tree
, tree
);
25358 /* Hash the 'auto' T. */
25361 auto_hash::hash (tree t
)
25363 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25364 /* Matching constrained-type-specifiers denote the same template
25365 parameter, so hash the constraint. */
25366 return hash_placeholder_constraint (c
);
25368 /* But unconstrained autos are all separate, so just hash the pointer. */
25369 return iterative_hash_object (t
, 0);
25372 /* Compare two 'auto's. */
25375 auto_hash::equal (tree t1
, tree t2
)
25380 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25381 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25383 /* Two unconstrained autos are distinct. */
25387 return equivalent_placeholder_constraints (c1
, c2
);
25390 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25391 constrained) auto, add it to the vector. */
25394 extract_autos_r (tree t
, void *data
)
25396 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25399 /* All the autos were built with index 0; fix that up now. */
25400 tree
*p
= hash
.find_slot (t
, INSERT
);
25403 /* If this is a repeated constrained-type-specifier, use the index we
25405 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25408 /* Otherwise this is new, so use the current count. */
25410 idx
= hash
.elements () - 1;
25412 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25415 /* Always keep walking. */
25419 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25420 says they can appear anywhere in the type. */
25423 extract_autos (tree type
)
25425 hash_set
<tree
> visited
;
25426 hash_table
<auto_hash
> hash (2);
25428 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25430 tree tree_vec
= make_tree_vec (hash
.elements());
25431 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25432 iter
!= hash
.end(); ++iter
)
25435 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25436 TREE_VEC_ELT (tree_vec
, i
)
25437 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25443 /* The stem for deduction guide names. */
25444 const char *const dguide_base
= "__dguide_";
25446 /* Return the name for a deduction guide for class template TMPL. */
25449 dguide_name (tree tmpl
)
25451 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25452 tree tname
= TYPE_IDENTIFIER (type
);
25453 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25454 + IDENTIFIER_LENGTH (tname
));
25455 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25456 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25457 IDENTIFIER_LENGTH (tname
) + 1);
25458 tree dname
= get_identifier (buf
);
25459 TREE_TYPE (dname
) = type
;
25463 /* True if NAME is the name of a deduction guide. */
25466 dguide_name_p (tree name
)
25468 return (TREE_CODE (name
) == IDENTIFIER_NODE
25469 && TREE_TYPE (name
)
25470 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25471 strlen (dguide_base
)));
25474 /* True if FN is a deduction guide. */
25477 deduction_guide_p (const_tree fn
)
25480 if (tree name
= DECL_NAME (fn
))
25481 return dguide_name_p (name
);
25485 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25488 copy_guide_p (const_tree fn
)
25490 gcc_assert (deduction_guide_p (fn
));
25491 if (!DECL_ARTIFICIAL (fn
))
25493 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25494 return (TREE_CHAIN (parms
) == void_list_node
25495 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25498 /* True if FN is a guide generated from a constructor template. */
25501 template_guide_p (const_tree fn
)
25503 gcc_assert (deduction_guide_p (fn
));
25504 if (!DECL_ARTIFICIAL (fn
))
25506 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25507 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25508 return PRIMARY_TEMPLATE_P (org
);
25512 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25513 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25514 template parameter types. Note that the handling of template template
25515 parameters relies on current_template_parms being set appropriately for the
25519 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25520 tree tsubst_args
, tsubst_flags_t complain
)
25522 tree oldidx
= get_template_parm_index (olddecl
);
25525 if (TREE_CODE (olddecl
) == TYPE_DECL
25526 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25528 tree oldtype
= TREE_TYPE (olddecl
);
25529 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25530 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25531 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25532 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25533 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25536 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25537 complain
, NULL_TREE
);
25540 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25541 DECL_NAME (olddecl
), newtype
);
25542 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25545 if (TREE_CODE (olddecl
) == TYPE_DECL
25546 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25548 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25549 = build_template_parm_index (index
, level
, level
,
25551 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25552 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25553 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25554 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25556 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25558 DECL_TEMPLATE_RESULT (newdecl
)
25559 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25560 DECL_NAME (olddecl
), newtype
);
25561 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25562 // First create a copy (ttargs) of tsubst_args with an
25563 // additional level for the template template parameter's own
25564 // template parameters (ttparms).
25565 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25566 (DECL_TEMPLATE_PARMS (olddecl
)));
25567 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25568 tree ttargs
= make_tree_vec (depth
+ 1);
25569 for (int i
= 0; i
< depth
; ++i
)
25570 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25571 TREE_VEC_ELT (ttargs
, depth
)
25572 = template_parms_level_to_args (ttparms
);
25573 // Substitute ttargs into ttparms to fix references to
25574 // other template parameters.
25575 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25577 // Now substitute again with args based on tparms, to reduce
25578 // the level of the ttparms.
25579 ttargs
= current_template_args ();
25580 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25582 // Finally, tack the adjusted parms onto tparms.
25583 ttparms
= tree_cons (size_int (depth
), ttparms
,
25584 current_template_parms
);
25585 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25590 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25592 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25593 TREE_CODE (oldconst
),
25594 DECL_NAME (oldconst
), newtype
);
25595 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25596 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25597 SET_DECL_TEMPLATE_PARM_P (newconst
);
25598 newidx
= build_template_parm_index (index
, level
, level
,
25599 newconst
, newtype
);
25600 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25601 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25602 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25608 /* Returns a C++17 class deduction guide template based on the constructor
25609 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25610 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25613 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25615 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25616 bool memtmpl
= false;
25619 tree fn_tmpl
= NULL_TREE
;
25624 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25627 type
= TREE_TYPE (type
);
25628 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25631 fparms
= void_list_node
;
25633 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25634 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25635 targs
= CLASSTYPE_TI_ARGS (type
);
25638 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25639 explicit_p
= false;
25643 ++processing_template_decl
;
25646 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25647 : DECL_TI_TEMPLATE (ctor
));
25649 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25650 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25652 type
= DECL_CONTEXT (ctor
);
25654 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25655 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25656 fully specialized args for the enclosing class. Strip those off, as
25657 the deduction guide won't have those template parameters. */
25658 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25659 TMPL_PARMS_DEPTH (tparms
));
25660 /* Discard the 'this' parameter. */
25661 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25662 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25663 ci
= get_constraints (ctor
);
25664 loc
= DECL_SOURCE_LOCATION (ctor
);
25665 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25667 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25671 /* For a member template constructor, we need to flatten the two
25672 template parameter lists into one, and then adjust the function
25673 signature accordingly. This gets...complicated. */
25674 tree save_parms
= current_template_parms
;
25676 /* For a member template we should have two levels of parms/args, one
25677 for the class and one for the constructor. We stripped
25678 specialized args for further enclosing classes above. */
25679 const int depth
= 2;
25680 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25682 /* Template args for translating references to the two-level template
25683 parameters into references to the one-level template parameters we
25685 tree tsubst_args
= copy_node (targs
);
25686 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25687 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25689 /* Template parms for the constructor template. */
25690 tree ftparms
= TREE_VALUE (tparms
);
25691 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25692 /* Template parms for the class template. */
25693 tparms
= TREE_CHAIN (tparms
);
25694 tree ctparms
= TREE_VALUE (tparms
);
25695 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25696 /* Template parms for the deduction guide start as a copy of the
25697 template parms for the class. We set current_template_parms for
25698 lookup_template_class_1. */
25699 current_template_parms
= tparms
= copy_node (tparms
);
25700 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25701 for (unsigned i
= 0; i
< clen
; ++i
)
25702 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25704 /* Now we need to rewrite the constructor parms to append them to the
25706 for (unsigned i
= 0; i
< flen
; ++i
)
25708 unsigned index
= i
+ clen
;
25709 unsigned level
= 1;
25710 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25711 tree olddecl
= TREE_VALUE (oldelt
);
25712 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25713 tsubst_args
, complain
);
25714 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25715 tsubst_args
, complain
, ctor
);
25716 tree list
= build_tree_list (newdef
, newdecl
);
25717 TEMPLATE_PARM_CONSTRAINTS (list
)
25718 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25719 tsubst_args
, complain
, ctor
);
25720 TREE_VEC_ELT (new_vec
, index
) = list
;
25721 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25724 /* Now we have a final set of template parms to substitute into the
25725 function signature. */
25726 targs
= template_parms_to_args (tparms
);
25727 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25729 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25731 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25733 current_template_parms
= save_parms
;
25735 --processing_template_decl
;
25740 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25741 tparms
= copy_node (tparms
);
25742 INNERMOST_TEMPLATE_PARMS (tparms
)
25743 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25746 tree fntype
= build_function_type (type
, fparms
);
25747 tree ded_fn
= build_lang_decl_loc (loc
,
25749 dguide_name (type
), fntype
);
25750 DECL_ARGUMENTS (ded_fn
) = fargs
;
25751 DECL_ARTIFICIAL (ded_fn
) = true;
25752 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25753 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25754 DECL_ARTIFICIAL (ded_tmpl
) = true;
25755 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25756 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25757 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25758 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25760 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25762 set_constraints (ded_tmpl
, ci
);
25767 /* Deduce template arguments for the class template placeholder PTYPE for
25768 template TMPL based on the initializer INIT, and return the resulting
25772 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25773 tsubst_flags_t complain
)
25775 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25777 /* We should have handled this in the caller. */
25778 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25780 if (complain
& tf_error
)
25781 error ("non-class template %qT used without template arguments", tmpl
);
25782 return error_mark_node
;
25785 tree type
= TREE_TYPE (tmpl
);
25787 bool try_list_ctor
= false;
25789 vec
<tree
,va_gc
> *args
;
25790 if (init
== NULL_TREE
25791 || TREE_CODE (init
) == TREE_LIST
)
25792 args
= make_tree_vector_from_list (init
);
25793 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25795 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25796 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25798 /* As an exception, the first phase in 16.3.1.7 (considering the
25799 initializer list as a single argument) is omitted if the
25800 initializer list consists of a single expression of type cv U,
25801 where U is a specialization of C or a class derived from a
25802 specialization of C. */
25803 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25804 tree etype
= TREE_TYPE (elt
);
25806 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25807 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25808 int err
= unify (tparms
, targs
, type
, etype
,
25809 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25811 try_list_ctor
= false;
25814 if (try_list_ctor
|| is_std_init_list (type
))
25815 args
= make_tree_vector_single (init
);
25817 args
= make_tree_vector_from_ctor (init
);
25820 args
= make_tree_vector_single (init
);
25822 tree dname
= dguide_name (tmpl
);
25823 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25824 /*type*/false, /*complain*/false,
25826 bool elided
= false;
25827 if (cands
== error_mark_node
)
25830 /* Prune explicit deduction guides in copy-initialization context. */
25831 if (flags
& LOOKUP_ONLYCONVERTING
)
25833 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25834 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25839 /* Found a nonconverting guide, prune the candidates. */
25840 tree pruned
= NULL_TREE
;
25841 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25842 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25843 pruned
= lookup_add (*iter
, pruned
);
25849 tree outer_args
= NULL_TREE
;
25850 if (DECL_CLASS_SCOPE_P (tmpl
)
25851 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25853 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25854 type
= TREE_TYPE (most_general_template (tmpl
));
25857 bool saw_ctor
= false;
25858 // FIXME cache artificial deduction guides
25859 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25861 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25862 if ((flags
& LOOKUP_ONLYCONVERTING
)
25863 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25866 cands
= lookup_add (guide
, cands
);
25871 tree call
= error_mark_node
;
25873 /* If this is list-initialization and the class has a list constructor, first
25874 try deducing from the list as a single argument, as [over.match.list]. */
25875 tree list_cands
= NULL_TREE
;
25876 if (try_list_ctor
&& cands
)
25877 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25880 if (is_list_ctor (dg
))
25881 list_cands
= lookup_add (dg
, list_cands
);
25885 ++cp_unevaluated_operand
;
25886 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25887 --cp_unevaluated_operand
;
25889 if (call
== error_mark_node
)
25891 /* That didn't work, now try treating the list as a sequence of
25893 release_tree_vector (args
);
25894 args
= make_tree_vector_from_ctor (init
);
25898 /* Maybe generate an implicit deduction guide. */
25899 if (call
== error_mark_node
&& args
->length () < 2)
25901 tree gtype
= NULL_TREE
;
25903 if (args
->length () == 1)
25904 /* Generate a copy guide. */
25905 gtype
= build_reference_type (type
);
25906 else if (!saw_ctor
)
25907 /* Generate a default guide. */
25912 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25913 cands
= lookup_add (guide
, cands
);
25917 if (elided
&& !cands
)
25919 error ("cannot deduce template arguments for copy-initialization"
25920 " of %qT, as it has no non-explicit deduction guides or "
25921 "user-declared constructors", type
);
25922 return error_mark_node
;
25924 else if (!cands
&& call
== error_mark_node
)
25926 error ("cannot deduce template arguments of %qT, as it has no viable "
25927 "deduction guides", type
);
25928 return error_mark_node
;
25931 if (call
== error_mark_node
)
25933 ++cp_unevaluated_operand
;
25934 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25935 --cp_unevaluated_operand
;
25938 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25940 error ("class template argument deduction failed:");
25942 ++cp_unevaluated_operand
;
25943 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25944 --cp_unevaluated_operand
;
25947 inform (input_location
, "explicit deduction guides not considered "
25948 "for copy-initialization");
25951 release_tree_vector (args
);
25953 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25956 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25957 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25958 The CONTEXT determines the context in which auto deduction is performed
25959 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25960 OUTER_TARGS are used during template argument deduction
25961 (context == adc_unify) to properly substitute the result, and is ignored
25964 For partial-concept-ids, extra args may be appended to the list of deduced
25965 template arguments prior to determining constraint satisfaction. */
25968 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25969 tsubst_flags_t complain
, auto_deduction_context context
,
25970 tree outer_targs
, int flags
)
25974 if (init
== error_mark_node
)
25975 return error_mark_node
;
25977 if (init
&& type_dependent_expression_p (init
)
25978 && context
!= adc_unify
)
25979 /* Defining a subset of type-dependent expressions that we can deduce
25980 from ahead of time isn't worth the trouble. */
25983 /* Similarly, we can't deduce from another undeduced decl. */
25984 if (init
&& undeduced_auto_decl (init
))
25987 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25988 /* C++17 class template argument deduction. */
25989 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25991 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
25992 /* Nothing we can do with this, even in deduction context. */
25995 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25996 with either a new invented type template parameter U or, if the
25997 initializer is a braced-init-list (8.5.4), with
25998 std::initializer_list<U>. */
25999 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26001 if (!DIRECT_LIST_INIT_P (init
))
26002 type
= listify_autos (type
, auto_node
);
26003 else if (CONSTRUCTOR_NELTS (init
) == 1)
26004 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26007 if (complain
& tf_warning_or_error
)
26009 if (permerror (input_location
, "direct-list-initialization of "
26010 "%<auto%> requires exactly one element"))
26011 inform (input_location
,
26012 "for deduction to %<std::initializer_list%>, use copy-"
26013 "list-initialization (i.e. add %<=%> before the %<{%>)");
26015 type
= listify_autos (type
, auto_node
);
26019 if (type
== error_mark_node
)
26020 return error_mark_node
;
26022 init
= resolve_nondeduced_context (init
, complain
);
26024 if (context
== adc_decomp_type
26025 && auto_node
== type
26026 && init
!= error_mark_node
26027 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26028 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26029 and initializer has array type, deduce cv-qualified array type. */
26030 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26032 else if (AUTO_IS_DECLTYPE (auto_node
))
26034 bool id
= (DECL_P (init
)
26035 || ((TREE_CODE (init
) == COMPONENT_REF
26036 || TREE_CODE (init
) == SCOPE_REF
)
26037 && !REF_PARENTHESIZED_P (init
)));
26038 targs
= make_tree_vec (1);
26039 TREE_VEC_ELT (targs
, 0)
26040 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26041 if (type
!= auto_node
)
26043 if (complain
& tf_error
)
26044 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26045 return error_mark_node
;
26050 tree parms
= build_tree_list (NULL_TREE
, type
);
26054 tparms
= extract_autos (type
);
26057 tparms
= make_tree_vec (1);
26058 TREE_VEC_ELT (tparms
, 0)
26059 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26062 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26063 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26064 DEDUCE_CALL
, LOOKUP_NORMAL
,
26065 NULL
, /*explain_p=*/false);
26068 if (processing_template_decl
)
26069 /* Try again at instantiation time. */
26071 if (type
&& type
!= error_mark_node
26072 && (complain
& tf_error
))
26073 /* If type is error_mark_node a diagnostic must have been
26074 emitted by now. Also, having a mention to '<type error>'
26075 in the diagnostic is not really useful to the user. */
26077 if (cfun
&& auto_node
== current_function_auto_return_pattern
26078 && LAMBDA_FUNCTION_P (current_function_decl
))
26079 error ("unable to deduce lambda return type from %qE", init
);
26081 error ("unable to deduce %qT from %qE", type
, init
);
26082 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26083 DEDUCE_CALL
, LOOKUP_NORMAL
,
26084 NULL
, /*explain_p=*/true);
26086 return error_mark_node
;
26090 /* Check any placeholder constraints against the deduced type. */
26091 if (flag_concepts
&& !processing_template_decl
)
26092 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26094 /* Use the deduced type to check the associated constraints. If we
26095 have a partial-concept-id, rebuild the argument list so that
26096 we check using the extra arguments. */
26097 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26098 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26099 if (TREE_VEC_LENGTH (cargs
) > 1)
26101 cargs
= copy_node (cargs
);
26102 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26106 if (!constraints_satisfied_p (constr
, cargs
))
26108 if (complain
& tf_warning_or_error
)
26112 case adc_unspecified
:
26114 error("placeholder constraints not satisfied");
26116 case adc_variable_type
:
26117 case adc_decomp_type
:
26118 error ("deduced initializer does not satisfy "
26119 "placeholder constraints");
26121 case adc_return_type
:
26122 error ("deduced return type does not satisfy "
26123 "placeholder constraints");
26125 case adc_requirement
:
26126 error ("deduced expression type does not satisfy "
26127 "placeholder constraints");
26130 diagnose_constraints (input_location
, constr
, targs
);
26132 return error_mark_node
;
26136 if (processing_template_decl
&& context
!= adc_unify
)
26137 outer_targs
= current_template_args ();
26138 targs
= add_to_template_args (outer_targs
, targs
);
26139 return tsubst (type
, targs
, complain
, NULL_TREE
);
26142 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26146 splice_late_return_type (tree type
, tree late_return_type
)
26148 if (is_auto (type
))
26150 if (late_return_type
)
26151 return late_return_type
;
26153 tree idx
= get_template_parm_index (type
);
26154 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26155 /* In an abbreviated function template we didn't know we were dealing
26156 with a function template when we saw the auto return type, so update
26157 it to have the correct level. */
26158 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26163 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26164 'decltype(auto)' or a deduced class template. */
26167 is_auto (const_tree type
)
26169 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26170 && (TYPE_IDENTIFIER (type
) == auto_identifier
26171 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26172 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26178 /* for_each_template_parm callback for type_uses_auto. */
26181 is_auto_r (tree tp
, void */
*data*/
)
26183 return is_auto (tp
);
26186 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26187 a use of `auto'. Returns NULL_TREE otherwise. */
26190 type_uses_auto (tree type
)
26192 if (type
== NULL_TREE
)
26194 else if (flag_concepts
)
26196 /* The Concepts TS allows multiple autos in one type-specifier; just
26197 return the first one we find, do_auto_deduction will collect all of
26199 if (uses_template_parms (type
))
26200 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26201 /*visited*/NULL
, /*nondeduced*/true);
26206 return find_type_usage (type
, is_auto
);
26209 /* For a given template T, return the vector of typedefs referenced
26210 in T for which access check is needed at T instantiation time.
26211 T is either a FUNCTION_DECL or a RECORD_TYPE.
26212 Those typedefs were added to T by the function
26213 append_type_to_template_for_access_check. */
26215 vec
<qualified_typedef_usage_t
, va_gc
> *
26216 get_types_needing_access_check (tree t
)
26219 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26221 if (!t
|| t
== error_mark_node
)
26224 if (!(ti
= get_template_info (t
)))
26227 if (CLASS_TYPE_P (t
)
26228 || TREE_CODE (t
) == FUNCTION_DECL
)
26230 if (!TI_TEMPLATE (ti
))
26233 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26239 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26240 tied to T. That list of typedefs will be access checked at
26241 T instantiation time.
26242 T is either a FUNCTION_DECL or a RECORD_TYPE.
26243 TYPE_DECL is a TYPE_DECL node representing a typedef.
26244 SCOPE is the scope through which TYPE_DECL is accessed.
26245 LOCATION is the location of the usage point of TYPE_DECL.
26247 This function is a subroutine of
26248 append_type_to_template_for_access_check. */
26251 append_type_to_template_for_access_check_1 (tree t
,
26254 location_t location
)
26256 qualified_typedef_usage_t typedef_usage
;
26259 if (!t
|| t
== error_mark_node
)
26262 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26263 || CLASS_TYPE_P (t
))
26265 && TREE_CODE (type_decl
) == TYPE_DECL
26268 if (!(ti
= get_template_info (t
)))
26271 gcc_assert (TI_TEMPLATE (ti
));
26273 typedef_usage
.typedef_decl
= type_decl
;
26274 typedef_usage
.context
= scope
;
26275 typedef_usage
.locus
= location
;
26277 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26280 /* Append TYPE_DECL to the template TEMPL.
26281 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26282 At TEMPL instanciation time, TYPE_DECL will be checked to see
26283 if it can be accessed through SCOPE.
26284 LOCATION is the location of the usage point of TYPE_DECL.
26286 e.g. consider the following code snippet:
26293 template<class U> struct S
26295 C::myint mi; // <-- usage point of the typedef C::myint
26300 At S<char> instantiation time, we need to check the access of C::myint
26301 In other words, we need to check the access of the myint typedef through
26302 the C scope. For that purpose, this function will add the myint typedef
26303 and the scope C through which its being accessed to a list of typedefs
26304 tied to the template S. That list will be walked at template instantiation
26305 time and access check performed on each typedefs it contains.
26306 Note that this particular code snippet should yield an error because
26307 myint is private to C. */
26310 append_type_to_template_for_access_check (tree templ
,
26313 location_t location
)
26315 qualified_typedef_usage_t
*iter
;
26318 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26320 /* Make sure we don't append the type to the template twice. */
26321 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26322 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26325 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26329 /* Convert the generic type parameters in PARM that match the types given in the
26330 range [START_IDX, END_IDX) from the current_template_parms into generic type
26334 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26336 tree current
= current_template_parms
;
26337 int depth
= TMPL_PARMS_DEPTH (current
);
26338 current
= INNERMOST_TEMPLATE_PARMS (current
);
26339 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26341 for (int i
= 0; i
< start_idx
; ++i
)
26342 TREE_VEC_ELT (replacement
, i
)
26343 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26345 for (int i
= start_idx
; i
< end_idx
; ++i
)
26347 /* Create a distinct parameter pack type from the current parm and add it
26348 to the replacement args to tsubst below into the generic function
26351 tree o
= TREE_TYPE (TREE_VALUE
26352 (TREE_VEC_ELT (current
, i
)));
26353 tree t
= copy_type (o
);
26354 TEMPLATE_TYPE_PARM_INDEX (t
)
26355 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26357 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26358 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26359 TYPE_MAIN_VARIANT (t
) = t
;
26360 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26361 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26362 TREE_VEC_ELT (replacement
, i
) = t
;
26363 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26366 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26367 TREE_VEC_ELT (replacement
, i
)
26368 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26370 /* If there are more levels then build up the replacement with the outer
26373 replacement
= add_to_template_args (template_parms_to_args
26374 (TREE_CHAIN (current_template_parms
)),
26377 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26380 /* Entries in the decl_constraint hash table. */
26381 struct GTY((for_user
)) constr_entry
26387 /* Hashing function and equality for constraint entries. */
26388 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26390 static hashval_t
hash (constr_entry
*e
)
26392 return (hashval_t
)DECL_UID (e
->decl
);
26395 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26397 return e1
->decl
== e2
->decl
;
26401 /* A mapping from declarations to constraint information. Note that
26402 both templates and their underlying declarations are mapped to the
26403 same constraint information.
26405 FIXME: This is defined in pt.c because garbage collection
26406 code is not being generated for constraint.cc. */
26408 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26410 /* Returns the template constraints of declaration T. If T is not
26411 constrained, return NULL_TREE. Note that T must be non-null. */
26414 get_constraints (tree t
)
26416 if (!flag_concepts
)
26419 gcc_assert (DECL_P (t
));
26420 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26421 t
= DECL_TEMPLATE_RESULT (t
);
26422 constr_entry elt
= { t
, NULL_TREE
};
26423 constr_entry
* found
= decl_constraints
->find (&elt
);
26430 /* Associate the given constraint information CI with the declaration
26431 T. If T is a template, then the constraints are associated with
26432 its underlying declaration. Don't build associations if CI is
26436 set_constraints (tree t
, tree ci
)
26440 gcc_assert (t
&& flag_concepts
);
26441 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26442 t
= DECL_TEMPLATE_RESULT (t
);
26443 gcc_assert (!get_constraints (t
));
26444 constr_entry elt
= {t
, ci
};
26445 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26446 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26451 /* Remove the associated constraints of the declaration T. */
26454 remove_constraints (tree t
)
26456 gcc_assert (DECL_P (t
));
26457 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26458 t
= DECL_TEMPLATE_RESULT (t
);
26460 constr_entry elt
= {t
, NULL_TREE
};
26461 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26463 decl_constraints
->clear_slot (slot
);
26466 /* Memoized satisfaction results for declarations. This
26467 maps the pair (constraint_info, arguments) to the result computed
26468 by constraints_satisfied_p. */
26470 struct GTY((for_user
)) constraint_sat_entry
26477 /* Hashing function and equality for constraint entries. */
26479 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26481 static hashval_t
hash (constraint_sat_entry
*e
)
26483 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26484 return iterative_hash_template_arg (e
->args
, val
);
26487 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26489 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26493 /* Memoized satisfaction results for concept checks. */
26495 struct GTY((for_user
)) concept_spec_entry
26502 /* Hashing function and equality for constraint entries. */
26504 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26506 static hashval_t
hash (concept_spec_entry
*e
)
26508 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26511 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26513 ++comparing_specializations
;
26514 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26515 --comparing_specializations
;
26520 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26521 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26523 /* Search for a memoized satisfaction result. Returns one of the
26524 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26527 lookup_constraint_satisfaction (tree ci
, tree args
)
26529 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26530 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26532 return found
->result
;
26537 /* Memoize the result of a satisfication test. Returns the saved result. */
26540 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26542 constraint_sat_entry elt
= {ci
, args
, result
};
26543 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26544 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26550 /* Search for a memoized satisfaction result for a concept. */
26553 lookup_concept_satisfaction (tree tmpl
, tree args
)
26555 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26556 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26558 return found
->result
;
26563 /* Memoize the result of a concept check. Returns the saved result. */
26566 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26568 concept_spec_entry elt
= {tmpl
, args
, result
};
26569 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26570 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26576 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26578 /* Returns a prior concept specialization. This returns the substituted
26579 and normalized constraints defined by the concept. */
26582 get_concept_expansion (tree tmpl
, tree args
)
26584 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26585 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26587 return found
->result
;
26592 /* Save a concept expansion for later. */
26595 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26597 concept_spec_entry elt
= {tmpl
, args
, def
};
26598 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26599 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26606 hash_subsumption_args (tree t1
, tree t2
)
26608 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26609 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26611 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26612 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26613 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26614 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26618 /* Compare the constraints of two subsumption entries. The LEFT1 and
26619 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26620 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26623 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26625 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26626 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26627 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26628 CHECK_CONSTR_ARGS (right1
)))
26629 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26630 CHECK_CONSTR_ARGS (right2
));
26634 /* Key/value pair for learning and memoizing subsumption results. This
26635 associates a pair of check constraints (including arguments) with
26636 a boolean value indicating the result. */
26638 struct GTY((for_user
)) subsumption_entry
26645 /* Hashing function and equality for constraint entries. */
26647 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26649 static hashval_t
hash (subsumption_entry
*e
)
26651 return hash_subsumption_args (e
->t1
, e
->t2
);
26654 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26656 ++comparing_specializations
;
26657 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26658 --comparing_specializations
;
26663 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26665 /* Search for a previously cached subsumption result. */
26668 lookup_subsumption_result (tree t1
, tree t2
)
26670 subsumption_entry elt
= { t1
, t2
, false };
26671 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26673 return &found
->result
;
26678 /* Save a subsumption result. */
26681 save_subsumption_result (tree t1
, tree t2
, bool result
)
26683 subsumption_entry elt
= {t1
, t2
, result
};
26684 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26685 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26691 /* Set up the hash table for constraint association. */
26694 init_constraint_processing (void)
26696 if (!flag_concepts
)
26699 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26700 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26701 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26702 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26703 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26706 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26710 declare_integer_pack (void)
26712 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26713 build_function_type_list (integer_type_node
,
26716 NULL_TREE
, ECF_CONST
);
26717 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26718 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26721 /* Set up the hash tables for template instantiations. */
26724 init_template_processing (void)
26726 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26727 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26729 if (cxx_dialect
>= cxx11
)
26730 declare_integer_pack ();
26733 /* Print stats about the template hash tables for -fstats. */
26736 print_template_statistics (void)
26738 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26739 "%f collisions\n", (long) decl_specializations
->size (),
26740 (long) decl_specializations
->elements (),
26741 decl_specializations
->collisions ());
26742 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26743 "%f collisions\n", (long) type_specializations
->size (),
26744 (long) type_specializations
->elements (),
26745 type_specializations
->collisions ());
26750 namespace selftest
{
26752 /* Verify that build_non_dependent_expr () works, for various expressions,
26753 and that location wrappers don't affect the results. */
26756 test_build_non_dependent_expr ()
26758 location_t loc
= BUILTINS_LOCATION
;
26760 /* Verify constants, without and with location wrappers. */
26761 tree int_cst
= build_int_cst (integer_type_node
, 42);
26762 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26764 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26765 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26766 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26768 tree string_lit
= build_string (4, "foo");
26769 TREE_TYPE (string_lit
) = char_array_type_node
;
26770 string_lit
= fix_string_type (string_lit
);
26771 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26773 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26774 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26775 ASSERT_EQ (wrapped_string_lit
,
26776 build_non_dependent_expr (wrapped_string_lit
));
26779 /* Verify that type_dependent_expression_p () works correctly, even
26780 in the presence of location wrapper nodes. */
26783 test_type_dependent_expression_p ()
26785 location_t loc
= BUILTINS_LOCATION
;
26787 tree name
= get_identifier ("foo");
26789 /* If no templates are involved, nothing is type-dependent. */
26790 gcc_assert (!processing_template_decl
);
26791 ASSERT_FALSE (type_dependent_expression_p (name
));
26793 ++processing_template_decl
;
26795 /* Within a template, an unresolved name is always type-dependent. */
26796 ASSERT_TRUE (type_dependent_expression_p (name
));
26798 /* Ensure it copes with NULL_TREE and errors. */
26799 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26800 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26802 /* A USING_DECL in a template should be type-dependent, even if wrapped
26803 with a location wrapper (PR c++/83799). */
26804 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26805 TREE_TYPE (using_decl
) = integer_type_node
;
26806 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26807 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26808 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26809 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26811 --processing_template_decl
;
26814 /* Run all of the selftests within this file. */
26819 test_build_non_dependent_expr ();
26820 test_type_dependent_expression_p ();
26823 } // namespace selftest
26825 #endif /* #if CHECKING_P */
26827 #include "gt-cp-pt.h"