1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 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"
47 /* The type of functions taking a tree, and some additional data, and
49 typedef int (*tree_fn_t
) (tree
, void*);
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static vec
<int> inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) vec
<tree
, va_gc
> *saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
81 // Implementation of the RAII helper for creating new local
83 local_specialization_stack::local_specialization_stack (lss_policy policy
)
84 : saved (local_specializations
)
86 if (policy
== lss_blank
|| !saved
)
87 local_specializations
= new hash_map
<tree
, tree
>;
89 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
92 local_specialization_stack::~local_specialization_stack ()
94 delete local_specializations
;
95 local_specializations
= saved
;
98 /* True if we've recursed into fn_type_unification too many times. */
99 static bool excessive_deduction_depth
;
101 struct GTY((for_user
)) spec_entry
108 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
110 static hashval_t
hash (spec_entry
*);
111 static bool equal (spec_entry
*, spec_entry
*);
114 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
116 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133 enum template_base_result
{
135 tbr_ambiguous_baseclass
,
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 bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
156 static int check_non_deducible_conversion (tree
, tree
, int, int,
157 struct conversion
**, bool);
158 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
160 static int type_unification_real (tree
, tree
, tree
, const tree
*,
161 unsigned int, int, unification_kind_t
,
162 vec
<deferred_access_check
, va_gc
> **,
164 static void note_template_header (int);
165 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
167 static tree
convert_template_argument (tree
, tree
, tree
,
168 tsubst_flags_t
, int, tree
);
169 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
170 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
171 static tree
expand_template_argument_pack (tree
);
172 static tree
build_template_parm_index (int, int, int, tree
, tree
);
173 static bool inline_needs_template_parms (tree
, bool);
174 static void push_inline_template_parms_recursive (tree
, int);
175 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
176 static int mark_template_parm (tree
, void *);
177 static int template_parm_this_level_p (tree
, void *);
178 static tree
tsubst_friend_function (tree
, tree
);
179 static tree
tsubst_friend_class (tree
, tree
);
180 static int can_complete_type_without_circularity (tree
);
181 static tree
get_bindings (tree
, tree
, tree
, bool);
182 static int template_decl_level (tree
);
183 static int check_cv_quals_for_unify (int, tree
, tree
);
184 static 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_parms (tree
, tree
, tsubst_flags_t
);
188 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
189 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
190 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
192 static bool check_specialization_scope (void);
193 static tree
process_partial_specialization (tree
);
194 static void set_current_access_from_decl (tree
);
195 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
197 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
198 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
200 static bool template_template_parm_bindings_ok_p (tree
, tree
);
201 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
202 static tree
for_each_template_parm_r (tree
*, int *, void *);
203 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
204 static void copy_default_args_to_explicit_spec (tree
);
205 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
206 static bool dependent_template_arg_p (tree
);
207 static bool any_template_arguments_need_structural_equality_p (tree
);
208 static bool dependent_type_p_r (tree
);
209 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
210 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
211 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
212 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
214 static tree
listify (tree
);
215 static tree
listify_autos (tree
, tree
);
216 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
217 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
218 static bool complex_alias_template_p (const_tree tmpl
);
219 static tree
get_underlying_template (tree
);
220 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
221 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
222 static tree
make_argument_pack (tree
);
223 static void register_parameter_specializations (tree
, tree
);
224 static tree
enclosing_instantiation_of (tree tctx
);
226 /* Make the current scope suitable for access checking when we are
227 processing T. T can be FUNCTION_DECL for instantiated function
228 template, VAR_DECL for static member variable, or TYPE_DECL for
229 alias template (needed by instantiate_decl). */
232 push_access_scope (tree t
)
234 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
235 || TREE_CODE (t
) == TYPE_DECL
);
237 if (DECL_FRIEND_CONTEXT (t
))
238 push_nested_class (DECL_FRIEND_CONTEXT (t
));
239 else if (DECL_CLASS_SCOPE_P (t
))
240 push_nested_class (DECL_CONTEXT (t
));
242 push_to_top_level ();
244 if (TREE_CODE (t
) == FUNCTION_DECL
)
246 vec_safe_push (saved_access_scope
, current_function_decl
);
247 current_function_decl
= t
;
251 /* Restore the scope set up by push_access_scope. T is the node we
255 pop_access_scope (tree t
)
257 if (TREE_CODE (t
) == FUNCTION_DECL
)
258 current_function_decl
= saved_access_scope
->pop();
260 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
263 pop_from_top_level ();
266 /* Do any processing required when DECL (a member template
267 declaration) is finished. Returns the TEMPLATE_DECL corresponding
268 to DECL, unless it is a specialization, in which case the DECL
269 itself is returned. */
272 finish_member_template_decl (tree decl
)
274 if (decl
== error_mark_node
)
275 return error_mark_node
;
277 gcc_assert (DECL_P (decl
));
279 if (TREE_CODE (decl
) == TYPE_DECL
)
283 type
= TREE_TYPE (decl
);
284 if (type
== error_mark_node
)
285 return error_mark_node
;
286 if (MAYBE_CLASS_TYPE_P (type
)
287 && CLASSTYPE_TEMPLATE_INFO (type
)
288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
290 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
291 check_member_template (tmpl
);
296 else if (TREE_CODE (decl
) == FIELD_DECL
)
297 error_at (DECL_SOURCE_LOCATION (decl
),
298 "data member %qD cannot be a member template", decl
);
299 else if (DECL_TEMPLATE_INFO (decl
))
301 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
303 check_member_template (DECL_TI_TEMPLATE (decl
));
304 return DECL_TI_TEMPLATE (decl
);
310 error_at (DECL_SOURCE_LOCATION (decl
),
311 "invalid member template declaration %qD", decl
);
313 return error_mark_node
;
316 /* Create a template info node. */
319 build_template_info (tree template_decl
, tree template_args
)
321 tree result
= make_node (TEMPLATE_INFO
);
322 TI_TEMPLATE (result
) = template_decl
;
323 TI_ARGS (result
) = template_args
;
327 /* Return the template info node corresponding to T, whatever T is. */
330 get_template_info (const_tree t
)
332 tree tinfo
= NULL_TREE
;
334 if (!t
|| t
== error_mark_node
)
337 if (TREE_CODE (t
) == NAMESPACE_DECL
338 || TREE_CODE (t
) == PARM_DECL
)
341 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
342 tinfo
= DECL_TEMPLATE_INFO (t
);
344 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
347 if (OVERLOAD_TYPE_P (t
))
348 tinfo
= TYPE_TEMPLATE_INFO (t
);
349 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
350 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
355 /* Returns the template nesting level of the indicated class TYPE.
365 A<T>::B<U> has depth two, while A<T> has depth one.
366 Both A<T>::B<int> and A<int>::B<U> have depth one, if
367 they are instantiations, not specializations.
369 This function is guaranteed to return 0 if passed NULL_TREE so
370 that, for example, `template_class_depth (current_class_type)' is
374 template_class_depth (tree type
)
378 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
380 tree tinfo
= get_template_info (type
);
382 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
383 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
387 type
= CP_DECL_CONTEXT (type
);
388 else if (LAMBDA_TYPE_P (type
) && LAMBDA_TYPE_EXTRA_SCOPE (type
))
389 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
391 type
= CP_TYPE_CONTEXT (type
);
397 /* Return TRUE if NODE instantiates a template that has arguments of
398 its own, be it directly a primary template or indirectly through a
399 partial specializations. */
401 instantiates_primary_template_p (tree node
)
403 tree tinfo
= get_template_info (node
);
407 tree tmpl
= TI_TEMPLATE (tinfo
);
408 if (PRIMARY_TEMPLATE_P (tmpl
))
411 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
414 /* So now we know we have a specialization, but it could be a full
415 or a partial specialization. To tell which, compare the depth of
416 its template arguments with those of its context. */
418 tree ctxt
= DECL_CONTEXT (tmpl
);
419 tree ctinfo
= get_template_info (ctxt
);
423 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
424 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
427 /* Subroutine of maybe_begin_member_template_processing.
428 Returns true if processing DECL needs us to push template parms. */
431 inline_needs_template_parms (tree decl
, bool nsdmi
)
433 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
436 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
437 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
440 /* Subroutine of maybe_begin_member_template_processing.
441 Push the template parms in PARMS, starting from LEVELS steps into the
442 chain, and ending at the beginning, since template parms are listed
446 push_inline_template_parms_recursive (tree parmlist
, int levels
)
448 tree parms
= TREE_VALUE (parmlist
);
452 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
454 ++processing_template_decl
;
455 current_template_parms
456 = tree_cons (size_int (processing_template_decl
),
457 parms
, current_template_parms
);
458 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
460 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
462 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
464 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
466 if (error_operand_p (parm
))
469 gcc_assert (DECL_P (parm
));
471 switch (TREE_CODE (parm
))
479 /* Push the CONST_DECL. */
480 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
489 /* Restore the template parameter context for a member template, a
490 friend template defined in a class definition, or a non-template
491 member of template class. */
494 maybe_begin_member_template_processing (tree decl
)
498 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
502 tree ctx
= DECL_CONTEXT (decl
);
503 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
504 /* Disregard full specializations (c++/60999). */
505 && uses_template_parms (ctx
)
506 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
509 if (inline_needs_template_parms (decl
, nsdmi
))
511 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
512 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
514 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
517 parms
= TREE_CHAIN (parms
);
520 push_inline_template_parms_recursive (parms
, levels
);
523 /* Remember how many levels of template parameters we pushed so that
524 we can pop them later. */
525 inline_parm_levels
.safe_push (levels
);
528 /* Undo the effects of maybe_begin_member_template_processing. */
531 maybe_end_member_template_processing (void)
536 if (inline_parm_levels
.length () == 0)
539 last
= inline_parm_levels
.pop ();
540 for (i
= 0; i
< last
; ++i
)
542 --processing_template_decl
;
543 current_template_parms
= TREE_CHAIN (current_template_parms
);
548 /* Return a new template argument vector which contains all of ARGS,
549 but has as its innermost set of arguments the EXTRA_ARGS. */
552 add_to_template_args (tree args
, tree extra_args
)
559 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
562 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
563 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
565 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
566 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
568 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
569 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
574 /* Like add_to_template_args, but only the outermost ARGS are added to
575 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
576 (EXTRA_ARGS) levels are added. This function is used to combine
577 the template arguments from a partial instantiation with the
578 template arguments used to attain the full instantiation from the
579 partial instantiation. */
582 add_outermost_template_args (tree args
, tree extra_args
)
586 /* If there are more levels of EXTRA_ARGS than there are ARGS,
587 something very fishy is going on. */
588 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
590 /* If *all* the new arguments will be the EXTRA_ARGS, just return
592 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
595 /* For the moment, we make ARGS look like it contains fewer levels. */
596 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
598 new_args
= add_to_template_args (args
, extra_args
);
600 /* Now, we restore ARGS to its full dimensions. */
601 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
606 /* Return the N levels of innermost template arguments from the ARGS. */
609 get_innermost_template_args (tree args
, int n
)
617 /* If N is 1, just return the innermost set of template arguments. */
619 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
621 /* If we're not removing anything, just return the arguments we were
623 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
624 gcc_assert (extra_levels
>= 0);
625 if (extra_levels
== 0)
628 /* Make a new set of arguments, not containing the outer arguments. */
629 new_args
= make_tree_vec (n
);
630 for (i
= 1; i
<= n
; ++i
)
631 SET_TMPL_ARGS_LEVEL (new_args
, i
,
632 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
637 /* The inverse of get_innermost_template_args: Return all but the innermost
638 EXTRA_LEVELS levels of template arguments from the ARGS. */
641 strip_innermost_template_args (tree args
, int extra_levels
)
644 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
649 /* If N is 1, just return the outermost set of template arguments. */
651 return TMPL_ARGS_LEVEL (args
, 1);
653 /* If we're not removing anything, just return the arguments we were
655 gcc_assert (extra_levels
>= 0);
656 if (extra_levels
== 0)
659 /* Make a new set of arguments, not containing the inner arguments. */
660 new_args
= make_tree_vec (n
);
661 for (i
= 1; i
<= n
; ++i
)
662 SET_TMPL_ARGS_LEVEL (new_args
, i
,
663 TMPL_ARGS_LEVEL (args
, i
));
668 /* We've got a template header coming up; push to a new level for storing
672 begin_template_parm_list (void)
674 /* We use a non-tag-transparent scope here, which causes pushtag to
675 put tags in this scope, rather than in the enclosing class or
676 namespace scope. This is the right thing, since we want
677 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
678 global template class, push_template_decl handles putting the
679 TEMPLATE_DECL into top-level scope. For a nested template class,
682 template <class T> struct S1 {
683 template <class T> struct S2 {};
686 pushtag contains special code to insert the TEMPLATE_DECL for S2
687 at the right scope. */
688 begin_scope (sk_template_parms
, NULL
);
689 ++processing_template_decl
;
690 ++processing_template_parmlist
;
691 note_template_header (0);
693 /* Add a dummy parameter level while we process the parameter list. */
694 current_template_parms
695 = tree_cons (size_int (processing_template_decl
),
697 current_template_parms
);
700 /* This routine is called when a specialization is declared. If it is
701 invalid to declare a specialization here, an error is reported and
702 false is returned, otherwise this routine will return true. */
705 check_specialization_scope (void)
707 tree scope
= current_scope ();
711 An explicit specialization shall be declared in the namespace of
712 which the template is a member, or, for member templates, in the
713 namespace of which the enclosing class or enclosing class
714 template is a member. An explicit specialization of a member
715 function, member class or static data member of a class template
716 shall be declared in the namespace of which the class template
718 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
720 error ("explicit specialization in non-namespace scope %qD", scope
);
726 In an explicit specialization declaration for a member of a class
727 template or a member template that appears in namespace scope,
728 the member template and some of its enclosing class templates may
729 remain unspecialized, except that the declaration shall not
730 explicitly specialize a class member template if its enclosing
731 class templates are not explicitly specialized as well. */
732 if (current_template_parms
)
734 error ("enclosing class templates are not explicitly specialized");
741 /* We've just seen template <>. */
744 begin_specialization (void)
746 begin_scope (sk_template_spec
, NULL
);
747 note_template_header (1);
748 return check_specialization_scope ();
751 /* Called at then end of processing a declaration preceded by
755 end_specialization (void)
758 reset_specialization ();
761 /* Any template <>'s that we have seen thus far are not referring to a
762 function specialization. */
765 reset_specialization (void)
767 processing_specialization
= 0;
768 template_header_count
= 0;
771 /* We've just seen a template header. If SPECIALIZATION is nonzero,
772 it was of the form template <>. */
775 note_template_header (int specialization
)
777 processing_specialization
= specialization
;
778 template_header_count
++;
781 /* We're beginning an explicit instantiation. */
784 begin_explicit_instantiation (void)
786 gcc_assert (!processing_explicit_instantiation
);
787 processing_explicit_instantiation
= true;
792 end_explicit_instantiation (void)
794 gcc_assert (processing_explicit_instantiation
);
795 processing_explicit_instantiation
= false;
798 /* An explicit specialization or partial specialization of TMPL is being
799 declared. Check that the namespace in which the specialization is
800 occurring is permissible. Returns false iff it is invalid to
801 specialize TMPL in the current namespace. */
804 check_specialization_namespace (tree tmpl
)
806 tree tpl_ns
= decl_namespace_context (tmpl
);
810 An explicit specialization shall be declared in a namespace enclosing the
811 specialized template. An explicit specialization whose declarator-id is
812 not qualified shall be declared in the nearest enclosing namespace of the
813 template, or, if the namespace is inline (7.3.1), any namespace from its
814 enclosing namespace set. */
815 if (current_scope() != DECL_CONTEXT (tmpl
)
816 && !at_namespace_scope_p ())
818 error ("specialization of %qD must appear at namespace scope", tmpl
);
822 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
823 /* Same or enclosing namespace. */
827 auto_diagnostic_group d
;
828 if (permerror (input_location
,
829 "specialization of %qD in different namespace", tmpl
))
830 inform (DECL_SOURCE_LOCATION (tmpl
),
831 " from definition of %q#D", tmpl
);
836 /* SPEC is an explicit instantiation. Check that it is valid to
837 perform this explicit instantiation in the current namespace. */
840 check_explicit_instantiation_namespace (tree spec
)
844 /* DR 275: An explicit instantiation shall appear in an enclosing
845 namespace of its template. */
846 ns
= decl_namespace_context (spec
);
847 if (!is_nested_namespace (current_namespace
, ns
))
848 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
849 "(which does not enclose namespace %qD)",
850 spec
, current_namespace
, ns
);
853 /* Returns the type of a template specialization only if that
854 specialization needs to be defined. Otherwise (e.g., if the type has
855 already been defined), the function returns NULL_TREE. */
858 maybe_new_partial_specialization (tree type
)
860 /* An implicit instantiation of an incomplete type implies
861 the definition of a new class template.
869 Here, S<T*> is an implicit instantiation of S whose type
871 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
874 /* It can also be the case that TYPE is a completed specialization.
875 Continuing the previous example, suppose we also declare:
881 Here, S<T*> refers to the specialization S<T*> defined
882 above. However, we need to differentiate definitions because
883 we intend to define a new partial specialization. In this case,
884 we rely on the fact that the constraints are different for
885 this declaration than that above.
887 Note that we also get here for injected class names and
888 late-parsed template definitions. We must ensure that we
889 do not create new type declarations for those cases. */
890 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
892 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
893 tree args
= CLASSTYPE_TI_ARGS (type
);
895 /* If there are no template parameters, this cannot be a new
896 partial template specialization? */
897 if (!current_template_parms
)
900 /* The injected-class-name is not a new partial specialization. */
901 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
904 /* If the constraints are not the same as those of the primary
905 then, we can probably create a new specialization. */
906 tree type_constr
= current_template_constraints ();
908 if (type
== TREE_TYPE (tmpl
))
910 tree main_constr
= get_constraints (tmpl
);
911 if (equivalent_constraints (type_constr
, main_constr
))
915 /* Also, if there's a pre-existing specialization with matching
916 constraints, then this also isn't new. */
917 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
920 tree spec_tmpl
= TREE_VALUE (specs
);
921 tree spec_args
= TREE_PURPOSE (specs
);
922 tree spec_constr
= get_constraints (spec_tmpl
);
923 if (comp_template_args (args
, spec_args
)
924 && equivalent_constraints (type_constr
, spec_constr
))
926 specs
= TREE_CHAIN (specs
);
929 /* Create a new type node (and corresponding type decl)
930 for the newly declared specialization. */
931 tree t
= make_class_type (TREE_CODE (type
));
932 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
933 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
935 /* We only need a separate type node for storing the definition of this
936 partial specialization; uses of S<T*> are unconstrained, so all are
937 equivalent. So keep TYPE_CANONICAL the same. */
938 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
940 /* Build the corresponding type decl. */
941 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
942 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
943 DECL_SOURCE_LOCATION (d
) = input_location
;
944 TREE_PRIVATE (d
) = (current_access_specifier
== access_private_node
);
945 TREE_PROTECTED (d
) = (current_access_specifier
== access_protected_node
);
953 /* The TYPE is being declared. If it is a template type, that means it
954 is a partial specialization. Do appropriate error-checking. */
957 maybe_process_partial_specialization (tree type
)
961 if (type
== error_mark_node
)
962 return error_mark_node
;
964 /* A lambda that appears in specialization context is not itself a
966 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
969 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
971 error ("name of class shadows template template parameter %qD",
973 return error_mark_node
;
976 context
= TYPE_CONTEXT (type
);
978 if (TYPE_ALIAS_P (type
))
980 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
982 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
983 error ("specialization of alias template %qD",
984 TI_TEMPLATE (tinfo
));
986 error ("explicit specialization of non-template %qT", type
);
987 return error_mark_node
;
989 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
991 /* This is for ordinary explicit specialization and partial
992 specialization of a template class such as:
994 template <> class C<int>;
998 template <class T> class C<T*>;
1000 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1002 if (tree t
= maybe_new_partial_specialization (type
))
1004 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1005 && !at_namespace_scope_p ())
1006 return error_mark_node
;
1007 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1008 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1009 if (processing_template_decl
)
1011 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1012 if (decl
== error_mark_node
)
1013 return error_mark_node
;
1014 return TREE_TYPE (decl
);
1017 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1018 error ("specialization of %qT after instantiation", type
);
1019 else if (errorcount
&& !processing_specialization
1020 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1021 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1022 /* Trying to define a specialization either without a template<> header
1023 or in an inappropriate place. We've already given an error, so just
1024 bail now so we don't actually define the specialization. */
1025 return error_mark_node
;
1027 else if (CLASS_TYPE_P (type
)
1028 && !CLASSTYPE_USE_TEMPLATE (type
)
1029 && CLASSTYPE_TEMPLATE_INFO (type
)
1030 && context
&& CLASS_TYPE_P (context
)
1031 && CLASSTYPE_TEMPLATE_INFO (context
))
1033 /* This is for an explicit specialization of member class
1034 template according to [temp.expl.spec/18]:
1036 template <> template <class U> class C<int>::D;
1038 The context `C<int>' must be an implicit instantiation.
1039 Otherwise this is just a member class template declared
1042 template <> class C<int> { template <class U> class D; };
1043 template <> template <class U> class C<int>::D;
1045 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1046 while in the second case, `C<int>::D' is a primary template
1047 and `C<T>::D' may not exist. */
1049 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1050 && !COMPLETE_TYPE_P (type
))
1053 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1055 if (current_namespace
1056 != decl_namespace_context (tmpl
))
1058 if (permerror (input_location
,
1059 "specialization of %qD in different namespace",
1061 inform (DECL_SOURCE_LOCATION (tmpl
),
1062 "from definition of %q#D", tmpl
);
1065 /* Check for invalid specialization after instantiation:
1067 template <> template <> class C<int>::D<int>;
1068 template <> template <class U> class C<int>::D; */
1070 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1071 t
; t
= TREE_CHAIN (t
))
1073 tree inst
= TREE_VALUE (t
);
1074 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1075 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1077 /* We already have a full specialization of this partial
1078 instantiation, or a full specialization has been
1079 looked up but not instantiated. Reassign it to the
1080 new member specialization template. */
1084 elt
.tmpl
= most_general_template (tmpl
);
1085 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1088 type_specializations
->remove_elt (&elt
);
1091 CLASSTYPE_TI_ARGS (inst
)
1092 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1095 = type_specializations
->find_slot (&elt
, INSERT
);
1096 entry
= ggc_alloc
<spec_entry
> ();
1101 /* But if we've had an implicit instantiation, that's a
1102 problem ([temp.expl.spec]/6). */
1103 error ("specialization %qT after instantiation %qT",
1107 /* Mark TYPE as a specialization. And as a result, we only
1108 have one level of template argument for the innermost
1110 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1111 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1112 CLASSTYPE_TI_ARGS (type
)
1113 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1116 else if (processing_specialization
)
1118 /* Someday C++0x may allow for enum template specialization. */
1119 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1120 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1121 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1122 "of %qD not allowed by ISO C++", type
);
1125 error ("explicit specialization of non-template %qT", type
);
1126 return error_mark_node
;
1133 /* Returns nonzero if we can optimize the retrieval of specializations
1134 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1135 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1138 optimize_specialization_lookup_p (tree tmpl
)
1140 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1141 && DECL_CLASS_SCOPE_P (tmpl
)
1142 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1144 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1145 /* The optimized lookup depends on the fact that the
1146 template arguments for the member function template apply
1147 purely to the containing class, which is not true if the
1148 containing class is an explicit or partial
1150 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1151 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1152 && !DECL_CONV_FN_P (tmpl
)
1153 /* It is possible to have a template that is not a member
1154 template and is not a member of a template class:
1156 template <typename T>
1157 struct S { friend A::f(); };
1159 Here, the friend function is a template, but the context does
1160 not have template information. The optimized lookup relies
1161 on having ARGS be the template arguments for both the class
1162 and the function template. */
1163 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1166 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1167 gone through coerce_template_parms by now. */
1170 verify_unstripped_args_1 (tree inner
)
1172 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1174 tree arg
= TREE_VEC_ELT (inner
, i
);
1175 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1177 else if (TYPE_P (arg
))
1178 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1179 else if (ARGUMENT_PACK_P (arg
))
1180 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1181 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1182 /* Allow typedefs on the type of a non-type argument, since a
1183 parameter can have them. */;
1185 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1190 verify_unstripped_args (tree args
)
1192 ++processing_template_decl
;
1193 if (!any_dependent_template_arguments_p (args
))
1194 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1195 --processing_template_decl
;
1198 /* Retrieve the specialization (in the sense of [temp.spec] - a
1199 specialization is either an instantiation or an explicit
1200 specialization) of TMPL for the given template ARGS. If there is
1201 no such specialization, return NULL_TREE. The ARGS are a vector of
1202 arguments, or a vector of vectors of arguments, in the case of
1203 templates with more than one level of parameters.
1205 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1206 then we search for a partial specialization matching ARGS. This
1207 parameter is ignored if TMPL is not a class template.
1209 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1210 result is a NONTYPE_ARGUMENT_PACK. */
1213 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1215 if (tmpl
== NULL_TREE
)
1218 if (args
== error_mark_node
)
1221 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1222 || TREE_CODE (tmpl
) == FIELD_DECL
);
1224 /* There should be as many levels of arguments as there are
1225 levels of parameters. */
1226 gcc_assert (TMPL_ARGS_DEPTH (args
)
1227 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1228 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1229 : template_class_depth (DECL_CONTEXT (tmpl
))));
1232 verify_unstripped_args (args
);
1234 /* Lambda functions in templates aren't instantiated normally, but through
1235 tsubst_lambda_expr. */
1236 if (lambda_fn_in_template_p (tmpl
))
1239 if (optimize_specialization_lookup_p (tmpl
))
1241 /* The template arguments actually apply to the containing
1242 class. Find the class specialization with those
1244 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1245 tree class_specialization
1246 = retrieve_specialization (class_template
, args
, 0);
1247 if (!class_specialization
)
1250 /* Find the instance of TMPL. */
1251 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1252 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1255 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1256 /* using-declarations can add base methods to the method vec,
1257 and we don't want those here. */
1258 && DECL_CONTEXT (fn
) == class_specialization
)
1267 hash_table
<spec_hasher
> *specializations
;
1271 elt
.spec
= NULL_TREE
;
1273 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1274 specializations
= type_specializations
;
1276 specializations
= decl_specializations
;
1279 hash
= spec_hasher::hash (&elt
);
1280 found
= specializations
->find_with_hash (&elt
, hash
);
1288 /* Like retrieve_specialization, but for local declarations. */
1291 retrieve_local_specialization (tree tmpl
)
1293 if (local_specializations
== NULL
)
1296 tree
*slot
= local_specializations
->get (tmpl
);
1297 return slot
? *slot
: NULL_TREE
;
1300 /* Returns nonzero iff DECL is a specialization of TMPL. */
1303 is_specialization_of (tree decl
, tree tmpl
)
1307 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1311 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1317 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1319 for (t
= TREE_TYPE (decl
);
1321 t
= CLASSTYPE_USE_TEMPLATE (t
)
1322 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1323 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1330 /* Returns nonzero iff DECL is a specialization of friend declaration
1331 FRIEND_DECL according to [temp.friend]. */
1334 is_specialization_of_friend (tree decl
, tree friend_decl
)
1336 bool need_template
= true;
1339 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1340 || TREE_CODE (decl
) == TYPE_DECL
);
1342 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1343 of a template class, we want to check if DECL is a specialization
1345 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1346 && DECL_TEMPLATE_INFO (friend_decl
)
1347 && !DECL_USE_TEMPLATE (friend_decl
))
1349 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1350 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1351 need_template
= false;
1353 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1354 && !PRIMARY_TEMPLATE_P (friend_decl
))
1355 need_template
= false;
1357 /* There is nothing to do if this is not a template friend. */
1358 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1361 if (is_specialization_of (decl
, friend_decl
))
1365 A member of a class template may be declared to be a friend of a
1366 non-template class. In this case, the corresponding member of
1367 every specialization of the class template is a friend of the
1368 class granting friendship.
1370 For example, given a template friend declaration
1372 template <class T> friend void A<T>::f();
1374 the member function below is considered a friend
1376 template <> struct A<int> {
1380 For this type of template friend, TEMPLATE_DEPTH below will be
1381 nonzero. To determine if DECL is a friend of FRIEND, we first
1382 check if the enclosing class is a specialization of another. */
1384 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1386 && DECL_CLASS_SCOPE_P (decl
)
1387 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1388 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1390 /* Next, we check the members themselves. In order to handle
1391 a few tricky cases, such as when FRIEND_DECL's are
1393 template <class T> friend void A<T>::g(T t);
1394 template <class T> template <T t> friend void A<T>::h();
1398 void A<int>::g(int);
1399 template <int> void A<int>::h();
1401 we need to figure out ARGS, the template arguments from
1402 the context of DECL. This is required for template substitution
1403 of `T' in the function parameter of `g' and template parameter
1404 of `h' in the above examples. Here ARGS corresponds to `int'. */
1406 tree context
= DECL_CONTEXT (decl
);
1407 tree args
= NULL_TREE
;
1408 int current_depth
= 0;
1410 while (current_depth
< template_depth
)
1412 if (CLASSTYPE_TEMPLATE_INFO (context
))
1414 if (current_depth
== 0)
1415 args
= TYPE_TI_ARGS (context
);
1417 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1420 context
= TYPE_CONTEXT (context
);
1423 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1428 tree friend_args_type
;
1429 tree decl_args_type
;
1431 /* Make sure that both DECL and FRIEND_DECL are templates or
1433 is_template
= DECL_TEMPLATE_INFO (decl
)
1434 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1435 if (need_template
^ is_template
)
1437 else if (is_template
)
1439 /* If both are templates, check template parameter list. */
1441 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1443 if (!comp_template_parms
1444 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1448 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1451 decl_type
= TREE_TYPE (decl
);
1453 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1454 tf_none
, NULL_TREE
);
1455 if (friend_type
== error_mark_node
)
1458 /* Check if return types match. */
1459 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1462 /* Check if function parameter types match, ignoring the
1463 `this' parameter. */
1464 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1465 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1467 friend_args_type
= TREE_CHAIN (friend_args_type
);
1468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1469 decl_args_type
= TREE_CHAIN (decl_args_type
);
1471 return compparms (decl_args_type
, friend_args_type
);
1475 /* DECL is a TYPE_DECL */
1477 tree decl_type
= TREE_TYPE (decl
);
1479 /* Make sure that both DECL and FRIEND_DECL are templates or
1482 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1483 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1485 if (need_template
^ is_template
)
1487 else if (is_template
)
1490 /* If both are templates, check the name of the two
1491 TEMPLATE_DECL's first because is_friend didn't. */
1492 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1493 != DECL_NAME (friend_decl
))
1496 /* Now check template parameter list. */
1498 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1500 return comp_template_parms
1501 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1505 return (DECL_NAME (decl
)
1506 == DECL_NAME (friend_decl
));
1512 /* Register the specialization SPEC as a specialization of TMPL with
1513 the indicated ARGS. IS_FRIEND indicates whether the specialization
1514 is actually just a friend declaration. ATTRLIST is the list of
1515 attributes that the specialization is declared with or NULL when
1516 it isn't. Returns SPEC, or an equivalent prior declaration, if
1519 We also store instantiations of field packs in the hash table, even
1520 though they are not themselves templates, to make lookup easier. */
1523 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1527 spec_entry
**slot
= NULL
;
1530 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1531 || (TREE_CODE (tmpl
) == FIELD_DECL
1532 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1534 if (TREE_CODE (spec
) == FUNCTION_DECL
1535 && uses_template_parms (DECL_TI_ARGS (spec
)))
1536 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1537 register it; we want the corresponding TEMPLATE_DECL instead.
1538 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1539 the more obvious `uses_template_parms (spec)' to avoid problems
1540 with default function arguments. In particular, given
1541 something like this:
1543 template <class T> void f(T t1, T t = T())
1545 the default argument expression is not substituted for in an
1546 instantiation unless and until it is actually needed. */
1549 if (optimize_specialization_lookup_p (tmpl
))
1550 /* We don't put these specializations in the hash table, but we might
1551 want to give an error about a mismatch. */
1552 fn
= retrieve_specialization (tmpl
, args
, 0);
1560 hash
= spec_hasher::hash (&elt
);
1563 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1565 fn
= ((spec_entry
*) *slot
)->spec
;
1570 /* We can sometimes try to re-register a specialization that we've
1571 already got. In particular, regenerate_decl_from_template calls
1572 duplicate_decls which will update the specialization list. But,
1573 we'll still get called again here anyhow. It's more convenient
1574 to simply allow this than to try to prevent it. */
1577 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1579 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1581 if (DECL_ODR_USED (fn
)
1582 || DECL_EXPLICIT_INSTANTIATION (fn
))
1584 error ("specialization of %qD after instantiation",
1586 return error_mark_node
;
1591 /* This situation should occur only if the first
1592 specialization is an implicit instantiation, the
1593 second is an explicit specialization, and the
1594 implicit instantiation has not yet been used. That
1595 situation can occur if we have implicitly
1596 instantiated a member function and then specialized
1599 We can also wind up here if a friend declaration that
1600 looked like an instantiation turns out to be a
1603 template <class T> void foo(T);
1604 class S { friend void foo<>(int) };
1605 template <> void foo(int);
1607 We transform the existing DECL in place so that any
1608 pointers to it become pointers to the updated
1611 If there was a definition for the template, but not
1612 for the specialization, we want this to look as if
1613 there were no definition, and vice versa. */
1614 DECL_INITIAL (fn
) = NULL_TREE
;
1615 duplicate_decls (spec
, fn
, is_friend
);
1616 /* The call to duplicate_decls will have applied
1619 An explicit specialization of a function template
1620 is inline only if it is explicitly declared to be,
1621 and independently of whether its function template
1624 to the primary function; now copy the inline bits to
1625 the various clones. */
1626 FOR_EACH_CLONE (clone
, fn
)
1628 DECL_DECLARED_INLINE_P (clone
)
1629 = DECL_DECLARED_INLINE_P (fn
);
1630 DECL_SOURCE_LOCATION (clone
)
1631 = DECL_SOURCE_LOCATION (fn
);
1632 DECL_DELETED_FN (clone
)
1633 = DECL_DELETED_FN (fn
);
1635 check_specialization_namespace (tmpl
);
1640 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1642 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1643 if (dd
== error_mark_node
)
1644 /* We've already complained in duplicate_decls. */
1645 return error_mark_node
;
1647 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1648 /* Dup decl failed, but this is a new definition. Set the
1649 line number so any errors match this new
1651 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1657 return duplicate_decls (spec
, fn
, is_friend
);
1659 /* A specialization must be declared in the same namespace as the
1660 template it is specializing. */
1661 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1662 && !check_specialization_namespace (tmpl
))
1663 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1665 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1667 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1668 gcc_assert (tmpl
&& args
&& spec
);
1671 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1672 && PRIMARY_TEMPLATE_P (tmpl
)
1673 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1674 || variable_template_p (tmpl
))
1675 /* If TMPL is a forward declaration of a template function, keep a list
1676 of all specializations in case we need to reassign them to a friend
1677 template later in tsubst_friend_function.
1679 Also keep a list of all variable template instantiations so that
1680 process_partial_specialization can check whether a later partial
1681 specialization would have used it. */
1682 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1683 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1689 /* Returns true iff two spec_entry nodes are equivalent. */
1691 int comparing_specializations
;
1694 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1698 ++comparing_specializations
;
1699 equal
= (e1
->tmpl
== e2
->tmpl
1700 && comp_template_args (e1
->args
, e2
->args
));
1701 if (equal
&& flag_concepts
1702 /* tmpl could be a FIELD_DECL for a capture pack. */
1703 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1704 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1705 && uses_template_parms (e1
->args
))
1707 /* Partial specializations of a variable template can be distinguished by
1709 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1710 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1711 equal
= equivalent_constraints (c1
, c2
);
1713 --comparing_specializations
;
1718 /* Returns a hash for a template TMPL and template arguments ARGS. */
1721 hash_tmpl_and_args (tree tmpl
, tree args
)
1723 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1724 return iterative_hash_template_arg (args
, val
);
1727 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1731 spec_hasher::hash (spec_entry
*e
)
1733 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1736 /* Recursively calculate a hash value for a template argument ARG, for use
1737 in the hash tables of template specializations. */
1740 iterative_hash_template_arg (tree arg
, hashval_t val
)
1742 unsigned HOST_WIDE_INT i
;
1743 enum tree_code code
;
1746 if (arg
== NULL_TREE
)
1747 return iterative_hash_object (arg
, val
);
1752 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1755 code
= TREE_CODE (arg
);
1756 tclass
= TREE_CODE_CLASS (code
);
1758 val
= iterative_hash_object (code
, val
);
1765 case IDENTIFIER_NODE
:
1766 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1770 int i
, len
= TREE_VEC_LENGTH (arg
);
1771 for (i
= 0; i
< len
; ++i
)
1772 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1776 case TYPE_PACK_EXPANSION
:
1777 case EXPR_PACK_EXPANSION
:
1778 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1779 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1781 case TYPE_ARGUMENT_PACK
:
1782 case NONTYPE_ARGUMENT_PACK
:
1783 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1786 for (; arg
; arg
= TREE_CHAIN (arg
))
1787 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1791 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1792 val
= iterative_hash_template_arg (*iter
, val
);
1798 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1799 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1801 val
= iterative_hash_template_arg (field
, val
);
1802 val
= iterative_hash_template_arg (value
, val
);
1808 if (!DECL_ARTIFICIAL (arg
))
1810 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1811 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1813 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1816 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1819 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1820 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1822 case TEMPLATE_PARM_INDEX
:
1823 val
= iterative_hash_template_arg
1824 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1825 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1826 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1829 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1830 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1831 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1834 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1836 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1840 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1841 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1842 val
= iterative_hash_object (code
, val
);
1843 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1846 /* [temp.over.link] Two lambda-expressions are never considered
1849 So just hash the closure type. */
1850 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1853 case IMPLICIT_CONV_EXPR
:
1854 case STATIC_CAST_EXPR
:
1855 case REINTERPRET_CAST_EXPR
:
1856 case CONST_CAST_EXPR
:
1857 case DYNAMIC_CAST_EXPR
:
1859 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1860 /* Now hash operands as usual. */
1865 tree fn
= CALL_EXPR_FN (arg
);
1866 if (tree name
= dependent_name (fn
))
1868 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1869 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1872 val
= iterative_hash_template_arg (fn
, val
);
1873 call_expr_arg_iterator ai
;
1874 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1875 x
= next_call_expr_arg (&ai
))
1876 val
= iterative_hash_template_arg (x
, val
);
1887 if (tree ats
= alias_template_specialization_p (arg
, nt_transparent
))
1889 // We want an alias specialization that survived strip_typedefs
1890 // to hash differently from its TYPE_CANONICAL, to avoid hash
1891 // collisions that compare as different in template_args_equal.
1892 // These could be dependent specializations that strip_typedefs
1893 // left alone, or untouched specializations because
1894 // coerce_template_parms returns the unconverted template
1895 // arguments if it sees incomplete argument packs.
1896 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
1897 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1899 if (TYPE_CANONICAL (arg
))
1900 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1902 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1903 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1904 /* Otherwise just compare the types during lookup. */
1907 case tcc_declaration
:
1909 return iterative_hash_expr (arg
, val
);
1912 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1914 unsigned n
= cp_tree_operand_length (arg
);
1915 for (i
= 0; i
< n
; ++i
)
1916 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1924 /* Unregister the specialization SPEC as a specialization of TMPL.
1925 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1926 if the SPEC was listed as a specialization of TMPL.
1928 Note that SPEC has been ggc_freed, so we can't look inside it. */
1931 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1936 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1937 elt
.args
= TI_ARGS (tinfo
);
1938 elt
.spec
= NULL_TREE
;
1940 entry
= decl_specializations
->find (&elt
);
1943 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1944 gcc_assert (new_spec
!= NULL_TREE
);
1945 entry
->spec
= new_spec
;
1952 /* Like register_specialization, but for local declarations. We are
1953 registering SPEC, an instantiation of TMPL. */
1956 register_local_specialization (tree spec
, tree tmpl
)
1958 gcc_assert (tmpl
!= spec
);
1959 local_specializations
->put (tmpl
, spec
);
1962 /* TYPE is a class type. Returns true if TYPE is an explicitly
1963 specialized class. */
1966 explicit_class_specialization_p (tree type
)
1968 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1970 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1973 /* Print the list of functions at FNS, going through all the overloads
1974 for each element of the list. Alternatively, FNS cannot be a
1975 TREE_LIST, in which case it will be printed together with all the
1978 MORE and *STR should respectively be FALSE and NULL when the function
1979 is called from the outside. They are used internally on recursive
1980 calls. print_candidates manages the two parameters and leaves NULL
1981 in *STR when it ends. */
1984 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1986 if (TREE_CODE (fns
) == TREE_LIST
)
1987 for (; fns
; fns
= TREE_CHAIN (fns
))
1988 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1990 for (lkp_iterator
iter (fns
); iter
;)
1995 const char *pfx
= *str
;
1999 pfx
= _("candidates are:");
2001 pfx
= _("candidate is:");
2002 *str
= get_spaces (pfx
);
2004 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2008 /* Print the list of candidate FNS in an error message. FNS can also
2009 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2012 print_candidates (tree fns
)
2015 print_candidates_1 (fns
, &str
);
2019 /* Get a (possibly) constrained template declaration for the
2020 purpose of ordering candidates. */
2022 get_template_for_ordering (tree list
)
2024 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2025 tree f
= TREE_VALUE (list
);
2026 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2027 return TI_TEMPLATE (ti
);
2031 /* Among candidates having the same signature, return the
2032 most constrained or NULL_TREE if there is no best candidate.
2033 If the signatures of candidates vary (e.g., template
2034 specialization vs. member function), then there can be no
2037 Note that we don't compare constraints on the functions
2038 themselves, but rather those of their templates. */
2040 most_constrained_function (tree candidates
)
2042 // Try to find the best candidate in a first pass.
2043 tree champ
= candidates
;
2044 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2046 int winner
= more_constrained (get_template_for_ordering (champ
),
2047 get_template_for_ordering (c
));
2049 champ
= c
; // The candidate is more constrained
2050 else if (winner
== 0)
2051 return NULL_TREE
; // Neither is more constrained
2054 // Verify that the champ is better than previous candidates.
2055 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2056 if (!more_constrained (get_template_for_ordering (champ
),
2057 get_template_for_ordering (c
)))
2065 /* Returns the template (one of the functions given by TEMPLATE_ID)
2066 which can be specialized to match the indicated DECL with the
2067 explicit template args given in TEMPLATE_ID. The DECL may be
2068 NULL_TREE if none is available. In that case, the functions in
2069 TEMPLATE_ID are non-members.
2071 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2072 specialization of a member template.
2074 The TEMPLATE_COUNT is the number of references to qualifying
2075 template classes that appeared in the name of the function. See
2076 check_explicit_specialization for a more accurate description.
2078 TSK indicates what kind of template declaration (if any) is being
2079 declared. TSK_TEMPLATE indicates that the declaration given by
2080 DECL, though a FUNCTION_DECL, has template parameters, and is
2081 therefore a template function.
2083 The template args (those explicitly specified and those deduced)
2084 are output in a newly created vector *TARGS_OUT.
2086 If it is impossible to determine the result, an error message is
2087 issued. The error_mark_node is returned to indicate failure. */
2090 determine_specialization (tree template_id
,
2093 int need_member_template
,
2099 tree explicit_targs
;
2100 tree candidates
= NULL_TREE
;
2102 /* A TREE_LIST of templates of which DECL may be a specialization.
2103 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2104 corresponding TREE_PURPOSE is the set of template arguments that,
2105 when used to instantiate the template, would produce a function
2106 with the signature of DECL. */
2107 tree templates
= NULL_TREE
;
2109 cp_binding_level
*b
;
2111 *targs_out
= NULL_TREE
;
2113 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2114 return error_mark_node
;
2116 /* We shouldn't be specializing a member template of an
2117 unspecialized class template; we already gave an error in
2118 check_specialization_scope, now avoid crashing. */
2120 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2121 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2123 gcc_assert (errorcount
);
2124 return error_mark_node
;
2127 fns
= TREE_OPERAND (template_id
, 0);
2128 explicit_targs
= TREE_OPERAND (template_id
, 1);
2130 if (fns
== error_mark_node
)
2131 return error_mark_node
;
2133 /* Check for baselinks. */
2134 if (BASELINK_P (fns
))
2135 fns
= BASELINK_FUNCTIONS (fns
);
2137 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2139 error_at (DECL_SOURCE_LOCATION (decl
),
2140 "%qD is not a function template", fns
);
2141 return error_mark_node
;
2143 else if (VAR_P (decl
) && !variable_template_p (fns
))
2145 error ("%qD is not a variable template", fns
);
2146 return error_mark_node
;
2149 /* Count the number of template headers specified for this
2152 for (b
= current_binding_level
;
2153 b
->kind
== sk_template_parms
;
2157 tree orig_fns
= fns
;
2159 if (variable_template_p (fns
))
2161 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2162 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2163 tf_warning_or_error
,
2164 /*req_all*/true, /*use_defarg*/true);
2165 if (targs
!= error_mark_node
)
2166 templates
= tree_cons (targs
, fns
, templates
);
2168 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2172 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2174 tree decl_arg_types
;
2178 /* In case of explicit specialization, we need to check if
2179 the number of template headers appearing in the specialization
2180 is correct. This is usually done in check_explicit_specialization,
2181 but the check done there cannot be exhaustive when specializing
2182 member functions. Consider the following code:
2184 template <> void A<int>::f(int);
2185 template <> template <> void A<int>::f(int);
2187 Assuming that A<int> is not itself an explicit specialization
2188 already, the first line specializes "f" which is a non-template
2189 member function, whilst the second line specializes "f" which
2190 is a template member function. So both lines are syntactically
2191 correct, and check_explicit_specialization does not reject
2194 Here, we can do better, as we are matching the specialization
2195 against the declarations. We count the number of template
2196 headers, and we check if they match TEMPLATE_COUNT + 1
2197 (TEMPLATE_COUNT is the number of qualifying template classes,
2198 plus there must be another header for the member template
2201 Notice that if header_count is zero, this is not a
2202 specialization but rather a template instantiation, so there
2203 is no check we can perform here. */
2204 if (header_count
&& header_count
!= template_count
+ 1)
2207 /* Check that the number of template arguments at the
2208 innermost level for DECL is the same as for FN. */
2209 if (current_binding_level
->kind
== sk_template_parms
2210 && !current_binding_level
->explicit_spec_p
2211 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2212 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2213 (current_template_parms
))))
2216 /* DECL might be a specialization of FN. */
2217 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2218 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2220 /* For a non-static member function, we need to make sure
2221 that the const qualification is the same. Since
2222 get_bindings does not try to merge the "this" parameter,
2223 we must do the comparison explicitly. */
2224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2226 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2227 TREE_VALUE (decl_arg_types
)))
2230 /* And the ref-qualification. */
2231 if (type_memfn_rqual (TREE_TYPE (decl
))
2232 != type_memfn_rqual (TREE_TYPE (fn
)))
2236 /* Skip the "this" parameter and, for constructors of
2237 classes with virtual bases, the VTT parameter. A
2238 full specialization of a constructor will have a VTT
2239 parameter, but a template never will. */
2241 = skip_artificial_parms_for (decl
, decl_arg_types
);
2243 = skip_artificial_parms_for (fn
, fn_arg_types
);
2245 /* Function templates cannot be specializations; there are
2246 no partial specializations of functions. Therefore, if
2247 the type of DECL does not match FN, there is no
2250 Note that it should never be the case that we have both
2251 candidates added here, and for regular member functions
2253 if (tsk
== tsk_template
)
2255 if (compparms (fn_arg_types
, decl_arg_types
))
2256 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2260 /* See whether this function might be a specialization of this
2261 template. Suppress access control because we might be trying
2262 to make this specialization a friend, and we have already done
2263 access control for the declaration of the specialization. */
2264 push_deferring_access_checks (dk_no_check
);
2265 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2266 pop_deferring_access_checks ();
2269 /* We cannot deduce template arguments that when used to
2270 specialize TMPL will produce DECL. */
2273 if (uses_template_parms (targs
))
2274 /* We deduced something involving 'auto', which isn't a valid
2275 template argument. */
2278 /* Remove, from the set of candidates, all those functions
2279 whose constraints are not satisfied. */
2280 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2283 // Then, try to form the new function type.
2284 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2285 if (insttype
== error_mark_node
)
2288 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2289 if (!compparms (fn_arg_types
, decl_arg_types
))
2292 /* Save this template, and the arguments deduced. */
2293 templates
= tree_cons (targs
, fn
, templates
);
2295 else if (need_member_template
)
2296 /* FN is an ordinary member function, and we need a
2297 specialization of a member template. */
2299 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2300 /* We can get IDENTIFIER_NODEs here in certain erroneous
2303 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2304 /* This is just an ordinary non-member function. Nothing can
2305 be a specialization of that. */
2307 else if (DECL_ARTIFICIAL (fn
))
2308 /* Cannot specialize functions that are created implicitly. */
2312 tree decl_arg_types
;
2314 /* This is an ordinary member function. However, since
2315 we're here, we can assume its enclosing class is a
2316 template class. For example,
2318 template <typename T> struct S { void f(); };
2319 template <> void S<int>::f() {}
2321 Here, S<int>::f is a non-template, but S<int> is a
2322 template class. If FN has the same type as DECL, we
2323 might be in business. */
2325 if (!DECL_TEMPLATE_INFO (fn
))
2326 /* Its enclosing class is an explicit specialization
2327 of a template class. This is not a candidate. */
2330 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2331 TREE_TYPE (TREE_TYPE (fn
))))
2332 /* The return types differ. */
2335 /* Adjust the type of DECL in case FN is a static member. */
2336 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2337 if (DECL_STATIC_FUNCTION_P (fn
)
2338 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2339 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2341 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2345 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2346 && (type_memfn_rqual (TREE_TYPE (decl
))
2347 != type_memfn_rqual (TREE_TYPE (fn
))))
2350 // If the deduced arguments do not satisfy the constraints,
2351 // this is not a candidate.
2352 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2355 // Add the candidate.
2356 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2360 if (templates
&& TREE_CHAIN (templates
))
2366 It is possible for a specialization with a given function
2367 signature to be instantiated from more than one function
2368 template. In such cases, explicit specification of the
2369 template arguments must be used to uniquely identify the
2370 function template specialization being specialized.
2372 Note that here, there's no suggestion that we're supposed to
2373 determine which of the candidate templates is most
2374 specialized. However, we, also have:
2378 Partial ordering of overloaded function template
2379 declarations is used in the following contexts to select
2380 the function template to which a function template
2381 specialization refers:
2383 -- when an explicit specialization refers to a function
2386 So, we do use the partial ordering rules, at least for now.
2387 This extension can only serve to make invalid programs valid,
2388 so it's safe. And, there is strong anecdotal evidence that
2389 the committee intended the partial ordering rules to apply;
2390 the EDG front end has that behavior, and John Spicer claims
2391 that the committee simply forgot to delete the wording in
2392 [temp.expl.spec]. */
2393 tree tmpl
= most_specialized_instantiation (templates
);
2394 if (tmpl
!= error_mark_node
)
2397 TREE_CHAIN (templates
) = NULL_TREE
;
2401 // Concepts allows multiple declarations of member functions
2402 // with the same signature. Like above, we need to rely on
2403 // on the partial ordering of those candidates to determine which
2405 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2407 if (tree cand
= most_constrained_function (candidates
))
2410 TREE_CHAIN (cand
) = NULL_TREE
;
2414 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2416 error ("template-id %qD for %q+D does not match any template "
2417 "declaration", template_id
, decl
);
2418 if (header_count
&& header_count
!= template_count
+ 1)
2419 inform (DECL_SOURCE_LOCATION (decl
),
2420 "saw %d %<template<>%>, need %d for "
2421 "specializing a member function template",
2422 header_count
, template_count
+ 1);
2424 print_candidates (orig_fns
);
2425 return error_mark_node
;
2427 else if ((templates
&& TREE_CHAIN (templates
))
2428 || (candidates
&& TREE_CHAIN (candidates
))
2429 || (templates
&& candidates
))
2431 error ("ambiguous template specialization %qD for %q+D",
2433 candidates
= chainon (candidates
, templates
);
2434 print_candidates (candidates
);
2435 return error_mark_node
;
2438 /* We have one, and exactly one, match. */
2441 tree fn
= TREE_VALUE (candidates
);
2442 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2444 /* Propagate the candidate's constraints to the declaration. */
2445 set_constraints (decl
, get_constraints (fn
));
2447 /* DECL is a re-declaration or partial instantiation of a template
2449 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2451 /* It was a specialization of an ordinary member function in a
2453 return DECL_TI_TEMPLATE (fn
);
2456 /* It was a specialization of a template. */
2457 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2458 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2460 *targs_out
= copy_node (targs
);
2461 SET_TMPL_ARGS_LEVEL (*targs_out
,
2462 TMPL_ARGS_DEPTH (*targs_out
),
2463 TREE_PURPOSE (templates
));
2466 *targs_out
= TREE_PURPOSE (templates
);
2467 return TREE_VALUE (templates
);
2470 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2471 but with the default argument values filled in from those in the
2475 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2478 tree new_spec_types
;
2483 if (spec_types
== void_list_node
)
2484 return void_list_node
;
2486 /* Substitute into the rest of the list. */
2488 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2489 TREE_CHAIN (tmpl_types
));
2491 /* Add the default argument for this parameter. */
2492 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2493 TREE_VALUE (spec_types
),
2497 /* DECL is an explicit specialization. Replicate default arguments
2498 from the template it specializes. (That way, code like:
2500 template <class T> void f(T = 3);
2501 template <> void f(double);
2504 works, as required.) An alternative approach would be to look up
2505 the correct default arguments at the call-site, but this approach
2506 is consistent with how implicit instantiations are handled. */
2509 copy_default_args_to_explicit_spec (tree decl
)
2514 tree new_spec_types
;
2518 tree object_type
= NULL_TREE
;
2519 tree in_charge
= NULL_TREE
;
2520 tree vtt
= NULL_TREE
;
2522 /* See if there's anything we need to do. */
2523 tmpl
= DECL_TI_TEMPLATE (decl
);
2524 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2525 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2526 if (TREE_PURPOSE (t
))
2531 old_type
= TREE_TYPE (decl
);
2532 spec_types
= TYPE_ARG_TYPES (old_type
);
2534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2536 /* Remove the this pointer, but remember the object's type for
2538 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2539 spec_types
= TREE_CHAIN (spec_types
);
2540 tmpl_types
= TREE_CHAIN (tmpl_types
);
2542 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2544 /* DECL may contain more parameters than TMPL due to the extra
2545 in-charge parameter in constructors and destructors. */
2546 in_charge
= spec_types
;
2547 spec_types
= TREE_CHAIN (spec_types
);
2549 if (DECL_HAS_VTT_PARM_P (decl
))
2552 spec_types
= TREE_CHAIN (spec_types
);
2556 /* Compute the merged default arguments. */
2558 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2560 /* Compute the new FUNCTION_TYPE. */
2564 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2569 /* Put the in-charge parameter back. */
2570 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2571 TREE_VALUE (in_charge
),
2574 new_type
= build_method_type_directly (object_type
,
2575 TREE_TYPE (old_type
),
2579 new_type
= build_function_type (TREE_TYPE (old_type
),
2581 new_type
= cp_build_type_attribute_variant (new_type
,
2582 TYPE_ATTRIBUTES (old_type
));
2583 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2585 TREE_TYPE (decl
) = new_type
;
2588 /* Return the number of template headers we expect to see for a definition
2589 or specialization of CTYPE or one of its non-template members. */
2592 num_template_headers_for_class (tree ctype
)
2594 int num_templates
= 0;
2596 while (ctype
&& CLASS_TYPE_P (ctype
))
2598 /* You're supposed to have one `template <...>' for every
2599 template class, but you don't need one for a full
2600 specialization. For example:
2602 template <class T> struct S{};
2603 template <> struct S<int> { void f(); };
2604 void S<int>::f () {}
2606 is correct; there shouldn't be a `template <>' for the
2607 definition of `S<int>::f'. */
2608 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2609 /* If CTYPE does not have template information of any
2610 kind, then it is not a template, nor is it nested
2611 within a template. */
2613 if (explicit_class_specialization_p (ctype
))
2615 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2618 ctype
= TYPE_CONTEXT (ctype
);
2621 return num_templates
;
2624 /* Do a simple sanity check on the template headers that precede the
2625 variable declaration DECL. */
2628 check_template_variable (tree decl
)
2630 tree ctx
= CP_DECL_CONTEXT (decl
);
2631 int wanted
= num_template_headers_for_class (ctx
);
2632 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2633 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2635 if (cxx_dialect
< cxx14
)
2636 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2637 "variable templates only available with "
2638 "%<-std=c++14%> or %<-std=gnu++14%>");
2640 // Namespace-scope variable templates should have a template header.
2643 if (template_header_count
> wanted
)
2645 auto_diagnostic_group d
;
2646 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2647 "too many template headers for %qD "
2650 if (warned
&& CLASS_TYPE_P (ctx
)
2651 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2652 inform (DECL_SOURCE_LOCATION (decl
),
2653 "members of an explicitly specialized class are defined "
2654 "without a template header");
2658 /* An explicit specialization whose declarator-id or class-head-name is not
2659 qualified shall be declared in the nearest enclosing namespace of the
2660 template, or, if the namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set.
2663 If the name declared in the explicit instantiation is an unqualified name,
2664 the explicit instantiation shall appear in the namespace where its template
2665 is declared or, if that namespace is inline (7.3.1), any namespace from its
2666 enclosing namespace set. */
2669 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2671 tree tmpl
= most_general_template (t
);
2672 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2673 && !is_nested_namespace (current_namespace
,
2674 CP_DECL_CONTEXT (tmpl
), true))
2676 if (processing_specialization
)
2677 permerror (loc
, "explicit specialization of %qD outside its "
2678 "namespace must use a nested-name-specifier", tmpl
);
2679 else if (processing_explicit_instantiation
2680 && cxx_dialect
>= cxx11
)
2681 /* This was allowed in C++98, so only pedwarn. */
2682 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2683 "outside its namespace must use a nested-name-"
2688 /* Warn for a template specialization SPEC that is missing some of a set
2689 of function or type attributes that the template TEMPL is declared with.
2690 ATTRLIST is a list of additional attributes that SPEC should be taken
2691 to ultimately be declared with. */
2694 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2696 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2697 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2699 /* Avoid warning if the difference between the primary and
2700 the specialization is not in one of the attributes below. */
2701 const char* const blacklist
[] = {
2702 "alloc_align", "alloc_size", "assume_aligned", "format",
2703 "format_arg", "malloc", "nonnull", NULL
2706 /* Put together a list of the black listed attributes that the primary
2707 template is declared with that the specialization is not, in case
2708 it's not apparent from the most recent declaration of the primary. */
2710 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2716 auto_diagnostic_group d
;
2717 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2718 "explicit specialization %q#D may be missing attributes",
2720 inform (DECL_SOURCE_LOCATION (tmpl
),
2722 ? G_("missing primary template attributes %s")
2723 : G_("missing primary template attribute %s"),
2724 pp_formatted_text (&str
));
2727 /* Check to see if the function just declared, as indicated in
2728 DECLARATOR, and in DECL, is a specialization of a function
2729 template. We may also discover that the declaration is an explicit
2730 instantiation at this point.
2732 Returns DECL, or an equivalent declaration that should be used
2733 instead if all goes well. Issues an error message if something is
2734 amiss. Returns error_mark_node if the error is not easily
2737 FLAGS is a bitmask consisting of the following flags:
2739 2: The function has a definition.
2740 4: The function is a friend.
2742 The TEMPLATE_COUNT is the number of references to qualifying
2743 template classes that appeared in the name of the function. For
2746 template <class T> struct S { void f(); };
2749 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2750 classes are not counted in the TEMPLATE_COUNT, so that in
2752 template <class T> struct S {};
2753 template <> struct S<int> { void f(); }
2754 template <> void S<int>::f();
2756 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2757 invalid; there should be no template <>.)
2759 If the function is a specialization, it is marked as such via
2760 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2761 is set up correctly, and it is added to the list of specializations
2762 for that template. */
2765 check_explicit_specialization (tree declarator
,
2771 int have_def
= flags
& 2;
2772 int is_friend
= flags
& 4;
2773 bool is_concept
= flags
& 8;
2774 int specialization
= 0;
2775 int explicit_instantiation
= 0;
2776 int member_specialization
= 0;
2777 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2778 tree dname
= DECL_NAME (decl
);
2783 if (!processing_specialization
)
2786 tsk
= tsk_excessive_parms
;
2789 tsk
= current_tmpl_spec_kind (template_count
);
2794 if (processing_specialization
&& !VAR_P (decl
))
2797 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2799 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2802 /* This could be something like:
2804 template <class T> void f(T);
2805 class S { friend void f<>(int); } */
2809 /* This case handles bogus declarations like template <>
2810 template <class T> void f<int>(); */
2812 error_at (cp_expr_loc_or_input_loc (declarator
),
2813 "template-id %qE in declaration of primary template",
2820 case tsk_invalid_member_spec
:
2821 /* The error has already been reported in
2822 check_specialization_scope. */
2823 return error_mark_node
;
2825 case tsk_invalid_expl_inst
:
2826 error ("template parameter list used in explicit instantiation");
2832 error ("definition provided for explicit instantiation");
2834 explicit_instantiation
= 1;
2837 case tsk_excessive_parms
:
2838 case tsk_insufficient_parms
:
2839 if (tsk
== tsk_excessive_parms
)
2840 error ("too many template parameter lists in declaration of %qD",
2842 else if (template_header_count
)
2843 error("too few template parameter lists in declaration of %qD", decl
);
2845 error("explicit specialization of %qD must be introduced by "
2846 "%<template <>%>", decl
);
2851 error ("explicit specialization declared %<concept%>");
2853 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2854 /* In cases like template<> constexpr bool v = true;
2855 We'll give an error in check_template_variable. */
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2860 member_specialization
= 1;
2866 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2868 /* This case handles bogus declarations like template <>
2869 template <class T> void f<int>(); */
2871 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2872 error_at (cp_expr_loc_or_input_loc (declarator
),
2873 "template-id %qE in declaration of primary template",
2875 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2877 /* Partial specialization of variable template. */
2878 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2882 else if (cxx_dialect
< cxx14
)
2883 error_at (cp_expr_loc_or_input_loc (declarator
),
2884 "non-type partial specialization %qE "
2885 "is not allowed", declarator
);
2887 error_at (cp_expr_loc_or_input_loc (declarator
),
2888 "non-class, non-variable partial specialization %qE "
2889 "is not allowed", declarator
);
2894 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2895 /* This is a specialization of a member template, without
2896 specialization the containing class. Something like:
2898 template <class T> struct S {
2899 template <class U> void f (U);
2901 template <> template <class U> void S<int>::f(U) {}
2903 That's a specialization -- but of the entire template. */
2911 if ((specialization
|| member_specialization
)
2912 /* This doesn't apply to variable templates. */
2913 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl
)))
2915 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2916 for (; t
; t
= TREE_CHAIN (t
))
2917 if (TREE_PURPOSE (t
))
2919 permerror (input_location
,
2920 "default argument specified in explicit specialization");
2925 if (specialization
|| member_specialization
|| explicit_instantiation
)
2927 tree tmpl
= NULL_TREE
;
2928 tree targs
= NULL_TREE
;
2929 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2931 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2932 if (!was_template_id
)
2936 gcc_assert (identifier_p (declarator
));
2941 /* If there is no class context, the explicit instantiation
2942 must be at namespace scope. */
2943 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2945 /* Find the namespace binding, using the declaration
2947 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2949 if (fns
== error_mark_node
)
2950 /* If lookup fails, look for a friend declaration so we can
2951 give a better diagnostic. */
2952 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2953 /*type*/false, /*complain*/true,
2956 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2958 error ("%qD is not a template function", dname
);
2959 fns
= error_mark_node
;
2963 declarator
= lookup_template_function (fns
, NULL_TREE
);
2966 if (declarator
== error_mark_node
)
2967 return error_mark_node
;
2969 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2971 if (!explicit_instantiation
)
2972 /* A specialization in class scope. This is invalid,
2973 but the error will already have been flagged by
2974 check_specialization_scope. */
2975 return error_mark_node
;
2978 /* It's not valid to write an explicit instantiation in
2981 class C { template void f(); }
2983 This case is caught by the parser. However, on
2986 template class C { void f(); };
2988 (which is invalid) we can get here. The error will be
2995 else if (ctype
!= NULL_TREE
2996 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2998 // We'll match variable templates in start_decl.
3002 /* Find the list of functions in ctype that have the same
3003 name as the declared function. */
3004 tree name
= TREE_OPERAND (declarator
, 0);
3006 if (constructor_name_p (name
, ctype
))
3008 if (DECL_CONSTRUCTOR_P (decl
)
3009 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3010 : !CLASSTYPE_DESTRUCTOR (ctype
))
3012 /* From [temp.expl.spec]:
3014 If such an explicit specialization for the member
3015 of a class template names an implicitly-declared
3016 special member function (clause _special_), the
3017 program is ill-formed.
3019 Similar language is found in [temp.explicit]. */
3020 error ("specialization of implicitly-declared special member function");
3021 return error_mark_node
;
3024 name
= DECL_NAME (decl
);
3027 /* For a type-conversion operator, We might be looking for
3028 `operator int' which will be a specialization of
3029 `operator T'. Grab all the conversion operators, and
3030 then select from them. */
3031 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3032 ? conv_op_identifier
: name
);
3034 if (fns
== NULL_TREE
)
3036 error ("no member function %qD declared in %qT", name
, ctype
);
3037 return error_mark_node
;
3040 TREE_OPERAND (declarator
, 0) = fns
;
3043 /* Figure out what exactly is being specialized at this point.
3044 Note that for an explicit instantiation, even one for a
3045 member function, we cannot tell a priori whether the
3046 instantiation is for a member template, or just a member
3047 function of a template class. Even if a member template is
3048 being instantiated, the member template arguments may be
3049 elided if they can be deduced from the rest of the
3051 tmpl
= determine_specialization (declarator
, decl
,
3053 member_specialization
,
3057 if (!tmpl
|| tmpl
== error_mark_node
)
3058 /* We couldn't figure out what this declaration was
3060 return error_mark_node
;
3063 if (TREE_CODE (decl
) == FUNCTION_DECL
3064 && DECL_HIDDEN_FRIEND_P (tmpl
))
3066 auto_diagnostic_group d
;
3067 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3068 "friend declaration %qD is not visible to "
3069 "explicit specialization", tmpl
))
3070 inform (DECL_SOURCE_LOCATION (tmpl
),
3071 "friend declaration here");
3073 else if (!ctype
&& !is_friend
3074 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3075 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3077 tree gen_tmpl
= most_general_template (tmpl
);
3079 if (explicit_instantiation
)
3081 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3082 is done by do_decl_instantiation later. */
3084 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3085 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3087 if (arg_depth
> parm_depth
)
3089 /* If TMPL is not the most general template (for
3090 example, if TMPL is a friend template that is
3091 injected into namespace scope), then there will
3092 be too many levels of TARGS. Remove some of them
3097 new_targs
= make_tree_vec (parm_depth
);
3098 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3099 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3100 = TREE_VEC_ELT (targs
, i
);
3104 return instantiate_template (tmpl
, targs
, tf_error
);
3107 /* If we thought that the DECL was a member function, but it
3108 turns out to be specializing a static member function,
3109 make DECL a static member function as well. */
3110 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3111 && DECL_STATIC_FUNCTION_P (tmpl
)
3112 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3113 revert_static_member_fn (decl
);
3115 /* If this is a specialization of a member template of a
3116 template class, we want to return the TEMPLATE_DECL, not
3117 the specialization of it. */
3118 if (tsk
== tsk_template
&& !was_template_id
)
3120 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3121 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3122 DECL_INITIAL (result
) = NULL_TREE
;
3126 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3127 DECL_SOURCE_LOCATION (result
)
3128 = DECL_SOURCE_LOCATION (decl
);
3129 /* We want to use the argument list specified in the
3130 definition, not in the original declaration. */
3131 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3132 for (parm
= DECL_ARGUMENTS (result
); parm
;
3133 parm
= DECL_CHAIN (parm
))
3134 DECL_CONTEXT (parm
) = result
;
3136 return register_specialization (tmpl
, gen_tmpl
, targs
,
3140 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3141 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3143 if (was_template_id
)
3144 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3146 /* Inherit default function arguments from the template
3147 DECL is specializing. */
3148 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3149 copy_default_args_to_explicit_spec (decl
);
3151 /* This specialization has the same protection as the
3152 template it specializes. */
3153 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3154 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3156 /* 7.1.1-1 [dcl.stc]
3158 A storage-class-specifier shall not be specified in an
3159 explicit specialization...
3161 The parser rejects these, so unless action is taken here,
3162 explicit function specializations will always appear with
3165 The action recommended by the C++ CWG in response to C++
3166 defect report 605 is to make the storage class and linkage
3167 of the explicit specialization match the templated function:
3169 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3171 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3173 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3174 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3176 /* A concept cannot be specialized. */
3177 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3179 error ("explicit specialization of function concept %qD",
3181 return error_mark_node
;
3184 /* This specialization has the same linkage and visibility as
3185 the function template it specializes. */
3186 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3187 if (! TREE_PUBLIC (decl
))
3189 DECL_INTERFACE_KNOWN (decl
) = 1;
3190 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3192 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3193 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3195 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3196 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3200 /* If DECL is a friend declaration, declared using an
3201 unqualified name, the namespace associated with DECL may
3202 have been set incorrectly. For example, in:
3204 template <typename T> void f(T);
3206 struct S { friend void f<int>(int); }
3209 we will have set the DECL_CONTEXT for the friend
3210 declaration to N, rather than to the global namespace. */
3211 if (DECL_NAMESPACE_SCOPE_P (decl
))
3212 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3214 if (is_friend
&& !have_def
)
3215 /* This is not really a declaration of a specialization.
3216 It's just the name of an instantiation. But, it's not
3217 a request for an instantiation, either. */
3218 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3219 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3220 /* A specialization is not necessarily COMDAT. */
3221 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3222 && DECL_DECLARED_INLINE_P (decl
));
3223 else if (VAR_P (decl
))
3224 DECL_COMDAT (decl
) = false;
3226 /* If this is a full specialization, register it so that we can find
3227 it again. Partial specializations will be registered in
3228 process_partial_specialization. */
3229 if (!processing_template_decl
)
3231 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3233 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3238 /* A 'structor should already have clones. */
3239 gcc_assert (decl
== error_mark_node
3240 || variable_template_p (tmpl
)
3241 || !(DECL_CONSTRUCTOR_P (decl
)
3242 || DECL_DESTRUCTOR_P (decl
))
3243 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3250 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3251 parameters. These are represented in the same format used for
3252 DECL_TEMPLATE_PARMS. */
3255 comp_template_parms (const_tree parms1
, const_tree parms2
)
3260 if (parms1
== parms2
)
3263 for (p1
= parms1
, p2
= parms2
;
3264 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3265 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3267 tree t1
= TREE_VALUE (p1
);
3268 tree t2
= TREE_VALUE (p2
);
3271 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3272 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3274 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3277 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3279 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3280 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3282 /* If either of the template parameters are invalid, assume
3283 they match for the sake of error recovery. */
3284 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3287 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3290 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3291 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3292 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3294 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3299 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3300 /* One set of parameters has more parameters lists than the
3307 /* Returns true if two template parameters are declared with
3308 equivalent constraints. */
3311 template_parameter_constraints_equivalent_p (const_tree parm1
, const_tree parm2
)
3313 tree req1
= TREE_TYPE (parm1
);
3314 tree req2
= TREE_TYPE (parm2
);
3318 return cp_tree_equal (req1
, req2
);
3322 /* Returns true when two template parameters are equivalent. */
3325 template_parameters_equivalent_p (const_tree parm1
, const_tree parm2
)
3327 tree decl1
= TREE_VALUE (parm1
);
3328 tree decl2
= TREE_VALUE (parm2
);
3330 /* If either of the template parameters are invalid, assume
3331 they match for the sake of error recovery. */
3332 if (error_operand_p (decl1
) || error_operand_p (decl2
))
3335 /* ... they declare parameters of the same kind. */
3336 if (TREE_CODE (decl1
) != TREE_CODE (decl2
))
3339 /* ... one parameter was introduced by a parameter declaration, then
3340 both are. This case arises as a result of eagerly rewriting declarations
3342 if (DECL_VIRTUAL_P (decl1
) != DECL_VIRTUAL_P (decl2
))
3345 /* ... if either declares a pack, they both do. */
3346 if (template_parameter_pack_p (decl1
) != template_parameter_pack_p (decl2
))
3349 if (TREE_CODE (decl1
) == PARM_DECL
)
3351 /* ... if they declare non-type parameters, the types are equivalent. */
3352 if (!same_type_p (TREE_TYPE (decl1
), TREE_TYPE (decl2
)))
3355 else if (TREE_CODE (decl2
) == TEMPLATE_DECL
)
3357 /* ... if they declare template template parameters, their template
3358 parameter lists are equivalent. */
3359 if (!template_heads_equivalent_p (decl1
, decl2
))
3363 /* ... if they are declared with a qualified-concept name, they both
3364 are, and those names are equivalent. */
3365 return template_parameter_constraints_equivalent_p (parm1
, parm2
);
3368 /* Returns true if two template parameters lists are equivalent.
3369 Two template parameter lists are equivalent if they have the
3370 same length and their corresponding parameters are equivalent.
3372 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3373 data structure returned by DECL_TEMPLATE_PARMS.
3375 This is generally the same implementation as comp_template_parms
3376 except that it also the concept names and arguments used to
3377 introduce parameters. */
3380 template_parameter_lists_equivalent_p (const_tree parms1
, const_tree parms2
)
3382 if (parms1
== parms2
)
3385 const_tree p1
= parms1
;
3386 const_tree p2
= parms2
;
3387 while (p1
!= NULL_TREE
&& p2
!= NULL_TREE
)
3389 tree list1
= TREE_VALUE (p1
);
3390 tree list2
= TREE_VALUE (p2
);
3392 if (TREE_VEC_LENGTH (list1
) != TREE_VEC_LENGTH (list2
))
3395 for (int i
= 0; i
< TREE_VEC_LENGTH (list2
); ++i
)
3397 tree parm1
= TREE_VEC_ELT (list1
, i
);
3398 tree parm2
= TREE_VEC_ELT (list2
, i
);
3399 if (!template_parameters_equivalent_p (parm1
, parm2
))
3403 p1
= TREE_CHAIN (p1
);
3404 p2
= TREE_CHAIN (p2
);
3407 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3413 /* Return true if the requires-clause of the template parameter lists are
3414 equivalent and false otherwise. */
3416 template_requirements_equivalent_p (const_tree parms1
, const_tree parms2
)
3418 tree req1
= TEMPLATE_PARMS_CONSTRAINTS (parms1
);
3419 tree req2
= TEMPLATE_PARMS_CONSTRAINTS (parms2
);
3420 if ((req1
!= NULL_TREE
) != (req2
!= NULL_TREE
))
3422 if (!cp_tree_equal (req1
, req2
))
3427 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3428 Two template heads are equivalent if their template parameter
3429 lists are equivalent and their requires clauses are equivalent.
3431 In pre-C++20, this is equivalent to calling comp_template_parms
3432 for the template parameters of TMPL1 and TMPL2. */
3435 template_heads_equivalent_p (const_tree tmpl1
, const_tree tmpl2
)
3437 tree parms1
= DECL_TEMPLATE_PARMS (tmpl1
);
3438 tree parms2
= DECL_TEMPLATE_PARMS (tmpl2
);
3440 /* Don't change the matching rules for pre-C++20. */
3441 if (cxx_dialect
< cxx2a
)
3442 return comp_template_parms (parms1
, parms2
);
3444 /* ... have the same number of template parameters, and their
3445 corresponding parameters are equivalent. */
3446 if (!template_parameter_lists_equivalent_p (parms1
, parms2
))
3449 /* ... if either has a requires-clause, they both do and their
3450 corresponding constraint-expressions are equivalent. */
3451 return template_requirements_equivalent_p (parms1
, parms2
);
3454 /* Determine whether PARM is a parameter pack. */
3457 template_parameter_pack_p (const_tree parm
)
3459 /* Determine if we have a non-type template parameter pack. */
3460 if (TREE_CODE (parm
) == PARM_DECL
)
3461 return (DECL_TEMPLATE_PARM_P (parm
)
3462 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3463 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3464 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3466 /* If this is a list of template parameters, we could get a
3467 TYPE_DECL or a TEMPLATE_DECL. */
3468 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3469 parm
= TREE_TYPE (parm
);
3471 /* Otherwise it must be a type template parameter. */
3472 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3473 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3474 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3477 /* Determine if T is a function parameter pack. */
3480 function_parameter_pack_p (const_tree t
)
3482 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3483 return DECL_PACK_P (t
);
3487 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3488 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3491 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3493 if (! primary_func_tmpl_inst
3494 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3495 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3498 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3501 /* Return true iff the function parameter PARAM_DECL was expanded
3502 from the function parameter pack PACK. */
3505 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3507 if (DECL_ARTIFICIAL (param_decl
)
3508 || !function_parameter_pack_p (pack
))
3511 /* The parameter pack and its pack arguments have the same
3513 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3516 /* Determine whether ARGS describes a variadic template args list,
3517 i.e., one that is terminated by a template argument pack. */
3520 template_args_variadic_p (tree args
)
3525 if (args
== NULL_TREE
)
3528 args
= INNERMOST_TEMPLATE_ARGS (args
);
3529 nargs
= TREE_VEC_LENGTH (args
);
3534 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3536 return ARGUMENT_PACK_P (last_parm
);
3539 /* Generate a new name for the parameter pack name NAME (an
3540 IDENTIFIER_NODE) that incorporates its */
3543 make_ith_pack_parameter_name (tree name
, int i
)
3545 /* Munge the name to include the parameter index. */
3546 #define NUMBUF_LEN 128
3547 char numbuf
[NUMBUF_LEN
];
3551 if (name
== NULL_TREE
)
3553 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3554 newname_len
= IDENTIFIER_LENGTH (name
)
3555 + strlen (numbuf
) + 2;
3556 newname
= (char*)alloca (newname_len
);
3557 snprintf (newname
, newname_len
,
3558 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3559 return get_identifier (newname
);
3562 /* Return true if T is a primary function, class or alias template
3563 specialization, not including the template pattern. */
3566 primary_template_specialization_p (const_tree t
)
3571 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3572 return (DECL_LANG_SPECIFIC (t
)
3573 && DECL_USE_TEMPLATE (t
)
3574 && DECL_TEMPLATE_INFO (t
)
3575 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3576 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3577 return (CLASSTYPE_TEMPLATE_INFO (t
)
3578 && CLASSTYPE_USE_TEMPLATE (t
)
3579 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3580 else if (alias_template_specialization_p (t
, nt_transparent
))
3585 /* Return true if PARM is a template template parameter. */
3588 template_template_parameter_p (const_tree parm
)
3590 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3593 /* Return true iff PARM is a DECL representing a type template
3597 template_type_parameter_p (const_tree parm
)
3600 && (TREE_CODE (parm
) == TYPE_DECL
3601 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3602 && DECL_TEMPLATE_PARM_P (parm
));
3605 /* Return the template parameters of T if T is a
3606 primary template instantiation, NULL otherwise. */
3609 get_primary_template_innermost_parameters (const_tree t
)
3611 tree parms
= NULL
, template_info
= NULL
;
3613 if ((template_info
= get_template_info (t
))
3614 && primary_template_specialization_p (t
))
3615 parms
= INNERMOST_TEMPLATE_PARMS
3616 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3621 /* Return the template parameters of the LEVELth level from the full list
3622 of template parameters PARMS. */
3625 get_template_parms_at_level (tree parms
, int level
)
3629 || TREE_CODE (parms
) != TREE_LIST
3630 || level
> TMPL_PARMS_DEPTH (parms
))
3633 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3634 if (TMPL_PARMS_DEPTH (p
) == level
)
3640 /* Returns the template arguments of T if T is a template instantiation,
3644 get_template_innermost_arguments (const_tree t
)
3646 tree args
= NULL
, template_info
= NULL
;
3648 if ((template_info
= get_template_info (t
))
3649 && TI_ARGS (template_info
))
3650 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3655 /* Return the argument pack elements of T if T is a template argument pack,
3659 get_template_argument_pack_elems (const_tree t
)
3661 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3662 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3665 return ARGUMENT_PACK_ARGS (t
);
3668 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3669 ARGUMENT_PACK_SELECT represents. */
3672 argument_pack_select_arg (tree t
)
3674 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3675 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3677 /* If the selected argument is an expansion E, that most likely means we were
3678 called from gen_elem_of_pack_expansion_instantiation during the
3679 substituting of an argument pack (of which the Ith element is a pack
3680 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3681 In this case, the Ith element resulting from this substituting is going to
3682 be a pack expansion, which pattern is the pattern of E. Let's return the
3683 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3684 resulting pack expansion from it. */
3685 if (PACK_EXPANSION_P (arg
))
3687 /* Make sure we aren't throwing away arg info. */
3688 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3689 arg
= PACK_EXPANSION_PATTERN (arg
);
3696 /* True iff FN is a function representing a built-in variadic parameter
3700 builtin_pack_fn_p (tree fn
)
3703 || TREE_CODE (fn
) != FUNCTION_DECL
3704 || !DECL_IS_BUILTIN (fn
))
3707 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3713 /* True iff CALL is a call to a function representing a built-in variadic
3717 builtin_pack_call_p (tree call
)
3719 if (TREE_CODE (call
) != CALL_EXPR
)
3721 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3724 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3727 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3730 tree ohi
= CALL_EXPR_ARG (call
, 0);
3731 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3732 false/*fn*/, true/*int_cst*/);
3734 if (value_dependent_expression_p (hi
))
3738 call
= copy_node (call
);
3739 CALL_EXPR_ARG (call
, 0) = hi
;
3741 tree ex
= make_pack_expansion (call
, complain
);
3742 tree vec
= make_tree_vec (1);
3743 TREE_VEC_ELT (vec
, 0) = ex
;
3748 hi
= cxx_constant_value (hi
);
3749 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3751 /* Calculate the largest value of len that won't make the size of the vec
3752 overflow an int. The compiler will exceed resource limits long before
3753 this, but it seems a decent place to diagnose. */
3754 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3756 if (len
< 0 || len
> max
)
3758 if ((complain
& tf_error
)
3759 && hi
!= error_mark_node
)
3760 error ("argument to %<__integer_pack%> must be between 0 and %d",
3762 return error_mark_node
;
3765 tree vec
= make_tree_vec (len
);
3767 for (int i
= 0; i
< len
; ++i
)
3768 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3774 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3778 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3781 if (!builtin_pack_call_p (call
))
3784 tree fn
= CALL_EXPR_FN (call
);
3786 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3787 return expand_integer_pack (call
, args
, complain
, in_decl
);
3792 /* Structure used to track the progress of find_parameter_packs_r. */
3793 struct find_parameter_pack_data
3795 /* TREE_LIST that will contain all of the parameter packs found by
3797 tree
* parameter_packs
;
3799 /* Set of AST nodes that have been visited by the traversal. */
3800 hash_set
<tree
> *visited
;
3802 /* True iff we're making a type pack expansion. */
3803 bool type_pack_expansion_p
;
3806 /* Identifies all of the argument packs that occur in a template
3807 argument and appends them to the TREE_LIST inside DATA, which is a
3808 find_parameter_pack_data structure. This is a subroutine of
3809 make_pack_expansion and uses_parameter_packs. */
3811 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3814 struct find_parameter_pack_data
* ppd
=
3815 (struct find_parameter_pack_data
*)data
;
3816 bool parameter_pack_p
= false;
3818 /* Handle type aliases/typedefs. */
3819 if (TYPE_ALIAS_P (t
))
3821 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3822 cp_walk_tree (&TI_ARGS (tinfo
),
3823 &find_parameter_packs_r
,
3829 /* Identify whether this is a parameter pack or not. */
3830 switch (TREE_CODE (t
))
3832 case TEMPLATE_PARM_INDEX
:
3833 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3834 parameter_pack_p
= true;
3837 case TEMPLATE_TYPE_PARM
:
3838 t
= TYPE_MAIN_VARIANT (t
);
3840 case TEMPLATE_TEMPLATE_PARM
:
3841 /* If the placeholder appears in the decl-specifier-seq of a function
3842 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3843 is a pack expansion, the invented template parameter is a template
3845 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3846 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3847 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3848 parameter_pack_p
= true;
3853 if (DECL_PACK_P (t
))
3855 /* We don't want to walk into the type of a PARM_DECL,
3856 because we don't want to see the type parameter pack. */
3858 parameter_pack_p
= true;
3863 if (DECL_PACK_P (t
))
3865 /* We don't want to walk into the type of a variadic capture proxy,
3866 because we don't want to see the type parameter pack. */
3868 parameter_pack_p
= true;
3870 else if (variable_template_specialization_p (t
))
3872 cp_walk_tree (&DECL_TI_ARGS (t
),
3873 find_parameter_packs_r
,
3880 if (builtin_pack_call_p (t
))
3881 parameter_pack_p
= true;
3885 parameter_pack_p
= true;
3888 /* Not a parameter pack. */
3892 if (parameter_pack_p
)
3894 /* Add this parameter pack to the list. */
3895 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3899 cp_walk_tree (&TYPE_CONTEXT (t
),
3900 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3902 /* This switch statement will return immediately if we don't find a
3904 switch (TREE_CODE (t
))
3906 case TEMPLATE_PARM_INDEX
:
3909 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3910 /* Check the template itself. */
3911 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3912 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3913 /* Check the template arguments. */
3914 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3919 case TEMPLATE_TYPE_PARM
:
3920 case TEMPLATE_TEMPLATE_PARM
:
3927 /* Ignore the declaration of a capture proxy for a parameter pack. */
3928 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3933 if (TYPE_PTRMEMFUNC_P (t
))
3939 if (TYPE_TEMPLATE_INFO (t
))
3940 cp_walk_tree (&TYPE_TI_ARGS (t
),
3941 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3947 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3952 cp_walk_tree (&TREE_TYPE (t
),
3953 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3957 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3962 case TYPE_PACK_EXPANSION
:
3963 case EXPR_PACK_EXPANSION
:
3968 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3973 case IDENTIFIER_NODE
:
3974 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3981 /* Look at explicit captures. */
3982 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3983 cap
; cap
= TREE_CHAIN (cap
))
3984 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3986 /* Since we defer implicit capture, look in the parms and body. */
3987 tree fn
= lambda_function (t
);
3988 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3990 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3998 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3999 type_pack_expansion_p to false so that any placeholders
4000 within the expression don't get marked as parameter packs. */
4001 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
4002 ppd
->type_pack_expansion_p
= false;
4003 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
4005 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
4011 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
4013 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
4015 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
4017 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4028 /* Determines if the expression or type T uses any parameter packs. */
4030 uses_parameter_packs (tree t
)
4032 tree parameter_packs
= NULL_TREE
;
4033 struct find_parameter_pack_data ppd
;
4034 ppd
.parameter_packs
= ¶meter_packs
;
4035 ppd
.visited
= new hash_set
<tree
>;
4036 ppd
.type_pack_expansion_p
= false;
4037 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4039 return parameter_packs
;
4042 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4043 representation a base-class initializer into a parameter pack
4044 expansion. If all goes well, the resulting node will be an
4045 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4048 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
4051 tree parameter_packs
= NULL_TREE
;
4052 bool for_types
= false;
4053 struct find_parameter_pack_data ppd
;
4055 if (!arg
|| arg
== error_mark_node
)
4058 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
4060 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4061 class initializer. In this case, the TREE_PURPOSE will be a
4062 _TYPE node (representing the base class expansion we're
4063 initializing) and the TREE_VALUE will be a TREE_LIST
4064 containing the initialization arguments.
4066 The resulting expansion looks somewhat different from most
4067 expansions. Rather than returning just one _EXPANSION, we
4068 return a TREE_LIST whose TREE_PURPOSE is a
4069 TYPE_PACK_EXPANSION containing the bases that will be
4070 initialized. The TREE_VALUE will be identical to the
4071 original TREE_VALUE, which is a list of arguments that will
4072 be passed to each base. We do not introduce any new pack
4073 expansion nodes into the TREE_VALUE (although it is possible
4074 that some already exist), because the TREE_PURPOSE and
4075 TREE_VALUE all need to be expanded together with the same
4076 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4077 resulting TREE_PURPOSE will mention the parameter packs in
4078 both the bases and the arguments to the bases. */
4081 tree parameter_packs
= NULL_TREE
;
4083 /* Determine which parameter packs will be used by the base
4085 ppd
.visited
= new hash_set
<tree
>;
4086 ppd
.parameter_packs
= ¶meter_packs
;
4087 ppd
.type_pack_expansion_p
= false;
4088 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
4089 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
4092 if (parameter_packs
== NULL_TREE
)
4094 if (complain
& tf_error
)
4095 error ("base initializer expansion %qT contains no parameter packs",
4098 return error_mark_node
;
4101 if (TREE_VALUE (arg
) != void_type_node
)
4103 /* Collect the sets of parameter packs used in each of the
4104 initialization arguments. */
4105 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
4107 /* Determine which parameter packs will be expanded in this
4109 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
4116 /* Create the pack expansion type for the base type. */
4117 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
4118 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
4119 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
4120 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
4122 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4123 they will rarely be compared to anything. */
4124 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
4126 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
4129 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
4132 /* Build the PACK_EXPANSION_* node. */
4134 ? cxx_make_type (TYPE_PACK_EXPANSION
)
4135 : make_node (EXPR_PACK_EXPANSION
);
4136 SET_PACK_EXPANSION_PATTERN (result
, arg
);
4137 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4139 /* Propagate type and const-expression information. */
4140 TREE_TYPE (result
) = TREE_TYPE (arg
);
4141 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4142 /* Mark this read now, since the expansion might be length 0. */
4143 mark_exp_read (arg
);
4146 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4147 they will rarely be compared to anything. */
4148 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4150 /* Determine which parameter packs will be expanded. */
4151 ppd
.parameter_packs
= ¶meter_packs
;
4152 ppd
.visited
= new hash_set
<tree
>;
4153 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4154 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4157 /* Make sure we found some parameter packs. */
4158 if (parameter_packs
== NULL_TREE
)
4160 if (complain
& tf_error
)
4163 error ("expansion pattern %qT contains no parameter packs", arg
);
4165 error ("expansion pattern %qE contains no parameter packs", arg
);
4167 return error_mark_node
;
4169 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4171 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4176 /* Checks T for any "bare" parameter packs, which have not yet been
4177 expanded, and issues an error if any are found. This operation can
4178 only be done on full expressions or types (e.g., an expression
4179 statement, "if" condition, etc.), because we could have expressions like:
4181 foo(f(g(h(args)))...)
4183 where "args" is a parameter pack. check_for_bare_parameter_packs
4184 should not be called for the subexpressions args, h(args),
4185 g(h(args)), or f(g(h(args))), because we would produce erroneous
4188 Returns TRUE and emits an error if there were bare parameter packs,
4189 returns FALSE otherwise. */
4191 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4193 tree parameter_packs
= NULL_TREE
;
4194 struct find_parameter_pack_data ppd
;
4196 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4199 /* A lambda might use a parameter pack from the containing context. */
4200 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4201 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4204 if (TREE_CODE (t
) == TYPE_DECL
)
4207 ppd
.parameter_packs
= ¶meter_packs
;
4208 ppd
.visited
= new hash_set
<tree
>;
4209 ppd
.type_pack_expansion_p
= false;
4210 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4213 if (parameter_packs
)
4215 if (loc
== UNKNOWN_LOCATION
)
4216 loc
= cp_expr_loc_or_input_loc (t
);
4217 error_at (loc
, "parameter packs not expanded with %<...%>:");
4218 while (parameter_packs
)
4220 tree pack
= TREE_VALUE (parameter_packs
);
4221 tree name
= NULL_TREE
;
4223 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4224 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4225 name
= TYPE_NAME (pack
);
4226 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4227 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4228 else if (TREE_CODE (pack
) == CALL_EXPR
)
4229 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4231 name
= DECL_NAME (pack
);
4234 inform (loc
, " %qD", name
);
4236 inform (loc
, " %s", "<anonymous>");
4238 parameter_packs
= TREE_CHAIN (parameter_packs
);
4247 /* Expand any parameter packs that occur in the template arguments in
4250 expand_template_argument_pack (tree args
)
4252 if (args
== error_mark_node
)
4253 return error_mark_node
;
4255 tree result_args
= NULL_TREE
;
4256 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4257 int num_result_args
= -1;
4258 int non_default_args_count
= -1;
4260 /* First, determine if we need to expand anything, and the number of
4261 slots we'll need. */
4262 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4264 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4265 if (arg
== NULL_TREE
)
4267 if (ARGUMENT_PACK_P (arg
))
4269 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4270 if (num_result_args
< 0)
4271 num_result_args
= in_arg
+ num_packed
;
4273 num_result_args
+= num_packed
;
4277 if (num_result_args
>= 0)
4282 /* If no expansion is necessary, we're done. */
4283 if (num_result_args
< 0)
4286 /* Expand arguments. */
4287 result_args
= make_tree_vec (num_result_args
);
4288 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4289 non_default_args_count
=
4290 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4291 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4293 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4294 if (ARGUMENT_PACK_P (arg
))
4296 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4297 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4298 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4299 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4300 if (non_default_args_count
> 0)
4301 non_default_args_count
+= num_packed
- 1;
4305 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4309 if (non_default_args_count
>= 0)
4310 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4314 /* Checks if DECL shadows a template parameter.
4316 [temp.local]: A template-parameter shall not be redeclared within its
4317 scope (including nested scopes).
4319 Emits an error and returns TRUE if the DECL shadows a parameter,
4320 returns FALSE otherwise. */
4323 check_template_shadow (tree decl
)
4327 /* If we're not in a template, we can't possibly shadow a template
4329 if (!current_template_parms
)
4332 /* Figure out what we're shadowing. */
4333 decl
= OVL_FIRST (decl
);
4334 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4336 /* If there's no previous binding for this name, we're not shadowing
4337 anything, let alone a template parameter. */
4341 /* If we're not shadowing a template parameter, we're done. Note
4342 that OLDDECL might be an OVERLOAD (or perhaps even an
4343 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4345 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4348 /* We check for decl != olddecl to avoid bogus errors for using a
4349 name inside a class. We check TPFI to avoid duplicate errors for
4350 inline member templates. */
4352 || (DECL_TEMPLATE_PARM_P (decl
)
4353 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4356 /* Don't complain about the injected class name, as we've already
4357 complained about the class itself. */
4358 if (DECL_SELF_REFERENCE_P (decl
))
4361 if (DECL_TEMPLATE_PARM_P (decl
))
4362 error ("declaration of template parameter %q+D shadows "
4363 "template parameter", decl
);
4365 error ("declaration of %q+#D shadows template parameter", decl
);
4366 inform (DECL_SOURCE_LOCATION (olddecl
),
4367 "template parameter %qD declared here", olddecl
);
4371 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4372 ORIG_LEVEL, DECL, and TYPE. */
4375 build_template_parm_index (int index
,
4381 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4382 TEMPLATE_PARM_IDX (t
) = index
;
4383 TEMPLATE_PARM_LEVEL (t
) = level
;
4384 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4385 TEMPLATE_PARM_DECL (t
) = decl
;
4386 TREE_TYPE (t
) = type
;
4387 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4388 TREE_READONLY (t
) = TREE_READONLY (decl
);
4393 /* Find the canonical type parameter for the given template type
4394 parameter. Returns the canonical type parameter, which may be TYPE
4395 if no such parameter existed. */
4398 canonical_type_parameter (tree type
)
4401 int idx
= TEMPLATE_TYPE_IDX (type
);
4402 if (!canonical_template_parms
)
4403 vec_alloc (canonical_template_parms
, idx
+ 1);
4405 if (canonical_template_parms
->length () <= (unsigned) idx
)
4406 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4408 list
= (*canonical_template_parms
)[idx
];
4409 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4410 list
= TREE_CHAIN (list
);
4413 return TREE_VALUE (list
);
4416 (*canonical_template_parms
)[idx
]
4417 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4422 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4423 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4424 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4425 new one is created. */
4428 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4429 tsubst_flags_t complain
)
4431 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4432 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4433 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4434 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4436 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4438 tree decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4439 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
),
4441 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4442 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4443 DECL_ARTIFICIAL (decl
) = 1;
4444 SET_DECL_TEMPLATE_PARM_P (decl
);
4446 tree tpi
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4447 TEMPLATE_PARM_LEVEL (index
) - levels
,
4448 TEMPLATE_PARM_ORIG_LEVEL (index
),
4450 TEMPLATE_PARM_DESCENDANTS (index
) = tpi
;
4451 TEMPLATE_PARM_PARAMETER_PACK (tpi
)
4452 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4454 /* Template template parameters need this. */
4456 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4458 inner
= build_decl (DECL_SOURCE_LOCATION (decl
),
4459 TYPE_DECL
, DECL_NAME (decl
), type
);
4460 DECL_TEMPLATE_RESULT (decl
) = inner
;
4461 DECL_ARTIFICIAL (inner
) = true;
4462 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4463 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4466 /* Attach the TPI to the decl. */
4467 if (TREE_CODE (inner
) == TYPE_DECL
)
4468 TEMPLATE_TYPE_PARM_INDEX (type
) = tpi
;
4470 DECL_INITIAL (decl
) = tpi
;
4473 return TEMPLATE_PARM_DESCENDANTS (index
);
4476 /* Process information from new template parameter PARM and append it
4477 to the LIST being built. This new parameter is a non-type
4478 parameter iff IS_NON_TYPE is true. This new parameter is a
4479 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4483 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4484 bool is_non_type
, bool is_parameter_pack
)
4489 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4490 tree defval
= TREE_PURPOSE (parm
);
4491 tree constr
= TREE_TYPE (parm
);
4495 tree p
= tree_last (list
);
4497 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4500 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4501 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4503 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4511 parm
= TREE_VALUE (parm
);
4513 SET_DECL_TEMPLATE_PARM_P (parm
);
4515 if (TREE_TYPE (parm
) != error_mark_node
)
4519 The top-level cv-qualifiers on the template-parameter are
4520 ignored when determining its type. */
4521 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4522 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4523 TREE_TYPE (parm
) = error_mark_node
;
4524 else if (uses_parameter_packs (TREE_TYPE (parm
))
4525 && !is_parameter_pack
4526 /* If we're in a nested template parameter list, the template
4527 template parameter could be a parameter pack. */
4528 && processing_template_parmlist
== 1)
4530 /* This template parameter is not a parameter pack, but it
4531 should be. Complain about "bare" parameter packs. */
4532 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4534 /* Recover by calling this a parameter pack. */
4535 is_parameter_pack
= true;
4539 /* A template parameter is not modifiable. */
4540 TREE_CONSTANT (parm
) = 1;
4541 TREE_READONLY (parm
) = 1;
4542 decl
= build_decl (parm_loc
,
4543 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4544 TREE_CONSTANT (decl
) = 1;
4545 TREE_READONLY (decl
) = 1;
4546 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4547 = build_template_parm_index (idx
, processing_template_decl
,
4548 processing_template_decl
,
4549 decl
, TREE_TYPE (parm
));
4551 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4552 = is_parameter_pack
;
4557 parm
= TREE_VALUE (TREE_VALUE (parm
));
4559 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4561 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4562 /* This is for distinguishing between real templates and template
4563 template parameters */
4564 TREE_TYPE (parm
) = t
;
4565 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4570 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4571 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4572 decl
= build_decl (parm_loc
,
4573 TYPE_DECL
, parm
, t
);
4576 TYPE_NAME (t
) = decl
;
4577 TYPE_STUB_DECL (t
) = decl
;
4579 TEMPLATE_TYPE_PARM_INDEX (t
)
4580 = build_template_parm_index (idx
, processing_template_decl
,
4581 processing_template_decl
,
4582 decl
, TREE_TYPE (parm
));
4583 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4584 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4586 DECL_ARTIFICIAL (decl
) = 1;
4587 SET_DECL_TEMPLATE_PARM_P (decl
);
4589 /* Build requirements for the type/template parameter.
4590 This must be done after SET_DECL_TEMPLATE_PARM_P or
4591 process_template_parm could fail. */
4592 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4594 decl
= pushdecl (decl
);
4598 /* Build the parameter node linking the parameter declaration,
4599 its default argument (if any), and its constraints (if any). */
4600 parm
= build_tree_list (defval
, parm
);
4601 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4603 return chainon (list
, parm
);
4606 /* The end of a template parameter list has been reached. Process the
4607 tree list into a parameter vector, converting each parameter into a more
4608 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4612 end_template_parm_list (tree parms
)
4616 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4618 /* Pop the dummy parameter level and add the real one. */
4619 current_template_parms
= TREE_CHAIN (current_template_parms
);
4621 current_template_parms
4622 = tree_cons (size_int (processing_template_decl
),
4623 saved_parmlist
, current_template_parms
);
4625 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4627 next
= TREE_CHAIN (parm
);
4628 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4629 TREE_CHAIN (parm
) = NULL_TREE
;
4632 --processing_template_parmlist
;
4634 return saved_parmlist
;
4637 // Explicitly indicate the end of the template parameter list. We assume
4638 // that the current template parameters have been constructed and/or
4639 // managed explicitly, as when creating new template template parameters
4640 // from a shorthand constraint.
4642 end_template_parm_list ()
4644 --processing_template_parmlist
;
4647 /* end_template_decl is called after a template declaration is seen. */
4650 end_template_decl (void)
4652 reset_specialization ();
4654 if (! processing_template_decl
)
4657 /* This matches the pushlevel in begin_template_parm_list. */
4660 --processing_template_decl
;
4661 current_template_parms
= TREE_CHAIN (current_template_parms
);
4664 /* Takes a TREE_LIST representing a template parameter and convert it
4665 into an argument suitable to be passed to the type substitution
4666 functions. Note that If the TREE_LIST contains an error_mark
4667 node, the returned argument is error_mark_node. */
4670 template_parm_to_arg (tree t
)
4674 || TREE_CODE (t
) != TREE_LIST
)
4677 if (error_operand_p (TREE_VALUE (t
)))
4678 return error_mark_node
;
4682 if (TREE_CODE (t
) == TYPE_DECL
4683 || TREE_CODE (t
) == TEMPLATE_DECL
)
4687 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4689 /* Turn this argument into a TYPE_ARGUMENT_PACK
4690 with a single element, which expands T. */
4691 tree vec
= make_tree_vec (1);
4693 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4695 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4697 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4698 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4703 t
= DECL_INITIAL (t
);
4705 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4707 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4708 with a single element, which expands T. */
4709 tree vec
= make_tree_vec (1);
4711 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4713 t
= convert_from_reference (t
);
4714 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4716 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4717 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4720 t
= convert_from_reference (t
);
4725 /* Given a single level of template parameters (a TREE_VEC), return it
4726 as a set of template arguments. */
4729 template_parms_level_to_args (tree parms
)
4731 tree a
= copy_node (parms
);
4732 TREE_TYPE (a
) = NULL_TREE
;
4733 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4734 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4737 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4742 /* Given a set of template parameters, return them as a set of template
4743 arguments. The template parameters are represented as a TREE_VEC, in
4744 the form documented in cp-tree.h for template arguments. */
4747 template_parms_to_args (tree parms
)
4750 tree args
= NULL_TREE
;
4751 int length
= TMPL_PARMS_DEPTH (parms
);
4754 /* If there is only one level of template parameters, we do not
4755 create a TREE_VEC of TREE_VECs. Instead, we return a single
4756 TREE_VEC containing the arguments. */
4758 args
= make_tree_vec (length
);
4760 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4762 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4765 TREE_VEC_ELT (args
, --l
) = a
;
4773 /* Within the declaration of a template, return the currently active
4774 template parameters as an argument TREE_VEC. */
4777 current_template_args (void)
4779 return template_parms_to_args (current_template_parms
);
4782 /* Return the fully generic arguments for of TMPL, i.e. what
4783 current_template_args would be while parsing it. */
4786 generic_targs_for (tree tmpl
)
4788 if (tmpl
== NULL_TREE
)
4790 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
4791 || DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4792 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4793 template parameter, it has no TEMPLATE_INFO; for a partial
4794 specialization, it has the arguments for the primary template, and we
4795 want the arguments for the partial specialization. */;
4796 else if (tree result
= DECL_TEMPLATE_RESULT (tmpl
))
4797 if (tree ti
= get_template_info (result
))
4798 return TI_ARGS (ti
);
4799 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
));
4802 /* Update the declared TYPE by doing any lookups which were thought to be
4803 dependent, but are not now that we know the SCOPE of the declarator. */
4806 maybe_update_decl_type (tree orig_type
, tree scope
)
4808 tree type
= orig_type
;
4810 if (type
== NULL_TREE
)
4813 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4814 type
= TREE_TYPE (type
);
4816 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4817 && dependent_type_p (type
)
4818 /* Don't bother building up the args in this case. */
4819 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4821 /* tsubst in the args corresponding to the template parameters,
4822 including auto if present. Most things will be unchanged, but
4823 make_typename_type and tsubst_qualified_id will resolve
4824 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4825 tree args
= current_template_args ();
4826 tree auto_node
= type_uses_auto (type
);
4830 tree auto_vec
= make_tree_vec (1);
4831 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4832 args
= add_to_template_args (args
, auto_vec
);
4834 pushed
= push_scope (scope
);
4835 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4840 if (type
== error_mark_node
)
4843 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4845 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4848 type
= TYPE_NAME (type
);
4853 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4854 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4855 the new template is a member template. */
4858 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4860 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4861 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4862 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4863 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4864 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4865 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4870 struct template_parm_data
4872 /* The level of the template parameters we are currently
4876 /* The index of the specialization argument we are currently
4880 /* An array whose size is the number of template parameters. The
4881 elements are nonzero if the parameter has been used in any one
4882 of the arguments processed so far. */
4885 /* An array whose size is the number of template arguments. The
4886 elements are nonzero if the argument makes use of template
4887 parameters of this level. */
4888 int* arg_uses_template_parms
;
4891 /* Subroutine of push_template_decl used to see if each template
4892 parameter in a partial specialization is used in the explicit
4893 argument list. If T is of the LEVEL given in DATA (which is
4894 treated as a template_parm_data*), then DATA->PARMS is marked
4898 mark_template_parm (tree t
, void* data
)
4902 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4904 template_parm_level_and_index (t
, &level
, &idx
);
4906 if (level
== tpd
->level
)
4908 tpd
->parms
[idx
] = 1;
4909 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4912 /* In C++17 the type of a non-type argument is a deduced context. */
4913 if (cxx_dialect
>= cxx17
4914 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4915 for_each_template_parm (TREE_TYPE (t
),
4916 &mark_template_parm
,
4919 /*include_nondeduced_p=*/false);
4921 /* Return zero so that for_each_template_parm will continue the
4922 traversal of the tree; we want to mark *every* template parm. */
4926 /* Process the partial specialization DECL. */
4929 process_partial_specialization (tree decl
)
4931 tree type
= TREE_TYPE (decl
);
4932 tree tinfo
= get_template_info (decl
);
4933 tree maintmpl
= TI_TEMPLATE (tinfo
);
4934 tree specargs
= TI_ARGS (tinfo
);
4935 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4936 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4939 int nargs
= TREE_VEC_LENGTH (inner_args
);
4942 bool did_error_intro
= false;
4943 struct template_parm_data tpd
;
4944 struct template_parm_data tpd2
;
4946 gcc_assert (current_template_parms
);
4948 /* A concept cannot be specialized. */
4949 if (flag_concepts
&& variable_concept_p (maintmpl
))
4951 error ("specialization of variable concept %q#D", maintmpl
);
4952 return error_mark_node
;
4955 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4956 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4958 /* We check that each of the template parameters given in the
4959 partial specialization is used in the argument list to the
4960 specialization. For example:
4962 template <class T> struct S;
4963 template <class T> struct S<T*>;
4965 The second declaration is OK because `T*' uses the template
4966 parameter T, whereas
4968 template <class T> struct S<int>;
4970 is no good. Even trickier is:
4981 The S2<T> declaration is actually invalid; it is a
4982 full-specialization. Of course,
4985 struct S2<T (*)(U)>;
4987 or some such would have been OK. */
4988 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4989 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4990 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4992 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4993 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4994 for (i
= 0; i
< nargs
; ++i
)
4996 tpd
.current_arg
= i
;
4997 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4998 &mark_template_parm
,
5001 /*include_nondeduced_p=*/false);
5003 for (i
= 0; i
< ntparms
; ++i
)
5004 if (tpd
.parms
[i
] == 0)
5006 /* One of the template parms was not used in a deduced context in the
5008 if (!did_error_intro
)
5010 error ("template parameters not deducible in "
5011 "partial specialization:");
5012 did_error_intro
= true;
5015 inform (input_location
, " %qD",
5016 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
5019 if (did_error_intro
)
5020 return error_mark_node
;
5022 /* [temp.class.spec]
5024 The argument list of the specialization shall not be identical to
5025 the implicit argument list of the primary template. */
5027 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
5028 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
5030 || !strictly_subsumes (current_template_constraints (),
5031 inner_args
, maintmpl
)))
5034 error ("partial specialization %q+D does not specialize "
5035 "any template arguments; to define the primary template, "
5036 "remove the template argument list", decl
);
5038 error ("partial specialization %q+D does not specialize any "
5039 "template arguments and is not more constrained than "
5040 "the primary template; to define the primary template, "
5041 "remove the template argument list", decl
);
5042 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5045 /* A partial specialization that replaces multiple parameters of the
5046 primary template with a pack expansion is less specialized for those
5048 if (nargs
< DECL_NTPARMS (maintmpl
))
5050 error ("partial specialization is not more specialized than the "
5051 "primary template because it replaces multiple parameters "
5052 "with a pack expansion");
5053 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
5054 /* Avoid crash in process_partial_specialization. */
5058 /* If we aren't in a dependent class, we can actually try deduction. */
5059 else if (tpd
.level
== 1
5060 /* FIXME we should be able to handle a partial specialization of a
5061 partial instantiation, but currently we can't (c++/41727). */
5062 && TMPL_ARGS_DEPTH (specargs
) == 1
5063 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
5065 auto_diagnostic_group d
;
5066 if (permerror (input_location
, "partial specialization %qD is not "
5067 "more specialized than", decl
))
5068 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
5072 /* [temp.class.spec]
5074 A partially specialized non-type argument expression shall not
5075 involve template parameters of the partial specialization except
5076 when the argument expression is a simple identifier.
5078 The type of a template parameter corresponding to a specialized
5079 non-type argument shall not be dependent on a parameter of the
5082 Also, we verify that pack expansions only occur at the
5083 end of the argument list. */
5084 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
5086 for (i
= 0; i
< nargs
; ++i
)
5088 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
5089 tree arg
= TREE_VEC_ELT (inner_args
, i
);
5090 tree packed_args
= NULL_TREE
;
5093 if (ARGUMENT_PACK_P (arg
))
5095 /* Extract the arguments from the argument pack. We'll be
5096 iterating over these in the following loop. */
5097 packed_args
= ARGUMENT_PACK_ARGS (arg
);
5098 len
= TREE_VEC_LENGTH (packed_args
);
5101 for (j
= 0; j
< len
; j
++)
5104 /* Get the Jth argument in the parameter pack. */
5105 arg
= TREE_VEC_ELT (packed_args
, j
);
5107 if (PACK_EXPANSION_P (arg
))
5109 /* Pack expansions must come at the end of the
5111 if ((packed_args
&& j
< len
- 1)
5112 || (!packed_args
&& i
< nargs
- 1))
5114 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5115 error ("parameter pack argument %qE must be at the "
5116 "end of the template argument list", arg
);
5118 error ("parameter pack argument %qT must be at the "
5119 "end of the template argument list", arg
);
5123 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5124 /* We only care about the pattern. */
5125 arg
= PACK_EXPANSION_PATTERN (arg
);
5127 if (/* These first two lines are the `non-type' bit. */
5129 && TREE_CODE (arg
) != TEMPLATE_DECL
5130 /* This next two lines are the `argument expression is not just a
5131 simple identifier' condition and also the `specialized
5132 non-type argument' bit. */
5133 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
5134 && !((REFERENCE_REF_P (arg
)
5135 || TREE_CODE (arg
) == VIEW_CONVERT_EXPR
)
5136 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
5138 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
5139 || (packed_args
&& uses_template_parms (arg
)))
5140 error_at (cp_expr_loc_or_input_loc (arg
),
5141 "template argument %qE involves template "
5142 "parameter(s)", arg
);
5145 /* Look at the corresponding template parameter,
5146 marking which template parameters its type depends
5148 tree type
= TREE_TYPE (parm
);
5152 /* We haven't yet initialized TPD2. Do so now. */
5153 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
5154 /* The number of parameters here is the number in the
5155 main template, which, as checked in the assertion
5157 tpd2
.parms
= XALLOCAVEC (int, nargs
);
5159 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
5162 /* Mark the template parameters. But this time, we're
5163 looking for the template parameters of the main
5164 template, not in the specialization. */
5165 tpd2
.current_arg
= i
;
5166 tpd2
.arg_uses_template_parms
[i
] = 0;
5167 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
5168 for_each_template_parm (type
,
5169 &mark_template_parm
,
5172 /*include_nondeduced_p=*/false);
5174 if (tpd2
.arg_uses_template_parms
[i
])
5176 /* The type depended on some template parameters.
5177 If they are fully specialized in the
5178 specialization, that's OK. */
5181 for (j
= 0; j
< nargs
; ++j
)
5182 if (tpd2
.parms
[j
] != 0
5183 && tpd
.arg_uses_template_parms
[j
])
5186 error_n (input_location
, count
,
5187 "type %qT of template argument %qE depends "
5188 "on a template parameter",
5189 "type %qT of template argument %qE depends "
5190 "on template parameters",
5199 /* We should only get here once. */
5200 if (TREE_CODE (decl
) == TYPE_DECL
)
5201 gcc_assert (!COMPLETE_TYPE_P (type
));
5203 // Build the template decl.
5204 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5205 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5206 TREE_TYPE (tmpl
) = type
;
5207 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5208 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5209 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5210 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5212 /* Give template template parms a DECL_CONTEXT of the template
5213 for which they are a parameter. */
5214 for (i
= 0; i
< ntparms
; ++i
)
5216 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5217 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5218 DECL_CONTEXT (parm
) = tmpl
;
5222 /* We didn't register this in check_explicit_specialization so we could
5223 wait until the constraints were set. */
5224 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5226 associate_classtype_constraints (type
);
5228 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5229 = tree_cons (specargs
, tmpl
,
5230 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5231 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5233 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5234 inst
= TREE_CHAIN (inst
))
5236 tree instance
= TREE_VALUE (inst
);
5237 if (TYPE_P (instance
)
5238 ? (COMPLETE_TYPE_P (instance
)
5239 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5240 : DECL_TEMPLATE_INSTANTIATION (instance
))
5242 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5243 tree inst_decl
= (DECL_P (instance
)
5244 ? instance
: TYPE_NAME (instance
));
5247 else if (spec
== error_mark_node
)
5248 permerror (input_location
,
5249 "declaration of %qD ambiguates earlier template "
5250 "instantiation for %qD", decl
, inst_decl
);
5251 else if (TREE_VALUE (spec
) == tmpl
)
5252 permerror (input_location
,
5253 "partial specialization of %qD after instantiation "
5254 "of %qD", decl
, inst_decl
);
5261 /* PARM is a template parameter of some form; return the corresponding
5262 TEMPLATE_PARM_INDEX. */
5265 get_template_parm_index (tree parm
)
5267 if (TREE_CODE (parm
) == PARM_DECL
5268 || TREE_CODE (parm
) == CONST_DECL
)
5269 parm
= DECL_INITIAL (parm
);
5270 else if (TREE_CODE (parm
) == TYPE_DECL
5271 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5272 parm
= TREE_TYPE (parm
);
5273 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5274 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5275 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5276 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5277 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5281 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5282 parameter packs used by the template parameter PARM. */
5285 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5287 /* A type parm can't refer to another parm. */
5288 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5290 else if (TREE_CODE (parm
) == PARM_DECL
)
5292 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5297 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5299 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5300 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5302 tree p
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
5303 if (template_parameter_pack_p (p
))
5304 /* Any packs in the type are expanded by this parameter. */;
5306 fixed_parameter_pack_p_1 (p
, ppd
);
5310 /* PARM is a template parameter pack. Return any parameter packs used in
5311 its type or the type of any of its template parameters. If there are
5312 any such packs, it will be instantiated into a fixed template parameter
5313 list by partial instantiation rather than be fully deduced. */
5316 fixed_parameter_pack_p (tree parm
)
5318 /* This can only be true in a member template. */
5319 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5321 /* This can only be true for a parameter pack. */
5322 if (!template_parameter_pack_p (parm
))
5324 /* A type parm can't refer to another parm. */
5325 if (TREE_CODE (parm
) == TYPE_DECL
)
5328 tree parameter_packs
= NULL_TREE
;
5329 struct find_parameter_pack_data ppd
;
5330 ppd
.parameter_packs
= ¶meter_packs
;
5331 ppd
.visited
= new hash_set
<tree
>;
5332 ppd
.type_pack_expansion_p
= false;
5334 fixed_parameter_pack_p_1 (parm
, &ppd
);
5337 return parameter_packs
;
5340 /* Check that a template declaration's use of default arguments and
5341 parameter packs is not invalid. Here, PARMS are the template
5342 parameters. IS_PRIMARY is true if DECL is the thing declared by
5343 a primary template. IS_PARTIAL is true if DECL is a partial
5346 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5347 function template declaration or a friend class template
5348 declaration. In the function case, 1 indicates a declaration, 2
5349 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5350 emitted for extraneous default arguments.
5352 Returns TRUE if there were no errors found, FALSE otherwise. */
5355 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5356 bool is_partial
, int is_friend_decl
)
5359 int last_level_to_check
;
5361 bool no_errors
= true;
5365 A default template-argument shall not be specified in a
5366 function template declaration or a function template definition, nor
5367 in the template-parameter-list of the definition of a member of a
5370 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5371 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5372 /* You can't have a function template declaration in a local
5373 scope, nor you can you define a member of a class template in a
5377 if ((TREE_CODE (decl
) == TYPE_DECL
5379 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5380 || (TREE_CODE (decl
) == FUNCTION_DECL
5381 && LAMBDA_FUNCTION_P (decl
)))
5382 /* A lambda doesn't have an explicit declaration; don't complain
5383 about the parms of the enclosing class. */
5386 if (current_class_type
5387 && !TYPE_BEING_DEFINED (current_class_type
)
5388 && DECL_LANG_SPECIFIC (decl
)
5389 && DECL_DECLARES_FUNCTION_P (decl
)
5390 /* If this is either a friend defined in the scope of the class
5391 or a member function. */
5392 && (DECL_FUNCTION_MEMBER_P (decl
)
5393 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5394 : DECL_FRIEND_CONTEXT (decl
)
5395 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5397 /* And, if it was a member function, it really was defined in
5398 the scope of the class. */
5399 && (!DECL_FUNCTION_MEMBER_P (decl
)
5400 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5401 /* We already checked these parameters when the template was
5402 declared, so there's no need to do it again now. This function
5403 was defined in class scope, but we're processing its body now
5404 that the class is complete. */
5407 /* Core issue 226 (C++0x only): the following only applies to class
5410 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5414 If a template-parameter has a default template-argument, all
5415 subsequent template-parameters shall have a default
5416 template-argument supplied. */
5417 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5419 tree inner_parms
= TREE_VALUE (parm_level
);
5420 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5421 int seen_def_arg_p
= 0;
5424 for (i
= 0; i
< ntparms
; ++i
)
5426 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5428 if (parm
== error_mark_node
)
5431 if (TREE_PURPOSE (parm
))
5433 else if (seen_def_arg_p
5434 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5436 error ("no default argument for %qD", TREE_VALUE (parm
));
5437 /* For better subsequent error-recovery, we indicate that
5438 there should have been a default argument. */
5439 TREE_PURPOSE (parm
) = error_mark_node
;
5442 else if (!is_partial
5444 /* Don't complain about an enclosing partial
5446 && parm_level
== parms
5447 && TREE_CODE (decl
) == TYPE_DECL
5449 && template_parameter_pack_p (TREE_VALUE (parm
))
5450 /* A fixed parameter pack will be partially
5451 instantiated into a fixed length list. */
5452 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5454 /* A primary class template can only have one
5455 parameter pack, at the end of the template
5458 error ("parameter pack %q+D must be at the end of the"
5459 " template parameter list", TREE_VALUE (parm
));
5461 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5469 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5473 /* For an ordinary class template, default template arguments are
5474 allowed at the innermost level, e.g.:
5475 template <class T = int>
5477 but, in a partial specialization, they're not allowed even
5478 there, as we have in [temp.class.spec]:
5480 The template parameter list of a specialization shall not
5481 contain default template argument values.
5483 So, for a partial specialization, or for a function template
5484 (in C++98/C++03), we look at all of them. */
5487 /* But, for a primary class template that is not a partial
5488 specialization we look at all template parameters except the
5490 parms
= TREE_CHAIN (parms
);
5492 /* Figure out what error message to issue. */
5493 if (is_friend_decl
== 2)
5494 msg
= G_("default template arguments may not be used in function template "
5495 "friend re-declaration");
5496 else if (is_friend_decl
)
5497 msg
= G_("default template arguments may not be used in template "
5498 "friend declarations");
5499 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5500 msg
= G_("default template arguments may not be used in function templates "
5501 "without %<-std=c++11%> or %<-std=gnu++11%>");
5502 else if (is_partial
)
5503 msg
= G_("default template arguments may not be used in "
5504 "partial specializations");
5505 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5506 msg
= G_("default argument for template parameter for class enclosing %qD");
5508 /* Per [temp.param]/9, "A default template-argument shall not be
5509 specified in the template-parameter-lists of the definition of
5510 a member of a class template that appears outside of the member's
5511 class.", thus if we aren't handling a member of a class template
5512 there is no need to examine the parameters. */
5515 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5516 /* If we're inside a class definition, there's no need to
5517 examine the parameters to the class itself. On the one
5518 hand, they will be checked when the class is defined, and,
5519 on the other, default arguments are valid in things like:
5520 template <class T = double>
5521 struct S { template <class U> void f(U); };
5522 Here the default argument for `S' has no bearing on the
5523 declaration of `f'. */
5524 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5526 /* Check everything. */
5527 last_level_to_check
= 0;
5529 for (parm_level
= parms
;
5530 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5531 parm_level
= TREE_CHAIN (parm_level
))
5533 tree inner_parms
= TREE_VALUE (parm_level
);
5537 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5538 for (i
= 0; i
< ntparms
; ++i
)
5540 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5543 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5548 if (is_friend_decl
== 2)
5555 /* Clear out the default argument so that we are not
5557 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5561 /* At this point, if we're still interested in issuing messages,
5562 they must apply to classes surrounding the object declared. */
5564 msg
= G_("default argument for template parameter for class "
5571 /* Worker for push_template_decl_real, called via
5572 for_each_template_parm. DATA is really an int, indicating the
5573 level of the parameters we are interested in. If T is a template
5574 parameter of that level, return nonzero. */
5577 template_parm_this_level_p (tree t
, void* data
)
5579 int this_level
= *(int *)data
;
5582 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5583 level
= TEMPLATE_PARM_LEVEL (t
);
5585 level
= TEMPLATE_TYPE_LEVEL (t
);
5586 return level
== this_level
;
5589 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5590 DATA is really an int, indicating the innermost outer level of parameters.
5591 If T is a template parameter of that level or further out, return
5595 template_parm_outer_level (tree t
, void *data
)
5597 int this_level
= *(int *)data
;
5600 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5601 level
= TEMPLATE_PARM_LEVEL (t
);
5603 level
= TEMPLATE_TYPE_LEVEL (t
);
5604 return level
<= this_level
;
5607 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5608 parameters given by current_template_args, or reuses a
5609 previously existing one, if appropriate. Returns the DECL, or an
5610 equivalent one, if it is replaced via a call to duplicate_decls.
5612 If IS_FRIEND is true, DECL is a friend declaration. */
5615 push_template_decl_real (tree decl
, bool is_friend
)
5623 int new_template_p
= 0;
5624 /* True if the template is a member template, in the sense of
5626 bool member_template_p
= false;
5628 if (decl
== error_mark_node
|| !current_template_parms
)
5629 return error_mark_node
;
5631 /* See if this is a partial specialization. */
5632 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5633 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5634 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5636 && DECL_LANG_SPECIFIC (decl
)
5637 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5638 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5640 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5644 /* For a friend, we want the context of the friend, not
5645 the type of which it is a friend. */
5646 ctx
= CP_DECL_CONTEXT (decl
);
5647 else if (CP_DECL_CONTEXT (decl
)
5648 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5649 /* In the case of a virtual function, we want the class in which
5651 ctx
= CP_DECL_CONTEXT (decl
);
5653 /* Otherwise, if we're currently defining some class, the DECL
5654 is assumed to be a member of the class. */
5655 ctx
= current_scope ();
5657 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5660 if (!DECL_CONTEXT (decl
))
5661 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5663 /* See if this is a primary template. */
5664 if (is_friend
&& ctx
5665 && uses_template_parms_level (ctx
, processing_template_decl
))
5666 /* A friend template that specifies a class context, i.e.
5667 template <typename T> friend void A<T>::f();
5670 else if (TREE_CODE (decl
) == TYPE_DECL
5671 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5674 is_primary
= template_parm_scope_p ();
5678 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5680 if (DECL_CLASS_SCOPE_P (decl
))
5681 member_template_p
= true;
5682 if (TREE_CODE (decl
) == TYPE_DECL
5683 && IDENTIFIER_ANON_P (DECL_NAME (decl
)))
5685 error ("template class without a name");
5686 return error_mark_node
;
5688 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5690 if (member_template_p
)
5692 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5693 error ("member template %qD may not have virt-specifiers", decl
);
5695 if (DECL_DESTRUCTOR_P (decl
))
5699 A destructor shall not be a member template. */
5700 error_at (DECL_SOURCE_LOCATION (decl
),
5701 "destructor %qD declared as member template", decl
);
5702 return error_mark_node
;
5704 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5705 && (!prototype_p (TREE_TYPE (decl
))
5706 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5707 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5708 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5709 == void_list_node
)))
5711 /* [basic.stc.dynamic.allocation]
5713 An allocation function can be a function
5714 template. ... Template allocation functions shall
5715 have two or more parameters. */
5716 error ("invalid template declaration of %qD", decl
);
5717 return error_mark_node
;
5720 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5721 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5723 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5724 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5725 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5727 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5728 if (TREE_CODE (t
) == TYPE_DECL
)
5730 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5731 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5734 else if (TREE_CODE (decl
) == TYPE_DECL
5735 && TYPE_DECL_ALIAS_P (decl
))
5736 /* alias-declaration */
5737 gcc_assert (!DECL_ARTIFICIAL (decl
));
5738 else if (VAR_P (decl
))
5739 /* C++14 variable template. */;
5740 else if (TREE_CODE (decl
) == CONCEPT_DECL
)
5741 /* C++2a concept definitions. */;
5744 error ("template declaration of %q#D", decl
);
5745 return error_mark_node
;
5749 /* Check to see that the rules regarding the use of default
5750 arguments are not being violated. We check args for a friend
5751 functions when we know whether it's a definition, introducing
5752 declaration or re-declaration. */
5753 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5754 check_default_tmpl_args (decl
, current_template_parms
,
5755 is_primary
, is_partial
, is_friend
);
5757 /* Ensure that there are no parameter packs in the type of this
5758 declaration that have not been expanded. */
5759 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5761 /* Check each of the arguments individually to see if there are
5762 any bare parameter packs. */
5763 tree type
= TREE_TYPE (decl
);
5764 tree arg
= DECL_ARGUMENTS (decl
);
5765 tree argtype
= TYPE_ARG_TYPES (type
);
5767 while (arg
&& argtype
)
5769 if (!DECL_PACK_P (arg
)
5770 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5772 /* This is a PARM_DECL that contains unexpanded parameter
5773 packs. We have already complained about this in the
5774 check_for_bare_parameter_packs call, so just replace
5775 these types with ERROR_MARK_NODE. */
5776 TREE_TYPE (arg
) = error_mark_node
;
5777 TREE_VALUE (argtype
) = error_mark_node
;
5780 arg
= DECL_CHAIN (arg
);
5781 argtype
= TREE_CHAIN (argtype
);
5784 /* Check for bare parameter packs in the return type and the
5785 exception specifiers. */
5786 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5787 /* Errors were already issued, set return type to int
5788 as the frontend doesn't expect error_mark_node as
5790 TREE_TYPE (type
) = integer_type_node
;
5791 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5792 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5794 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5795 && TYPE_DECL_ALIAS_P (decl
))
5796 ? DECL_ORIGINAL_TYPE (decl
)
5797 : TREE_TYPE (decl
)))
5799 TREE_TYPE (decl
) = error_mark_node
;
5800 return error_mark_node
;
5804 return process_partial_specialization (decl
);
5806 args
= current_template_args ();
5809 || TREE_CODE (ctx
) == FUNCTION_DECL
5810 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5811 || (TREE_CODE (decl
) == TYPE_DECL
5812 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5813 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5815 if (DECL_LANG_SPECIFIC (decl
)
5816 && DECL_TEMPLATE_INFO (decl
)
5817 && DECL_TI_TEMPLATE (decl
))
5818 tmpl
= DECL_TI_TEMPLATE (decl
);
5819 /* If DECL is a TYPE_DECL for a class-template, then there won't
5820 be DECL_LANG_SPECIFIC. The information equivalent to
5821 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5822 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5823 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5824 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5826 /* Since a template declaration already existed for this
5827 class-type, we must be redeclaring it here. Make sure
5828 that the redeclaration is valid. */
5829 redeclare_class_template (TREE_TYPE (decl
),
5830 current_template_parms
,
5831 current_template_constraints ());
5832 /* We don't need to create a new TEMPLATE_DECL; just use the
5833 one we already had. */
5834 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5838 tmpl
= build_template_decl (decl
, current_template_parms
,
5842 if (DECL_LANG_SPECIFIC (decl
)
5843 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5845 /* A specialization of a member template of a template
5847 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5848 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5849 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5855 tree a
, t
, current
, parms
;
5857 tree tinfo
= get_template_info (decl
);
5861 error ("template definition of non-template %q#D", decl
);
5862 return error_mark_node
;
5865 tmpl
= TI_TEMPLATE (tinfo
);
5867 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5868 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5869 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5870 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5874 /* The declaration is a specialization of a member
5875 template, declared outside the class. Therefore, the
5876 innermost template arguments will be NULL, so we
5877 replace them with the arguments determined by the
5878 earlier call to check_explicit_specialization. */
5879 args
= DECL_TI_ARGS (decl
);
5882 = build_template_decl (decl
, current_template_parms
,
5884 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5885 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5886 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5887 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5888 DECL_TEMPLATE_INFO (new_tmpl
)
5889 = build_template_info (tmpl
, args
);
5891 register_specialization (new_tmpl
,
5892 most_general_template (tmpl
),
5898 /* Make sure the template headers we got make sense. */
5900 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5901 i
= TMPL_PARMS_DEPTH (parms
);
5902 if (TMPL_ARGS_DEPTH (args
) != i
)
5904 error ("expected %d levels of template parms for %q#D, got %d",
5905 i
, decl
, TMPL_ARGS_DEPTH (args
));
5906 DECL_INTERFACE_KNOWN (decl
) = 1;
5907 return error_mark_node
;
5910 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5912 a
= TMPL_ARGS_LEVEL (args
, i
);
5913 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5915 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5917 if (current
== decl
)
5918 error ("got %d template parameters for %q#D",
5919 TREE_VEC_LENGTH (a
), decl
);
5921 error ("got %d template parameters for %q#T",
5922 TREE_VEC_LENGTH (a
), current
);
5923 error (" but %d required", TREE_VEC_LENGTH (t
));
5924 /* Avoid crash in import_export_decl. */
5925 DECL_INTERFACE_KNOWN (decl
) = 1;
5926 return error_mark_node
;
5929 if (current
== decl
)
5931 else if (current
== NULL_TREE
)
5932 /* Can happen in erroneous input. */
5935 current
= get_containing_scope (current
);
5938 /* Check that the parms are used in the appropriate qualifying scopes
5939 in the declarator. */
5940 if (!comp_template_args
5942 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5944 error ("template arguments to %qD do not match original "
5945 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5946 if (!uses_template_parms (TI_ARGS (tinfo
)))
5947 inform (input_location
, "use %<template<>%> for"
5948 " an explicit specialization");
5949 /* Avoid crash in import_export_decl. */
5950 DECL_INTERFACE_KNOWN (decl
) = 1;
5951 return error_mark_node
;
5955 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5956 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5958 /* Push template declarations for global functions and types. Note
5959 that we do not try to push a global template friend declared in a
5960 template class; such a thing may well depend on the template
5961 parameters of the class. */
5962 if (new_template_p
&& !ctx
5963 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5965 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5966 if (tmpl
== error_mark_node
)
5967 return error_mark_node
;
5969 /* Hide template friend classes that haven't been declared yet. */
5970 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5972 DECL_ANTICIPATED (tmpl
) = 1;
5973 DECL_FRIEND_P (tmpl
) = 1;
5979 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5981 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5983 /* Give template template parms a DECL_CONTEXT of the template
5984 for which they are a parameter. */
5985 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5986 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5988 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5989 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5990 DECL_CONTEXT (parm
) = tmpl
;
5993 if (TREE_CODE (decl
) == TYPE_DECL
5994 && TYPE_DECL_ALIAS_P (decl
))
5997 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl
)))
5999 /* ??? Why don't we do this here for all templates? */
6000 constr
= build_constraints (constr
, NULL_TREE
);
6001 set_constraints (decl
, constr
);
6003 if (complex_alias_template_p (tmpl
))
6004 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
6008 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6009 back to its most general template. If TMPL is a specialization,
6010 ARGS may only have the innermost set of arguments. Add the missing
6011 argument levels if necessary. */
6012 if (DECL_TEMPLATE_INFO (tmpl
))
6013 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
6015 info
= build_template_info (tmpl
, args
);
6017 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
6018 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
6022 retrofit_lang_decl (decl
);
6023 if (DECL_LANG_SPECIFIC (decl
))
6024 DECL_TEMPLATE_INFO (decl
) = info
;
6027 if (flag_implicit_templates
6029 && TREE_PUBLIC (decl
)
6030 && VAR_OR_FUNCTION_DECL_P (decl
))
6031 /* Set DECL_COMDAT on template instantiations; if we force
6032 them to be emitted by explicit instantiation,
6033 mark_needed will tell cgraph to do the right thing. */
6034 DECL_COMDAT (decl
) = true;
6036 return DECL_TEMPLATE_RESULT (tmpl
);
6040 push_template_decl (tree decl
)
6042 return push_template_decl_real (decl
, false);
6045 /* FN is an inheriting constructor that inherits from the constructor
6046 template INHERITED; turn FN into a constructor template with a matching
6050 add_inherited_template_parms (tree fn
, tree inherited
)
6053 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
6054 inner_parms
= copy_node (inner_parms
);
6056 = tree_cons (size_int (processing_template_decl
+ 1),
6057 inner_parms
, current_template_parms
);
6058 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
6059 tree args
= template_parms_to_args (parms
);
6060 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
6061 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
6062 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
6063 DECL_ARTIFICIAL (tmpl
) = true;
6064 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
6068 /* Called when a class template TYPE is redeclared with the indicated
6069 template PARMS, e.g.:
6071 template <class T> struct S;
6072 template <class T> struct S {}; */
6075 redeclare_class_template (tree type
, tree parms
, tree cons
)
6081 if (!TYPE_TEMPLATE_INFO (type
))
6083 error ("%qT is not a template type", type
);
6087 tmpl
= TYPE_TI_TEMPLATE (type
);
6088 if (!PRIMARY_TEMPLATE_P (tmpl
))
6089 /* The type is nested in some template class. Nothing to worry
6090 about here; there are no new template parameters for the nested
6096 error ("template specifiers not specified in declaration of %qD",
6101 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6102 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
6104 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
6106 error_n (input_location
, TREE_VEC_LENGTH (parms
),
6107 "redeclared with %d template parameter",
6108 "redeclared with %d template parameters",
6109 TREE_VEC_LENGTH (parms
));
6110 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
6111 "previous declaration %qD used %d template parameter",
6112 "previous declaration %qD used %d template parameters",
6113 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
6117 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
6124 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
6125 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
6128 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
6129 if (error_operand_p (tmpl_parm
))
6132 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6133 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
6134 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
6136 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6138 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
6139 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
6140 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
6141 || (TREE_CODE (tmpl_parm
) != PARM_DECL
6142 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
6143 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
6144 || (TREE_CODE (tmpl_parm
) == PARM_DECL
6145 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
6146 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
6148 error ("template parameter %q+#D", tmpl_parm
);
6149 error ("redeclared here as %q#D", parm
);
6153 /* The parameters can be declared to introduce different
6155 tree p1
= TREE_VEC_ELT (tmpl_parms
, i
);
6156 tree p2
= TREE_VEC_ELT (parms
, i
);
6157 if (!template_parameter_constraints_equivalent_p (p1
, p2
))
6159 error ("declaration of template parameter %q+#D with different "
6160 "constraints", parm
);
6161 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6162 "original declaration appeared here");
6166 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
6168 /* We have in [temp.param]:
6170 A template-parameter may not be given default arguments
6171 by two different declarations in the same scope. */
6172 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
6173 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
6174 "original definition appeared here");
6178 if (parm_default
!= NULL_TREE
)
6179 /* Update the previous template parameters (which are the ones
6180 that will really count) with the new default value. */
6181 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
6182 else if (tmpl_default
!= NULL_TREE
)
6183 /* Update the new parameters, too; they'll be used as the
6184 parameters for any members. */
6185 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
6187 /* Give each template template parm in this redeclaration a
6188 DECL_CONTEXT of the template for which they are a parameter. */
6189 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
6191 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
6192 DECL_CONTEXT (parm
) = tmpl
;
6195 if (TREE_CODE (parm
) == TYPE_DECL
)
6196 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
6199 tree ci
= get_constraints (tmpl
);
6200 tree req1
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
6201 tree req2
= cons
? CI_TEMPLATE_REQS (cons
) : NULL_TREE
;
6203 /* Two classes with different constraints declare different entities. */
6204 if (!cp_tree_equal (req1
, req2
))
6206 error_at (input_location
, "redeclaration %q#D with different "
6207 "constraints", tmpl
);
6208 inform (DECL_SOURCE_LOCATION (tmpl
),
6209 "original declaration appeared here");
6216 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6217 to be used when the caller has already checked
6218 (processing_template_decl
6219 && !instantiation_dependent_expression_p (expr)
6220 && potential_constant_expression (expr))
6221 and cleared processing_template_decl. */
6224 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6226 return tsubst_copy_and_build (expr
,
6229 /*in_decl=*/NULL_TREE
,
6230 /*function_p=*/false,
6231 /*integral_constant_expression_p=*/true);
6234 /* Simplify EXPR if it is a non-dependent expression. Returns the
6235 (possibly simplified) expression. */
6238 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6240 if (expr
== NULL_TREE
)
6243 /* If we're in a template, but EXPR isn't value dependent, simplify
6244 it. We're supposed to treat:
6246 template <typename T> void f(T[1 + 1]);
6247 template <typename T> void f(T[2]);
6249 as two declarations of the same function, for example. */
6250 if (processing_template_decl
6251 && is_nondependent_constant_expression (expr
))
6253 processing_template_decl_sentinel s
;
6254 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6260 instantiate_non_dependent_expr (tree expr
)
6262 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6265 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6266 an uninstantiated expression. */
6269 instantiate_non_dependent_or_null (tree expr
)
6271 if (expr
== NULL_TREE
)
6273 if (processing_template_decl
)
6275 if (!is_nondependent_constant_expression (expr
))
6279 processing_template_decl_sentinel s
;
6280 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6286 /* True iff T is a specialization of a variable template. */
6289 variable_template_specialization_p (tree t
)
6291 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6293 tree tmpl
= DECL_TI_TEMPLATE (t
);
6294 return variable_template_p (tmpl
);
6297 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6298 template declaration, or a TYPE_DECL for an alias declaration. */
6301 alias_type_or_template_p (tree t
)
6305 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6308 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6309 || DECL_ALIAS_TEMPLATE_P (t
));
6312 /* If T is a specialization of an alias template, return it; otherwise return
6313 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6316 alias_template_specialization_p (const_tree t
,
6317 bool transparent_typedefs
)
6322 /* It's an alias template specialization if it's an alias and its
6323 TYPE_NAME is a specialization of a primary template. */
6324 if (typedef_variant_p (t
))
6326 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6327 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
6328 return CONST_CAST_TREE (t
);
6329 if (transparent_typedefs
)
6330 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6332 transparent_typedefs
);
6338 /* An alias template is complex from a SFINAE perspective if a template-id
6339 using that alias can be ill-formed when the expansion is not, as with
6340 the void_t template. We determine this by checking whether the
6341 expansion for the alias template uses all its template parameters. */
6343 struct uses_all_template_parms_data
6350 uses_all_template_parms_r (tree t
, void *data_
)
6352 struct uses_all_template_parms_data
&data
6353 = *(struct uses_all_template_parms_data
*)data_
;
6354 tree idx
= get_template_parm_index (t
);
6356 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6357 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6362 complex_alias_template_p (const_tree tmpl
)
6364 /* A renaming alias isn't complex. */
6365 if (get_underlying_template (CONST_CAST_TREE (tmpl
)) != tmpl
)
6368 /* Any other constrained alias is complex. */
6369 if (get_constraints (tmpl
))
6372 struct uses_all_template_parms_data data
;
6373 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6374 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6375 data
.level
= TMPL_PARMS_DEPTH (parms
);
6376 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6377 data
.seen
= XALLOCAVEC (bool, len
);
6378 for (int i
= 0; i
< len
; ++i
)
6379 data
.seen
[i
] = false;
6381 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6382 for (int i
= 0; i
< len
; ++i
)
6388 /* If T is a specialization of a complex alias template with dependent
6389 template-arguments, return it; otherwise return NULL_TREE. If T is a
6390 typedef to such a specialization, return the specialization. */
6393 dependent_alias_template_spec_p (const_tree t
, bool transparent_typedefs
)
6395 if (!TYPE_P (t
) || !typedef_variant_p (t
))
6398 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6400 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
))
6401 && (any_dependent_template_arguments_p
6402 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)))))
6403 return CONST_CAST_TREE (t
);
6405 if (transparent_typedefs
)
6407 tree utype
= DECL_ORIGINAL_TYPE (TYPE_NAME (t
));
6408 return dependent_alias_template_spec_p (utype
, transparent_typedefs
);
6414 /* Return the number of innermost template parameters in TMPL. */
6417 num_innermost_template_parms (const_tree tmpl
)
6419 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6420 return TREE_VEC_LENGTH (parms
);
6423 /* Return either TMPL or another template that it is equivalent to under DR
6424 1286: An alias that just changes the name of a template is equivalent to
6425 the other template. */
6428 get_underlying_template (tree tmpl
)
6430 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6431 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6433 /* Determine if the alias is equivalent to an underlying template. */
6434 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6435 /* The underlying type may have been ill-formed. Don't proceed. */
6438 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6442 tree underlying
= TI_TEMPLATE (tinfo
);
6443 if (!PRIMARY_TEMPLATE_P (underlying
)
6444 || (num_innermost_template_parms (tmpl
)
6445 != num_innermost_template_parms (underlying
)))
6448 tree alias_args
= INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl
));
6449 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6452 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6453 it's appropriate to treat a less-constrained alias as equivalent. */
6454 if (!at_least_as_constrained (underlying
, tmpl
))
6457 /* Alias is equivalent. Strip it and repeat. */
6464 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6465 must be a reference-to-function or a pointer-to-function type, as specified
6466 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6467 and check that the resulting function has external linkage. */
6470 convert_nontype_argument_function (tree type
, tree expr
,
6471 tsubst_flags_t complain
)
6475 linkage_kind linkage
;
6477 fn
= instantiate_type (type
, fns
, tf_none
);
6478 if (fn
== error_mark_node
)
6479 return error_mark_node
;
6481 if (value_dependent_expression_p (fn
))
6484 fn_no_ptr
= strip_fnptr_conv (fn
);
6485 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6486 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6487 if (BASELINK_P (fn_no_ptr
))
6488 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6490 /* [temp.arg.nontype]/1
6492 A template-argument for a non-type, non-template template-parameter
6495 -- the address of an object or function with external [C++11: or
6496 internal] linkage. */
6498 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6499 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6501 if (complain
& tf_error
)
6503 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6504 error_at (loc
, "%qE is not a valid template argument for type %qT",
6506 if (TYPE_PTR_P (type
))
6507 inform (loc
, "it must be the address of a function "
6508 "with external linkage");
6510 inform (loc
, "it must be the name of a function with "
6511 "external linkage");
6516 linkage
= decl_linkage (fn_no_ptr
);
6517 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6519 if (complain
& tf_error
)
6521 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6522 if (cxx_dialect
>= cxx11
)
6523 error_at (loc
, "%qE is not a valid template argument for type "
6524 "%qT because %qD has no linkage",
6525 expr
, type
, fn_no_ptr
);
6527 error_at (loc
, "%qE is not a valid template argument for type "
6528 "%qT because %qD does not have external linkage",
6529 expr
, type
, fn_no_ptr
);
6535 if (TYPE_REF_P (type
))
6537 if (REFERENCE_REF_P (fn
))
6538 fn
= TREE_OPERAND (fn
, 0);
6540 fn
= build_address (fn
);
6542 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6543 fn
= build_nop (type
, fn
);
6548 /* Subroutine of convert_nontype_argument.
6549 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6550 Emit an error otherwise. */
6553 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6554 tsubst_flags_t complain
)
6556 tree orig_expr
= expr
;
6558 if (null_ptr_cst_p (expr
))
6560 if (TREE_CODE (expr
) == PTRMEM_CST
6561 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6562 PTRMEM_CST_CLASS (expr
)))
6564 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6566 if (processing_template_decl
6567 && TREE_CODE (expr
) == ADDR_EXPR
6568 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6570 if (complain
& tf_error
)
6572 location_t loc
= cp_expr_loc_or_input_loc (orig_expr
);
6573 error_at (loc
, "%qE is not a valid template argument for type %qT",
6575 if (TREE_CODE (expr
) != PTRMEM_CST
)
6576 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6578 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6583 /* Returns TRUE iff the address of OP is value-dependent.
6585 14.6.2.4 [temp.dep.temp]:
6586 A non-integral non-type template-argument is dependent if its type is
6587 dependent or it has either of the following forms
6590 and contains a nested-name-specifier which specifies a class-name that
6591 names a dependent type.
6593 We generalize this to just say that the address of a member of a
6594 dependent class is value-dependent; the above doesn't cover the
6595 address of a static data member named with an unqualified-id. */
6598 has_value_dependent_address (tree op
)
6600 STRIP_ANY_LOCATION_WRAPPER (op
);
6602 /* We could use get_inner_reference here, but there's no need;
6603 this is only relevant for template non-type arguments, which
6604 can only be expressed as &id-expression. */
6607 tree ctx
= CP_DECL_CONTEXT (op
);
6608 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6615 /* The next set of functions are used for providing helpful explanatory
6616 diagnostics for failed overload resolution. Their messages should be
6617 indented by two spaces for consistency with the messages in
6621 unify_success (bool /*explain_p*/)
6626 /* Other failure functions should call this one, to provide a single function
6627 for setting a breakpoint on. */
6630 unify_invalid (bool /*explain_p*/)
6636 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6639 inform (input_location
,
6640 " couldn%'t deduce template parameter %qD", parm
);
6641 return unify_invalid (explain_p
);
6645 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6648 inform (input_location
,
6649 " types %qT and %qT have incompatible cv-qualifiers",
6651 return unify_invalid (explain_p
);
6655 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6658 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6659 return unify_invalid (explain_p
);
6663 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6666 inform (input_location
,
6667 " template parameter %qD is not a parameter pack, but "
6670 return unify_invalid (explain_p
);
6674 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6677 inform (input_location
,
6678 " template argument %qE does not match "
6679 "pointer-to-member constant %qE",
6681 return unify_invalid (explain_p
);
6685 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6688 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6689 return unify_invalid (explain_p
);
6693 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6696 inform (input_location
,
6697 " inconsistent parameter pack deduction with %qT and %qT",
6699 return unify_invalid (explain_p
);
6703 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6708 inform (input_location
,
6709 " deduced conflicting types for parameter %qT (%qT and %qT)",
6710 parm
, first
, second
);
6712 inform (input_location
,
6713 " deduced conflicting values for non-type parameter "
6714 "%qE (%qE and %qE)", parm
, first
, second
);
6716 return unify_invalid (explain_p
);
6720 unify_vla_arg (bool explain_p
, tree arg
)
6723 inform (input_location
,
6724 " variable-sized array type %qT is not "
6725 "a valid template argument",
6727 return unify_invalid (explain_p
);
6731 unify_method_type_error (bool explain_p
, tree arg
)
6734 inform (input_location
,
6735 " member function type %qT is not a valid template argument",
6737 return unify_invalid (explain_p
);
6741 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6746 inform_n (input_location
, wanted
,
6747 " candidate expects at least %d argument, %d provided",
6748 " candidate expects at least %d arguments, %d provided",
6751 inform_n (input_location
, wanted
,
6752 " candidate expects %d argument, %d provided",
6753 " candidate expects %d arguments, %d provided",
6756 return unify_invalid (explain_p
);
6760 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6762 return unify_arity (explain_p
, have
, wanted
);
6766 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6767 bool least_p
= false)
6769 return unify_arity (explain_p
, have
, wanted
, least_p
);
6773 unify_arg_conversion (bool explain_p
, tree to_type
,
6774 tree from_type
, tree arg
)
6777 inform (cp_expr_loc_or_input_loc (arg
),
6778 " cannot convert %qE (type %qT) to type %qT",
6779 arg
, from_type
, to_type
);
6780 return unify_invalid (explain_p
);
6784 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6785 tree parm
, tree arg
)
6790 case tbr_ambiguous_baseclass
:
6791 inform (input_location
, " %qT is an ambiguous base class of %qT",
6795 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6798 return unify_invalid (explain_p
);
6802 unify_inconsistent_template_template_parameters (bool explain_p
)
6805 inform (input_location
,
6806 " template parameters of a template template argument are "
6807 "inconsistent with other deduced template arguments");
6808 return unify_invalid (explain_p
);
6812 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6815 inform (input_location
,
6816 " cannot deduce a template for %qT from non-template type %qT",
6818 return unify_invalid (explain_p
);
6822 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6825 inform (input_location
,
6826 " template argument %qE does not match %qE", arg
, parm
);
6827 return unify_invalid (explain_p
);
6830 /* True if T is a C++20 template parameter object to store the argument for a
6831 template parameter of class type. */
6834 template_parm_object_p (const_tree t
)
6836 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6837 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6840 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6841 argument for TYPE, points to an unsuitable object. */
6844 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6846 switch (TREE_CODE (expr
))
6849 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6853 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6858 unsigned i
; tree elt
;
6859 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6860 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6867 tree decl
= TREE_OPERAND (expr
, 0);
6871 if (complain
& tf_error
)
6872 error_at (cp_expr_loc_or_input_loc (expr
),
6873 "%qE is not a valid template argument of type %qT "
6874 "because %qE is not a variable", expr
, type
, decl
);
6877 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6879 if (complain
& tf_error
)
6880 error_at (cp_expr_loc_or_input_loc (expr
),
6881 "%qE is not a valid template argument of type %qT "
6882 "in C++98 because %qD does not have external linkage",
6886 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6887 && decl_linkage (decl
) == lk_none
)
6889 if (complain
& tf_error
)
6890 error_at (cp_expr_loc_or_input_loc (expr
),
6891 "%qE is not a valid template argument of type %qT "
6892 "because %qD has no linkage", expr
, type
, decl
);
6895 /* C++17: For a non-type template-parameter of reference or pointer
6896 type, the value of the constant expression shall not refer to (or
6897 for a pointer type, shall not be the address of):
6898 * a subobject (4.5),
6899 * a temporary object (15.2),
6900 * a string literal (5.13.5),
6901 * the result of a typeid expression (8.2.8), or
6902 * a predefined __func__ variable (11.4.1). */
6903 else if (DECL_ARTIFICIAL (decl
))
6905 if (complain
& tf_error
)
6906 error ("the address of %qD is not a valid template argument",
6910 else if (!same_type_ignoring_top_level_qualifiers_p
6911 (strip_array_types (TREE_TYPE (type
)),
6912 strip_array_types (TREE_TYPE (decl
))))
6914 if (complain
& tf_error
)
6915 error ("the address of the %qT subobject of %qD is not a "
6916 "valid template argument", TREE_TYPE (type
), decl
);
6919 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6921 if (complain
& tf_error
)
6922 error ("the address of %qD is not a valid template argument "
6923 "because it does not have static storage duration",
6931 if (!INDIRECT_TYPE_P (type
))
6932 /* We're only concerned about pointers and references here. */;
6933 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6934 /* Null pointer values are OK in C++11. */;
6939 if (complain
& tf_error
)
6940 error ("%qD is not a valid template argument "
6941 "because %qD is a variable, not the address of "
6942 "a variable", expr
, expr
);
6947 if (complain
& tf_error
)
6948 error ("%qE is not a valid template argument for %qT "
6949 "because it is not the address of a variable",
6959 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6960 template argument EXPR. */
6963 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6965 if (TREE_CODE (expr
) == TARGET_EXPR
)
6966 expr
= TARGET_EXPR_INITIAL (expr
);
6968 if (!TREE_CONSTANT (expr
))
6970 if ((complain
& tf_error
)
6971 && require_rvalue_constant_expression (expr
))
6972 cxx_constant_value (expr
);
6973 return error_mark_node
;
6975 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6976 return error_mark_node
;
6978 tree name
= mangle_template_parm_object (expr
);
6979 tree decl
= get_global_binding (name
);
6983 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6984 decl
= create_temporary_var (type
);
6985 TREE_STATIC (decl
) = true;
6986 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6987 TREE_READONLY (decl
) = true;
6988 DECL_NAME (decl
) = name
;
6989 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6990 DECL_CONTEXT (decl
) = global_namespace
;
6991 comdat_linkage (decl
);
6992 pushdecl_top_level_and_finish (decl
, expr
);
6996 /* Attempt to convert the non-type template parameter EXPR to the
6997 indicated TYPE. If the conversion is successful, return the
6998 converted value. If the conversion is unsuccessful, return
6999 NULL_TREE if we issued an error message, or error_mark_node if we
7000 did not. We issue error messages for out-and-out bad template
7001 parameters, but not simply because the conversion failed, since we
7002 might be just trying to do argument deduction. Both TYPE and EXPR
7003 must be non-dependent.
7005 The conversion follows the special rules described in
7006 [temp.arg.nontype], and it is much more strict than an implicit
7009 This function is called twice for each template argument (see
7010 lookup_template_class for a more accurate description of this
7011 problem). This means that we need to handle expressions which
7012 are not valid in a C++ source, but can be created from the
7013 first call (for instance, casts to perform conversions). These
7014 hacks can go away after we fix the double coercion problem. */
7017 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
7020 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7022 /* Detect immediately string literals as invalid non-type argument.
7023 This special-case is not needed for correctness (we would easily
7024 catch this later), but only to provide better diagnostic for this
7025 common user mistake. As suggested by DR 100, we do not mention
7026 linkage issues in the diagnostic as this is not the point. */
7027 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
7029 if (complain
& tf_error
)
7030 error ("%qE is not a valid template argument for type %qT "
7031 "because string literals can never be used in this context",
7036 /* Add the ADDR_EXPR now for the benefit of
7037 value_dependent_expression_p. */
7038 if (TYPE_PTROBV_P (type
)
7039 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
7041 expr
= decay_conversion (expr
, complain
);
7042 if (expr
== error_mark_node
)
7043 return error_mark_node
;
7046 /* If we are in a template, EXPR may be non-dependent, but still
7047 have a syntactic, rather than semantic, form. For example, EXPR
7048 might be a SCOPE_REF, rather than the VAR_DECL to which the
7049 SCOPE_REF refers. Preserving the qualifying scope is necessary
7050 so that access checking can be performed when the template is
7051 instantiated -- but here we need the resolved form so that we can
7052 convert the argument. */
7053 bool non_dep
= false;
7054 if (TYPE_REF_OBJ_P (type
)
7055 && has_value_dependent_address (expr
))
7056 /* If we want the address and it's value-dependent, don't fold. */;
7057 else if (processing_template_decl
7058 && is_nondependent_constant_expression (expr
))
7060 if (error_operand_p (expr
))
7061 return error_mark_node
;
7062 expr_type
= TREE_TYPE (expr
);
7064 /* If the argument is non-dependent, perform any conversions in
7065 non-dependent context as well. */
7066 processing_template_decl_sentinel
s (non_dep
);
7068 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
7070 if (value_dependent_expression_p (expr
))
7071 expr
= canonicalize_expr_argument (expr
, complain
);
7073 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7074 to a non-type argument of "nullptr". */
7075 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
7076 expr
= fold_simple (convert (type
, expr
));
7078 /* In C++11, integral or enumeration non-type template arguments can be
7079 arbitrary constant expressions. Pointer and pointer to
7080 member arguments can be general constant expressions that evaluate
7081 to a null value, but otherwise still need to be of a specific form. */
7082 if (cxx_dialect
>= cxx11
)
7084 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
7085 /* A PTRMEM_CST is already constant, and a valid template
7086 argument for a parameter of pointer to member type, we just want
7087 to leave it in that form rather than lower it to a
7089 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7090 || cxx_dialect
>= cxx17
)
7092 /* Calling build_converted_constant_expr might create a call to
7093 a conversion function with a value-dependent argument, which
7094 could invoke taking the address of a temporary representing
7095 the result of the conversion. */
7096 if (COMPOUND_LITERAL_P (expr
)
7097 && CONSTRUCTOR_IS_DEPENDENT (expr
)
7098 && MAYBE_CLASS_TYPE_P (expr_type
)
7099 && TYPE_HAS_CONVERSION (expr_type
))
7101 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
7102 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr
) = true;
7105 /* C++17: A template-argument for a non-type template-parameter shall
7106 be a converted constant expression (8.20) of the type of the
7107 template-parameter. */
7108 expr
= build_converted_constant_expr (type
, expr
, complain
);
7109 if (expr
== error_mark_node
)
7110 /* Make sure we return NULL_TREE only if we have really issued
7111 an error, as described above. */
7112 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
7113 expr
= maybe_constant_value (expr
, NULL_TREE
,
7114 /*manifestly_const_eval=*/true);
7115 expr
= convert_from_reference (expr
);
7117 else if (TYPE_PTR_OR_PTRMEM_P (type
))
7119 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
7120 /*manifestly_const_eval=*/true);
7121 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
7122 : null_member_pointer_value_p (folded
))
7127 if (TYPE_REF_P (type
))
7128 expr
= mark_lvalue_use (expr
);
7130 expr
= mark_rvalue_use (expr
);
7132 /* HACK: Due to double coercion, we can get a
7133 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7134 which is the tree that we built on the first call (see
7135 below when coercing to reference to object or to reference to
7136 function). We just strip everything and get to the arg.
7137 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7139 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
7141 tree probe_type
, probe
= expr
;
7142 if (REFERENCE_REF_P (probe
))
7143 probe
= TREE_OPERAND (probe
, 0);
7144 probe_type
= TREE_TYPE (probe
);
7145 if (TREE_CODE (probe
) == NOP_EXPR
)
7147 /* ??? Maybe we could use convert_from_reference here, but we
7148 would need to relax its constraints because the NOP_EXPR
7149 could actually change the type to something more cv-qualified,
7150 and this is not folded by convert_from_reference. */
7151 tree addr
= TREE_OPERAND (probe
, 0);
7152 if (TYPE_REF_P (probe_type
)
7153 && TREE_CODE (addr
) == ADDR_EXPR
7154 && TYPE_PTR_P (TREE_TYPE (addr
))
7155 && (same_type_ignoring_top_level_qualifiers_p
7156 (TREE_TYPE (probe_type
),
7157 TREE_TYPE (TREE_TYPE (addr
)))))
7159 expr
= TREE_OPERAND (addr
, 0);
7160 expr_type
= TREE_TYPE (probe_type
);
7165 /* [temp.arg.nontype]/5, bullet 1
7167 For a non-type template-parameter of integral or enumeration type,
7168 integral promotions (_conv.prom_) and integral conversions
7169 (_conv.integral_) are applied. */
7170 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7172 if (cxx_dialect
< cxx11
)
7174 tree t
= build_converted_constant_expr (type
, expr
, complain
);
7175 t
= maybe_constant_value (t
);
7176 if (t
!= error_mark_node
)
7180 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
7181 return error_mark_node
;
7183 /* Notice that there are constant expressions like '4 % 0' which
7184 do not fold into integer constants. */
7185 if (TREE_CODE (expr
) != INTEGER_CST
7186 && !value_dependent_expression_p (expr
))
7188 if (complain
& tf_error
)
7190 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
7191 if (!require_potential_constant_expression (expr
))
7192 expr
= error_mark_node
;
7194 expr
= cxx_constant_value (expr
);
7195 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
7196 inform (loc
, "in template argument for type %qT", type
);
7197 if (expr
== error_mark_node
)
7199 /* else cxx_constant_value complained but gave us
7200 a real constant, so go ahead. */
7201 if (TREE_CODE (expr
) != INTEGER_CST
)
7203 /* Some assemble time constant expressions like
7204 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7205 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7206 as we can emit them into .rodata initializers of
7207 variables, yet they can't fold into an INTEGER_CST at
7208 compile time. Refuse them here. */
7209 gcc_checking_assert (reduced_constant_expression_p (expr
));
7210 error_at (loc
, "template argument %qE for type %qT not "
7211 "a constant integer", expr
, type
);
7219 /* Avoid typedef problems. */
7220 if (TREE_TYPE (expr
) != type
)
7221 expr
= fold_convert (type
, expr
);
7223 /* [temp.arg.nontype]/5, bullet 2
7225 For a non-type template-parameter of type pointer to object,
7226 qualification conversions (_conv.qual_) and the array-to-pointer
7227 conversion (_conv.array_) are applied. */
7228 else if (TYPE_PTROBV_P (type
))
7230 tree decayed
= expr
;
7232 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7233 decay_conversion or an explicit cast. If it's a problematic cast,
7234 we'll complain about it below. */
7235 if (TREE_CODE (expr
) == NOP_EXPR
)
7239 if (TREE_CODE (probe
) == ADDR_EXPR
7240 && TYPE_PTR_P (TREE_TYPE (probe
)))
7243 expr_type
= TREE_TYPE (expr
);
7247 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7249 A template-argument for a non-type, non-template template-parameter
7250 shall be one of: [...]
7252 -- the name of a non-type template-parameter;
7253 -- the address of an object or function with external linkage, [...]
7254 expressed as "& id-expression" where the & is optional if the name
7255 refers to a function or array, or if the corresponding
7256 template-parameter is a reference.
7258 Here, we do not care about functions, as they are invalid anyway
7259 for a parameter of type pointer-to-object. */
7261 if (value_dependent_expression_p (expr
))
7262 /* Non-type template parameters are OK. */
7264 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7265 /* Null pointer values are OK in C++11. */;
7266 else if (TREE_CODE (expr
) != ADDR_EXPR
7267 && !INDIRECT_TYPE_P (expr_type
))
7268 /* Other values, like integer constants, might be valid
7269 non-type arguments of some other type. */
7270 return error_mark_node
;
7271 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7276 expr
= perform_qualification_conversions (type
, expr
);
7277 if (expr
== error_mark_node
)
7278 return error_mark_node
;
7280 /* [temp.arg.nontype]/5, bullet 3
7282 For a non-type template-parameter of type reference to object, no
7283 conversions apply. The type referred to by the reference may be more
7284 cv-qualified than the (otherwise identical) type of the
7285 template-argument. The template-parameter is bound directly to the
7286 template-argument, which must be an lvalue. */
7287 else if (TYPE_REF_OBJ_P (type
))
7289 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7291 return error_mark_node
;
7293 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7295 if (complain
& tf_error
)
7296 error ("%qE is not a valid template argument for type %qT "
7297 "because of conflicts in cv-qualification", expr
, type
);
7301 if (!lvalue_p (expr
))
7303 if (complain
& tf_error
)
7304 error ("%qE is not a valid template argument for type %qT "
7305 "because it is not an lvalue", expr
, type
);
7309 /* [temp.arg.nontype]/1
7311 A template-argument for a non-type, non-template template-parameter
7312 shall be one of: [...]
7314 -- the address of an object or function with external linkage. */
7315 if (INDIRECT_REF_P (expr
)
7316 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7318 expr
= TREE_OPERAND (expr
, 0);
7321 if (complain
& tf_error
)
7322 error ("%q#D is not a valid template argument for type %qT "
7323 "because a reference variable does not have a constant "
7324 "address", expr
, type
);
7329 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7330 && value_dependent_expression_p (expr
))
7331 /* OK, dependent reference. We don't want to ask whether a DECL is
7332 itself value-dependent, since what we want here is its address. */;
7335 expr
= build_address (expr
);
7337 if (invalid_tparm_referent_p (type
, expr
, complain
))
7341 if (!same_type_p (type
, TREE_TYPE (expr
)))
7342 expr
= build_nop (type
, expr
);
7344 /* [temp.arg.nontype]/5, bullet 4
7346 For a non-type template-parameter of type pointer to function, only
7347 the function-to-pointer conversion (_conv.func_) is applied. If the
7348 template-argument represents a set of overloaded functions (or a
7349 pointer to such), the matching function is selected from the set
7351 else if (TYPE_PTRFN_P (type
))
7353 /* If the argument is a template-id, we might not have enough
7354 context information to decay the pointer. */
7355 if (!type_unknown_p (expr_type
))
7357 expr
= decay_conversion (expr
, complain
);
7358 if (expr
== error_mark_node
)
7359 return error_mark_node
;
7362 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7363 /* Null pointer values are OK in C++11. */
7364 return perform_qualification_conversions (type
, expr
);
7366 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7367 if (!expr
|| expr
== error_mark_node
)
7370 /* [temp.arg.nontype]/5, bullet 5
7372 For a non-type template-parameter of type reference to function, no
7373 conversions apply. If the template-argument represents a set of
7374 overloaded functions, the matching function is selected from the set
7376 else if (TYPE_REFFN_P (type
))
7378 if (TREE_CODE (expr
) == ADDR_EXPR
)
7380 if (complain
& tf_error
)
7382 error ("%qE is not a valid template argument for type %qT "
7383 "because it is a pointer", expr
, type
);
7384 inform (input_location
, "try using %qE instead",
7385 TREE_OPERAND (expr
, 0));
7390 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7391 if (!expr
|| expr
== error_mark_node
)
7394 /* [temp.arg.nontype]/5, bullet 6
7396 For a non-type template-parameter of type pointer to member function,
7397 no conversions apply. If the template-argument represents a set of
7398 overloaded member functions, the matching member function is selected
7399 from the set (_over.over_). */
7400 else if (TYPE_PTRMEMFUNC_P (type
))
7402 expr
= instantiate_type (type
, expr
, tf_none
);
7403 if (expr
== error_mark_node
)
7404 return error_mark_node
;
7406 /* [temp.arg.nontype] bullet 1 says the pointer to member
7407 expression must be a pointer-to-member constant. */
7408 if (!value_dependent_expression_p (expr
)
7409 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7412 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7413 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7414 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7415 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7417 /* [temp.arg.nontype]/5, bullet 7
7419 For a non-type template-parameter of type pointer to data member,
7420 qualification conversions (_conv.qual_) are applied. */
7421 else if (TYPE_PTRDATAMEM_P (type
))
7423 /* [temp.arg.nontype] bullet 1 says the pointer to member
7424 expression must be a pointer-to-member constant. */
7425 if (!value_dependent_expression_p (expr
)
7426 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7429 expr
= perform_qualification_conversions (type
, expr
);
7430 if (expr
== error_mark_node
)
7433 else if (NULLPTR_TYPE_P (type
))
7435 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7437 if (complain
& tf_error
)
7438 error ("%qE is not a valid template argument for type %qT "
7439 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7444 else if (CLASS_TYPE_P (type
))
7446 /* Replace the argument with a reference to the corresponding template
7447 parameter object. */
7448 if (!value_dependent_expression_p (expr
))
7449 expr
= get_template_parm_object (expr
, complain
);
7450 if (expr
== error_mark_node
)
7453 /* A template non-type parameter must be one of the above. */
7457 /* Sanity check: did we actually convert the argument to the
7459 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7460 (type
, TREE_TYPE (expr
)));
7461 return convert_from_reference (expr
);
7464 /* Subroutine of coerce_template_template_parms, which returns 1 if
7465 PARM_PARM and ARG_PARM match using the rule for the template
7466 parameters of template template parameters. Both PARM and ARG are
7467 template parameters; the rest of the arguments are the same as for
7468 coerce_template_template_parms.
7471 coerce_template_template_parm (tree parm
,
7473 tsubst_flags_t complain
,
7477 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7478 || parm
== NULL_TREE
|| error_operand_p (parm
))
7481 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7484 switch (TREE_CODE (parm
))
7487 /* We encounter instantiations of templates like
7488 template <template <template <class> class> class TT>
7491 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7492 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7494 if (!coerce_template_template_parms
7495 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7501 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7502 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7503 /* Argument is a parameter pack but parameter is not. */
7508 /* The tsubst call is used to handle cases such as
7510 template <int> class C {};
7511 template <class T, template <T> class TT> class D {};
7514 i.e. the parameter list of TT depends on earlier parameters. */
7515 if (!uses_template_parms (TREE_TYPE (arg
)))
7517 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7518 if (!uses_template_parms (t
)
7519 && !same_type_p (t
, TREE_TYPE (arg
)))
7523 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7524 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7525 /* Argument is a parameter pack but parameter is not. */
7537 /* Coerce template argument list ARGLIST for use with template
7538 template-parameter TEMPL. */
7541 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7542 tsubst_flags_t complain
)
7544 /* Consider an example where a template template parameter declared as
7546 template <class T, class U = std::allocator<T> > class TT
7548 The template parameter level of T and U are one level larger than
7549 of TT. To proper process the default argument of U, say when an
7550 instantiation `TT<int>' is seen, we need to build the full
7551 arguments containing {int} as the innermost level. Outer levels,
7552 available when not appearing as default template argument, can be
7553 obtained from the arguments of the enclosing template.
7555 Suppose that TT is later substituted with std::vector. The above
7556 instantiation is `TT<int, std::allocator<T> >' with TT at
7557 level 1, and T at level 2, while the template arguments at level 1
7558 becomes {std::vector} and the inner level 2 is {int}. */
7560 tree outer
= DECL_CONTEXT (templ
);
7562 outer
= generic_targs_for (outer
);
7563 else if (current_template_parms
)
7565 /* This is an argument of the current template, so we haven't set
7566 DECL_CONTEXT yet. */
7567 tree relevant_template_parms
;
7569 /* Parameter levels that are greater than the level of the given
7570 template template parm are irrelevant. */
7571 relevant_template_parms
= current_template_parms
;
7572 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7573 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7574 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7576 outer
= template_parms_to_args (relevant_template_parms
);
7580 arglist
= add_to_template_args (outer
, arglist
);
7582 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7583 return coerce_template_parms (parmlist
, arglist
, templ
,
7585 /*require_all_args=*/true,
7586 /*use_default_args=*/true);
7589 /* A cache of template template parameters with match-all default
7591 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7593 /* T is a bound template template-parameter. Copy its arguments into default
7594 arguments of the template template-parameter's template parameters. */
7597 add_defaults_to_ttp (tree otmpl
)
7599 if (tree
*c
= hash_map_safe_get (defaulted_ttp_cache
, otmpl
))
7602 tree ntmpl
= copy_node (otmpl
);
7604 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7605 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7606 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7607 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7608 TYPE_NAME (ntype
) = ntmpl
;
7609 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7611 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7612 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7613 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7614 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7616 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7617 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7618 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7619 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7620 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7622 tree o
= TREE_VEC_ELT (vec
, i
);
7623 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7625 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7626 TREE_PURPOSE (n
) = any_targ_node
;
7630 hash_map_safe_put
<hm_ggc
> (defaulted_ttp_cache
, otmpl
, ntmpl
);
7634 /* ARG is a bound potential template template-argument, and PARGS is a list
7635 of arguments for the corresponding template template-parameter. Adjust
7636 PARGS as appropriate for application to ARG's template, and if ARG is a
7637 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7638 arguments to the template template parameter. */
7641 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7643 ++processing_template_decl
;
7644 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7645 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7647 /* When comparing two template template-parameters in partial ordering,
7648 rewrite the one currently being used as an argument to have default
7649 arguments for all parameters. */
7650 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7651 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7652 if (pargs
!= error_mark_node
)
7653 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7654 TYPE_TI_ARGS (arg
));
7659 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7660 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7661 /*require_all*/true,
7662 /*use_default*/true);
7664 --processing_template_decl
;
7668 /* Subroutine of unify for the case when PARM is a
7669 BOUND_TEMPLATE_TEMPLATE_PARM. */
7672 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7675 tree parmvec
= TYPE_TI_ARGS (parm
);
7676 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7678 /* The template template parm might be variadic and the argument
7679 not, so flatten both argument lists. */
7680 parmvec
= expand_template_argument_pack (parmvec
);
7681 argvec
= expand_template_argument_pack (argvec
);
7685 /* In keeping with P0522R0, adjust P's template arguments
7686 to apply to A's template; then flatten it again. */
7687 tree nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7688 nparmvec
= expand_template_argument_pack (nparmvec
);
7690 if (unify (tparms
, targs
, nparmvec
, argvec
,
7691 UNIFY_ALLOW_NONE
, explain_p
))
7694 /* If the P0522 adjustment eliminated a pack expansion, deduce
7697 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7698 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7699 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7704 /* Deduce arguments T, i from TT<T> or TT<i>.
7705 We check each element of PARMVEC and ARGVEC individually
7706 rather than the whole TREE_VEC since they can have
7707 different number of elements, which is allowed under N2555. */
7709 int len
= TREE_VEC_LENGTH (parmvec
);
7711 /* Check if the parameters end in a pack, making them
7713 int parm_variadic_p
= 0;
7715 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7716 parm_variadic_p
= 1;
7718 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7719 /* If the template argument list of P contains a pack
7720 expansion that is not the last template argument, the
7721 entire template argument list is a non-deduced
7723 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7724 return unify_success (explain_p
);
7726 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7727 return unify_too_few_arguments (explain_p
,
7728 TREE_VEC_LENGTH (argvec
), len
);
7730 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7731 if (unify (tparms
, targs
,
7732 TREE_VEC_ELT (parmvec
, i
),
7733 TREE_VEC_ELT (argvec
, i
),
7734 UNIFY_ALLOW_NONE
, explain_p
))
7738 && unify_pack_expansion (tparms
, targs
,
7741 /*subr=*/true, explain_p
))
7748 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7749 template template parameters. Both PARM_PARMS and ARG_PARMS are
7750 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7753 Consider the example:
7754 template <class T> class A;
7755 template<template <class U> class TT> class B;
7757 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7758 the parameters to A, and OUTER_ARGS contains A. */
7761 coerce_template_template_parms (tree parm_parms
,
7763 tsubst_flags_t complain
,
7767 int nparms
, nargs
, i
;
7771 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7772 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7774 nparms
= TREE_VEC_LENGTH (parm_parms
);
7775 nargs
= TREE_VEC_LENGTH (arg_parms
);
7779 /* P0522R0: A template template-parameter P is at least as specialized as
7780 a template template-argument A if, given the following rewrite to two
7781 function templates, the function template corresponding to P is at
7782 least as specialized as the function template corresponding to A
7783 according to the partial ordering rules for function templates
7784 ([temp.func.order]). Given an invented class template X with the
7785 template parameter list of A (including default arguments):
7787 * Each of the two function templates has the same template parameters,
7788 respectively, as P or A.
7790 * Each function template has a single function parameter whose type is
7791 a specialization of X with template arguments corresponding to the
7792 template parameters from the respective function template where, for
7793 each template parameter PP in the template parameter list of the
7794 function template, a corresponding template argument AA is formed. If
7795 PP declares a parameter pack, then AA is the pack expansion
7796 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7798 If the rewrite produces an invalid type, then P is not at least as
7799 specialized as A. */
7801 /* So coerce P's args to apply to A's parms, and then deduce between A's
7802 args and the converted args. If that succeeds, A is at least as
7803 specialized as P, so they match.*/
7804 tree pargs
= template_parms_level_to_args (parm_parms
);
7805 pargs
= add_outermost_template_args (outer_args
, pargs
);
7806 ++processing_template_decl
;
7807 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7808 /*require_all*/true, /*use_default*/true);
7809 --processing_template_decl
;
7810 if (pargs
!= error_mark_node
)
7812 tree targs
= make_tree_vec (nargs
);
7813 tree aargs
= template_parms_level_to_args (arg_parms
);
7814 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7820 /* Determine whether we have a parameter pack at the end of the
7821 template template parameter's template parameter list. */
7822 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7824 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7826 if (error_operand_p (parm
))
7829 switch (TREE_CODE (parm
))
7833 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7838 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7848 && !(variadic_p
&& nargs
>= nparms
- 1))
7851 /* Check all of the template parameters except the parameter pack at
7852 the end (if any). */
7853 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7855 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7856 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7859 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7860 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7862 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7870 /* Check each of the template parameters in the template
7871 argument against the template parameter pack at the end of
7872 the template template parameter. */
7873 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7876 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7878 for (; i
< nargs
; ++i
)
7880 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7883 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7885 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7894 /* Verifies that the deduced template arguments (in TARGS) for the
7895 template template parameters (in TPARMS) represent valid bindings,
7896 by comparing the template parameter list of each template argument
7897 to the template parameter list of its corresponding template
7898 template parameter, in accordance with DR150. This
7899 routine can only be called after all template arguments have been
7900 deduced. It will return TRUE if all of the template template
7901 parameter bindings are okay, FALSE otherwise. */
7903 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7905 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7908 /* We're dealing with template parms in this process. */
7909 ++processing_template_decl
;
7911 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7913 for (i
= 0; i
< ntparms
; ++i
)
7915 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7916 tree targ
= TREE_VEC_ELT (targs
, i
);
7918 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7920 tree packed_args
= NULL_TREE
;
7923 if (ARGUMENT_PACK_P (targ
))
7925 /* Look inside the argument pack. */
7926 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7927 len
= TREE_VEC_LENGTH (packed_args
);
7930 for (idx
= 0; idx
< len
; ++idx
)
7932 tree targ_parms
= NULL_TREE
;
7935 /* Extract the next argument from the argument
7937 targ
= TREE_VEC_ELT (packed_args
, idx
);
7939 if (PACK_EXPANSION_P (targ
))
7940 /* Look at the pattern of the pack expansion. */
7941 targ
= PACK_EXPANSION_PATTERN (targ
);
7943 /* Extract the template parameters from the template
7945 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7946 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7947 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7948 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7950 /* Verify that we can coerce the template template
7951 parameters from the template argument to the template
7952 parameter. This requires an exact match. */
7954 && !coerce_template_template_parms
7955 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7970 --processing_template_decl
;
7974 /* Since type attributes aren't mangled, we need to strip them from
7975 template type arguments. */
7978 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7980 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7982 bool removed_attributes
= false;
7983 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7984 if (removed_attributes
7985 && (complain
& tf_warning
))
7986 warning (OPT_Wignored_attributes
,
7987 "ignoring attributes on template argument %qT", arg
);
7991 /* And from inside dependent non-type arguments like sizeof(Type). */
7994 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7996 if (!arg
|| arg
== error_mark_node
)
7998 bool removed_attributes
= false;
7999 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
8000 if (removed_attributes
8001 && (complain
& tf_warning
))
8002 warning (OPT_Wignored_attributes
,
8003 "ignoring attributes in template argument %qE", arg
);
8007 // A template declaration can be substituted for a constrained
8008 // template template parameter only when the argument is more
8009 // constrained than the parameter.
8011 is_compatible_template_arg (tree parm
, tree arg
)
8013 tree parm_cons
= get_constraints (parm
);
8015 /* For now, allow constrained template template arguments
8016 and unconstrained template template parameters. */
8017 if (parm_cons
== NULL_TREE
)
8020 /* If the template parameter is constrained, we need to rewrite its
8021 constraints in terms of the ARG's template parameters. This ensures
8022 that all of the template parameter types will have the same depth.
8024 Note that this is only valid when coerce_template_template_parm is
8025 true for the innermost template parameters of PARM and ARG. In other
8026 words, because coercion is successful, this conversion will be valid. */
8027 tree new_args
= NULL_TREE
;
8030 tree aparms
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8031 new_args
= template_parms_level_to_args (aparms
);
8032 parm_cons
= tsubst_constraint_info (parm_cons
, new_args
,
8033 tf_none
, NULL_TREE
);
8034 if (parm_cons
== error_mark_node
)
8038 return weakly_subsumes (parm_cons
, new_args
, arg
);
8041 // Convert a placeholder argument into a binding to the original
8042 // parameter. The original parameter is saved as the TREE_TYPE of
8045 convert_wildcard_argument (tree parm
, tree arg
)
8047 TREE_TYPE (arg
) = parm
;
8051 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8052 because one of them is dependent. But we need to represent the
8053 conversion for the benefit of cp_tree_equal. */
8056 maybe_convert_nontype_argument (tree type
, tree arg
)
8058 /* Auto parms get no conversion. */
8059 if (type_uses_auto (type
))
8061 /* We don't need or want to add this conversion now if we're going to use the
8062 argument for deduction. */
8063 if (value_dependent_expression_p (arg
))
8066 type
= cv_unqualified (type
);
8067 tree argtype
= TREE_TYPE (arg
);
8068 if (same_type_p (type
, argtype
))
8071 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
8072 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
8076 /* Convert the indicated template ARG as necessary to match the
8077 indicated template PARM. Returns the converted ARG, or
8078 error_mark_node if the conversion was unsuccessful. Error and
8079 warning messages are issued under control of COMPLAIN. This
8080 conversion is for the Ith parameter in the parameter list. ARGS is
8081 the full set of template arguments deduced so far. */
8084 convert_template_argument (tree parm
,
8087 tsubst_flags_t complain
,
8093 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
8095 if (parm
== error_mark_node
|| error_operand_p (arg
))
8096 return error_mark_node
;
8098 /* Trivially convert placeholders. */
8099 if (TREE_CODE (arg
) == WILDCARD_DECL
)
8100 return convert_wildcard_argument (parm
, arg
);
8102 if (arg
== any_targ_node
)
8105 if (TREE_CODE (arg
) == TREE_LIST
8106 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
8108 /* The template argument was the name of some
8109 member function. That's usually
8110 invalid, but static members are OK. In any
8111 case, grab the underlying fields/functions
8112 and issue an error later if required. */
8113 TREE_TYPE (arg
) = unknown_type_node
;
8118 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
8119 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
8120 || requires_tmpl_type
);
8122 /* When determining whether an argument pack expansion is a template,
8123 look at the pattern. */
8124 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
8125 arg
= PACK_EXPANSION_PATTERN (arg
);
8127 /* Deal with an injected-class-name used as a template template arg. */
8128 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
8130 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
8131 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8133 if (cxx_dialect
>= cxx11
)
8134 /* OK under DR 1004. */;
8135 else if (complain
& tf_warning_or_error
)
8136 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
8137 " used as template template argument", TYPE_NAME (arg
));
8138 else if (flag_pedantic_errors
)
8146 ((TREE_CODE (arg
) == TEMPLATE_DECL
8147 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
8148 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
8149 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8150 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
8153 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
8154 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
8155 arg
= TYPE_STUB_DECL (arg
);
8157 is_type
= TYPE_P (arg
) || is_tmpl_type
;
8159 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
8160 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
8162 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
8164 if (complain
& tf_error
)
8165 error ("invalid use of destructor %qE as a type", orig_arg
);
8166 return error_mark_node
;
8169 permerror (input_location
,
8170 "to refer to a type member of a template parameter, "
8171 "use %<typename %E%>", orig_arg
);
8173 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
8174 TREE_OPERAND (arg
, 1),
8180 if (is_type
!= requires_type
)
8184 if (complain
& tf_error
)
8186 error ("type/value mismatch at argument %d in template "
8187 "parameter list for %qD",
8191 /* The template argument is a type, but we're expecting
8193 inform (input_location
,
8194 " expected a constant of type %qT, got %qT",
8196 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
8197 /* [temp.arg]/2: "In a template-argument, an ambiguity
8198 between a type-id and an expression is resolved to a
8199 type-id, regardless of the form of the corresponding
8200 template-parameter." So give the user a clue. */
8201 if (TREE_CODE (arg
) == FUNCTION_TYPE
)
8202 inform (input_location
, " ambiguous template argument "
8203 "for non-type template parameter is treated as "
8206 else if (requires_tmpl_type
)
8207 inform (input_location
,
8208 " expected a class template, got %qE", orig_arg
);
8210 inform (input_location
,
8211 " expected a type, got %qE", orig_arg
);
8214 return error_mark_node
;
8216 if (is_tmpl_type
^ requires_tmpl_type
)
8218 if (in_decl
&& (complain
& tf_error
))
8220 error ("type/value mismatch at argument %d in template "
8221 "parameter list for %qD",
8224 inform (input_location
,
8225 " expected a type, got %qT", DECL_NAME (arg
));
8227 inform (input_location
,
8228 " expected a class template, got %qT", orig_arg
);
8230 return error_mark_node
;
8233 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
8234 /* We already did the appropriate conversion when packing args. */
8238 if (requires_tmpl_type
)
8240 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
8241 /* The number of argument required is not known yet.
8242 Just accept it for now. */
8246 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
8249 /* Strip alias templates that are equivalent to another
8251 arg
= get_underlying_template (arg
);
8252 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
8254 if (coerce_template_template_parms (parmparm
, argparm
,
8260 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8262 if (val
!= error_mark_node
)
8264 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8265 val
= TREE_TYPE (val
);
8266 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8267 val
= make_pack_expansion (val
, complain
);
8272 if (in_decl
&& (complain
& tf_error
))
8274 error ("type/value mismatch at argument %d in "
8275 "template parameter list for %qD",
8277 inform (input_location
,
8278 " expected a template of type %qD, got %qT",
8282 val
= error_mark_node
;
8285 // Check that the constraints are compatible before allowing the
8287 if (val
!= error_mark_node
)
8288 if (!is_compatible_template_arg (parm
, arg
))
8290 if (in_decl
&& (complain
& tf_error
))
8292 error ("constraint mismatch at argument %d in "
8293 "template parameter list for %qD",
8295 inform (input_location
, " expected %qD but got %qD",
8298 val
= error_mark_node
;
8304 /* We only form one instance of each template specialization.
8305 Therefore, if we use a non-canonical variant (i.e., a
8306 typedef), any future messages referring to the type will use
8307 the typedef, which is confusing if those future uses do not
8308 themselves also use the typedef. */
8310 val
= canonicalize_type_argument (val
, complain
);
8314 tree t
= TREE_TYPE (parm
);
8316 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8317 > TMPL_ARGS_DEPTH (args
))
8318 /* We don't have enough levels of args to do any substitution. This
8319 can happen in the context of -fnew-ttp-matching. */;
8320 else if (tree a
= type_uses_auto (t
))
8322 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8323 if (t
== error_mark_node
)
8324 return error_mark_node
;
8327 t
= tsubst (t
, args
, complain
, in_decl
);
8329 if (invalid_nontype_parm_type_p (t
, complain
))
8330 return error_mark_node
;
8332 if (t
!= TREE_TYPE (parm
))
8333 t
= canonicalize_type_argument (t
, complain
);
8335 if (!type_dependent_expression_p (orig_arg
)
8336 && !uses_template_parms (t
))
8337 /* We used to call digest_init here. However, digest_init
8338 will report errors, which we don't want when complain
8339 is zero. More importantly, digest_init will try too
8340 hard to convert things: for example, `0' should not be
8341 converted to pointer type at this point according to
8342 the standard. Accepting this is not merely an
8343 extension, since deciding whether or not these
8344 conversions can occur is part of determining which
8345 function template to call, or whether a given explicit
8346 argument specification is valid. */
8347 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8350 val
= canonicalize_expr_argument (orig_arg
, complain
);
8351 val
= maybe_convert_nontype_argument (t
, val
);
8355 if (val
== NULL_TREE
)
8356 val
= error_mark_node
;
8357 else if (val
== error_mark_node
&& (complain
& tf_error
))
8358 error_at (cp_expr_loc_or_input_loc (orig_arg
),
8359 "could not convert template argument %qE from %qT to %qT",
8360 orig_arg
, TREE_TYPE (orig_arg
), t
);
8362 if (INDIRECT_REF_P (val
))
8364 /* Reject template arguments that are references to built-in
8365 functions with no library fallbacks. */
8366 const_tree inner
= TREE_OPERAND (val
, 0);
8367 const_tree innertype
= TREE_TYPE (inner
);
8369 && TYPE_REF_P (innertype
)
8370 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8371 && TREE_OPERAND_LENGTH (inner
) > 0
8372 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8373 return error_mark_node
;
8376 if (TREE_CODE (val
) == SCOPE_REF
)
8378 /* Strip typedefs from the SCOPE_REF. */
8379 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8380 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8382 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8383 QUALIFIED_NAME_IS_TEMPLATE (val
));
8390 /* Coerces the remaining template arguments in INNER_ARGS (from
8391 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8392 Returns the coerced argument pack. PARM_IDX is the position of this
8393 parameter in the template parameter list. ARGS is the original
8394 template argument list. */
8396 coerce_template_parameter_pack (tree parms
,
8404 tsubst_flags_t complain
)
8406 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8407 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8410 tree packed_parms
= NULL_TREE
;
8412 if (arg_idx
> nargs
)
8415 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8417 /* When the template parameter is a non-type template parameter pack
8418 or template template parameter pack whose type or template
8419 parameters use parameter packs, we know exactly how many arguments
8420 we are looking for. Build a vector of the instantiated decls for
8421 these template parameters in PACKED_PARMS. */
8422 /* We can't use make_pack_expansion here because it would interpret a
8423 _DECL as a use rather than a declaration. */
8424 tree decl
= TREE_VALUE (parm
);
8425 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8426 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8427 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8428 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8430 TREE_VEC_LENGTH (args
)--;
8431 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8432 TREE_VEC_LENGTH (args
)++;
8434 if (packed_parms
== error_mark_node
)
8435 return error_mark_node
;
8437 /* If we're doing a partial instantiation of a member template,
8438 verify that all of the types used for the non-type
8439 template parameter pack are, in fact, valid for non-type
8440 template parameters. */
8442 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8444 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8445 for (j
= 0; j
< len
; ++j
)
8447 tree t
= TREE_VEC_ELT (packed_parms
, j
);
8448 if (TREE_CODE (t
) == PARM_DECL
8449 && invalid_nontype_parm_type_p (TREE_TYPE (t
), complain
))
8450 return error_mark_node
;
8452 /* We don't know how many args we have yet, just
8453 use the unconverted ones for now. */
8457 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8459 /* Check if we have a placeholder pack, which indicates we're
8460 in the context of a introduction list. In that case we want
8461 to match this pack to the single placeholder. */
8462 else if (arg_idx
< nargs
8463 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8464 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8466 nargs
= arg_idx
+ 1;
8467 packed_args
= make_tree_vec (1);
8470 packed_args
= make_tree_vec (nargs
- arg_idx
);
8472 /* Convert the remaining arguments, which will be a part of the
8473 parameter pack "parm". */
8474 int first_pack_arg
= arg_idx
;
8475 for (; arg_idx
< nargs
; ++arg_idx
)
8477 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8478 tree actual_parm
= TREE_VALUE (parm
);
8479 int pack_idx
= arg_idx
- first_pack_arg
;
8483 /* Once we've packed as many args as we have types, stop. */
8484 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8486 else if (PACK_EXPANSION_P (arg
))
8487 /* We don't know how many args we have yet, just
8488 use the unconverted ones for now. */
8491 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8494 if (arg
== error_mark_node
)
8496 if (complain
& tf_error
)
8497 error ("template argument %d is invalid", arg_idx
+ 1);
8500 arg
= convert_template_argument (actual_parm
,
8501 arg
, new_args
, complain
, parm_idx
,
8503 if (arg
== error_mark_node
)
8505 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8508 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8509 && TREE_VEC_LENGTH (packed_args
) > 0)
8511 if (complain
& tf_error
)
8512 error ("wrong number of template arguments (%d, should be %d)",
8513 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8514 return error_mark_node
;
8517 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8518 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8519 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8522 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8523 TREE_CONSTANT (argument_pack
) = 1;
8526 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8528 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8529 TREE_VEC_LENGTH (packed_args
));
8530 return argument_pack
;
8533 /* Returns the number of pack expansions in the template argument vector
8537 pack_expansion_args_count (tree args
)
8542 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8544 tree elt
= TREE_VEC_ELT (args
, i
);
8545 if (elt
&& PACK_EXPANSION_P (elt
))
8551 /* Convert all template arguments to their appropriate types, and
8552 return a vector containing the innermost resulting template
8553 arguments. If any error occurs, return error_mark_node. Error and
8554 warning messages are issued under control of COMPLAIN.
8556 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8557 for arguments not specified in ARGS. Otherwise, if
8558 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8559 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8560 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8564 coerce_template_parms (tree parms
,
8567 tsubst_flags_t complain
,
8568 bool require_all_args
,
8569 bool use_default_args
)
8571 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8572 tree orig_inner_args
;
8575 tree new_inner_args
;
8577 /* When used as a boolean value, indicates whether this is a
8578 variadic template parameter list. Since it's an int, we can also
8579 subtract it from nparms to get the number of non-variadic
8582 int variadic_args_p
= 0;
8583 int post_variadic_parms
= 0;
8585 /* Adjustment to nparms for fixed parameter packs. */
8586 int fixed_pack_adjust
= 0;
8587 int fixed_packs
= 0;
8590 /* Likewise for parameters with default arguments. */
8593 if (args
== error_mark_node
)
8594 return error_mark_node
;
8596 nparms
= TREE_VEC_LENGTH (parms
);
8598 /* Determine if there are any parameter packs or default arguments. */
8599 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8601 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8603 ++post_variadic_parms
;
8604 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8606 if (TREE_PURPOSE (parm
))
8610 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8611 /* If there are no parameters that follow a parameter pack, we need to
8612 expand any argument packs so that we can deduce a parameter pack from
8613 some non-packed args followed by an argument pack, as in variadic85.C.
8614 If there are such parameters, we need to leave argument packs intact
8615 so the arguments are assigned properly. This can happen when dealing
8616 with a nested class inside a partial specialization of a class
8617 template, as in variadic92.C, or when deducing a template parameter pack
8618 from a sub-declarator, as in variadic114.C. */
8619 if (!post_variadic_parms
)
8620 inner_args
= expand_template_argument_pack (inner_args
);
8622 /* Count any pack expansion args. */
8623 variadic_args_p
= pack_expansion_args_count (inner_args
);
8625 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8626 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8627 || (nargs
< nparms
- variadic_p
8630 && (!use_default_args
8631 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8632 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8635 if (complain
& tf_error
)
8637 if (variadic_p
|| default_p
)
8639 nparms
-= variadic_p
+ default_p
;
8640 error ("wrong number of template arguments "
8641 "(%d, should be at least %d)", nargs
, nparms
);
8644 error ("wrong number of template arguments "
8645 "(%d, should be %d)", nargs
, nparms
);
8648 inform (DECL_SOURCE_LOCATION (in_decl
),
8649 "provided for %qD", in_decl
);
8652 return error_mark_node
;
8654 /* We can't pass a pack expansion to a non-pack parameter of an alias
8655 template (DR 1430). */
8657 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8658 || concept_definition_p (in_decl
))
8660 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8662 if (complain
& tf_error
)
8664 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8666 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8667 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8669 if (PACK_EXPANSION_P (arg
)
8670 && !template_parameter_pack_p (parm
))
8672 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8673 error_at (location_of (arg
),
8674 "pack expansion argument for non-pack parameter "
8675 "%qD of alias template %qD", parm
, in_decl
);
8677 error_at (location_of (arg
),
8678 "pack expansion argument for non-pack parameter "
8679 "%qD of concept %qD", parm
, in_decl
);
8680 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8687 return error_mark_node
;
8690 /* We need to evaluate the template arguments, even though this
8691 template-id may be nested within a "sizeof". */
8694 new_inner_args
= make_tree_vec (nparms
);
8695 new_args
= add_outermost_template_args (args
, new_inner_args
);
8696 int pack_adjust
= 0;
8697 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8702 /* Get the Ith template parameter. */
8703 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8705 if (parm
== error_mark_node
)
8707 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8711 /* Calculate the next argument. */
8712 if (arg_idx
< nargs
)
8713 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8717 if (template_parameter_pack_p (TREE_VALUE (parm
))
8718 && (arg
|| require_all_args
|| !(complain
& tf_partial
))
8719 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8721 /* Some arguments will be placed in the
8722 template parameter pack PARM. */
8723 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8724 inner_args
, arg_idx
,
8728 if (arg
== NULL_TREE
)
8730 /* We don't know how many args we have yet, just use the
8731 unconverted (and still packed) ones for now. */
8732 new_inner_args
= orig_inner_args
;
8737 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8739 /* Store this argument. */
8740 if (arg
== error_mark_node
)
8743 /* We are done with all of the arguments. */
8749 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8750 arg_idx
+= pack_adjust
;
8751 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8754 fixed_pack_adjust
+= pack_adjust
;
8762 if (PACK_EXPANSION_P (arg
))
8764 /* "If every valid specialization of a variadic template
8765 requires an empty template parameter pack, the template is
8766 ill-formed, no diagnostic required." So check that the
8767 pattern works with this parameter. */
8768 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8769 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8773 if (conv
== error_mark_node
)
8775 if (complain
& tf_error
)
8776 inform (input_location
, "so any instantiation with a "
8777 "non-empty parameter pack would be ill-formed");
8780 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8781 /* Recover from missing typename. */
8782 TREE_VEC_ELT (inner_args
, arg_idx
)
8783 = make_pack_expansion (conv
, complain
);
8785 /* We don't know how many args we have yet, just
8786 use the unconverted ones for now. */
8787 new_inner_args
= inner_args
;
8792 else if (require_all_args
)
8794 /* There must be a default arg in this case. */
8795 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8797 /* The position of the first default template argument,
8798 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8800 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8801 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8802 arg_idx
- pack_adjust
);
8807 if (arg
== error_mark_node
)
8809 if (complain
& tf_error
)
8810 error ("template argument %d is invalid", arg_idx
+ 1);
8814 /* This can occur if there was an error in the template
8815 parameter list itself (which we would already have
8816 reported) that we are trying to recover from, e.g., a class
8817 template with a parameter list such as
8818 template<typename..., typename> (cpp0x/variadic150.C). */
8821 /* This can also happen with a fixed parameter pack (71834). */
8822 if (arg_idx
>= nargs
)
8826 arg
= convert_template_argument (TREE_VALUE (parm
),
8827 arg
, new_args
, complain
,
8830 if (arg
== error_mark_node
)
8833 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8836 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8838 /* If we had fixed parameter packs, we didn't know how many arguments we
8839 actually needed earlier; now we do. */
8840 nparms
+= fixed_pack_adjust
;
8841 variadic_p
-= fixed_packs
;
8845 if (arg_idx
< nargs
)
8847 /* We had some pack expansion arguments that will only work if the packs
8848 are empty, but wait until instantiation time to complain.
8849 See variadic-ttp3.C. */
8851 /* Except that we can't provide empty packs to alias templates or
8852 concepts when there are no corresponding parameters. Basically,
8853 we can get here with this:
8855 template<typename T> concept C = true;
8857 template<typename... Args>
8861 When parsing C<Args...>, we try to form a concept check of
8862 C<?, Args...>. Without the extra check for substituting an empty
8863 pack past the last parameter, we can accept the check as valid.
8865 FIXME: This may be valid for alias templates (but I doubt it).
8867 FIXME: The error could be better also. */
8868 if (in_decl
&& concept_definition_p (in_decl
))
8870 if (complain
& tf_error
)
8871 error_at (location_of (TREE_VEC_ELT (args
, arg_idx
)),
8872 "too many arguments");
8873 return error_mark_node
;
8876 int len
= nparms
+ (nargs
- arg_idx
);
8877 tree args
= make_tree_vec (len
);
8879 for (; i
< nparms
; ++i
)
8880 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8881 for (; i
< len
; ++i
, ++arg_idx
)
8882 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8883 arg_idx
- pack_adjust
);
8884 new_inner_args
= args
;
8889 gcc_assert (!(complain
& tf_error
) || seen_error ());
8890 return error_mark_node
;
8893 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8894 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8895 TREE_VEC_LENGTH (new_inner_args
));
8897 return new_inner_args
;
8900 /* Convert all template arguments to their appropriate types, and
8901 return a vector containing the innermost resulting template
8902 arguments. If any error occurs, return error_mark_node. Error and
8903 warning messages are not issued.
8905 Note that no function argument deduction is performed, and default
8906 arguments are used to fill in unspecified arguments. */
8908 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8910 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8913 /* Convert all template arguments to their appropriate type, and
8914 instantiate default arguments as needed. This returns a vector
8915 containing the innermost resulting template arguments, or
8916 error_mark_node if unsuccessful. */
8918 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8919 tsubst_flags_t complain
)
8921 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8924 /* Like coerce_template_parms. If PARMS represents all template
8925 parameters levels, this function returns a vector of vectors
8926 representing all the resulting argument levels. Note that in this
8927 case, only the innermost arguments are coerced because the
8928 outermost ones are supposed to have been coerced already.
8930 Otherwise, if PARMS represents only (the innermost) vector of
8931 parameters, this function returns a vector containing just the
8932 innermost resulting arguments. */
8935 coerce_innermost_template_parms (tree parms
,
8938 tsubst_flags_t complain
,
8939 bool require_all_args
,
8940 bool use_default_args
)
8942 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8943 int args_depth
= TMPL_ARGS_DEPTH (args
);
8946 if (parms_depth
> 1)
8948 coerced_args
= make_tree_vec (parms_depth
);
8952 for (level
= parms
, cur_depth
= parms_depth
;
8953 parms_depth
> 0 && level
!= NULL_TREE
;
8954 level
= TREE_CHAIN (level
), --cur_depth
)
8957 if (cur_depth
== args_depth
)
8958 l
= coerce_template_parms (TREE_VALUE (level
),
8959 args
, in_decl
, complain
,
8963 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8965 if (l
== error_mark_node
)
8966 return error_mark_node
;
8968 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8972 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8973 args
, in_decl
, complain
,
8976 return coerced_args
;
8979 /* Returns 1 if template args OT and NT are equivalent. */
8982 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8986 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8988 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8991 if (TREE_CODE (nt
) == TREE_VEC
)
8992 /* For member templates */
8993 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8994 else if (PACK_EXPANSION_P (ot
))
8995 return (PACK_EXPANSION_P (nt
)
8996 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8997 PACK_EXPANSION_PATTERN (nt
))
8998 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8999 PACK_EXPANSION_EXTRA_ARGS (nt
)));
9000 else if (ARGUMENT_PACK_P (ot
))
9005 if (!ARGUMENT_PACK_P (nt
))
9008 opack
= ARGUMENT_PACK_ARGS (ot
);
9009 npack
= ARGUMENT_PACK_ARGS (nt
);
9010 len
= TREE_VEC_LENGTH (opack
);
9011 if (TREE_VEC_LENGTH (npack
) != len
)
9013 for (i
= 0; i
< len
; ++i
)
9014 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
9015 TREE_VEC_ELT (npack
, i
)))
9019 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
9021 else if (TYPE_P (nt
))
9025 /* Don't treat an alias template specialization with dependent
9026 arguments as equivalent to its underlying type when used as a
9027 template argument; we need them to be distinct so that we
9028 substitute into the specialization arguments at instantiation
9029 time. And aliases can't be equivalent without being ==, so
9030 we don't need to look any deeper.
9032 During partial ordering, however, we need to treat them normally so
9033 that we can order uses of the same alias with different
9034 cv-qualification (79960). */
9036 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
9039 return same_type_p (ot
, nt
);
9041 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
9045 /* Try to treat a template non-type argument that has been converted
9046 to the parameter type as equivalent to one that hasn't yet. */
9047 for (enum tree_code code1
= TREE_CODE (ot
);
9048 CONVERT_EXPR_CODE_P (code1
)
9049 || code1
== NON_LVALUE_EXPR
;
9050 code1
= TREE_CODE (ot
))
9051 ot
= TREE_OPERAND (ot
, 0);
9052 for (enum tree_code code2
= TREE_CODE (nt
);
9053 CONVERT_EXPR_CODE_P (code2
)
9054 || code2
== NON_LVALUE_EXPR
;
9055 code2
= TREE_CODE (nt
))
9056 nt
= TREE_OPERAND (nt
, 0);
9058 return cp_tree_equal (ot
, nt
);
9062 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9063 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9064 NEWARG_PTR with the offending arguments if they are non-NULL. */
9067 comp_template_args (tree oldargs
, tree newargs
,
9068 tree
*oldarg_ptr
, tree
*newarg_ptr
,
9073 if (oldargs
== newargs
)
9076 if (!oldargs
|| !newargs
)
9079 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
9082 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
9084 tree nt
= TREE_VEC_ELT (newargs
, i
);
9085 tree ot
= TREE_VEC_ELT (oldargs
, i
);
9087 if (! template_args_equal (ot
, nt
, partial_order
))
9089 if (oldarg_ptr
!= NULL
)
9091 if (newarg_ptr
!= NULL
)
9100 comp_template_args_porder (tree oargs
, tree nargs
)
9102 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
9105 /* Implement a freelist interface for objects of type T.
9107 Head is a separate object, rather than a regular member, so that we
9108 can define it as a GTY deletable pointer, which is highly
9109 desirable. A data member could be declared that way, but then the
9110 containing object would implicitly get GTY((user)), which would
9111 prevent us from instantiating freelists as global objects.
9112 Although this way we can create freelist global objects, they're
9113 such thin wrappers that instantiating temporaries at every use
9114 loses nothing and saves permanent storage for the freelist object.
9116 Member functions next, anew, poison and reinit have default
9117 implementations that work for most of the types we're interested
9118 in, but if they don't work for some type, they should be explicitly
9119 specialized. See the comments before them for requirements, and
9120 the example specializations for the tree_list_freelist. */
9121 template <typename T
>
9124 /* Return the next object in a chain. We could just do type
9125 punning, but if we access the object with its underlying type, we
9126 avoid strict-aliasing trouble. This needs only work between
9127 poison and reinit. */
9128 static T
*&next (T
*obj
) { return obj
->next
; }
9130 /* Return a newly allocated, uninitialized or minimally-initialized
9131 object of type T. Any initialization performed by anew should
9132 either remain across the life of the object and the execution of
9133 poison, or be redone by reinit. */
9134 static T
*anew () { return ggc_alloc
<T
> (); }
9136 /* Optionally scribble all over the bits holding the object, so that
9137 they become (mostly?) uninitialized memory. This is called while
9138 preparing to make the object part of the free list. */
9139 static void poison (T
*obj
) {
9140 T
*p ATTRIBUTE_UNUSED
= obj
;
9141 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9143 #ifdef ENABLE_GC_CHECKING
9144 /* Poison the data, to indicate the data is garbage. */
9145 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
9146 memset (p
, 0xa5, sizeof (*p
));
9148 /* Let valgrind know the object is free. */
9149 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
9151 /* Let valgrind know the next portion of the object is available,
9152 but uninitialized. */
9153 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9156 /* Bring an object that underwent at least one lifecycle after anew
9157 and before the most recent free and poison, back to a usable
9158 state, reinitializing whatever is needed for it to be
9159 functionally equivalent to an object just allocated and returned
9160 by anew. This may poison or clear the next field, used by
9161 freelist housekeeping after poison was called. */
9162 static void reinit (T
*obj
) {
9163 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
9165 #ifdef ENABLE_GC_CHECKING
9166 memset (q
, 0xa5, sizeof (*q
));
9168 /* Let valgrind know the entire object is available, but
9170 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
9173 /* Reference a GTY-deletable pointer that points to the first object
9174 in the free list proper. */
9177 /* Construct a freelist object chaining objects off of HEAD. */
9178 freelist (T
*&head
) : head(head
) {}
9180 /* Add OBJ to the free object list. The former head becomes OBJ's
9189 /* Take an object from the free list, if one is available, or
9190 allocate a new one. Objects taken from the free list should be
9191 regarded as filled with garbage, except for bits that are
9192 configured to be preserved across free and alloc. */
9207 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9208 want to allocate a TREE_LIST using the usual interface, and ensure
9209 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9210 build_tree_list logic in reinit, so this could go out of sync. */
9213 freelist
<tree_node
>::next (tree obj
)
9215 return TREE_CHAIN (obj
);
9219 freelist
<tree_node
>::anew ()
9221 return build_tree_list (NULL
, NULL
);
9225 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
9227 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
9228 tree p ATTRIBUTE_UNUSED
= obj
;
9229 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9230 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
9232 #ifdef ENABLE_GC_CHECKING
9233 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9235 /* Poison the data, to indicate the data is garbage. */
9236 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
9237 memset (p
, 0xa5, size
);
9239 /* Let valgrind know the object is free. */
9240 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
9241 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9242 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9243 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
9245 #ifdef ENABLE_GC_CHECKING
9246 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
9247 /* Keep TREE_CHAIN functional. */
9248 TREE_SET_CODE (obj
, TREE_LIST
);
9250 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9255 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
9257 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
9259 #ifdef ENABLE_GC_CHECKING
9260 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
9261 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9262 memset (obj
, 0, sizeof (tree_list
));
9265 /* Let valgrind know the entire object is available, but
9267 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
9269 #ifdef ENABLE_GC_CHECKING
9270 TREE_SET_CODE (obj
, TREE_LIST
);
9272 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
9276 /* Point to the first object in the TREE_LIST freelist. */
9277 static GTY((deletable
)) tree tree_list_freelist_head
;
9278 /* Return the/an actual TREE_LIST freelist. */
9279 static inline freelist
<tree_node
>
9280 tree_list_freelist ()
9282 return tree_list_freelist_head
;
9285 /* Point to the first object in the tinst_level freelist. */
9286 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
9287 /* Return the/an actual tinst_level freelist. */
9288 static inline freelist
<tinst_level
>
9289 tinst_level_freelist ()
9291 return tinst_level_freelist_head
;
9294 /* Point to the first object in the pending_template freelist. */
9295 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9296 /* Return the/an actual pending_template freelist. */
9297 static inline freelist
<pending_template
>
9298 pending_template_freelist ()
9300 return pending_template_freelist_head
;
9303 /* Build the TREE_LIST object out of a split list, store it
9304 permanently, and return it. */
9306 tinst_level::to_list ()
9308 gcc_assert (split_list_p ());
9309 tree ret
= tree_list_freelist ().alloc ();
9310 TREE_PURPOSE (ret
) = tldcl
;
9311 TREE_VALUE (ret
) = targs
;
9314 gcc_assert (tree_list_p ());
9318 const unsigned short tinst_level::refcount_infinity
;
9320 /* Increment OBJ's refcount unless it is already infinite. */
9321 static tinst_level
*
9322 inc_refcount_use (tinst_level
*obj
)
9324 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9329 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9331 tinst_level::free (tinst_level
*obj
)
9333 if (obj
->tree_list_p ())
9334 tree_list_freelist ().free (obj
->get_node ());
9335 tinst_level_freelist ().free (obj
);
9338 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9339 OBJ's DECL and OBJ, and start over with the tinst_level object that
9340 used to be referenced by OBJ's NEXT. */
9342 dec_refcount_use (tinst_level
*obj
)
9345 && obj
->refcount
!= tinst_level::refcount_infinity
9346 && !--obj
->refcount
)
9348 tinst_level
*next
= obj
->next
;
9349 tinst_level::free (obj
);
9354 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9355 and of the former PTR. Omitting the second argument is equivalent
9356 to passing (T*)NULL; this is allowed because passing the
9357 zero-valued integral constant NULL confuses type deduction and/or
9358 overload resolution. */
9359 template <typename T
>
9361 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9364 ptr
= inc_refcount_use (obj
);
9365 dec_refcount_use (save
);
9369 add_pending_template (tree d
)
9371 tree ti
= (TYPE_P (d
)
9372 ? CLASSTYPE_TEMPLATE_INFO (d
)
9373 : DECL_TEMPLATE_INFO (d
));
9374 struct pending_template
*pt
;
9377 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9380 /* We are called both from instantiate_decl, where we've already had a
9381 tinst_level pushed, and instantiate_template, where we haven't.
9383 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9384 level
= !current_tinst_level
9385 || current_tinst_level
->maybe_get_node () != d
;
9388 push_tinst_level (d
);
9390 pt
= pending_template_freelist ().alloc ();
9393 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9394 if (last_pending_template
)
9395 last_pending_template
->next
= pt
;
9397 pending_templates
= pt
;
9399 last_pending_template
= pt
;
9401 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9408 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9409 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9410 documentation for TEMPLATE_ID_EXPR. */
9413 lookup_template_function (tree fns
, tree arglist
)
9415 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9416 return error_mark_node
;
9418 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9420 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9422 error ("%q#D is not a function template", fns
);
9423 return error_mark_node
;
9426 if (BASELINK_P (fns
))
9428 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9430 BASELINK_FUNCTIONS (fns
),
9435 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9438 /* Within the scope of a template class S<T>, the name S gets bound
9439 (in build_self_reference) to a TYPE_DECL for the class, not a
9440 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9441 or one of its enclosing classes, and that type is a template,
9442 return the associated TEMPLATE_DECL. Otherwise, the original
9445 Also handle the case when DECL is a TREE_LIST of ambiguous
9446 injected-class-names from different bases. */
9449 maybe_get_template_decl_from_type_decl (tree decl
)
9451 if (decl
== NULL_TREE
)
9454 /* DR 176: A lookup that finds an injected-class-name (10.2
9455 [class.member.lookup]) can result in an ambiguity in certain cases
9456 (for example, if it is found in more than one base class). If all of
9457 the injected-class-names that are found refer to specializations of
9458 the same class template, and if the name is followed by a
9459 template-argument-list, the reference refers to the class template
9460 itself and not a specialization thereof, and is not ambiguous. */
9461 if (TREE_CODE (decl
) == TREE_LIST
)
9463 tree t
, tmpl
= NULL_TREE
;
9464 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9466 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9469 else if (tmpl
!= elt
)
9472 if (tmpl
&& t
== NULL_TREE
)
9478 return (decl
!= NULL_TREE
9479 && DECL_SELF_REFERENCE_P (decl
)
9480 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9481 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9484 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9485 parameters, find the desired type.
9487 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9489 IN_DECL, if non-NULL, is the template declaration we are trying to
9492 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9493 the class we are looking up.
9495 Issue error and warning messages under control of COMPLAIN.
9497 If the template class is really a local class in a template
9498 function, then the FUNCTION_CONTEXT is the function in which it is
9501 ??? Note that this function is currently called *twice* for each
9502 template-id: the first time from the parser, while creating the
9503 incomplete type (finish_template_type), and the second type during the
9504 real instantiation (instantiate_template_class). This is surely something
9505 that we want to avoid. It also causes some problems with argument
9506 coercion (see convert_nontype_argument for more information on this). */
9509 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9510 int entering_scope
, tsubst_flags_t complain
)
9512 tree templ
= NULL_TREE
, parmlist
;
9519 if (identifier_p (d1
))
9521 tree value
= innermost_non_namespace_value (d1
);
9522 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9527 push_decl_namespace (context
);
9528 templ
= lookup_name (d1
);
9529 templ
= maybe_get_template_decl_from_type_decl (templ
);
9531 pop_decl_namespace ();
9534 context
= DECL_CONTEXT (templ
);
9536 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9538 tree type
= TREE_TYPE (d1
);
9540 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9541 an implicit typename for the second A. Deal with it. */
9542 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9543 type
= TREE_TYPE (type
);
9545 if (CLASSTYPE_TEMPLATE_INFO (type
))
9547 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9548 d1
= DECL_NAME (templ
);
9551 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9552 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9554 templ
= TYPE_TI_TEMPLATE (d1
);
9555 d1
= DECL_NAME (templ
);
9557 else if (DECL_TYPE_TEMPLATE_P (d1
))
9560 d1
= DECL_NAME (templ
);
9561 context
= DECL_CONTEXT (templ
);
9563 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9566 d1
= DECL_NAME (templ
);
9569 /* Issue an error message if we didn't find a template. */
9572 if (complain
& tf_error
)
9573 error ("%qT is not a template", d1
);
9574 return error_mark_node
;
9577 if (TREE_CODE (templ
) != TEMPLATE_DECL
9578 /* Make sure it's a user visible template, if it was named by
9580 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9581 && !PRIMARY_TEMPLATE_P (templ
)))
9583 if (complain
& tf_error
)
9585 error ("non-template type %qT used as a template", d1
);
9587 error ("for template declaration %q+D", in_decl
);
9589 return error_mark_node
;
9592 complain
&= ~tf_user
;
9594 /* An alias that just changes the name of a template is equivalent to the
9595 other template, so if any of the arguments are pack expansions, strip
9596 the alias to avoid problems with a pack expansion passed to a non-pack
9597 alias template parameter (DR 1430). */
9598 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9599 templ
= get_underlying_template (templ
);
9601 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9604 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9605 if (arglist2
== error_mark_node
9606 || (!uses_template_parms (arglist2
)
9607 && check_instantiated_args (templ
, arglist2
, complain
)))
9608 return error_mark_node
;
9610 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9615 tree template_type
= TREE_TYPE (templ
);
9618 tree found
= NULL_TREE
;
9621 int is_dependent_type
;
9622 int use_partial_inst_tmpl
= false;
9624 if (template_type
== error_mark_node
)
9625 /* An error occurred while building the template TEMPL, and a
9626 diagnostic has most certainly been emitted for that
9627 already. Let's propagate that error. */
9628 return error_mark_node
;
9630 gen_tmpl
= most_general_template (templ
);
9631 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9632 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9633 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9635 if (arg_depth
== 1 && parm_depth
> 1)
9637 /* We've been given an incomplete set of template arguments.
9640 template <class T> struct S1 {
9641 template <class U> struct S2 {};
9642 template <class U> struct S2<U*> {};
9645 we will be called with an ARGLIST of `U*', but the
9646 TEMPLATE will be `template <class T> template
9647 <class U> struct S1<T>::S2'. We must fill in the missing
9649 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9650 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9651 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9654 /* Now we should have enough arguments. */
9655 gcc_assert (parm_depth
== arg_depth
);
9657 /* From here on, we're only interested in the most general
9660 /* Calculate the BOUND_ARGS. These will be the args that are
9661 actually tsubst'd into the definition to create the
9663 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9665 /*require_all_args=*/true,
9666 /*use_default_args=*/true);
9668 if (arglist
== error_mark_node
)
9669 /* We were unable to bind the arguments. */
9670 return error_mark_node
;
9672 /* In the scope of a template class, explicit references to the
9673 template class refer to the type of the template, not any
9674 instantiation of it. For example, in:
9676 template <class T> class C { void f(C<T>); }
9678 the `C<T>' is just the same as `C'. Outside of the
9679 class, however, such a reference is an instantiation. */
9681 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9682 || currently_open_class (template_type
))
9684 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9686 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9687 return template_type
;
9690 /* If we already have this specialization, return it. */
9691 elt
.tmpl
= gen_tmpl
;
9693 elt
.spec
= NULL_TREE
;
9694 hash
= spec_hasher::hash (&elt
);
9695 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9700 /* If the the template's constraints are not satisfied,
9701 then we cannot form a valid type.
9703 Note that the check is deferred until after the hash
9704 lookup. This prevents redundant checks on previously
9705 instantiated specializations. */
9707 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
9708 && !constraints_satisfied_p (gen_tmpl
, arglist
))
9710 if (complain
& tf_error
)
9712 auto_diagnostic_group d
;
9713 error ("template constraint failure for %qD", gen_tmpl
);
9714 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9716 return error_mark_node
;
9719 is_dependent_type
= uses_template_parms (arglist
);
9721 /* If the deduced arguments are invalid, then the binding
9723 if (!is_dependent_type
9724 && check_instantiated_args (gen_tmpl
,
9725 INNERMOST_TEMPLATE_ARGS (arglist
),
9727 return error_mark_node
;
9729 if (!is_dependent_type
9730 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9731 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9732 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9734 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9735 DECL_NAME (gen_tmpl
),
9736 /*tag_scope=*/ts_global
);
9740 context
= DECL_CONTEXT (gen_tmpl
);
9741 if (context
&& TYPE_P (context
))
9743 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9744 context
= complete_type (context
);
9747 context
= tsubst (context
, arglist
, complain
, in_decl
);
9749 if (context
== error_mark_node
)
9750 return error_mark_node
;
9753 context
= global_namespace
;
9755 /* Create the type. */
9756 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9758 /* The user referred to a specialization of an alias
9759 template represented by GEN_TMPL.
9761 [temp.alias]/2 says:
9763 When a template-id refers to the specialization of an
9764 alias template, it is equivalent to the associated
9765 type obtained by substitution of its
9766 template-arguments for the template-parameters in the
9767 type-id of the alias template. */
9769 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9770 /* Note that the call above (by indirectly calling
9771 register_specialization in tsubst_decl) registers the
9772 TYPE_DECL representing the specialization of the alias
9773 template. So next time someone substitutes ARGLIST for
9774 the template parms into the alias template (GEN_TMPL),
9775 she'll get that TYPE_DECL back. */
9777 if (t
== error_mark_node
)
9780 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9782 if (!is_dependent_type
)
9784 set_current_access_from_decl (TYPE_NAME (template_type
));
9785 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9786 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9787 arglist
, complain
, in_decl
),
9788 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9789 arglist
, complain
, in_decl
),
9790 SCOPED_ENUM_P (template_type
), NULL
);
9792 if (t
== error_mark_node
)
9797 /* We don't want to call start_enum for this type, since
9798 the values for the enumeration constants may involve
9799 template parameters. And, no one should be interested
9800 in the enumeration constants for such a type. */
9801 t
= cxx_make_type (ENUMERAL_TYPE
);
9802 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9804 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9805 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9806 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9808 else if (CLASS_TYPE_P (template_type
))
9810 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9811 instantiated here. */
9812 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9814 t
= make_class_type (TREE_CODE (template_type
));
9815 CLASSTYPE_DECLARED_CLASS (t
)
9816 = CLASSTYPE_DECLARED_CLASS (template_type
);
9817 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9819 /* A local class. Make sure the decl gets registered properly. */
9820 if (context
== current_function_decl
)
9821 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9823 return error_mark_node
;
9825 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9826 /* This instantiation is another name for the primary
9827 template type. Set the TYPE_CANONICAL field
9829 TYPE_CANONICAL (t
) = template_type
;
9830 else if (any_template_arguments_need_structural_equality_p (arglist
))
9831 /* Some of the template arguments require structural
9832 equality testing, so this template class requires
9833 structural equality testing. */
9834 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9839 /* If we called start_enum or pushtag above, this information
9840 will already be set up. */
9843 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9845 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9846 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9847 DECL_SOURCE_LOCATION (type_decl
)
9848 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9851 type_decl
= TYPE_NAME (t
);
9853 if (CLASS_TYPE_P (template_type
))
9855 TREE_PRIVATE (type_decl
)
9856 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9857 TREE_PROTECTED (type_decl
)
9858 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9859 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9861 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9862 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9866 if (OVERLOAD_TYPE_P (t
)
9867 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9869 static const char *tags
[] = {"abi_tag", "may_alias"};
9871 for (unsigned ix
= 0; ix
!= 2; ix
++)
9874 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9878 = tree_cons (TREE_PURPOSE (attributes
),
9879 TREE_VALUE (attributes
),
9880 TYPE_ATTRIBUTES (t
));
9884 /* Let's consider the explicit specialization of a member
9885 of a class template specialization that is implicitly instantiated,
9890 template<class U> struct M {}; //#0
9895 struct S<int>::M<char> //#1
9899 [temp.expl.spec]/4 says this is valid.
9901 In this case, when we write:
9904 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9907 When we encounter #1, we want to store the partial instantiation
9908 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9910 For all cases other than this "explicit specialization of member of a
9911 class template", we just want to store the most general template into
9912 the CLASSTYPE_TI_TEMPLATE of M.
9914 This case of "explicit specialization of member of a class template"
9916 1/ the enclosing class is an instantiation of, and therefore not
9917 the same as, the context of the most general template, and
9918 2/ we aren't looking at the partial instantiation itself, i.e.
9919 the innermost arguments are not the same as the innermost parms of
9920 the most general template.
9922 So it's only when 1/ and 2/ happens that we want to use the partial
9923 instantiation of the member template in lieu of its most general
9926 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9927 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9928 /* the enclosing class must be an instantiation... */
9929 && CLASS_TYPE_P (context
)
9930 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9932 TREE_VEC_LENGTH (arglist
)--;
9933 ++processing_template_decl
;
9934 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9935 tree partial_inst_args
=
9936 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9937 arglist
, complain
, NULL_TREE
);
9938 --processing_template_decl
;
9939 TREE_VEC_LENGTH (arglist
)++;
9940 if (partial_inst_args
== error_mark_node
)
9941 return error_mark_node
;
9942 use_partial_inst_tmpl
=
9943 /*...and we must not be looking at the partial instantiation
9945 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9949 if (!use_partial_inst_tmpl
)
9950 /* This case is easy; there are no member templates involved. */
9954 /* This is a full instantiation of a member template. Find
9955 the partial instantiation of which this is an instance. */
9957 /* Temporarily reduce by one the number of levels in the ARGLIST
9958 so as to avoid comparing the last set of arguments. */
9959 TREE_VEC_LENGTH (arglist
)--;
9960 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9961 TREE_VEC_LENGTH (arglist
)++;
9962 /* FOUND is either a proper class type, or an alias
9963 template specialization. In the later case, it's a
9964 TYPE_DECL, resulting from the substituting of arguments
9965 for parameters in the TYPE_DECL of the alias template
9966 done earlier. So be careful while getting the template
9968 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9970 : (TREE_CODE (found
) == TYPE_DECL
9971 ? DECL_TI_TEMPLATE (found
)
9972 : CLASSTYPE_TI_TEMPLATE (found
)));
9974 if (DECL_CLASS_TEMPLATE_P (found
)
9975 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
9977 /* If this partial instantiation is specialized, we want to
9978 use it for hash table lookup. */
9980 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
9981 hash
= spec_hasher::hash (&elt
);
9985 // Build template info for the new specialization.
9986 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9989 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9990 gcc_checking_assert (*slot
== NULL
);
9991 entry
= ggc_alloc
<spec_entry
> ();
9995 /* Note this use of the partial instantiation so we can check it
9996 later in maybe_process_partial_specialization. */
9997 DECL_TEMPLATE_INSTANTIATIONS (found
)
9998 = tree_cons (arglist
, t
,
9999 DECL_TEMPLATE_INSTANTIATIONS (found
));
10001 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
10002 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
10003 /* Now that the type has been registered on the instantiations
10004 list, we set up the enumerators. Because the enumeration
10005 constants may involve the enumeration type itself, we make
10006 sure to register the type first, and then create the
10007 constants. That way, doing tsubst_expr for the enumeration
10008 constants won't result in recursive calls here; we'll find
10009 the instantiation and exit above. */
10010 tsubst_enum (template_type
, t
, arglist
);
10012 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
10013 /* If the type makes use of template parameters, the
10014 code that generates debugging information will crash. */
10015 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
10017 /* Possibly limit visibility based on template args. */
10018 TREE_PUBLIC (type_decl
) = 1;
10019 determine_visibility (type_decl
);
10021 inherit_targ_abi_tags (t
);
10027 /* Wrapper for lookup_template_class_1. */
10030 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
10031 int entering_scope
, tsubst_flags_t complain
)
10034 timevar_push (TV_TEMPLATE_INST
);
10035 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
10036 entering_scope
, complain
);
10037 timevar_pop (TV_TEMPLATE_INST
);
10041 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10044 lookup_template_variable (tree templ
, tree arglist
)
10046 if (flag_concepts
&& variable_concept_p (templ
))
10047 return build_concept_check (templ
, arglist
, tf_none
);
10049 /* The type of the expression is NULL_TREE since the template-id could refer
10050 to an explicit or partial specialization. */
10051 return build2 (TEMPLATE_ID_EXPR
, NULL_TREE
, templ
, arglist
);
10054 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10057 finish_template_variable (tree var
, tsubst_flags_t complain
)
10059 tree templ
= TREE_OPERAND (var
, 0);
10060 tree arglist
= TREE_OPERAND (var
, 1);
10062 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
10063 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
10065 templ
= most_general_template (templ
);
10066 tree parms
= DECL_TEMPLATE_PARMS (templ
);
10067 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
10069 /*use_default*/true);
10071 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
10073 if (complain
& tf_error
)
10075 auto_diagnostic_group d
;
10076 error ("use of invalid variable template %qE", var
);
10077 diagnose_constraints (location_of (var
), templ
, arglist
);
10079 return error_mark_node
;
10082 return instantiate_template (templ
, arglist
, complain
);
10085 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10086 TARGS template args, and instantiate it if it's not dependent. */
10089 lookup_and_finish_template_variable (tree templ
, tree targs
,
10090 tsubst_flags_t complain
)
10092 templ
= lookup_template_variable (templ
, targs
);
10093 if (!any_dependent_template_arguments_p (targs
))
10095 templ
= finish_template_variable (templ
, complain
);
10099 return convert_from_reference (templ
);
10103 struct pair_fn_data
10108 /* True when we should also visit template parameters that occur in
10109 non-deduced contexts. */
10110 bool include_nondeduced_p
;
10111 hash_set
<tree
> *visited
;
10114 /* Called from for_each_template_parm via walk_tree. */
10117 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
10120 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
10121 tree_fn_t fn
= pfd
->fn
;
10122 void *data
= pfd
->data
;
10123 tree result
= NULL_TREE
;
10125 #define WALK_SUBTREE(NODE) \
10128 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10129 pfd->include_nondeduced_p, \
10131 if (result) goto out; \
10135 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
10139 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
10140 WALK_SUBTREE (TYPE_CONTEXT (t
));
10142 switch (TREE_CODE (t
))
10145 if (TYPE_PTRMEMFUNC_P (t
))
10147 /* Fall through. */
10150 case ENUMERAL_TYPE
:
10151 if (!TYPE_TEMPLATE_INFO (t
))
10152 *walk_subtrees
= 0;
10154 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10158 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
10159 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
10163 /* Since we're not going to walk subtrees, we have to do this
10164 explicitly here. */
10165 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
10166 /* Fall through. */
10168 case FUNCTION_TYPE
:
10169 /* Check the return type. */
10170 WALK_SUBTREE (TREE_TYPE (t
));
10172 /* Check the parameter types. Since default arguments are not
10173 instantiated until they are needed, the TYPE_ARG_TYPES may
10174 contain expressions that involve template parameters. But,
10175 no-one should be looking at them yet. And, once they're
10176 instantiated, they don't contain template parameters, so
10177 there's no point in looking at them then, either. */
10181 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
10182 WALK_SUBTREE (TREE_VALUE (parm
));
10184 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10185 want walk_tree walking into them itself. */
10186 *walk_subtrees
= 0;
10189 if (flag_noexcept_type
)
10191 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
10193 WALK_SUBTREE (TREE_PURPOSE (spec
));
10198 case DECLTYPE_TYPE
:
10199 case UNDERLYING_TYPE
:
10200 if (pfd
->include_nondeduced_p
10201 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
10203 pfd
->include_nondeduced_p
,
10205 return error_mark_node
;
10206 *walk_subtrees
= false;
10209 case FUNCTION_DECL
:
10211 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10212 WALK_SUBTREE (DECL_TI_ARGS (t
));
10213 /* Fall through. */
10217 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
10218 WALK_SUBTREE (DECL_INITIAL (t
));
10219 if (DECL_CONTEXT (t
)
10220 && pfd
->include_nondeduced_p
)
10221 WALK_SUBTREE (DECL_CONTEXT (t
));
10224 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10225 /* Record template parameters such as `T' inside `TT<T>'. */
10226 WALK_SUBTREE (TYPE_TI_ARGS (t
));
10227 /* Fall through. */
10229 case TEMPLATE_TEMPLATE_PARM
:
10230 case TEMPLATE_TYPE_PARM
:
10231 case TEMPLATE_PARM_INDEX
:
10232 if (fn
&& (*fn
)(t
, data
))
10238 case TEMPLATE_DECL
:
10239 /* A template template parameter is encountered. */
10240 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10241 WALK_SUBTREE (TREE_TYPE (t
));
10243 /* Already substituted template template parameter */
10244 *walk_subtrees
= 0;
10247 case TYPENAME_TYPE
:
10248 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10249 partial instantiation. */
10250 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
10254 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
10255 && pfd
->include_nondeduced_p
)
10256 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
10260 case COMPONENT_REF
:
10261 /* If there's no type, then this thing must be some expression
10262 involving template parameters. */
10263 if (!fn
&& !TREE_TYPE (t
))
10264 return error_mark_node
;
10269 case IMPLICIT_CONV_EXPR
:
10270 case REINTERPRET_CAST_EXPR
:
10271 case CONST_CAST_EXPR
:
10272 case STATIC_CAST_EXPR
:
10273 case DYNAMIC_CAST_EXPR
:
10277 case PSEUDO_DTOR_EXPR
:
10279 return error_mark_node
;
10283 if (pfd
->include_nondeduced_p
)
10284 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10287 case REQUIRES_EXPR
:
10290 return error_mark_node
;
10292 /* Recursively walk the type of each constraint variable. */
10293 tree p
= TREE_OPERAND (t
, 0);
10296 WALK_SUBTREE (TREE_TYPE (p
));
10297 p
= TREE_CHAIN (p
);
10306 #undef WALK_SUBTREE
10308 /* We didn't find any template parameters we liked. */
10313 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10314 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10315 call FN with the parameter and the DATA.
10316 If FN returns nonzero, the iteration is terminated, and
10317 for_each_template_parm returns 1. Otherwise, the iteration
10318 continues. If FN never returns a nonzero value, the value
10319 returned by for_each_template_parm is 0. If FN is NULL, it is
10320 considered to be the function which always returns 1.
10322 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10323 parameters that occur in non-deduced contexts. When false, only
10324 visits those template parameters that can be deduced. */
10327 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10328 hash_set
<tree
> *visited
,
10329 bool include_nondeduced_p
,
10332 struct pair_fn_data pfd
;
10337 pfd
.any_fn
= any_fn
;
10339 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10341 /* Walk the tree. (Conceptually, we would like to walk without
10342 duplicates, but for_each_template_parm_r recursively calls
10343 for_each_template_parm, so we would need to reorganize a fair
10344 bit to use walk_tree_without_duplicates, so we keep our own
10347 pfd
.visited
= visited
;
10349 pfd
.visited
= new hash_set
<tree
>;
10350 result
= cp_walk_tree (&t
,
10351 for_each_template_parm_r
,
10358 delete pfd
.visited
;
10365 struct find_template_parameter_info
10367 explicit find_template_parameter_info (int d
)
10371 hash_set
<tree
> visited
;
10372 hash_set
<tree
> parms
;
10376 /* Appends the declaration of T to the list in DATA. */
10379 keep_template_parm (tree t
, void* data
)
10381 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10383 /* Template parameters declared within the expression are not part of
10384 the parameter mapping. For example, in this concept:
10386 template<typename T>
10387 concept C = requires { <expr> } -> same_as<int>;
10389 the return specifier same_as<int> declares a new decltype parameter
10390 that must not be part of the parameter mapping. The same is true
10391 for generic lambda parameters, lambda template parameters, etc. */
10394 template_parm_level_and_index (t
, &level
, &index
);
10395 if (level
> ftpi
->max_depth
)
10398 /* Arguments like const T yield parameters like const T. This means that
10399 a template-id like X<T, const T> would yield two distinct parameters:
10400 T and const T. Adjust types to their unqualified versions. */
10402 t
= TYPE_MAIN_VARIANT (t
);
10403 ftpi
->parms
.add (t
);
10408 /* Ensure that we recursively examine certain terms that are not normally
10409 visited in for_each_template_parm_r. */
10412 any_template_parm_r (tree t
, void *data
)
10414 find_template_parameter_info
*ftpi
= (find_template_parameter_info
*)data
;
10416 #define WALK_SUBTREE(NODE) \
10419 for_each_template_parm (NODE, keep_template_parm, data, \
10420 &ftpi->visited, true, \
10421 any_template_parm_r); \
10425 switch (TREE_CODE (t
))
10429 case ENUMERAL_TYPE
:
10430 /* Search for template parameters in type aliases. */
10431 if (tree ats
= alias_template_specialization_p (t
, nt_opaque
))
10433 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (ats
);
10434 WALK_SUBTREE (TI_ARGS (tinfo
));
10438 case TEMPLATE_TYPE_PARM
:
10439 /* Type constraints of a placeholder type may contain parameters. */
10441 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
10442 WALK_SUBTREE (constr
);
10445 case TEMPLATE_ID_EXPR
:
10446 /* Search through references to variable templates. */
10447 WALK_SUBTREE (TREE_OPERAND (t
, 0));
10448 WALK_SUBTREE (TREE_OPERAND (t
, 1));
10453 WALK_SUBTREE (TREE_TYPE (t
));
10457 /* A parameter or constraint variable may also depend on a template
10458 parameter without explicitly naming it. */
10459 WALK_SUBTREE (TREE_TYPE (t
));
10466 /* Keep walking. */
10470 /* Returns a list of unique template parameters found within T. */
10473 find_template_parameters (tree t
, int depth
)
10475 find_template_parameter_info
ftpi (depth
);
10476 for_each_template_parm (t
, keep_template_parm
, &ftpi
, &ftpi
.visited
,
10477 /*include_nondeduced*/true, any_template_parm_r
);
10478 tree list
= NULL_TREE
;
10479 for (hash_set
<tree
>::iterator iter
= ftpi
.parms
.begin();
10480 iter
!= ftpi
.parms
.end(); ++iter
)
10481 list
= tree_cons (NULL_TREE
, *iter
, list
);
10485 /* Returns true if T depends on any template parameter. */
10488 uses_template_parms (tree t
)
10490 if (t
== NULL_TREE
)
10494 int saved_processing_template_decl
;
10496 saved_processing_template_decl
= processing_template_decl
;
10497 if (!saved_processing_template_decl
)
10498 processing_template_decl
= 1;
10500 dependent_p
= dependent_type_p (t
);
10501 else if (TREE_CODE (t
) == TREE_VEC
)
10502 dependent_p
= any_dependent_template_arguments_p (t
);
10503 else if (TREE_CODE (t
) == TREE_LIST
)
10504 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10505 || uses_template_parms (TREE_CHAIN (t
)));
10506 else if (TREE_CODE (t
) == TYPE_DECL
)
10507 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10508 else if (DECL_P (t
)
10510 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10511 || TREE_CODE (t
) == OVERLOAD
10513 || identifier_p (t
)
10514 || TREE_CODE (t
) == TRAIT_EXPR
10515 || TREE_CODE (t
) == CONSTRUCTOR
10516 || CONSTANT_CLASS_P (t
))
10517 dependent_p
= (type_dependent_expression_p (t
)
10518 || value_dependent_expression_p (t
));
10521 gcc_assert (t
== error_mark_node
);
10522 dependent_p
= false;
10525 processing_template_decl
= saved_processing_template_decl
;
10527 return dependent_p
;
10530 /* Returns true iff current_function_decl is an incompletely instantiated
10531 template. Useful instead of processing_template_decl because the latter
10532 is set to 0 during instantiate_non_dependent_expr. */
10535 in_template_function (void)
10537 tree fn
= current_function_decl
;
10539 ++processing_template_decl
;
10540 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10541 && DECL_TEMPLATE_INFO (fn
)
10542 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10543 --processing_template_decl
;
10547 /* Returns true if T depends on any template parameter with level LEVEL. */
10550 uses_template_parms_level (tree t
, int level
)
10552 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10553 /*include_nondeduced_p=*/true);
10556 /* Returns true if the signature of DECL depends on any template parameter from
10557 its enclosing class. */
10560 uses_outer_template_parms (tree decl
)
10562 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10565 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10566 &depth
, NULL
, /*include_nondeduced_p=*/true))
10568 if (PRIMARY_TEMPLATE_P (decl
)
10569 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10570 (DECL_TEMPLATE_PARMS (decl
)),
10571 template_parm_outer_level
,
10572 &depth
, NULL
, /*include_nondeduced_p=*/true))
10574 tree ci
= get_constraints (decl
);
10576 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10577 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10578 &depth
, NULL
, /*nondeduced*/true))
10583 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10584 ill-formed translation unit, i.e. a variable or function that isn't
10585 usable in a constant expression. */
10588 neglectable_inst_p (tree d
)
10590 return (d
&& DECL_P (d
)
10591 && !undeduced_auto_decl (d
)
10592 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10593 : decl_maybe_constant_var_p (d
)));
10596 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10597 neglectable and instantiated from within an erroneous instantiation. */
10600 limit_bad_template_recursion (tree decl
)
10602 struct tinst_level
*lev
= current_tinst_level
;
10603 int errs
= errorcount
+ sorrycount
;
10604 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10607 for (; lev
; lev
= lev
->next
)
10608 if (neglectable_inst_p (lev
->maybe_get_node ()))
10611 return (lev
&& errs
> lev
->errors
);
10614 static int tinst_depth
;
10615 extern int max_tinst_depth
;
10618 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10620 /* We're starting to instantiate D; record the template instantiation context
10621 at LOC for diagnostics and to restore it later. */
10624 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10626 struct tinst_level
*new_level
;
10628 if (tinst_depth
>= max_tinst_depth
)
10630 /* Tell error.c not to try to instantiate any templates. */
10632 fatal_error (input_location
,
10633 "template instantiation depth exceeds maximum of %d"
10634 " (use %<-ftemplate-depth=%> to increase the maximum)",
10639 /* If the current instantiation caused problems, don't let it instantiate
10640 anything else. Do allow deduction substitution and decls usable in
10641 constant expressions. */
10642 if (!targs
&& limit_bad_template_recursion (tldcl
))
10644 /* Avoid no_linkage_errors and unused function warnings for this
10646 TREE_NO_WARNING (tldcl
) = 1;
10650 /* When not -quiet, dump template instantiations other than functions, since
10651 announce_function will take care of those. */
10652 if (!quiet_flag
&& !targs
10653 && TREE_CODE (tldcl
) != TREE_LIST
10654 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10655 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10657 new_level
= tinst_level_freelist ().alloc ();
10658 new_level
->tldcl
= tldcl
;
10659 new_level
->targs
= targs
;
10660 new_level
->locus
= loc
;
10661 new_level
->errors
= errorcount
+ sorrycount
;
10662 new_level
->next
= NULL
;
10663 new_level
->refcount
= 0;
10664 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10665 set_refcount_ptr (current_tinst_level
, new_level
);
10668 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10669 depth_reached
= tinst_depth
;
10674 /* We're starting substitution of TMPL<ARGS>; record the template
10675 substitution context for diagnostics and to restore it later. */
10678 push_tinst_level (tree tmpl
, tree args
)
10680 return push_tinst_level_loc (tmpl
, args
, input_location
);
10683 /* We're starting to instantiate D; record INPUT_LOCATION and the
10684 template instantiation context for diagnostics and to restore it
10688 push_tinst_level (tree d
)
10690 return push_tinst_level_loc (d
, input_location
);
10693 /* Likewise, but record LOC as the program location. */
10696 push_tinst_level_loc (tree d
, location_t loc
)
10698 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10699 return push_tinst_level_loc (d
, NULL
, loc
);
10702 /* We're done instantiating this template; return to the instantiation
10706 pop_tinst_level (void)
10708 /* Restore the filename and line number stashed away when we started
10709 this instantiation. */
10710 input_location
= current_tinst_level
->locus
;
10711 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10715 /* We're instantiating a deferred template; restore the template
10716 instantiation context in which the instantiation was requested, which
10717 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10720 reopen_tinst_level (struct tinst_level
*level
)
10722 struct tinst_level
*t
;
10725 for (t
= level
; t
; t
= t
->next
)
10728 set_refcount_ptr (current_tinst_level
, level
);
10729 pop_tinst_level ();
10730 if (current_tinst_level
)
10731 current_tinst_level
->errors
= errorcount
+sorrycount
;
10732 return level
->maybe_get_node ();
10735 /* Returns the TINST_LEVEL which gives the original instantiation
10738 struct tinst_level
*
10739 outermost_tinst_level (void)
10741 struct tinst_level
*level
= current_tinst_level
;
10743 while (level
->next
)
10744 level
= level
->next
;
10748 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10749 vector of template arguments, as for tsubst.
10751 Returns an appropriate tsubst'd friend declaration. */
10754 tsubst_friend_function (tree decl
, tree args
)
10758 if (TREE_CODE (decl
) == FUNCTION_DECL
10759 && DECL_TEMPLATE_INSTANTIATION (decl
)
10760 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10761 /* This was a friend declared with an explicit template
10762 argument list, e.g.:
10764 friend void f<>(T);
10766 to indicate that f was a template instantiation, not a new
10767 function declaration. Now, we have to figure out what
10768 instantiation of what template. */
10770 tree template_id
, arglist
, fns
;
10773 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10775 /* Friend functions are looked up in the containing namespace scope.
10776 We must enter that scope, to avoid finding member functions of the
10777 current class with same name. */
10778 push_nested_namespace (ns
);
10779 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10780 tf_warning_or_error
, NULL_TREE
,
10781 /*integral_constant_expression_p=*/false);
10782 pop_nested_namespace (ns
);
10783 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10784 tf_warning_or_error
, NULL_TREE
);
10785 template_id
= lookup_template_function (fns
, arglist
);
10787 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10788 tmpl
= determine_specialization (template_id
, new_friend
,
10790 /*need_member_template=*/0,
10791 TREE_VEC_LENGTH (args
),
10793 return instantiate_template (tmpl
, new_args
, tf_error
);
10796 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10798 /* The NEW_FRIEND will look like an instantiation, to the
10799 compiler, but is not an instantiation from the point of view of
10800 the language. For example, we might have had:
10802 template <class T> struct S {
10803 template <class U> friend void f(T, U);
10806 Then, in S<int>, template <class U> void f(int, U) is not an
10807 instantiation of anything. */
10808 if (new_friend
== error_mark_node
)
10809 return error_mark_node
;
10811 DECL_USE_TEMPLATE (new_friend
) = 0;
10812 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10814 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10815 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10816 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10818 /* Attach the template requirements to the new declaration
10819 for declaration matching. We need to rebuild the requirements
10820 so that parameter levels match. */
10821 if (tree ci
= get_constraints (decl
))
10823 tree parms
= DECL_TEMPLATE_PARMS (new_friend
);
10824 tree args
= generic_targs_for (new_friend
);
10825 tree treqs
= tsubst_constraint (CI_TEMPLATE_REQS (ci
), args
,
10826 tf_warning_or_error
, NULL_TREE
);
10827 tree freqs
= tsubst_constraint (CI_DECLARATOR_REQS (ci
), args
,
10828 tf_warning_or_error
, NULL_TREE
);
10830 /* Update the constraints -- these won't really be valid for
10831 checking, but that's not what we need them for. These ensure
10832 that the declared function can find the friend during
10833 declaration matching. */
10834 tree new_ci
= get_constraints (new_friend
);
10835 CI_TEMPLATE_REQS (new_ci
) = treqs
;
10836 CI_DECLARATOR_REQS (new_ci
) = freqs
;
10838 /* Also update the template parameter list. */
10839 TEMPLATE_PARMS_CONSTRAINTS (parms
) = treqs
;
10843 /* The mangled name for the NEW_FRIEND is incorrect. The function
10844 is not a template instantiation and should not be mangled like
10845 one. Therefore, we forget the mangling here; we'll recompute it
10846 later if we need it. */
10847 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10849 SET_DECL_RTL (new_friend
, NULL
);
10850 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10853 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10856 tree new_friend_template_info
;
10857 tree new_friend_result_template_info
;
10859 int new_friend_is_defn
;
10861 /* We must save some information from NEW_FRIEND before calling
10862 duplicate decls since that function will free NEW_FRIEND if
10864 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10865 new_friend_is_defn
=
10866 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10867 (template_for_substitution (new_friend
)))
10869 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10871 /* This declaration is a `primary' template. */
10872 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10874 new_friend_result_template_info
10875 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10878 new_friend_result_template_info
= NULL_TREE
;
10880 /* Inside pushdecl_namespace_level, we will push into the
10881 current namespace. However, the friend function should go
10882 into the namespace of the template. */
10883 ns
= decl_namespace_context (new_friend
);
10884 push_nested_namespace (ns
);
10885 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10886 pop_nested_namespace (ns
);
10888 if (old_decl
== error_mark_node
)
10889 return error_mark_node
;
10891 if (old_decl
!= new_friend
)
10893 /* This new friend declaration matched an existing
10894 declaration. For example, given:
10896 template <class T> void f(T);
10897 template <class U> class C {
10898 template <class T> friend void f(T) {}
10901 the friend declaration actually provides the definition
10902 of `f', once C has been instantiated for some type. So,
10903 old_decl will be the out-of-class template declaration,
10904 while new_friend is the in-class definition.
10906 But, if `f' was called before this point, the
10907 instantiation of `f' will have DECL_TI_ARGS corresponding
10908 to `T' but not to `U', references to which might appear
10909 in the definition of `f'. Previously, the most general
10910 template for an instantiation of `f' was the out-of-class
10911 version; now it is the in-class version. Therefore, we
10912 run through all specialization of `f', adding to their
10913 DECL_TI_ARGS appropriately. In particular, they need a
10914 new set of outer arguments, corresponding to the
10915 arguments for this class instantiation.
10917 The same situation can arise with something like this:
10919 friend void f(int);
10920 template <class T> class C {
10921 friend void f(T) {}
10924 when `C<int>' is instantiated. Now, `f(int)' is defined
10927 if (!new_friend_is_defn
)
10928 /* On the other hand, if the in-class declaration does
10929 *not* provide a definition, then we don't want to alter
10930 existing definitions. We can just leave everything
10935 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10936 tree new_args
= TI_ARGS (new_friend_template_info
);
10938 /* Overwrite whatever template info was there before, if
10939 any, with the new template information pertaining to
10940 the declaration. */
10941 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10943 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10945 /* We should have called reregister_specialization in
10946 duplicate_decls. */
10947 gcc_assert (retrieve_specialization (new_template
,
10951 /* Instantiate it if the global has already been used. */
10952 if (DECL_ODR_USED (old_decl
))
10953 instantiate_decl (old_decl
, /*defer_ok=*/true,
10954 /*expl_inst_class_mem_p=*/false);
10960 /* Indicate that the old function template is a partial
10962 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10963 = new_friend_result_template_info
;
10965 gcc_assert (new_template
10966 == most_general_template (new_template
));
10967 gcc_assert (new_template
!= old_decl
);
10969 /* Reassign any specializations already in the hash table
10970 to the new more general template, and add the
10971 additional template args. */
10972 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10974 t
= TREE_CHAIN (t
))
10976 tree spec
= TREE_VALUE (t
);
10979 elt
.tmpl
= old_decl
;
10980 elt
.args
= DECL_TI_ARGS (spec
);
10981 elt
.spec
= NULL_TREE
;
10983 decl_specializations
->remove_elt (&elt
);
10985 DECL_TI_ARGS (spec
)
10986 = add_outermost_template_args (new_args
,
10987 DECL_TI_ARGS (spec
));
10989 register_specialization
10990 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10993 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10997 /* The information from NEW_FRIEND has been merged into OLD_DECL
10998 by duplicate_decls. */
10999 new_friend
= old_decl
;
11004 tree context
= DECL_CONTEXT (new_friend
);
11008 template <class T> class C {
11009 template <class U> friend void C1<U>::f (); // case 1
11010 friend void C2<T>::f (); // case 2
11012 we only need to make sure CONTEXT is a complete type for
11013 case 2. To distinguish between the two cases, we note that
11014 CONTEXT of case 1 remains dependent type after tsubst while
11015 this isn't true for case 2. */
11016 ++processing_template_decl
;
11017 dependent_p
= dependent_type_p (context
);
11018 --processing_template_decl
;
11021 && !complete_type_or_else (context
, NULL_TREE
))
11022 return error_mark_node
;
11024 if (COMPLETE_TYPE_P (context
))
11026 tree fn
= new_friend
;
11027 /* do_friend adds the TEMPLATE_DECL for any member friend
11028 template even if it isn't a member template, i.e.
11029 template <class T> friend A<T>::f();
11030 Look through it in that case. */
11031 if (TREE_CODE (fn
) == TEMPLATE_DECL
11032 && !PRIMARY_TEMPLATE_P (fn
))
11033 fn
= DECL_TEMPLATE_RESULT (fn
);
11034 /* Check to see that the declaration is really present, and,
11035 possibly obtain an improved declaration. */
11036 fn
= check_classfn (context
, fn
, NULL_TREE
);
11046 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11047 template arguments, as for tsubst.
11049 Returns an appropriate tsubst'd friend type or error_mark_node on
11053 tsubst_friend_class (tree friend_tmpl
, tree args
)
11057 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
11059 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
11060 return TREE_TYPE (tmpl
);
11063 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
11064 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11065 push_nested_namespace (context
);
11068 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
11069 push_nested_class (context
);
11072 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
11073 /*non_class=*/false, /*block_p=*/false,
11074 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
11076 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
11078 /* The friend template has already been declared. Just
11079 check to see that the declarations match, and install any new
11080 default parameters. We must tsubst the default parameters,
11081 of course. We only need the innermost template parameters
11082 because that is all that redeclare_class_template will look
11084 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
11085 > TMPL_ARGS_DEPTH (args
))
11087 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
11088 args
, tf_warning_or_error
);
11089 location_t saved_input_location
= input_location
;
11090 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
11091 tree cons
= get_constraints (tmpl
);
11092 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
11093 input_location
= saved_input_location
;
11098 /* The friend template has not already been declared. In this
11099 case, the instantiation of the template class will cause the
11100 injection of this template into the namespace scope. */
11101 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
11103 if (tmpl
!= error_mark_node
)
11105 /* The new TMPL is not an instantiation of anything, so we
11106 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11107 for the new type because that is supposed to be the
11108 corresponding template decl, i.e., TMPL. */
11109 DECL_USE_TEMPLATE (tmpl
) = 0;
11110 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
11111 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
11112 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
11113 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
11115 /* It is hidden. */
11116 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
11117 DECL_ANTICIPATED (tmpl
)
11118 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
11120 /* Inject this template into the enclosing namspace scope. */
11121 tmpl
= pushdecl_namespace_level (tmpl
, true);
11125 if (TREE_CODE (context
) == NAMESPACE_DECL
)
11126 pop_nested_namespace (context
);
11128 pop_nested_class ();
11130 return TREE_TYPE (tmpl
);
11133 /* Returns zero if TYPE cannot be completed later due to circularity.
11134 Otherwise returns one. */
11137 can_complete_type_without_circularity (tree type
)
11139 if (type
== NULL_TREE
|| type
== error_mark_node
)
11141 else if (COMPLETE_TYPE_P (type
))
11143 else if (TREE_CODE (type
) == ARRAY_TYPE
)
11144 return can_complete_type_without_circularity (TREE_TYPE (type
));
11145 else if (CLASS_TYPE_P (type
)
11146 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
11152 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
11153 tsubst_flags_t
, tree
);
11155 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11156 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11159 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
11160 tsubst_flags_t complain
, tree in_decl
)
11162 gcc_assert (ATTR_IS_DEPENDENT (t
));
11164 tree val
= TREE_VALUE (t
);
11165 if (val
== NULL_TREE
)
11166 /* Nothing to do. */;
11167 else if ((flag_openmp
|| flag_openmp_simd
)
11168 && is_attribute_p ("omp declare simd",
11169 get_attribute_name (t
)))
11171 tree clauses
= TREE_VALUE (val
);
11172 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
11173 complain
, in_decl
);
11174 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11175 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11176 tree parms
= DECL_ARGUMENTS (*decl_p
);
11178 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
11180 val
= build_tree_list (NULL_TREE
, clauses
);
11184 else if (flag_openmp
11185 && is_attribute_p ("omp declare variant base",
11186 get_attribute_name (t
)))
11188 ++cp_unevaluated_operand
;
11190 = tsubst_expr (TREE_PURPOSE (val
), args
, complain
,
11191 in_decl
, /*integral_constant_expression_p=*/false);
11192 --cp_unevaluated_operand
;
11193 tree chain
= TREE_CHAIN (val
);
11194 location_t match_loc
= cp_expr_loc_or_input_loc (TREE_PURPOSE (chain
));
11195 tree ctx
= copy_list (TREE_VALUE (val
));
11196 tree simd
= get_identifier ("simd");
11197 tree score
= get_identifier (" score");
11198 tree condition
= get_identifier ("condition");
11199 for (tree t1
= ctx
; t1
; t1
= TREE_CHAIN (t1
))
11201 const char *set
= IDENTIFIER_POINTER (TREE_PURPOSE (t1
));
11202 TREE_VALUE (t1
) = copy_list (TREE_VALUE (t1
));
11203 for (tree t2
= TREE_VALUE (t1
); t2
; t2
= TREE_CHAIN (t2
))
11205 if (TREE_PURPOSE (t2
) == simd
&& set
[0] == 'c')
11207 tree clauses
= TREE_VALUE (t2
);
11208 clauses
= tsubst_omp_clauses (clauses
,
11209 C_ORT_OMP_DECLARE_SIMD
, args
,
11210 complain
, in_decl
);
11211 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
11212 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
11213 TREE_VALUE (t2
) = clauses
;
11217 TREE_VALUE (t2
) = copy_list (TREE_VALUE (t2
));
11218 for (tree t3
= TREE_VALUE (t2
); t3
; t3
= TREE_CHAIN (t3
))
11219 if (TREE_VALUE (t3
))
11222 = ((TREE_PURPOSE (t2
) != condition
|| set
[0] != 'u')
11223 && TREE_PURPOSE (t3
) != score
);
11224 tree v
= TREE_VALUE (t3
);
11225 if (TREE_CODE (v
) == STRING_CST
&& allow_string
)
11227 v
= tsubst_expr (v
, args
, complain
, in_decl
, true);
11228 v
= fold_non_dependent_expr (v
);
11229 if (!INTEGRAL_TYPE_P (TREE_TYPE (v
))
11230 || (TREE_PURPOSE (t3
) == score
11231 ? TREE_CODE (v
) != INTEGER_CST
11232 : !tree_fits_shwi_p (v
)))
11235 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11237 if (TREE_PURPOSE (t3
) == score
)
11238 error_at (loc
, "score argument must be "
11239 "constant integer expression");
11240 else if (allow_string
)
11241 error_at (loc
, "property must be constant "
11242 "integer expression or string "
11245 error_at (loc
, "property must be constant "
11246 "integer expression");
11249 else if (TREE_PURPOSE (t3
) == score
11250 && tree_int_cst_sgn (v
) < 0)
11253 = cp_expr_loc_or_loc (TREE_VALUE (t3
),
11255 error_at (loc
, "score argument must be "
11259 TREE_VALUE (t3
) = v
;
11264 val
= tree_cons (varid
, ctx
, chain
);
11266 /* If the first attribute argument is an identifier, don't
11267 pass it through tsubst. Attributes like mode, format,
11268 cleanup and several target specific attributes expect it
11270 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
11273 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
11274 /*integral_constant_expression_p=*/false);
11275 if (chain
!= TREE_CHAIN (val
))
11276 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
11278 else if (PACK_EXPANSION_P (val
))
11280 /* An attribute pack expansion. */
11281 tree purp
= TREE_PURPOSE (t
);
11282 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
11283 if (pack
== error_mark_node
)
11284 return error_mark_node
;
11285 int len
= TREE_VEC_LENGTH (pack
);
11286 tree list
= NULL_TREE
;
11288 for (int i
= 0; i
< len
; ++i
)
11290 tree elt
= TREE_VEC_ELT (pack
, i
);
11291 *q
= build_tree_list (purp
, elt
);
11292 q
= &TREE_CHAIN (*q
);
11297 val
= tsubst_expr (val
, args
, complain
, in_decl
,
11298 /*integral_constant_expression_p=*/false);
11300 if (val
!= TREE_VALUE (t
))
11301 return build_tree_list (TREE_PURPOSE (t
), val
);
11305 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11306 unchanged or a new TREE_LIST chain. */
11309 tsubst_attributes (tree attributes
, tree args
,
11310 tsubst_flags_t complain
, tree in_decl
)
11312 tree last_dep
= NULL_TREE
;
11314 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
11315 if (ATTR_IS_DEPENDENT (t
))
11318 attributes
= copy_list (attributes
);
11323 for (tree
*p
= &attributes
; *p
; )
11326 if (ATTR_IS_DEPENDENT (t
))
11328 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
11333 p
= &TREE_CHAIN (*p
);
11334 *p
= TREE_CHAIN (t
);
11338 p
= &TREE_CHAIN (*p
);
11344 /* Apply any attributes which had to be deferred until instantiation
11345 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11346 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11349 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
11350 tree args
, tsubst_flags_t complain
, tree in_decl
)
11352 tree last_dep
= NULL_TREE
;
11356 if (attributes
== NULL_TREE
)
11359 if (DECL_P (*decl_p
))
11361 if (TREE_TYPE (*decl_p
) == error_mark_node
)
11363 p
= &DECL_ATTRIBUTES (*decl_p
);
11364 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11365 to our attributes parameter. */
11366 gcc_assert (*p
== attributes
);
11370 p
= &TYPE_ATTRIBUTES (*decl_p
);
11371 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11372 lookup_template_class_1, and should be preserved. */
11373 gcc_assert (*p
!= attributes
);
11375 p
= &TREE_CHAIN (*p
);
11378 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
11379 if (ATTR_IS_DEPENDENT (t
))
11382 attributes
= copy_list (attributes
);
11389 tree late_attrs
= NULL_TREE
;
11390 tree
*q
= &late_attrs
;
11395 if (ATTR_IS_DEPENDENT (t
))
11397 *p
= TREE_CHAIN (t
);
11398 TREE_CHAIN (t
) = NULL_TREE
;
11399 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
11401 q
= &TREE_CHAIN (*q
);
11404 p
= &TREE_CHAIN (t
);
11407 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
11411 /* Perform (or defer) access check for typedefs that were referenced
11412 from within the template TMPL code.
11413 This is a subroutine of instantiate_decl and instantiate_class_template.
11414 TMPL is the template to consider and TARGS is the list of arguments of
11418 perform_typedefs_access_check (tree tmpl
, tree targs
)
11421 qualified_typedef_usage_t
*iter
;
11424 || (!CLASS_TYPE_P (tmpl
)
11425 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
11428 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
11430 tree type_decl
= iter
->typedef_decl
;
11431 tree type_scope
= iter
->context
;
11433 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
11436 if (uses_template_parms (type_decl
))
11437 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
11438 if (uses_template_parms (type_scope
))
11439 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
11441 /* Make access check error messages point to the location
11442 of the use of the typedef. */
11443 iloc_sentinel
ils (iter
->locus
);
11444 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
11445 type_decl
, type_decl
,
11446 tf_warning_or_error
);
11451 instantiate_class_template_1 (tree type
)
11453 tree templ
, args
, pattern
, t
, member
;
11457 unsigned int saved_maximum_field_alignment
;
11460 if (type
== error_mark_node
)
11461 return error_mark_node
;
11463 if (COMPLETE_OR_OPEN_TYPE_P (type
)
11464 || uses_template_parms (type
))
11467 /* Figure out which template is being instantiated. */
11468 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
11469 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
11471 /* Mark the type as in the process of being defined. */
11472 TYPE_BEING_DEFINED (type
) = 1;
11474 /* We may be in the middle of deferred access check. Disable
11476 deferring_access_check_sentinel
acs (dk_no_deferred
);
11478 /* Determine what specialization of the original template to
11480 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
11481 if (t
== error_mark_node
)
11482 return error_mark_node
;
11485 /* This TYPE is actually an instantiation of a partial
11486 specialization. We replace the innermost set of ARGS with
11487 the arguments appropriate for substitution. For example,
11490 template <class T> struct S {};
11491 template <class T> struct S<T*> {};
11493 and supposing that we are instantiating S<int*>, ARGS will
11494 presently be {int*} -- but we need {int}. */
11495 pattern
= TREE_TYPE (t
);
11496 args
= TREE_PURPOSE (t
);
11500 pattern
= TREE_TYPE (templ
);
11501 args
= CLASSTYPE_TI_ARGS (type
);
11504 /* If the template we're instantiating is incomplete, then clearly
11505 there's nothing we can do. */
11506 if (!COMPLETE_TYPE_P (pattern
))
11508 /* We can try again later. */
11509 TYPE_BEING_DEFINED (type
) = 0;
11513 /* If we've recursively instantiated too many templates, stop. */
11514 if (! push_tinst_level (type
))
11517 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11518 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11520 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11521 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11522 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11523 fn_context
= error_mark_node
;
11525 push_to_top_level ();
11528 cp_unevaluated_operand
= 0;
11529 c_inhibit_evaluation_warnings
= 0;
11531 /* Use #pragma pack from the template context. */
11532 saved_maximum_field_alignment
= maximum_field_alignment
;
11533 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11535 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11537 /* Set the input location to the most specialized template definition.
11538 This is needed if tsubsting causes an error. */
11539 typedecl
= TYPE_MAIN_DECL (pattern
);
11540 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11541 DECL_SOURCE_LOCATION (typedecl
);
11543 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11544 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11545 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11546 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11547 if (ANON_AGGR_TYPE_P (pattern
))
11548 SET_ANON_AGGR_TYPE_P (type
);
11549 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11551 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11552 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11553 /* Adjust visibility for template arguments. */
11554 determine_visibility (TYPE_MAIN_DECL (type
));
11556 if (CLASS_TYPE_P (type
))
11557 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11559 pbinfo
= TYPE_BINFO (pattern
);
11561 /* We should never instantiate a nested class before its enclosing
11562 class; we need to look up the nested class by name before we can
11563 instantiate it, and that lookup should instantiate the enclosing
11565 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11566 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11568 base_list
= NULL_TREE
;
11569 if (BINFO_N_BASE_BINFOS (pbinfo
))
11575 /* We must enter the scope containing the type, as that is where
11576 the accessibility of types named in dependent bases are
11578 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11580 /* Substitute into each of the bases to determine the actual
11582 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11585 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11586 tree expanded_bases
= NULL_TREE
;
11589 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11592 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11593 args
, tf_error
, NULL_TREE
);
11594 if (expanded_bases
== error_mark_node
)
11597 len
= TREE_VEC_LENGTH (expanded_bases
);
11600 for (idx
= 0; idx
< len
; idx
++)
11602 if (expanded_bases
)
11603 /* Extract the already-expanded base class. */
11604 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11606 /* Substitute to figure out the base class. */
11607 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11610 if (base
== error_mark_node
)
11613 base_list
= tree_cons (access
, base
, base_list
);
11614 if (BINFO_VIRTUAL_P (pbase_binfo
))
11615 TREE_TYPE (base_list
) = integer_type_node
;
11619 /* The list is now in reverse order; correct that. */
11620 base_list
= nreverse (base_list
);
11623 pop_scope (pushed_scope
);
11625 /* Now call xref_basetypes to set up all the base-class
11627 xref_basetypes (type
, base_list
);
11629 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11630 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11631 args
, tf_error
, NULL_TREE
);
11632 fixup_attribute_variants (type
);
11634 /* Now that our base classes are set up, enter the scope of the
11635 class, so that name lookups into base classes, etc. will work
11636 correctly. This is precisely analogous to what we do in
11637 begin_class_definition when defining an ordinary non-template
11638 class, except we also need to push the enclosing classes. */
11639 push_nested_class (type
);
11641 /* Now members are processed in the order of declaration. */
11642 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11643 member
; member
= TREE_CHAIN (member
))
11645 tree t
= TREE_VALUE (member
);
11647 if (TREE_PURPOSE (member
))
11651 if (LAMBDA_TYPE_P (t
))
11652 /* A closure type for a lambda in an NSDMI or default argument.
11653 Ignore it; it will be regenerated when needed. */
11656 /* Build new CLASSTYPE_NESTED_UTDS. */
11659 bool class_template_p
;
11661 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11662 && TYPE_LANG_SPECIFIC (t
)
11663 && CLASSTYPE_IS_TEMPLATE (t
));
11664 /* If the member is a class template, then -- even after
11665 substitution -- there may be dependent types in the
11666 template argument list for the class. We increment
11667 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11668 that function will assume that no types are dependent
11669 when outside of a template. */
11670 if (class_template_p
)
11671 ++processing_template_decl
;
11672 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11673 if (class_template_p
)
11674 --processing_template_decl
;
11675 if (newtag
== error_mark_node
)
11678 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11680 tree name
= TYPE_IDENTIFIER (t
);
11682 if (class_template_p
)
11683 /* Unfortunately, lookup_template_class sets
11684 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11685 instantiation (i.e., for the type of a member
11686 template class nested within a template class.)
11687 This behavior is required for
11688 maybe_process_partial_specialization to work
11689 correctly, but is not accurate in this case;
11690 the TAG is not an instantiation of anything.
11691 (The corresponding TEMPLATE_DECL is an
11692 instantiation, but the TYPE is not.) */
11693 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11695 /* Now, we call pushtag to put this NEWTAG into the scope of
11696 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11697 pushtag calling push_template_decl. We don't have to do
11698 this for enums because it will already have been done in
11701 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11702 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11705 else if (DECL_DECLARES_FUNCTION_P (t
))
11709 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11710 ++processing_template_decl
;
11711 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11712 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11713 --processing_template_decl
;
11714 set_current_access_from_decl (r
);
11715 finish_member_declaration (r
);
11716 /* Instantiate members marked with attribute used. */
11717 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11719 if (TREE_CODE (r
) == FUNCTION_DECL
11720 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11721 cp_check_omp_declare_reduction (r
);
11723 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11724 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11725 /* A closure type for a lambda in an NSDMI or default argument.
11726 Ignore it; it will be regenerated when needed. */;
11729 /* Build new TYPE_FIELDS. */
11730 if (TREE_CODE (t
) == STATIC_ASSERT
)
11734 ++c_inhibit_evaluation_warnings
;
11736 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11737 tf_warning_or_error
, NULL_TREE
,
11738 /*integral_constant_expression_p=*/true);
11739 --c_inhibit_evaluation_warnings
;
11741 finish_static_assert (condition
,
11742 STATIC_ASSERT_MESSAGE (t
),
11743 STATIC_ASSERT_SOURCE_LOCATION (t
),
11744 /*member_p=*/true);
11746 else if (TREE_CODE (t
) != CONST_DECL
)
11749 tree vec
= NULL_TREE
;
11752 /* The file and line for this declaration, to
11753 assist in error message reporting. Since we
11754 called push_tinst_level above, we don't need to
11756 input_location
= DECL_SOURCE_LOCATION (t
);
11758 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11759 ++processing_template_decl
;
11760 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11761 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11762 --processing_template_decl
;
11764 if (TREE_CODE (r
) == TREE_VEC
)
11766 /* A capture pack became multiple fields. */
11768 len
= TREE_VEC_LENGTH (vec
);
11771 for (int i
= 0; i
< len
; ++i
)
11774 r
= TREE_VEC_ELT (vec
, i
);
11779 [t]he initialization (and any associated
11780 side-effects) of a static data member does
11781 not occur unless the static data member is
11782 itself used in a way that requires the
11783 definition of the static data member to
11786 Therefore, we do not substitute into the
11787 initialized for the static data member here. */
11788 finish_static_data_member_decl
11790 /*init=*/NULL_TREE
,
11791 /*init_const_expr_p=*/false,
11792 /*asmspec_tree=*/NULL_TREE
,
11794 /* Instantiate members marked with attribute used. */
11795 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11798 else if (TREE_CODE (r
) == FIELD_DECL
)
11800 /* Determine whether R has a valid type and can be
11801 completed later. If R is invalid, then its type
11802 is replaced by error_mark_node. */
11803 tree rtype
= TREE_TYPE (r
);
11804 if (can_complete_type_without_circularity (rtype
))
11805 complete_type (rtype
);
11807 if (!complete_or_array_type_p (rtype
))
11809 /* If R's type couldn't be completed and
11810 it isn't a flexible array member (whose
11811 type is incomplete by definition) give
11813 cxx_incomplete_type_error (r
, rtype
);
11814 TREE_TYPE (r
) = error_mark_node
;
11816 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11817 && TYPE_DOMAIN (rtype
) == NULL_TREE
11818 && (TREE_CODE (type
) == UNION_TYPE
11819 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11821 error ("flexible array member %qD in union", r
);
11822 TREE_TYPE (r
) = error_mark_node
;
11824 else if (!verify_type_context (input_location
,
11825 TCTX_FIELD
, rtype
))
11826 TREE_TYPE (r
) = error_mark_node
;
11829 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11830 such a thing will already have been added to the field
11831 list by tsubst_enum in finish_member_declaration in the
11832 CLASSTYPE_NESTED_UTDS case above. */
11833 if (!(TREE_CODE (r
) == TYPE_DECL
11834 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11835 && DECL_ARTIFICIAL (r
)))
11837 set_current_access_from_decl (r
);
11838 finish_member_declaration (r
);
11846 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11847 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11849 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11851 tree friend_type
= t
;
11852 bool adjust_processing_template_decl
= false;
11854 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11856 /* template <class T> friend class C; */
11857 friend_type
= tsubst_friend_class (friend_type
, args
);
11858 adjust_processing_template_decl
= true;
11860 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11862 /* template <class T> friend class C::D; */
11863 friend_type
= tsubst (friend_type
, args
,
11864 tf_warning_or_error
, NULL_TREE
);
11865 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11866 friend_type
= TREE_TYPE (friend_type
);
11867 adjust_processing_template_decl
= true;
11869 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11870 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11872 /* This could be either
11876 when dependent_type_p is false or
11878 template <class U> friend class T::C;
11881 /* Bump processing_template_decl in case this is something like
11882 template <class T> friend struct A<T>::B. */
11883 ++processing_template_decl
;
11884 friend_type
= tsubst (friend_type
, args
,
11885 tf_warning_or_error
, NULL_TREE
);
11886 if (dependent_type_p (friend_type
))
11887 adjust_processing_template_decl
= true;
11888 --processing_template_decl
;
11890 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11891 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11892 && TYPE_HIDDEN_P (friend_type
))
11896 where C hasn't been declared yet. Let's lookup name
11897 from namespace scope directly, bypassing any name that
11898 come from dependent base class. */
11899 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11901 /* The call to xref_tag_from_type does injection for friend
11903 push_nested_namespace (ns
);
11905 xref_tag_from_type (friend_type
, NULL_TREE
,
11906 /*tag_scope=*/ts_current
);
11907 pop_nested_namespace (ns
);
11909 else if (uses_template_parms (friend_type
))
11910 /* friend class C<T>; */
11911 friend_type
= tsubst (friend_type
, args
,
11912 tf_warning_or_error
, NULL_TREE
);
11917 where C is already declared or
11919 friend class C<int>;
11921 We don't have to do anything in these cases. */
11923 if (adjust_processing_template_decl
)
11924 /* Trick make_friend_class into realizing that the friend
11925 we're adding is a template, not an ordinary class. It's
11926 important that we use make_friend_class since it will
11927 perform some error-checking and output cross-reference
11929 ++processing_template_decl
;
11931 if (friend_type
!= error_mark_node
)
11932 make_friend_class (type
, friend_type
, /*complain=*/false);
11934 if (adjust_processing_template_decl
)
11935 --processing_template_decl
;
11939 /* Build new DECL_FRIENDLIST. */
11942 /* The file and line for this declaration, to
11943 assist in error message reporting. Since we
11944 called push_tinst_level above, we don't need to
11946 input_location
= DECL_SOURCE_LOCATION (t
);
11948 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11950 ++processing_template_decl
;
11951 push_deferring_access_checks (dk_no_check
);
11954 r
= tsubst_friend_function (t
, args
);
11955 add_friend (type
, r
, /*complain=*/false);
11956 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11958 pop_deferring_access_checks ();
11959 --processing_template_decl
;
11967 /* Restore these before substituting into the lambda capture
11969 cp_unevaluated_operand
= saved_unevaluated_operand
;
11970 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11973 /* Set the file and line number information to whatever is given for
11974 the class itself. This puts error messages involving generated
11975 implicit functions at a predictable point, and the same point
11976 that would be used for non-template classes. */
11977 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11979 unreverse_member_declarations (type
);
11980 finish_struct_1 (type
);
11981 TYPE_BEING_DEFINED (type
) = 0;
11983 /* We don't instantiate default arguments for member functions. 14.7.1:
11985 The implicit instantiation of a class template specialization causes
11986 the implicit instantiation of the declarations, but not of the
11987 definitions or default arguments, of the class member functions,
11988 member classes, static data members and member templates.... */
11990 /* Some typedefs referenced from within the template code need to be access
11991 checked at template instantiation time, i.e now. These types were
11992 added to the template at parsing time. Let's get those and perform
11993 the access checks then. */
11994 perform_typedefs_access_check (pattern
, args
);
11995 perform_deferred_access_checks (tf_warning_or_error
);
11996 pop_nested_class ();
11997 maximum_field_alignment
= saved_maximum_field_alignment
;
11999 pop_from_top_level ();
12000 pop_tinst_level ();
12002 /* The vtable for a template class can be emitted in any translation
12003 unit in which the class is instantiated. When there is no key
12004 method, however, finish_struct_1 will already have added TYPE to
12005 the keyed_classes. */
12006 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
12007 vec_safe_push (keyed_classes
, type
);
12012 /* Wrapper for instantiate_class_template_1. */
12015 instantiate_class_template (tree type
)
12018 timevar_push (TV_TEMPLATE_INST
);
12019 ret
= instantiate_class_template_1 (type
);
12020 timevar_pop (TV_TEMPLATE_INST
);
12025 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12031 else if (TYPE_P (t
))
12032 r
= tsubst (t
, args
, complain
, in_decl
);
12035 if (!(complain
& tf_warning
))
12036 ++c_inhibit_evaluation_warnings
;
12037 r
= tsubst_expr (t
, args
, complain
, in_decl
,
12038 /*integral_constant_expression_p=*/true);
12039 if (!(complain
& tf_warning
))
12040 --c_inhibit_evaluation_warnings
;
12046 /* Given a function parameter pack TMPL_PARM and some function parameters
12047 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12048 and set *SPEC_P to point at the next point in the list. */
12051 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
12053 /* Collect all of the extra "packed" parameters into an
12056 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
12057 tree spec_parm
= *spec_p
;
12060 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
12062 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
12065 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12066 parmvec
= make_tree_vec (len
);
12067 spec_parm
= *spec_p
;
12068 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
12070 tree elt
= spec_parm
;
12071 if (DECL_PACK_P (elt
))
12072 elt
= make_pack_expansion (elt
);
12073 TREE_VEC_ELT (parmvec
, i
) = elt
;
12076 /* Build the argument packs. */
12077 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
12078 *spec_p
= spec_parm
;
12083 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12084 NONTYPE_ARGUMENT_PACK. */
12087 make_fnparm_pack (tree spec_parm
)
12089 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
12092 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12093 pack expansion with no extra args, 2 if it has extra args, or 0
12094 if it is not a pack expansion. */
12097 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
12099 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12100 /* We're being called before this happens in tsubst_pack_expansion. */
12101 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12102 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
12103 if (i
>= TREE_VEC_LENGTH (vec
))
12105 tree elt
= TREE_VEC_ELT (vec
, i
);
12107 /* A decl pack is itself an expansion. */
12108 elt
= TREE_TYPE (elt
);
12109 if (!PACK_EXPANSION_P (elt
))
12111 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
12117 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12120 make_argument_pack_select (tree arg_pack
, unsigned index
)
12122 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
12124 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
12125 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12130 /* This is a subroutine of tsubst_pack_expansion.
12132 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12133 mechanism to store the (non complete list of) arguments of the
12134 substitution and return a non substituted pack expansion, in order
12135 to wait for when we have enough arguments to really perform the
12139 use_pack_expansion_extra_args_p (tree parm_packs
,
12141 bool has_empty_arg
)
12143 /* If one pack has an expansion and another pack has a normal
12144 argument or if one pack has an empty argument and an another
12145 one hasn't then tsubst_pack_expansion cannot perform the
12146 substitution and need to fall back on the
12147 PACK_EXPANSION_EXTRA mechanism. */
12148 if (parm_packs
== NULL_TREE
)
12150 else if (has_empty_arg
)
12152 /* If all the actual packs are pack expansions, we can still
12153 subsitute directly. */
12154 for (tree p
= parm_packs
; p
; p
= TREE_CHAIN (p
))
12156 tree a
= TREE_VALUE (p
);
12157 if (TREE_CODE (a
) == ARGUMENT_PACK_SELECT
)
12158 a
= ARGUMENT_PACK_SELECT_FROM_PACK (a
);
12159 a
= ARGUMENT_PACK_ARGS (a
);
12160 if (TREE_VEC_LENGTH (a
) == 1)
12161 a
= TREE_VEC_ELT (a
, 0);
12162 if (PACK_EXPANSION_P (a
))
12169 bool has_expansion_arg
= false;
12170 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
12172 bool has_non_expansion_arg
= false;
12173 for (tree parm_pack
= parm_packs
;
12175 parm_pack
= TREE_CHAIN (parm_pack
))
12177 tree arg
= TREE_VALUE (parm_pack
);
12179 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
12181 /* We can't substitute a pack expansion with extra args into
12185 has_expansion_arg
= true;
12187 has_non_expansion_arg
= true;
12190 if (has_expansion_arg
&& has_non_expansion_arg
)
12196 /* [temp.variadic]/6 says that:
12198 The instantiation of a pack expansion [...]
12199 produces a list E1,E2, ..., En, where N is the number of elements
12200 in the pack expansion parameters.
12202 This subroutine of tsubst_pack_expansion produces one of these Ei.
12204 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12205 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12206 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12207 INDEX is the index 'i' of the element Ei to produce. ARGS,
12208 COMPLAIN, and IN_DECL are the same parameters as for the
12209 tsubst_pack_expansion function.
12211 The function returns the resulting Ei upon successful completion,
12212 or error_mark_node.
12214 Note that this function possibly modifies the ARGS parameter, so
12215 it's the responsibility of the caller to restore it. */
12218 gen_elem_of_pack_expansion_instantiation (tree pattern
,
12221 tree args
/* This parm gets
12223 tsubst_flags_t complain
,
12227 bool ith_elem_is_expansion
= false;
12229 /* For each parameter pack, change the substitution of the parameter
12230 pack to the ith argument in its argument pack, then expand the
12232 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
12234 tree parm
= TREE_PURPOSE (pack
);
12235 tree arg_pack
= TREE_VALUE (pack
);
12236 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
12238 ith_elem_is_expansion
|=
12239 argument_pack_element_is_expansion_p (arg_pack
, index
);
12241 /* Select the Ith argument from the pack. */
12242 if (TREE_CODE (parm
) == PARM_DECL
12244 || TREE_CODE (parm
) == FIELD_DECL
)
12248 aps
= make_argument_pack_select (arg_pack
, index
);
12249 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
12250 return error_mark_node
;
12251 register_local_specialization (aps
, parm
);
12254 aps
= retrieve_local_specialization (parm
);
12259 template_parm_level_and_index (parm
, &level
, &idx
);
12263 aps
= make_argument_pack_select (arg_pack
, index
);
12264 /* Update the corresponding argument. */
12265 TMPL_ARG (args
, level
, idx
) = aps
;
12268 /* Re-use the ARGUMENT_PACK_SELECT. */
12269 aps
= TMPL_ARG (args
, level
, idx
);
12271 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
12274 /* Substitute into the PATTERN with the (possibly altered)
12276 if (pattern
== in_decl
)
12277 /* Expanding a fixed parameter pack from
12278 coerce_template_parameter_pack. */
12279 t
= tsubst_decl (pattern
, args
, complain
);
12280 else if (pattern
== error_mark_node
)
12281 t
= error_mark_node
;
12282 else if (!TYPE_P (pattern
))
12283 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12284 /*integral_constant_expression_p=*/false);
12286 t
= tsubst (pattern
, args
, complain
, in_decl
);
12288 /* If the Ith argument pack element is a pack expansion, then
12289 the Ith element resulting from the substituting is going to
12290 be a pack expansion as well. */
12291 if (ith_elem_is_expansion
)
12292 t
= make_pack_expansion (t
, complain
);
12297 /* When the unexpanded parameter pack in a fold expression expands to an empty
12298 sequence, the value of the expression is as follows; the program is
12299 ill-formed if the operator is not listed in this table.
12306 expand_empty_fold (tree t
, tsubst_flags_t complain
)
12308 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
12309 if (!FOLD_EXPR_MODIFY_P (t
))
12312 case TRUTH_ANDIF_EXPR
:
12313 return boolean_true_node
;
12314 case TRUTH_ORIF_EXPR
:
12315 return boolean_false_node
;
12316 case COMPOUND_EXPR
:
12322 if (complain
& tf_error
)
12323 error_at (location_of (t
),
12324 "fold of empty expansion over %O", code
);
12325 return error_mark_node
;
12328 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12329 form an expression that combines the two terms using the
12333 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
12335 tree op
= FOLD_EXPR_OP (t
);
12336 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
12338 // Handle compound assignment operators.
12339 if (FOLD_EXPR_MODIFY_P (t
))
12340 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
12344 case COMPOUND_EXPR
:
12345 return build_x_compound_expr (input_location
, left
, right
, complain
);
12347 return build_x_binary_op (input_location
, code
,
12348 left
, TREE_CODE (left
),
12349 right
, TREE_CODE (right
),
12355 /* Substitute ARGS into the pack of a fold expression T. */
12358 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12360 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
12363 /* Substitute ARGS into the pack of a fold expression T. */
12366 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12368 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
12371 /* Expand a PACK of arguments into a grouped as left fold.
12372 Given a pack containing elements A0, A1, ..., An and an
12373 operator @, this builds the expression:
12375 ((A0 @ A1) @ A2) ... @ An
12377 Note that PACK must not be empty.
12379 The operator is defined by the original fold expression T. */
12382 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12384 tree left
= TREE_VEC_ELT (pack
, 0);
12385 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
12387 tree right
= TREE_VEC_ELT (pack
, i
);
12388 left
= fold_expression (t
, left
, right
, complain
);
12393 /* Substitute into a unary left fold expression. */
12396 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12399 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12400 if (pack
== error_mark_node
)
12401 return error_mark_node
;
12402 if (PACK_EXPANSION_P (pack
))
12404 tree r
= copy_node (t
);
12405 FOLD_EXPR_PACK (r
) = pack
;
12408 if (TREE_VEC_LENGTH (pack
) == 0)
12409 return expand_empty_fold (t
, complain
);
12411 return expand_left_fold (t
, pack
, complain
);
12414 /* Substitute into a binary left fold expression.
12416 Do ths by building a single (non-empty) vector of argumnts and
12417 building the expression from those elements. */
12420 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
12423 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12424 if (pack
== error_mark_node
)
12425 return error_mark_node
;
12426 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12427 if (init
== error_mark_node
)
12428 return error_mark_node
;
12430 if (PACK_EXPANSION_P (pack
))
12432 tree r
= copy_node (t
);
12433 FOLD_EXPR_PACK (r
) = pack
;
12434 FOLD_EXPR_INIT (r
) = init
;
12438 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
12439 TREE_VEC_ELT (vec
, 0) = init
;
12440 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
12441 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
12443 return expand_left_fold (t
, vec
, complain
);
12446 /* Expand a PACK of arguments into a grouped as right fold.
12447 Given a pack containing elementns A0, A1, ..., and an
12448 operator @, this builds the expression:
12450 A0@ ... (An-2 @ (An-1 @ An))
12452 Note that PACK must not be empty.
12454 The operator is defined by the original fold expression T. */
12457 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
12459 // Build the expression.
12460 int n
= TREE_VEC_LENGTH (pack
);
12461 tree right
= TREE_VEC_ELT (pack
, n
- 1);
12462 for (--n
; n
!= 0; --n
)
12464 tree left
= TREE_VEC_ELT (pack
, n
- 1);
12465 right
= fold_expression (t
, left
, right
, complain
);
12470 /* Substitute into a unary right fold expression. */
12473 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12476 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12477 if (pack
== error_mark_node
)
12478 return error_mark_node
;
12479 if (PACK_EXPANSION_P (pack
))
12481 tree r
= copy_node (t
);
12482 FOLD_EXPR_PACK (r
) = pack
;
12485 if (TREE_VEC_LENGTH (pack
) == 0)
12486 return expand_empty_fold (t
, complain
);
12488 return expand_right_fold (t
, pack
, complain
);
12491 /* Substitute into a binary right fold expression.
12493 Do ths by building a single (non-empty) vector of arguments and
12494 building the expression from those elements. */
12497 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
12500 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
12501 if (pack
== error_mark_node
)
12502 return error_mark_node
;
12503 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
12504 if (init
== error_mark_node
)
12505 return error_mark_node
;
12507 if (PACK_EXPANSION_P (pack
))
12509 tree r
= copy_node (t
);
12510 FOLD_EXPR_PACK (r
) = pack
;
12511 FOLD_EXPR_INIT (r
) = init
;
12515 int n
= TREE_VEC_LENGTH (pack
);
12516 tree vec
= make_tree_vec (n
+ 1);
12517 for (int i
= 0; i
< n
; ++i
)
12518 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
12519 TREE_VEC_ELT (vec
, n
) = init
;
12521 return expand_right_fold (t
, vec
, complain
);
12524 /* Walk through the pattern of a pack expansion, adding everything in
12525 local_specializations to a list. */
12530 hash_set
<tree
> internal
;
12532 tsubst_flags_t complain
;
12534 el_data (tsubst_flags_t c
)
12535 : extra (NULL_TREE
), complain (c
) {}
12538 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12540 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12541 tree
*extra
= &data
.extra
;
12542 tsubst_flags_t complain
= data
.complain
;
12544 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12545 /* Remember local typedefs (85214). */
12546 tp
= &TYPE_NAME (*tp
);
12548 if (TREE_CODE (*tp
) == DECL_EXPR
)
12549 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12550 else if (tree spec
= retrieve_local_specialization (*tp
))
12552 if (data
.internal
.contains (*tp
))
12553 /* Don't mess with variables declared within the pattern. */
12555 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12557 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12558 tree args
= ARGUMENT_PACK_ARGS (spec
);
12559 if (TREE_VEC_LENGTH (args
) == 1)
12561 tree elt
= TREE_VEC_ELT (args
, 0);
12562 if (PACK_EXPANSION_P (elt
))
12563 elt
= PACK_EXPANSION_PATTERN (elt
);
12564 if (DECL_PACK_P (elt
))
12567 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12569 /* Handle lambda capture here, since we aren't doing any
12570 substitution now, and so tsubst_copy won't call
12571 process_outer_var_ref. */
12572 tree args
= ARGUMENT_PACK_ARGS (spec
);
12573 int len
= TREE_VEC_LENGTH (args
);
12574 for (int i
= 0; i
< len
; ++i
)
12576 tree arg
= TREE_VEC_ELT (args
, i
);
12578 if (outer_automatic_var_p (arg
))
12579 carg
= process_outer_var_ref (arg
, complain
);
12582 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12586 spec
= copy_node (spec
);
12587 args
= copy_node (args
);
12588 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12589 register_local_specialization (spec
, *tp
);
12591 TREE_VEC_ELT (args
, i
) = carg
;
12596 if (outer_automatic_var_p (spec
))
12597 spec
= process_outer_var_ref (spec
, complain
);
12598 *extra
= tree_cons (*tp
, spec
, *extra
);
12603 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12605 el_data
data (complain
);
12606 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12610 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12611 for use in PACK_EXPANSION_EXTRA_ARGS. */
12614 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12617 if (local_specializations
)
12618 if (tree locals
= extract_local_specs (pattern
, complain
))
12619 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12623 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12624 normal template args to ARGS. */
12627 add_extra_args (tree extra
, tree args
)
12629 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12631 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12633 /* The partial instantiation involved local declarations collected in
12634 extract_local_specs; map from the general template to our local
12636 tree gen
= TREE_PURPOSE (elt
);
12637 tree inst
= TREE_VALUE (elt
);
12639 if (tree local
= retrieve_local_specialization (inst
))
12641 /* else inst is already a full instantiation of the pack. */
12642 register_local_specialization (inst
, gen
);
12644 gcc_assert (!TREE_PURPOSE (extra
));
12645 extra
= TREE_VALUE (extra
);
12648 /* I think we should always be able to substitute dependent args into the
12649 pattern. If that turns out to be incorrect in some cases, enable the
12650 alternate code (and add complain/in_decl parms to this function). */
12651 gcc_checking_assert (!uses_template_parms (extra
));
12653 if (!uses_template_parms (extra
))
12655 gcc_unreachable ();
12656 extra
= tsubst_template_args (extra
, args
, complain
, in_decl
);
12657 args
= add_outermost_template_args (args
, extra
);
12661 args
= add_to_template_args (extra
, args
);
12665 /* Substitute ARGS into T, which is an pack expansion
12666 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12667 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12668 (if only a partial substitution could be performed) or
12669 ERROR_MARK_NODE if there was an error. */
12671 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12675 tree pack
, packs
= NULL_TREE
;
12676 bool unsubstituted_packs
= false;
12677 bool unsubstituted_fn_pack
= false;
12680 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12681 bool need_local_specializations
= false;
12684 gcc_assert (PACK_EXPANSION_P (t
));
12685 pattern
= PACK_EXPANSION_PATTERN (t
);
12687 /* Add in any args remembered from an earlier partial instantiation. */
12688 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12690 levels
= TMPL_ARGS_DEPTH (args
);
12692 /* Determine the argument packs that will instantiate the parameter
12693 packs used in the expansion expression. While we're at it,
12694 compute the number of arguments to be expanded and make sure it
12696 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12697 pack
= TREE_CHAIN (pack
))
12699 tree parm_pack
= TREE_VALUE (pack
);
12700 tree arg_pack
= NULL_TREE
;
12701 tree orig_arg
= NULL_TREE
;
12704 if (TREE_CODE (parm_pack
) == BASES
)
12706 gcc_assert (parm_pack
== pattern
);
12707 if (BASES_DIRECT (parm_pack
))
12708 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12713 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12714 args
, complain
, in_decl
,
12717 else if (builtin_pack_call_p (parm_pack
))
12719 if (parm_pack
!= pattern
)
12721 if (complain
& tf_error
)
12722 sorry ("%qE is not the entire pattern of the pack expansion",
12724 return error_mark_node
;
12726 return expand_builtin_pack_call (parm_pack
, args
,
12727 complain
, in_decl
);
12729 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12731 /* We know we have correct local_specializations if this
12732 expansion is at function scope, or if we're dealing with a
12733 local parameter in a requires expression; for the latter,
12734 tsubst_requires_expr set it up appropriately. */
12735 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12736 arg_pack
= retrieve_local_specialization (parm_pack
);
12738 /* We can't rely on local_specializations for a parameter
12739 name used later in a function declaration (such as in a
12740 late-specified return type). Even if it exists, it might
12741 have the wrong value for a recursive call. */
12742 need_local_specializations
= true;
12746 /* This parameter pack was used in an unevaluated context. Just
12747 make a dummy decl, since it's only used for its type. */
12748 ++cp_unevaluated_operand
;
12749 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12750 --cp_unevaluated_operand
;
12751 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12752 /* Partial instantiation of the parm_pack, we can't build
12753 up an argument pack yet. */
12754 arg_pack
= NULL_TREE
;
12756 arg_pack
= make_fnparm_pack (arg_pack
);
12758 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12759 /* This argument pack isn't fully instantiated yet. We set this
12760 flag rather than clear arg_pack because we do want to do the
12761 optimization below, and we don't want to substitute directly
12762 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12763 where it isn't expected). */
12764 unsubstituted_fn_pack
= true;
12766 else if (is_capture_proxy (parm_pack
))
12768 arg_pack
= retrieve_local_specialization (parm_pack
);
12769 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12770 unsubstituted_fn_pack
= true;
12775 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12776 if (level
<= levels
)
12777 arg_pack
= TMPL_ARG (args
, level
, idx
);
12780 orig_arg
= arg_pack
;
12781 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12782 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12784 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12785 /* This can only happen if we forget to expand an argument
12786 pack somewhere else. Just return an error, silently. */
12788 result
= make_tree_vec (1);
12789 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12796 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12798 /* Don't bother trying to do a partial substitution with
12799 incomplete packs; we'll try again after deduction. */
12800 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12805 else if (len
!= my_len
12806 && !unsubstituted_fn_pack
)
12808 if (!(complain
& tf_error
))
12809 /* Fail quietly. */;
12810 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12811 error ("mismatched argument pack lengths while expanding %qT",
12814 error ("mismatched argument pack lengths while expanding %qE",
12816 return error_mark_node
;
12819 /* Keep track of the parameter packs and their corresponding
12821 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12822 TREE_TYPE (packs
) = orig_arg
;
12826 /* We can't substitute for this parameter pack. We use a flag as
12827 well as the missing_level counter because function parameter
12828 packs don't have a level. */
12829 if (!(processing_template_decl
|| is_auto (parm_pack
)))
12831 gcc_unreachable ();
12833 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12834 unsubstituted_packs
= true;
12838 /* If the expansion is just T..., return the matching argument pack, unless
12839 we need to call convert_from_reference on all the elements. This is an
12840 important optimization; see c++/68422. */
12841 if (!unsubstituted_packs
12842 && TREE_PURPOSE (packs
) == pattern
)
12844 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12846 /* If the argument pack is a single pack expansion, pull it out. */
12847 if (TREE_VEC_LENGTH (args
) == 1
12848 && pack_expansion_args_count (args
))
12849 return TREE_VEC_ELT (args
, 0);
12851 /* Types need no adjustment, nor does sizeof..., and if we still have
12852 some pack expansion args we won't do anything yet. */
12853 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12854 || PACK_EXPANSION_SIZEOF_P (t
)
12855 || pack_expansion_args_count (args
))
12857 /* Also optimize expression pack expansions if we can tell that the
12858 elements won't have reference type. */
12859 tree type
= TREE_TYPE (pattern
);
12860 if (type
&& !TYPE_REF_P (type
)
12861 && !PACK_EXPANSION_P (type
)
12862 && !WILDCARD_TYPE_P (type
))
12864 /* Otherwise use the normal path so we get convert_from_reference. */
12867 /* We cannot expand this expansion expression, because we don't have
12868 all of the argument packs we need. */
12869 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12871 /* We got some full packs, but we can't substitute them in until we
12872 have values for all the packs. So remember these until then. */
12874 t
= make_pack_expansion (pattern
, complain
);
12875 PACK_EXPANSION_EXTRA_ARGS (t
)
12876 = build_extra_args (pattern
, args
, complain
);
12879 else if (unsubstituted_packs
)
12881 /* There were no real arguments, we're just replacing a parameter
12882 pack with another version of itself. Substitute into the
12883 pattern and return a PACK_EXPANSION_*. The caller will need to
12885 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12886 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12887 /*integral_constant_expression_p=*/false);
12889 t
= tsubst (pattern
, args
, complain
, in_decl
);
12890 t
= make_pack_expansion (t
, complain
);
12894 gcc_assert (len
>= 0);
12896 if (need_local_specializations
)
12898 /* We're in a late-specified return type, so create our own local
12899 specializations map; the current map is either NULL or (in the
12900 case of recursive unification) might have bindings that we don't
12901 want to use or alter. */
12902 saved_local_specializations
= local_specializations
;
12903 local_specializations
= new hash_map
<tree
, tree
>;
12906 /* For each argument in each argument pack, substitute into the
12908 result
= make_tree_vec (len
);
12909 tree elem_args
= copy_template_args (args
);
12910 for (i
= 0; i
< len
; ++i
)
12912 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12914 elem_args
, complain
,
12916 TREE_VEC_ELT (result
, i
) = t
;
12917 if (t
== error_mark_node
)
12919 result
= error_mark_node
;
12924 /* Update ARGS to restore the substitution from parameter packs to
12925 their argument packs. */
12926 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12928 tree parm
= TREE_PURPOSE (pack
);
12930 if (TREE_CODE (parm
) == PARM_DECL
12932 || TREE_CODE (parm
) == FIELD_DECL
)
12933 register_local_specialization (TREE_TYPE (pack
), parm
);
12938 if (TREE_VALUE (pack
) == NULL_TREE
)
12941 template_parm_level_and_index (parm
, &level
, &idx
);
12943 /* Update the corresponding argument. */
12944 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12945 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12948 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12952 if (need_local_specializations
)
12954 delete local_specializations
;
12955 local_specializations
= saved_local_specializations
;
12958 /* If the dependent pack arguments were such that we end up with only a
12959 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12960 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12961 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12962 return TREE_VEC_ELT (result
, 0);
12967 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12968 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12969 parameter packs; all parms generated from a function parameter pack will
12970 have the same DECL_PARM_INDEX. */
12973 get_pattern_parm (tree parm
, tree tmpl
)
12975 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12978 if (DECL_ARTIFICIAL (parm
))
12980 for (patparm
= DECL_ARGUMENTS (pattern
);
12981 patparm
; patparm
= DECL_CHAIN (patparm
))
12982 if (DECL_ARTIFICIAL (patparm
)
12983 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12988 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12989 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12990 gcc_assert (DECL_PARM_INDEX (patparm
)
12991 == DECL_PARM_INDEX (parm
));
12997 /* Make an argument pack out of the TREE_VEC VEC. */
13000 make_argument_pack (tree vec
)
13003 tree elt
= TREE_VEC_ELT (vec
, 0);
13005 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13008 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
13009 TREE_CONSTANT (pack
) = 1;
13011 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
13015 /* Return an exact copy of template args T that can be modified
13019 copy_template_args (tree t
)
13021 if (t
== error_mark_node
)
13024 int len
= TREE_VEC_LENGTH (t
);
13025 tree new_vec
= make_tree_vec (len
);
13027 for (int i
= 0; i
< len
; ++i
)
13029 tree elt
= TREE_VEC_ELT (t
, i
);
13030 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
13031 elt
= copy_template_args (elt
);
13032 TREE_VEC_ELT (new_vec
, i
) = elt
;
13035 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
13036 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
13041 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13044 tsubst_argument_pack (tree orig_arg
, tree args
, tsubst_flags_t complain
,
13047 /* Substitute into each of the arguments. */
13048 tree new_arg
= TYPE_P (orig_arg
)
13049 ? cxx_make_type (TREE_CODE (orig_arg
))
13050 : make_node (TREE_CODE (orig_arg
));
13052 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
13053 args
, complain
, in_decl
);
13054 if (pack_args
== error_mark_node
)
13055 new_arg
= error_mark_node
;
13057 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
13059 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
13060 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
13065 /* Substitute ARGS into the vector or list of template arguments T. */
13068 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13071 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
13074 if (t
== error_mark_node
)
13075 return error_mark_node
;
13077 len
= TREE_VEC_LENGTH (t
);
13078 elts
= XALLOCAVEC (tree
, len
);
13080 for (i
= 0; i
< len
; i
++)
13082 tree orig_arg
= TREE_VEC_ELT (t
, i
);
13085 if (TREE_CODE (orig_arg
) == TREE_VEC
)
13086 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
13087 else if (PACK_EXPANSION_P (orig_arg
))
13089 /* Substitute into an expansion expression. */
13090 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
13092 if (TREE_CODE (new_arg
) == TREE_VEC
)
13093 /* Add to the expanded length adjustment the number of
13094 expanded arguments. We subtract one from this
13095 measurement, because the argument pack expression
13096 itself is already counted as 1 in
13097 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13098 the argument pack is empty. */
13099 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
13101 else if (ARGUMENT_PACK_P (orig_arg
))
13102 new_arg
= tsubst_argument_pack (orig_arg
, args
, complain
, in_decl
);
13104 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
13106 if (new_arg
== error_mark_node
)
13107 return error_mark_node
;
13110 if (new_arg
!= orig_arg
)
13117 /* Make space for the expanded arguments coming from template
13119 t
= make_tree_vec (len
+ expanded_len_adjust
);
13120 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13121 arguments for a member template.
13122 In that case each TREE_VEC in ORIG_T represents a level of template
13123 arguments, and ORIG_T won't carry any non defaulted argument count.
13124 It will rather be the nested TREE_VECs that will carry one.
13125 In other words, ORIG_T carries a non defaulted argument count only
13126 if it doesn't contain any nested TREE_VEC. */
13127 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
13129 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
13130 count
+= expanded_len_adjust
;
13131 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
13133 for (i
= 0, out
= 0; i
< len
; i
++)
13135 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
13136 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
13137 && TREE_CODE (elts
[i
]) == TREE_VEC
)
13141 /* Now expand the template argument pack "in place". */
13142 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
13143 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
13147 TREE_VEC_ELT (t
, out
) = elts
[i
];
13155 /* Substitute ARGS into one level PARMS of template parameters. */
13158 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
13160 if (parms
== error_mark_node
)
13161 return error_mark_node
;
13163 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
13165 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
13167 tree tuple
= TREE_VEC_ELT (parms
, i
);
13169 if (tuple
== error_mark_node
)
13172 TREE_VEC_ELT (new_vec
, i
) =
13173 tsubst_template_parm (tuple
, args
, complain
);
13179 /* Return the result of substituting ARGS into the template parameters
13180 given by PARMS. If there are m levels of ARGS and m + n levels of
13181 PARMS, then the result will contain n levels of PARMS. For
13182 example, if PARMS is `template <class T> template <class U>
13183 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13184 result will be `template <int*, double, class V>'. */
13187 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
13189 tree r
= NULL_TREE
;
13192 /* When substituting into a template, we must set
13193 PROCESSING_TEMPLATE_DECL as the template parameters may be
13194 dependent if they are based on one-another, and the dependency
13195 predicates are short-circuit outside of templates. */
13196 ++processing_template_decl
;
13198 for (new_parms
= &r
;
13199 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
13200 new_parms
= &(TREE_CHAIN (*new_parms
)),
13201 parms
= TREE_CHAIN (parms
))
13203 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
13206 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
13207 - TMPL_ARGS_DEPTH (args
)),
13208 new_vec
, NULL_TREE
);
13211 --processing_template_decl
;
13216 /* Return the result of substituting ARGS into one template parameter
13217 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13218 parameter and which TREE_PURPOSE is the default argument of the
13219 template parameter. */
13222 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
13224 tree default_value
, parm_decl
;
13226 if (args
== NULL_TREE
13228 || t
== error_mark_node
)
13231 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13233 default_value
= TREE_PURPOSE (t
);
13234 parm_decl
= TREE_VALUE (t
);
13235 tree constraint
= TEMPLATE_PARM_CONSTRAINTS (t
);
13237 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
13238 if (TREE_CODE (parm_decl
) == PARM_DECL
13239 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
13240 parm_decl
= error_mark_node
;
13241 default_value
= tsubst_template_arg (default_value
, args
,
13242 complain
, NULL_TREE
);
13243 constraint
= tsubst_constraint (constraint
, args
, complain
, NULL_TREE
);
13245 tree r
= build_tree_list (default_value
, parm_decl
);
13246 TEMPLATE_PARM_CONSTRAINTS (r
) = constraint
;
13250 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13251 type T. If T is not an aggregate or enumeration type, it is
13252 handled as if by tsubst. IN_DECL is as for tsubst. If
13253 ENTERING_SCOPE is nonzero, T is the context for a template which
13254 we are presently tsubst'ing. Return the substituted value. */
13257 tsubst_aggr_type (tree t
,
13259 tsubst_flags_t complain
,
13261 int entering_scope
)
13263 if (t
== NULL_TREE
)
13266 switch (TREE_CODE (t
))
13269 if (TYPE_PTRMEMFUNC_P (t
))
13270 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
13272 /* Fall through. */
13273 case ENUMERAL_TYPE
:
13275 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
13281 /* In "sizeof(X<I>)" we need to evaluate "I". */
13284 /* First, determine the context for the type we are looking
13286 context
= TYPE_CONTEXT (t
);
13287 if (context
&& TYPE_P (context
))
13289 context
= tsubst_aggr_type (context
, args
, complain
,
13290 in_decl
, /*entering_scope=*/1);
13291 /* If context is a nested class inside a class template,
13292 it may still need to be instantiated (c++/33959). */
13293 context
= complete_type (context
);
13296 /* Then, figure out what arguments are appropriate for the
13297 type we are trying to find. For example, given:
13299 template <class T> struct S;
13300 template <class T, class U> void f(T, U) { S<U> su; }
13302 and supposing that we are instantiating f<int, double>,
13303 then our ARGS will be {int, double}, but, when looking up
13304 S we only want {double}. */
13305 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
13306 complain
, in_decl
);
13307 if (argvec
== error_mark_node
)
13308 r
= error_mark_node
;
13311 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
13312 entering_scope
, complain
);
13313 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13319 /* This is not a template type, so there's nothing to do. */
13323 return tsubst (t
, args
, complain
, in_decl
);
13327 static GTY((cache
)) decl_tree_cache_map
*defarg_inst
;
13329 /* Substitute into the default argument ARG (a default argument for
13330 FN), which has the indicated TYPE. */
13333 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
13334 tsubst_flags_t complain
)
13336 int errs
= errorcount
+ sorrycount
;
13338 /* This can happen in invalid code. */
13339 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
13342 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
13343 parm
= chain_index (parmnum
, parm
);
13344 tree parmtype
= TREE_TYPE (parm
);
13345 if (DECL_BY_REFERENCE (parm
))
13346 parmtype
= TREE_TYPE (parmtype
);
13347 if (parmtype
== error_mark_node
)
13348 return error_mark_node
;
13350 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
13353 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
13356 /* This default argument came from a template. Instantiate the
13357 default argument here, not in tsubst. In the case of
13366 we must be careful to do name lookup in the scope of S<T>,
13367 rather than in the current class. */
13368 push_to_top_level ();
13369 push_access_scope (fn
);
13370 push_deferring_access_checks (dk_no_deferred
);
13371 start_lambda_scope (parm
);
13373 /* The default argument expression may cause implicitly defined
13374 member functions to be synthesized, which will result in garbage
13375 collection. We must treat this situation as if we were within
13376 the body of function so as to avoid collecting live data on the
13379 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
13380 complain
, NULL_TREE
,
13381 /*integral_constant_expression_p=*/false);
13384 finish_lambda_scope ();
13386 /* Make sure the default argument is reasonable. */
13387 arg
= check_default_argument (type
, arg
, complain
);
13389 if (errorcount
+sorrycount
> errs
13390 && (complain
& tf_warning_or_error
))
13391 inform (input_location
,
13392 " when instantiating default argument for call to %qD", fn
);
13394 pop_deferring_access_checks ();
13395 pop_access_scope (fn
);
13396 pop_from_top_level ();
13398 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
13401 defarg_inst
= decl_tree_cache_map::create_ggc (37);
13402 defarg_inst
->put (parm
, arg
);
13408 /* Substitute into all the default arguments for FN. */
13411 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
13416 tmpl_args
= DECL_TI_ARGS (fn
);
13418 /* If this function is not yet instantiated, we certainly don't need
13419 its default arguments. */
13420 if (uses_template_parms (tmpl_args
))
13422 /* Don't do this again for clones. */
13423 if (DECL_CLONED_FUNCTION_P (fn
))
13427 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
13429 arg
= TREE_CHAIN (arg
), ++i
)
13430 if (TREE_PURPOSE (arg
))
13431 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
13433 TREE_PURPOSE (arg
),
13437 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13438 static GTY((cache
)) decl_tree_cache_map
*explicit_specifier_map
;
13440 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13443 store_explicit_specifier (tree v
, tree t
)
13445 if (!explicit_specifier_map
)
13446 explicit_specifier_map
= decl_tree_cache_map::create_ggc (37);
13447 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
13448 explicit_specifier_map
->put (v
, t
);
13451 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13454 lookup_explicit_specifier (tree v
)
13456 return *explicit_specifier_map
->get (v
);
13459 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13462 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
13463 tree lambda_fntype
)
13465 tree gen_tmpl
, argvec
;
13466 hashval_t hash
= 0;
13469 /* Nobody should be tsubst'ing into non-template functions. */
13470 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
13472 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
13474 /* If T is not dependent, just return it. */
13475 if (!uses_template_parms (DECL_TI_ARGS (t
))
13476 && !LAMBDA_FUNCTION_P (t
))
13479 /* Calculate the most general template of which R is a
13481 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
13483 /* We're substituting a lambda function under tsubst_lambda_expr but not
13484 directly from it; find the matching function we're already inside.
13485 But don't do this if T is a generic lambda with a single level of
13486 template parms, as in that case we're doing a normal instantiation. */
13487 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
13488 && (!generic_lambda_fn_p (t
)
13489 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
13490 return enclosing_instantiation_of (t
);
13492 /* Calculate the complete set of arguments used to
13494 argvec
= tsubst_template_args (DECL_TI_ARGS
13495 (DECL_TEMPLATE_RESULT
13496 (DECL_TI_TEMPLATE (t
))),
13497 args
, complain
, in_decl
);
13498 if (argvec
== error_mark_node
)
13499 return error_mark_node
;
13501 /* Check to see if we already have this specialization. */
13502 if (!lambda_fntype
)
13504 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13505 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
13509 /* We can see more levels of arguments than parameters if
13510 there was a specialization of a member template, like
13513 template <class T> struct S { template <class U> void f(); }
13514 template <> template <class U> void S<int>::f(U);
13516 Here, we'll be substituting into the specialization,
13517 because that's where we can find the code we actually
13518 want to generate, but we'll have enough arguments for
13519 the most general template.
13521 We also deal with the peculiar case:
13523 template <class T> struct S {
13524 template <class U> friend void f();
13526 template <class U> void f() {}
13528 template void f<double>();
13530 Here, the ARGS for the instantiation of will be {int,
13531 double}. But, we only need as many ARGS as there are
13532 levels of template parameters in CODE_PATTERN. We are
13533 careful not to get fooled into reducing the ARGS in
13536 template <class T> struct S { template <class U> void f(U); }
13537 template <class T> template <> void S<T>::f(int) {}
13539 which we can spot because the pattern will be a
13540 specialization in this case. */
13541 int args_depth
= TMPL_ARGS_DEPTH (args
);
13543 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
13545 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
13546 args
= get_innermost_template_args (args
, parms_depth
);
13550 /* This special case arises when we have something like this:
13552 template <class T> struct S {
13553 friend void f<int>(int, double);
13556 Here, the DECL_TI_TEMPLATE for the friend declaration
13557 will be an IDENTIFIER_NODE. We are being called from
13558 tsubst_friend_function, and we want only to create a
13559 new decl (R) with appropriate types so that we can call
13560 determine_specialization. */
13561 gen_tmpl
= NULL_TREE
;
13562 argvec
= NULL_TREE
;
13565 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13567 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13568 bool member
= ctx
&& TYPE_P (ctx
);
13570 if (member
&& !closure
)
13571 ctx
= tsubst_aggr_type (ctx
, args
,
13572 complain
, t
, /*entering_scope=*/1);
13574 tree type
= (lambda_fntype
? lambda_fntype
13575 : tsubst (TREE_TYPE (t
), args
,
13576 complain
| tf_fndecl_type
, in_decl
));
13577 if (type
== error_mark_node
)
13578 return error_mark_node
;
13580 /* If we hit excessive deduction depth, the type is bogus even if
13581 it isn't error_mark_node, so don't build a decl. */
13582 if (excessive_deduction_depth
)
13583 return error_mark_node
;
13585 /* We do NOT check for matching decls pushed separately at this
13586 point, as they may not represent instantiations of this
13587 template, and in any case are considered separate under the
13589 tree r
= copy_decl (t
);
13590 DECL_USE_TEMPLATE (r
) = 0;
13591 TREE_TYPE (r
) = type
;
13592 /* Clear out the mangled name and RTL for the instantiation. */
13593 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13594 SET_DECL_RTL (r
, NULL
);
13595 /* Leave DECL_INITIAL set on deleted instantiations. */
13596 if (!DECL_DELETED_FN (r
))
13597 DECL_INITIAL (r
) = NULL_TREE
;
13598 DECL_CONTEXT (r
) = ctx
;
13600 /* Handle explicit(dependent-expr). */
13601 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13603 tree spec
= lookup_explicit_specifier (t
);
13604 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13605 /*function_p=*/false,
13607 spec
= build_explicit_specifier (spec
, complain
);
13608 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13611 /* OpenMP UDRs have the only argument a reference to the declared
13612 type. We want to diagnose if the declared type is a reference,
13613 which is invalid, but as references to references are usually
13614 quietly merged, diagnose it here. */
13615 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13618 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13619 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13620 if (TYPE_REF_P (argtype
))
13621 error_at (DECL_SOURCE_LOCATION (t
),
13622 "reference type %qT in "
13623 "%<#pragma omp declare reduction%>", argtype
);
13624 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13625 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13629 if (member
&& DECL_CONV_FN_P (r
))
13630 /* Type-conversion operator. Reconstruct the name, in
13631 case it's the name of one of the template's parameters. */
13632 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13634 tree parms
= DECL_ARGUMENTS (t
);
13636 parms
= DECL_CHAIN (parms
);
13637 parms
= tsubst (parms
, args
, complain
, t
);
13638 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13639 DECL_CONTEXT (parm
) = r
;
13642 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13643 DECL_CHAIN (tparm
) = parms
;
13646 DECL_ARGUMENTS (r
) = parms
;
13647 DECL_RESULT (r
) = NULL_TREE
;
13649 TREE_STATIC (r
) = 0;
13650 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13651 DECL_EXTERNAL (r
) = 1;
13652 /* If this is an instantiation of a function with internal
13653 linkage, we already know what object file linkage will be
13654 assigned to the instantiation. */
13655 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13656 DECL_DEFER_OUTPUT (r
) = 0;
13657 DECL_CHAIN (r
) = NULL_TREE
;
13658 DECL_PENDING_INLINE_INFO (r
) = 0;
13659 DECL_PENDING_INLINE_P (r
) = 0;
13660 DECL_SAVED_TREE (r
) = NULL_TREE
;
13661 DECL_STRUCT_FUNCTION (r
) = NULL
;
13663 /* We'll re-clone as appropriate in instantiate_template. */
13664 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13666 /* If we aren't complaining now, return on error before we register
13667 the specialization so that we'll complain eventually. */
13668 if ((complain
& tf_error
) == 0
13669 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13670 && !grok_op_properties (r
, /*complain=*/false))
13671 return error_mark_node
;
13673 /* Associate the constraints directly with the instantiation. We
13674 don't substitute through the constraints; that's only done when
13675 they are checked. */
13676 if (tree ci
= get_constraints (t
))
13677 set_constraints (r
, ci
);
13679 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13680 SET_DECL_FRIEND_CONTEXT (r
,
13681 tsubst (DECL_FRIEND_CONTEXT (t
),
13682 args
, complain
, in_decl
));
13684 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13685 this in the special friend case mentioned above where
13686 GEN_TMPL is NULL. */
13687 if (gen_tmpl
&& !closure
)
13689 DECL_TEMPLATE_INFO (r
)
13690 = build_template_info (gen_tmpl
, argvec
);
13691 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13694 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13696 /* We instantiated this while substituting into
13697 the type earlier (template/friend54.C). */
13700 /* We're not supposed to instantiate default arguments
13701 until they are called, for a template. But, for a
13704 template <class T> void f ()
13705 { extern void g(int i = T()); }
13707 we should do the substitution when the template is
13708 instantiated. We handle the member function case in
13709 instantiate_class_template since the default arguments
13710 might refer to other members of the class. */
13712 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13713 && !uses_template_parms (argvec
))
13714 tsubst_default_arguments (r
, complain
);
13717 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13719 /* Copy the list of befriending classes. */
13720 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13722 friends
= &TREE_CHAIN (*friends
))
13724 *friends
= copy_node (*friends
);
13725 TREE_VALUE (*friends
)
13726 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13729 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13731 maybe_retrofit_in_chrg (r
);
13732 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13733 return error_mark_node
;
13734 /* If this is an instantiation of a member template, clone it.
13735 If it isn't, that'll be handled by
13736 clone_constructors_and_destructors. */
13737 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13738 clone_function_decl (r
, /*update_methods=*/false);
13740 else if ((complain
& tf_error
) != 0
13741 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13742 && !grok_op_properties (r
, /*complain=*/true))
13743 return error_mark_node
;
13745 /* Possibly limit visibility based on template args. */
13746 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13747 if (DECL_VISIBILITY_SPECIFIED (t
))
13749 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13750 DECL_ATTRIBUTES (r
)
13751 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13753 determine_visibility (r
);
13754 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13755 && !processing_template_decl
)
13756 defaulted_late_check (r
);
13758 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13759 args
, complain
, in_decl
);
13761 if (tree attr
= lookup_attribute ("omp declare variant base",
13762 DECL_ATTRIBUTES (r
)))
13763 omp_declare_variant_finalize (r
, attr
);
13768 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13771 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13772 tree lambda_fntype
)
13774 /* We can get here when processing a member function template,
13775 member class template, or template template parameter. */
13776 tree decl
= DECL_TEMPLATE_RESULT (t
);
13782 hashval_t hash
= 0;
13784 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13786 /* Template template parameter is treated here. */
13787 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13788 if (new_type
== error_mark_node
)
13789 r
= error_mark_node
;
13790 /* If we get a real template back, return it. This can happen in
13791 the context of most_specialized_partial_spec. */
13792 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13795 /* The new TEMPLATE_DECL was built in
13796 reduce_template_parm_level. */
13797 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13801 if (!lambda_fntype
)
13803 /* We might already have an instance of this template.
13804 The ARGS are for the surrounding class type, so the
13805 full args contain the tsubst'd args for the context,
13806 plus the innermost args from the template decl. */
13807 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13808 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13809 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13810 /* Because this is a template, the arguments will still be
13811 dependent, even after substitution. If
13812 PROCESSING_TEMPLATE_DECL is not set, the dependency
13813 predicates will short-circuit. */
13814 ++processing_template_decl
;
13815 full_args
= tsubst_template_args (tmpl_args
, args
,
13816 complain
, in_decl
);
13817 --processing_template_decl
;
13818 if (full_args
== error_mark_node
)
13819 return error_mark_node
;
13821 /* If this is a default template template argument,
13822 tsubst might not have changed anything. */
13823 if (full_args
== tmpl_args
)
13826 hash
= hash_tmpl_and_args (t
, full_args
);
13827 spec
= retrieve_specialization (t
, full_args
, hash
);
13828 if (spec
!= NULL_TREE
)
13831 /* Type partial instantiations are stored as the type by
13832 lookup_template_class_1, not here as the template. */
13833 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
13838 /* Make a new template decl. It will be similar to the
13839 original, but will record the current template arguments.
13840 We also create a new function declaration, which is just
13841 like the old one, but points to this new template, rather
13842 than the old one. */
13844 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13845 DECL_CHAIN (r
) = NULL_TREE
;
13847 // Build new template info linking to the original template decl.
13848 if (!lambda_fntype
)
13850 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13851 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13854 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13856 /* The template parameters for this new template are all the
13857 template parameters for the old template, except the
13858 outermost level of parameters. */
13859 DECL_TEMPLATE_PARMS (r
)
13860 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13863 if (TREE_CODE (decl
) == TYPE_DECL
13864 && !TYPE_DECL_ALIAS_P (decl
))
13867 ++processing_template_decl
;
13868 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13869 --processing_template_decl
;
13870 if (new_type
== error_mark_node
)
13871 return error_mark_node
;
13873 TREE_TYPE (r
) = new_type
;
13874 /* For a partial specialization, we need to keep pointing to
13875 the primary template. */
13876 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13877 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13878 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13879 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13880 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13885 ++processing_template_decl
;
13886 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13887 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13889 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13890 --processing_template_decl
;
13891 if (new_decl
== error_mark_node
)
13892 return error_mark_node
;
13894 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13895 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13896 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13899 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13900 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13904 DECL_TI_TEMPLATE (new_decl
) = r
;
13905 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13909 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13910 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13912 if (PRIMARY_TEMPLATE_P (t
))
13913 DECL_PRIMARY_TEMPLATE (r
) = r
;
13915 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13917 /* Record this non-type partial instantiation. */
13918 register_specialization (r
, t
,
13919 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13925 /* True if FN is the op() for a lambda in an uninstantiated template. */
13928 lambda_fn_in_template_p (tree fn
)
13930 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13932 tree closure
= DECL_CONTEXT (fn
);
13933 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13936 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13937 which the above is true. */
13940 instantiated_lambda_fn_p (tree fn
)
13942 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13944 tree closure
= DECL_CONTEXT (fn
);
13945 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13946 return LAMBDA_EXPR_INSTANTIATED (lam
);
13949 /* We're instantiating a variable from template function TCTX. Return the
13950 corresponding current enclosing scope. This gets complicated because lambda
13951 functions in templates are regenerated rather than instantiated, but generic
13952 lambda functions are subsequently instantiated. */
13955 enclosing_instantiation_of (tree otctx
)
13958 tree fn
= current_function_decl
;
13959 int lambda_count
= 0;
13961 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
13962 || instantiated_lambda_fn_p (tctx
));
13963 tctx
= decl_function_context (tctx
))
13965 for (; fn
; fn
= decl_function_context (fn
))
13968 int flambda_count
= 0;
13969 for (; fn
&& instantiated_lambda_fn_p (fn
);
13970 fn
= decl_function_context (fn
))
13972 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13973 ? most_general_template (fn
) != most_general_template (tctx
)
13976 if (flambda_count
!= lambda_count
)
13978 gcc_assert (flambda_count
> lambda_count
);
13979 for (; flambda_count
> lambda_count
; --flambda_count
)
13980 ofn
= decl_function_context (ofn
);
13982 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13983 || DECL_CONV_FN_P (ofn
));
13986 gcc_unreachable ();
13989 /* Substitute the ARGS into the T, which is a _DECL. Return the
13990 result of the substitution. Issue error and warning messages under
13991 control of COMPLAIN. */
13994 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13996 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13997 location_t saved_loc
;
13998 tree r
= NULL_TREE
;
14000 hashval_t hash
= 0;
14002 /* Set the filename and linenumber to improve error-reporting. */
14003 saved_loc
= input_location
;
14004 input_location
= DECL_SOURCE_LOCATION (t
);
14006 switch (TREE_CODE (t
))
14008 case TEMPLATE_DECL
:
14009 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14012 case FUNCTION_DECL
:
14013 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
14018 tree type
= NULL_TREE
;
14020 tree expanded_types
= NULL_TREE
;
14021 tree prev_r
= NULL_TREE
;
14022 tree first_r
= NULL_TREE
;
14024 if (DECL_PACK_P (t
))
14026 /* If there is a local specialization that isn't a
14027 parameter pack, it means that we're doing a "simple"
14028 substitution from inside tsubst_pack_expansion. Just
14029 return the local specialization (which will be a single
14031 tree spec
= retrieve_local_specialization (t
);
14033 && TREE_CODE (spec
) == PARM_DECL
14034 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
14037 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14038 the parameters in this function parameter pack. */
14039 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14040 complain
, in_decl
);
14041 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14043 len
= TREE_VEC_LENGTH (expanded_types
);
14045 /* Zero-length parameter packs are boring. Just substitute
14048 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
14053 /* All we did was update the type. Make a note of that. */
14054 type
= expanded_types
;
14055 expanded_types
= NULL_TREE
;
14059 /* Loop through all of the parameters we'll build. When T is
14060 a function parameter pack, LEN is the number of expanded
14061 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14063 for (i
= 0; i
< len
; ++i
)
14067 if (DECL_TEMPLATE_PARM_P (t
))
14068 SET_DECL_TEMPLATE_PARM_P (r
);
14070 if (expanded_types
)
14071 /* We're on the Ith parameter of the function parameter
14074 /* Get the Ith type. */
14075 type
= TREE_VEC_ELT (expanded_types
, i
);
14077 /* Rename the parameter to include the index. */
14079 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14082 /* We're dealing with a normal parameter. */
14083 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14085 type
= type_decays_to (type
);
14086 TREE_TYPE (r
) = type
;
14087 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14089 if (DECL_INITIAL (r
))
14091 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
14092 DECL_INITIAL (r
) = TREE_TYPE (r
);
14094 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
14095 complain
, in_decl
);
14098 DECL_CONTEXT (r
) = NULL_TREE
;
14100 if (!DECL_TEMPLATE_PARM_P (r
))
14101 DECL_ARG_TYPE (r
) = type_passed_as (type
);
14103 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14104 args
, complain
, in_decl
);
14106 /* Keep track of the first new parameter we
14107 generate. That's what will be returned to the
14112 /* Build a proper chain of parameters when substituting
14113 into a function parameter pack. */
14115 DECL_CHAIN (prev_r
) = r
;
14118 /* If cp_unevaluated_operand is set, we're just looking for a
14119 single dummy parameter, so don't keep going. */
14120 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
14121 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
14122 complain
, DECL_CHAIN (t
));
14124 /* FIRST_R contains the start of the chain we've built. */
14131 tree type
= NULL_TREE
;
14132 tree vec
= NULL_TREE
;
14133 tree expanded_types
= NULL_TREE
;
14136 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14138 /* This field is a lambda capture pack. Return a TREE_VEC of
14139 the expanded fields to instantiate_class_template_1. */
14140 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
14141 complain
, in_decl
);
14142 if (TREE_CODE (expanded_types
) == TREE_VEC
)
14144 len
= TREE_VEC_LENGTH (expanded_types
);
14145 vec
= make_tree_vec (len
);
14149 /* All we did was update the type. Make a note of that. */
14150 type
= expanded_types
;
14151 expanded_types
= NULL_TREE
;
14155 for (int i
= 0; i
< len
; ++i
)
14158 if (expanded_types
)
14160 type
= TREE_VEC_ELT (expanded_types
, i
);
14162 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
14165 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14167 if (type
== error_mark_node
)
14168 RETURN (error_mark_node
);
14169 TREE_TYPE (r
) = type
;
14170 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14172 if (DECL_C_BIT_FIELD (r
))
14173 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14175 DECL_BIT_FIELD_REPRESENTATIVE (r
)
14176 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
14178 /*integral_constant_expression_p=*/true);
14179 if (DECL_INITIAL (t
))
14181 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14182 NSDMI in perform_member_init. Still set DECL_INITIAL
14183 so that we know there is one. */
14184 DECL_INITIAL (r
) = void_node
;
14185 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
14186 retrofit_lang_decl (r
);
14187 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
14189 /* We don't have to set DECL_CONTEXT here; it is set by
14190 finish_member_declaration. */
14191 DECL_CHAIN (r
) = NULL_TREE
;
14193 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
14194 args
, complain
, in_decl
);
14197 TREE_VEC_ELT (vec
, i
) = r
;
14206 /* We reach here only for member using decls. We also need to check
14207 uses_template_parms because DECL_DEPENDENT_P is not set for a
14208 using-declaration that designates a member of the current
14209 instantiation (c++/53549). */
14210 if (DECL_DEPENDENT_P (t
)
14211 || uses_template_parms (USING_DECL_SCOPE (t
)))
14213 tree scope
= USING_DECL_SCOPE (t
);
14214 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
14215 if (PACK_EXPANSION_P (scope
))
14217 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
14218 int len
= TREE_VEC_LENGTH (vec
);
14219 r
= make_tree_vec (len
);
14220 for (int i
= 0; i
< len
; ++i
)
14222 tree escope
= TREE_VEC_ELT (vec
, i
);
14223 tree elt
= do_class_using_decl (escope
, name
);
14226 r
= error_mark_node
;
14231 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
14232 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
14234 TREE_VEC_ELT (r
, i
) = elt
;
14239 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
14240 complain
, in_decl
);
14241 r
= do_class_using_decl (inst_scope
, name
);
14243 r
= error_mark_node
;
14246 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
14247 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
14254 DECL_CHAIN (r
) = NULL_TREE
;
14261 tree argvec
= NULL_TREE
;
14262 tree gen_tmpl
= NULL_TREE
;
14264 tree tmpl
= NULL_TREE
;
14266 tree type
= NULL_TREE
;
14269 if (TREE_TYPE (t
) == error_mark_node
)
14270 RETURN (error_mark_node
);
14272 if (TREE_CODE (t
) == TYPE_DECL
14273 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
14275 /* If this is the canonical decl, we don't have to
14276 mess with instantiations, and often we can't (for
14277 typename, template type parms and such). Note that
14278 TYPE_NAME is not correct for the above test if
14279 we've copied the type for a typedef. */
14280 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14281 if (type
== error_mark_node
)
14282 RETURN (error_mark_node
);
14283 r
= TYPE_NAME (type
);
14287 /* Check to see if we already have the specialization we
14290 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
14292 /* T is a static data member or namespace-scope entity.
14293 We have to substitute into namespace-scope variables
14294 (not just variable templates) because of cases like:
14296 template <class T> void f() { extern T t; }
14298 where the entity referenced is not known until
14299 instantiation time. */
14301 ctx
= DECL_CONTEXT (t
);
14302 if (DECL_CLASS_SCOPE_P (t
))
14304 ctx
= tsubst_aggr_type (ctx
, args
,
14306 in_decl
, /*entering_scope=*/1);
14307 /* If CTX is unchanged, then T is in fact the
14308 specialization we want. That situation occurs when
14309 referencing a static data member within in its own
14310 class. We can use pointer equality, rather than
14311 same_type_p, because DECL_CONTEXT is always
14313 if (ctx
== DECL_CONTEXT (t
)
14314 /* ... unless T is a member template; in which
14315 case our caller can be willing to create a
14316 specialization of that template represented
14318 && !(DECL_TI_TEMPLATE (t
)
14319 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
14325 tmpl
= DECL_TI_TEMPLATE (t
);
14326 gen_tmpl
= most_general_template (tmpl
);
14327 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
14328 if (argvec
!= error_mark_node
)
14329 argvec
= (coerce_innermost_template_parms
14330 (DECL_TEMPLATE_PARMS (gen_tmpl
),
14331 argvec
, t
, complain
,
14332 /*all*/true, /*defarg*/true));
14333 if (argvec
== error_mark_node
)
14334 RETURN (error_mark_node
);
14335 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
14336 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
14341 /* A local variable. */
14343 /* Subsequent calls to pushdecl will fill this in. */
14345 /* Unless this is a reference to a static variable from an
14346 enclosing function, in which case we need to fill it in now. */
14347 if (TREE_STATIC (t
))
14349 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14350 if (fn
!= current_function_decl
)
14353 spec
= retrieve_local_specialization (t
);
14355 /* If we already have the specialization we need, there is
14356 nothing more to do. */
14363 /* Create a new node for the specialization we need. */
14364 if (type
== NULL_TREE
)
14366 if (is_typedef_decl (t
))
14367 type
= DECL_ORIGINAL_TYPE (t
);
14369 type
= TREE_TYPE (t
);
14371 && VAR_HAD_UNKNOWN_BOUND (t
)
14372 && type
!= error_mark_node
)
14373 type
= strip_array_domain (type
);
14374 tree sub_args
= args
;
14375 if (tree auto_node
= type_uses_auto (type
))
14377 /* Mask off any template args past the variable's context so we
14378 don't replace the auto with an unrelated argument. */
14379 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
14380 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
14382 /* This should never happen with the new lambda instantiation
14383 model, but keep the handling just in case. */
14384 gcc_assert (!CHECKING_P
),
14385 sub_args
= strip_innermost_template_args (args
, extra
);
14387 type
= tsubst (type
, sub_args
, complain
, in_decl
);
14388 /* Substituting the type might have recursively instantiated this
14389 same alias (c++/86171). */
14390 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
14391 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
14400 DECL_INITIALIZED_P (r
) = 0;
14401 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
14402 if (type
== error_mark_node
)
14403 RETURN (error_mark_node
);
14404 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14406 /* It may seem that this case cannot occur, since:
14411 declares a function, not a variable. However:
14414 template <typename T> void g() { T t; }
14415 template void g<f>();
14417 is an attempt to declare a variable with function
14419 error ("variable %qD has function type",
14420 /* R is not yet sufficiently initialized, so we
14421 just use its name. */
14423 RETURN (error_mark_node
);
14425 type
= complete_type (type
);
14426 /* Wait until cp_finish_decl to set this again, to handle
14427 circular dependency (template/instantiate6.C). */
14428 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
14429 type
= check_var_type (DECL_NAME (r
), type
,
14430 DECL_SOURCE_LOCATION (r
));
14431 if (DECL_HAS_VALUE_EXPR_P (t
))
14433 tree ve
= DECL_VALUE_EXPR (t
);
14434 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
14435 /*constant_expression_p=*/false);
14436 if (REFERENCE_REF_P (ve
))
14438 gcc_assert (TYPE_REF_P (type
));
14439 ve
= TREE_OPERAND (ve
, 0);
14441 SET_DECL_VALUE_EXPR (r
, ve
);
14443 if (CP_DECL_THREAD_LOCAL_P (r
)
14444 && !processing_template_decl
)
14445 set_decl_tls_model (r
, decl_default_tls_model (r
));
14447 else if (DECL_SELF_REFERENCE_P (t
))
14448 SET_DECL_SELF_REFERENCE_P (r
);
14449 TREE_TYPE (r
) = type
;
14450 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
14451 DECL_CONTEXT (r
) = ctx
;
14452 /* Clear out the mangled name and RTL for the instantiation. */
14453 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
14454 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
14455 SET_DECL_RTL (r
, NULL
);
14456 /* The initializer must not be expanded until it is required;
14457 see [temp.inst]. */
14458 DECL_INITIAL (r
) = NULL_TREE
;
14459 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
14462 if (DECL_LANG_SPECIFIC (r
))
14463 SET_DECL_DEPENDENT_INIT_P (r
, false);
14465 SET_DECL_MODE (r
, VOIDmode
);
14467 /* Possibly limit visibility based on template args. */
14468 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
14469 if (DECL_VISIBILITY_SPECIFIED (t
))
14471 DECL_VISIBILITY_SPECIFIED (r
) = 0;
14472 DECL_ATTRIBUTES (r
)
14473 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
14475 determine_visibility (r
);
14480 /* A static data member declaration is always marked
14481 external when it is declared in-class, even if an
14482 initializer is present. We mimic the non-template
14483 processing here. */
14484 DECL_EXTERNAL (r
) = 1;
14485 if (DECL_NAMESPACE_SCOPE_P (t
))
14486 DECL_NOT_REALLY_EXTERN (r
) = 1;
14488 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
14489 SET_DECL_IMPLICIT_INSTANTIATION (r
);
14490 /* Remember whether we require constant initialization of
14491 a non-constant template variable. */
14492 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r
))
14493 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t
));
14494 if (!error_operand_p (r
) || (complain
& tf_error
))
14495 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
14499 if (DECL_LANG_SPECIFIC (r
))
14500 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
14501 if (!cp_unevaluated_operand
)
14502 register_local_specialization (r
, t
);
14505 DECL_CHAIN (r
) = NULL_TREE
;
14507 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
14509 args
, complain
, in_decl
);
14511 /* Preserve a typedef that names a type. */
14512 if (is_typedef_decl (r
) && type
!= error_mark_node
)
14514 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
14515 set_underlying_type (r
);
14516 if (TYPE_DECL_ALIAS_P (r
))
14517 /* An alias template specialization can be dependent
14518 even if its underlying type is not. */
14519 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
14522 layout_decl (r
, 0);
14527 gcc_unreachable ();
14532 /* Restore the file and line information. */
14533 input_location
= saved_loc
;
14538 /* Substitute into the complete parameter type list PARMS. */
14541 tsubst_function_parms (tree parms
,
14543 tsubst_flags_t complain
,
14546 return tsubst_arg_types (parms
, args
, NULL_TREE
, complain
, in_decl
);
14549 /* Substitute into the ARG_TYPES of a function type.
14550 If END is a TREE_CHAIN, leave it and any following types
14554 tsubst_arg_types (tree arg_types
,
14557 tsubst_flags_t complain
,
14560 tree remaining_arg_types
;
14561 tree type
= NULL_TREE
;
14563 tree expanded_args
= NULL_TREE
;
14566 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
14569 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
14570 args
, end
, complain
, in_decl
);
14571 if (remaining_arg_types
== error_mark_node
)
14572 return error_mark_node
;
14574 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14576 /* For a pack expansion, perform substitution on the
14577 entire expression. Later on, we'll handle the arguments
14579 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14580 args
, complain
, in_decl
);
14582 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14583 /* So that we'll spin through the parameters, one by one. */
14584 i
= TREE_VEC_LENGTH (expanded_args
);
14587 /* We only partially substituted into the parameter
14588 pack. Our type is TYPE_PACK_EXPANSION. */
14589 type
= expanded_args
;
14590 expanded_args
= NULL_TREE
;
14598 type
= TREE_VEC_ELT (expanded_args
, i
);
14600 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14602 if (type
== error_mark_node
)
14603 return error_mark_node
;
14604 if (VOID_TYPE_P (type
))
14606 if (complain
& tf_error
)
14608 error ("invalid parameter type %qT", type
);
14610 error ("in declaration %q+D", in_decl
);
14612 return error_mark_node
;
14615 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14616 return error_mark_node
;
14618 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14619 top-level qualifiers as required. */
14620 type
= cv_unqualified (type_decays_to (type
));
14622 /* We do not substitute into default arguments here. The standard
14623 mandates that they be instantiated only when needed, which is
14624 done in build_over_call. */
14625 default_arg
= TREE_PURPOSE (arg_types
);
14627 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14628 since the new op() won't have any associated template arguments for us
14629 to refer to later. */
14630 if (lambda_fn_in_template_p (in_decl
))
14631 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14632 false/*fn*/, false/*constexpr*/);
14634 if (default_arg
&& TREE_CODE (default_arg
) == DEFERRED_PARSE
)
14636 /* We've instantiated a template before its default arguments
14637 have been parsed. This can happen for a nested template
14638 class, and is not an error unless we require the default
14639 argument in a call of this function. */
14640 remaining_arg_types
=
14641 tree_cons (default_arg
, type
, remaining_arg_types
);
14642 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg
),
14643 remaining_arg_types
);
14646 remaining_arg_types
=
14647 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14650 return remaining_arg_types
;
14653 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14654 *not* handle the exception-specification for FNTYPE, because the
14655 initial substitution of explicitly provided template parameters
14656 during argument deduction forbids substitution into the
14657 exception-specification:
14661 All references in the function type of the function template to the
14662 corresponding template parameters are replaced by the specified tem-
14663 plate argument values. If a substitution in a template parameter or
14664 in the function type of the function template results in an invalid
14665 type, type deduction fails. [Note: The equivalent substitution in
14666 exception specifications is done only when the function is instanti-
14667 ated, at which point a program is ill-formed if the substitution
14668 results in an invalid type.] */
14671 tsubst_function_type (tree t
,
14673 tsubst_flags_t complain
,
14677 tree arg_types
= NULL_TREE
;
14680 /* The TYPE_CONTEXT is not used for function/method types. */
14681 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14683 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14685 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14687 if (late_return_type_p
)
14689 /* Substitute the argument types. */
14690 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14691 complain
, in_decl
);
14692 if (arg_types
== error_mark_node
)
14693 return error_mark_node
;
14695 tree save_ccp
= current_class_ptr
;
14696 tree save_ccr
= current_class_ref
;
14697 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14698 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14699 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14702 /* DR 1207: 'this' is in scope in the trailing return type. */
14703 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14706 /* Substitute the return type. */
14707 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14711 current_class_ptr
= save_ccp
;
14712 current_class_ref
= save_ccr
;
14716 /* Substitute the return type. */
14717 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14719 if (return_type
== error_mark_node
)
14720 return error_mark_node
;
14721 /* DR 486 clarifies that creation of a function type with an
14722 invalid return type is a deduction failure. */
14723 if (TREE_CODE (return_type
) == ARRAY_TYPE
14724 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14726 if (complain
& tf_error
)
14728 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14729 error ("function returning an array");
14731 error ("function returning a function");
14733 return error_mark_node
;
14736 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14737 return error_mark_node
;
14739 if (!late_return_type_p
)
14741 /* Substitute the argument types. */
14742 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14743 complain
, in_decl
);
14744 if (arg_types
== error_mark_node
)
14745 return error_mark_node
;
14748 /* Construct a new type node and return it. */
14749 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14751 fntype
= build_function_type (return_type
, arg_types
);
14752 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14756 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14757 /* Don't pick up extra function qualifiers from the basetype. */
14758 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14759 if (! MAYBE_CLASS_TYPE_P (r
))
14763 Type deduction may fail for any of the following
14766 -- Attempting to create "pointer to member of T" when T
14767 is not a class type. */
14768 if (complain
& tf_error
)
14769 error ("creating pointer to member function of non-class type %qT",
14771 return error_mark_node
;
14774 fntype
= build_method_type_directly (r
, return_type
,
14775 TREE_CHAIN (arg_types
));
14777 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14779 /* See comment above. */
14780 tree raises
= NULL_TREE
;
14781 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14782 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14787 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14788 ARGS into that specification, and return the substituted
14789 specification. If there is no specification, return NULL_TREE. */
14792 tsubst_exception_specification (tree fntype
,
14794 tsubst_flags_t complain
,
14801 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14802 new_specs
= NULL_TREE
;
14803 if (specs
&& TREE_PURPOSE (specs
))
14805 /* A noexcept-specifier. */
14806 tree expr
= TREE_PURPOSE (specs
);
14807 if (TREE_CODE (expr
) == INTEGER_CST
)
14811 /* Defer instantiation of noexcept-specifiers to avoid
14812 excessive instantiations (c++/49107). */
14813 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14814 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14816 /* We already partially instantiated this member template,
14817 so combine the new args with the old. */
14818 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14819 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14820 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14821 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14825 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14826 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14831 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14833 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14835 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14837 new_specs
= tsubst_copy_and_build
14838 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14839 /*integral_constant_expression_p=*/true);
14841 new_specs
= build_noexcept_spec (new_specs
, complain
);
14845 if (! TREE_VALUE (specs
))
14852 tree expanded_specs
= NULL_TREE
;
14854 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14856 /* Expand the pack expansion type. */
14857 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14861 if (expanded_specs
== error_mark_node
)
14862 return error_mark_node
;
14863 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14864 len
= TREE_VEC_LENGTH (expanded_specs
);
14867 /* We're substituting into a member template, so
14868 we got a TYPE_PACK_EXPANSION back. Add that
14869 expansion and move on. */
14870 gcc_assert (TREE_CODE (expanded_specs
)
14871 == TYPE_PACK_EXPANSION
);
14872 new_specs
= add_exception_specifier (new_specs
,
14875 specs
= TREE_CHAIN (specs
);
14880 for (i
= 0; i
< len
; ++i
)
14882 if (expanded_specs
)
14883 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14885 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14886 if (spec
== error_mark_node
)
14888 new_specs
= add_exception_specifier (new_specs
, spec
,
14892 specs
= TREE_CHAIN (specs
);
14898 /* Take the tree structure T and replace template parameters used
14899 therein with the argument vector ARGS. IN_DECL is an associated
14900 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14901 Issue error and warning messages under control of COMPLAIN. Note
14902 that we must be relatively non-tolerant of extensions here, in
14903 order to preserve conformance; if we allow substitutions that
14904 should not be allowed, we may allow argument deductions that should
14905 not succeed, and therefore report ambiguous overload situations
14906 where there are none. In theory, we could allow the substitution,
14907 but indicate that it should have failed, and allow our caller to
14908 make sure that the right thing happens, but we don't try to do this
14911 This function is used for dealing with types, decls and the like;
14912 for expressions, use tsubst_expr or tsubst_copy. */
14915 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14917 enum tree_code code
;
14918 tree type
, r
= NULL_TREE
;
14920 if (t
== NULL_TREE
|| t
== error_mark_node
14921 || t
== integer_type_node
14922 || t
== void_type_node
14923 || t
== char_type_node
14924 || t
== unknown_type_node
14925 || TREE_CODE (t
) == NAMESPACE_DECL
14926 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14930 return tsubst_decl (t
, args
, complain
);
14932 if (args
== NULL_TREE
)
14935 code
= TREE_CODE (t
);
14937 if (code
== IDENTIFIER_NODE
)
14938 type
= IDENTIFIER_TYPE_VALUE (t
);
14940 type
= TREE_TYPE (t
);
14942 gcc_assert (type
!= unknown_type_node
);
14944 /* Reuse typedefs. We need to do this to handle dependent attributes,
14945 such as attribute aligned. */
14947 && typedef_variant_p (t
))
14949 tree decl
= TYPE_NAME (t
);
14951 if (alias_template_specialization_p (t
, nt_opaque
))
14953 /* DECL represents an alias template and we want to
14955 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14956 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14957 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14959 else if (DECL_CLASS_SCOPE_P (decl
)
14960 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14961 && uses_template_parms (DECL_CONTEXT (decl
)))
14963 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14964 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14965 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14967 else if (DECL_FUNCTION_SCOPE_P (decl
)
14968 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14969 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14970 r
= retrieve_local_specialization (decl
);
14972 /* The typedef is from a non-template context. */
14978 r
= cp_build_qualified_type_real
14979 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14980 complain
| tf_ignore_bad_quals
);
14985 /* We don't have an instantiation yet, so drop the typedef. */
14986 int quals
= cp_type_quals (t
);
14987 t
= DECL_ORIGINAL_TYPE (decl
);
14988 t
= cp_build_qualified_type_real (t
, quals
,
14989 complain
| tf_ignore_bad_quals
);
14993 bool fndecl_type
= (complain
& tf_fndecl_type
);
14994 complain
&= ~tf_fndecl_type
;
14997 && code
!= TYPENAME_TYPE
14998 && code
!= TEMPLATE_TYPE_PARM
14999 && code
!= TEMPLATE_PARM_INDEX
15000 && code
!= IDENTIFIER_NODE
15001 && code
!= FUNCTION_TYPE
15002 && code
!= METHOD_TYPE
)
15003 type
= tsubst (type
, args
, complain
, in_decl
);
15004 if (type
== error_mark_node
)
15005 return error_mark_node
;
15011 case ENUMERAL_TYPE
:
15012 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
15013 /*entering_scope=*/0);
15016 case IDENTIFIER_NODE
:
15027 if (t
== integer_type_node
)
15030 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
15031 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
15035 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
15037 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
15038 /*integral_constant_expression_p=*/false);
15040 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15042 if (TREE_CODE (max
) == NOP_EXPR
15043 && TREE_SIDE_EFFECTS (omax
)
15044 && !TREE_TYPE (max
))
15045 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
15047 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15048 with TREE_SIDE_EFFECTS that indicates this is not an integral
15049 constant expression. */
15050 if (processing_template_decl
15051 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
15053 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
15054 TREE_SIDE_EFFECTS (max
) = 1;
15057 return compute_array_index_type (NULL_TREE
, max
, complain
);
15060 case TEMPLATE_TYPE_PARM
:
15061 case TEMPLATE_TEMPLATE_PARM
:
15062 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15063 case TEMPLATE_PARM_INDEX
:
15068 tree arg
= NULL_TREE
;
15070 /* Early in template argument deduction substitution, we don't
15071 want to reduce the level of 'auto', or it will be confused
15072 with a normal template parm in subsequent deduction. */
15073 if (is_auto (t
) && (complain
& tf_partial
))
15078 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
15079 template_parm_level_and_index (t
, &level
, &idx
);
15081 levels
= TMPL_ARGS_DEPTH (args
);
15082 if (level
<= levels
15083 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
15085 arg
= TMPL_ARG (args
, level
, idx
);
15087 /* See through ARGUMENT_PACK_SELECT arguments. */
15088 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
15089 arg
= argument_pack_select_arg (arg
);
15092 if (arg
== error_mark_node
)
15093 return error_mark_node
;
15094 else if (arg
!= NULL_TREE
)
15096 if (ARGUMENT_PACK_P (arg
))
15097 /* If ARG is an argument pack, we don't actually want to
15098 perform a substitution here, because substitutions
15099 for argument packs are only done
15100 element-by-element. We can get to this point when
15101 substituting the type of a non-type template
15102 parameter pack, when that type actually contains
15103 template parameter packs from an outer template, e.g.,
15105 template<typename... Types> struct A {
15106 template<Types... Values> struct B { };
15110 if (code
== TEMPLATE_TYPE_PARM
)
15114 /* When building concept checks for the purpose of
15115 deducing placeholders, we can end up with wildcards
15116 where types are expected. Adjust this to the deduced
15118 if (TREE_CODE (arg
) == WILDCARD_DECL
)
15119 arg
= TREE_TYPE (TREE_TYPE (arg
));
15121 gcc_assert (TYPE_P (arg
));
15123 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
15125 return cp_build_qualified_type_real
15126 (arg
, quals
, complain
| tf_ignore_bad_quals
);
15128 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15130 /* We are processing a type constructed from a
15131 template template parameter. */
15132 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
15133 args
, complain
, in_decl
);
15134 if (argvec
== error_mark_node
)
15135 return error_mark_node
;
15137 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
15138 || TREE_CODE (arg
) == TEMPLATE_DECL
15139 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
15141 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
15142 /* Consider this code:
15144 template <template <class> class Template>
15146 template <class Arg> using Bind = Template<Arg>;
15149 template <template <class> class Template, class Arg>
15150 using Instantiate = Template<Arg>; //#0
15152 template <template <class> class Template,
15155 Instantiate<Internal<Template>::template Bind,
15158 When #1 is parsed, the
15159 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15160 parameter `Template' in #0 matches the
15161 UNBOUND_CLASS_TEMPLATE representing the argument
15162 `Internal<Template>::template Bind'; We then want
15163 to assemble the type `Bind<Argument>' that can't
15164 be fully created right now, because
15165 `Internal<Template>' not being complete, the Bind
15166 template cannot be looked up in that context. So
15167 we need to "store" `Bind<Argument>' for later
15168 when the context of Bind becomes complete. Let's
15169 store that in a TYPENAME_TYPE. */
15170 return make_typename_type (TYPE_CONTEXT (arg
),
15171 build_nt (TEMPLATE_ID_EXPR
,
15172 TYPE_IDENTIFIER (arg
),
15177 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15178 are resolving nested-types in the signature of a
15179 member function templates. Otherwise ARG is a
15180 TEMPLATE_DECL and is the real template to be
15182 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15183 arg
= TYPE_NAME (arg
);
15185 r
= lookup_template_class (arg
,
15187 DECL_CONTEXT (arg
),
15188 /*entering_scope=*/0,
15190 return cp_build_qualified_type_real
15191 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
15193 else if (code
== TEMPLATE_TEMPLATE_PARM
)
15196 /* TEMPLATE_PARM_INDEX. */
15197 return convert_from_reference (unshare_expr (arg
));
15201 /* This can happen during the attempted tsubst'ing in
15202 unify. This means that we don't yet have any information
15203 about the template parameter in question. */
15206 /* If we get here, we must have been looking at a parm for a
15207 more deeply nested template. Make a new version of this
15208 template parameter, but with a lower level. */
15211 case TEMPLATE_TYPE_PARM
:
15212 case TEMPLATE_TEMPLATE_PARM
:
15213 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15214 if (cp_type_quals (t
))
15216 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
15217 r
= cp_build_qualified_type_real
15218 (r
, cp_type_quals (t
),
15219 complain
| (code
== TEMPLATE_TYPE_PARM
15220 ? tf_ignore_bad_quals
: 0));
15222 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15223 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15224 && (r
= (TEMPLATE_PARM_DESCENDANTS
15225 (TEMPLATE_TYPE_PARM_INDEX (t
))))
15226 && (r
= TREE_TYPE (r
))
15227 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
15228 /* Break infinite recursion when substituting the constraints
15229 of a constrained placeholder. */;
15230 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
15231 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
15232 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
15233 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
15234 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
15235 && (TEMPLATE_PARM_LEVEL (r
)
15236 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
15237 /* Cache the simple case of lowering a type parameter. */
15242 TEMPLATE_TYPE_PARM_INDEX (r
)
15243 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
15244 r
, levels
, args
, complain
);
15245 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
15246 TYPE_MAIN_VARIANT (r
) = r
;
15247 TYPE_POINTER_TO (r
) = NULL_TREE
;
15248 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
15250 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
15252 /* Propagate constraints on placeholders since they are
15253 only instantiated during satisfaction. */
15254 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
15255 PLACEHOLDER_TYPE_CONSTRAINTS (r
) = constr
;
15256 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
15258 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
15259 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
15263 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
15264 /* We have reduced the level of the template
15265 template parameter, but not the levels of its
15266 template parameters, so canonical_type_parameter
15267 will not be able to find the canonical template
15268 template parameter for this level. Thus, we
15269 require structural equality checking to compare
15270 TEMPLATE_TEMPLATE_PARMs. */
15271 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15272 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
15273 SET_TYPE_STRUCTURAL_EQUALITY (r
);
15275 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
15277 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
15279 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
15280 /* We might need to substitute into the types of non-type
15281 template parameters. */
15282 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
15283 complain
, in_decl
);
15284 if (tmpl
== error_mark_node
)
15285 return error_mark_node
;
15286 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
15287 complain
, in_decl
);
15288 if (argvec
== error_mark_node
)
15289 return error_mark_node
;
15291 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
15292 = build_template_info (tmpl
, argvec
);
15297 case TEMPLATE_PARM_INDEX
:
15298 /* OK, now substitute the type of the non-type parameter. We
15299 couldn't do it earlier because it might be an auto parameter,
15300 and we wouldn't need to if we had an argument. */
15301 type
= tsubst (type
, args
, complain
, in_decl
);
15302 if (type
== error_mark_node
)
15303 return error_mark_node
;
15304 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
15308 gcc_unreachable ();
15316 tree purpose
, value
, chain
;
15318 if (t
== void_list_node
)
15321 purpose
= TREE_PURPOSE (t
);
15324 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
15325 if (purpose
== error_mark_node
)
15326 return error_mark_node
;
15328 value
= TREE_VALUE (t
);
15331 value
= tsubst (value
, args
, complain
, in_decl
);
15332 if (value
== error_mark_node
)
15333 return error_mark_node
;
15335 chain
= TREE_CHAIN (t
);
15336 if (chain
&& chain
!= void_type_node
)
15338 chain
= tsubst (chain
, args
, complain
, in_decl
);
15339 if (chain
== error_mark_node
)
15340 return error_mark_node
;
15342 if (purpose
== TREE_PURPOSE (t
)
15343 && value
== TREE_VALUE (t
)
15344 && chain
== TREE_CHAIN (t
))
15346 return hash_tree_cons (purpose
, value
, chain
);
15350 /* We should never be tsubsting a binfo. */
15351 gcc_unreachable ();
15354 /* A vector of template arguments. */
15355 gcc_assert (!type
);
15356 return tsubst_template_args (t
, args
, complain
, in_decl
);
15359 case REFERENCE_TYPE
:
15361 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
15366 Type deduction may fail for any of the following
15369 -- Attempting to create a pointer to reference type.
15370 -- Attempting to create a reference to a reference type or
15371 a reference to void.
15373 Core issue 106 says that creating a reference to a reference
15374 during instantiation is no longer a cause for failure. We
15375 only enforce this check in strict C++98 mode. */
15376 if ((TYPE_REF_P (type
)
15377 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
15378 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
15380 static location_t last_loc
;
15382 /* We keep track of the last time we issued this error
15383 message to avoid spewing a ton of messages during a
15384 single bad template instantiation. */
15385 if (complain
& tf_error
15386 && last_loc
!= input_location
)
15388 if (VOID_TYPE_P (type
))
15389 error ("forming reference to void");
15390 else if (code
== POINTER_TYPE
)
15391 error ("forming pointer to reference type %qT", type
);
15393 error ("forming reference to reference type %qT", type
);
15394 last_loc
= input_location
;
15397 return error_mark_node
;
15399 else if (TREE_CODE (type
) == FUNCTION_TYPE
15400 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
15401 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
15403 if (complain
& tf_error
)
15405 if (code
== POINTER_TYPE
)
15406 error ("forming pointer to qualified function type %qT",
15409 error ("forming reference to qualified function type %qT",
15412 return error_mark_node
;
15414 else if (code
== POINTER_TYPE
)
15416 r
= build_pointer_type (type
);
15417 if (TREE_CODE (type
) == METHOD_TYPE
)
15418 r
= build_ptrmemfunc_type (r
);
15420 else if (TYPE_REF_P (type
))
15421 /* In C++0x, during template argument substitution, when there is an
15422 attempt to create a reference to a reference type, reference
15423 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15425 "If a template-argument for a template-parameter T names a type
15426 that is a reference to a type A, an attempt to create the type
15427 'lvalue reference to cv T' creates the type 'lvalue reference to
15428 A,' while an attempt to create the type type rvalue reference to
15429 cv T' creates the type T"
15431 r
= cp_build_reference_type
15433 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
15435 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
15436 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
15438 if (r
!= error_mark_node
)
15439 /* Will this ever be needed for TYPE_..._TO values? */
15446 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
15447 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
15451 Type deduction may fail for any of the following
15454 -- Attempting to create "pointer to member of T" when T
15455 is not a class type. */
15456 if (complain
& tf_error
)
15457 error ("creating pointer to member of non-class type %qT", r
);
15458 return error_mark_node
;
15460 if (TYPE_REF_P (type
))
15462 if (complain
& tf_error
)
15463 error ("creating pointer to member reference type %qT", type
);
15464 return error_mark_node
;
15466 if (VOID_TYPE_P (type
))
15468 if (complain
& tf_error
)
15469 error ("creating pointer to member of type void");
15470 return error_mark_node
;
15472 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
15473 if (TREE_CODE (type
) == FUNCTION_TYPE
)
15475 /* The type of the implicit object parameter gets its
15476 cv-qualifiers from the FUNCTION_TYPE. */
15479 = build_memfn_type (type
, r
, type_memfn_quals (type
),
15480 type_memfn_rqual (type
));
15481 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
15482 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
15486 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
15490 case FUNCTION_TYPE
:
15495 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
15496 if (fntype
== error_mark_node
)
15497 return error_mark_node
;
15499 /* Substitute the exception specification. */
15500 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
15501 /*defer_ok*/fndecl_type
);
15502 if (specs
== error_mark_node
)
15503 return error_mark_node
;
15505 fntype
= build_exception_variant (fntype
, specs
);
15510 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
15511 if (domain
== error_mark_node
)
15512 return error_mark_node
;
15514 /* As an optimization, we avoid regenerating the array type if
15515 it will obviously be the same as T. */
15516 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
15519 /* These checks should match the ones in create_array_type_for_decl.
15523 The deduction may fail for any of the following reasons:
15525 -- Attempting to create an array with an element type that
15526 is void, a function type, or a reference type, or [DR337]
15527 an abstract class type. */
15528 if (VOID_TYPE_P (type
)
15529 || TREE_CODE (type
) == FUNCTION_TYPE
15530 || (TREE_CODE (type
) == ARRAY_TYPE
15531 && TYPE_DOMAIN (type
) == NULL_TREE
)
15532 || TYPE_REF_P (type
))
15534 if (complain
& tf_error
)
15535 error ("creating array of %qT", type
);
15536 return error_mark_node
;
15539 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
15540 return error_mark_node
;
15542 r
= build_cplus_array_type (type
, domain
);
15544 if (!valid_array_size_p (input_location
, r
, in_decl
,
15545 (complain
& tf_error
)))
15546 return error_mark_node
;
15548 if (TYPE_USER_ALIGN (t
))
15550 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
15551 TYPE_USER_ALIGN (r
) = 1;
15557 case TYPENAME_TYPE
:
15559 tree ctx
= TYPE_CONTEXT (t
);
15560 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
15562 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
15563 if (ctx
== error_mark_node
15564 || TREE_VEC_LENGTH (ctx
) > 1)
15565 return error_mark_node
;
15566 if (TREE_VEC_LENGTH (ctx
) == 0)
15568 if (complain
& tf_error
)
15569 error ("%qD is instantiated for an empty pack",
15570 TYPENAME_TYPE_FULLNAME (t
));
15571 return error_mark_node
;
15573 ctx
= TREE_VEC_ELT (ctx
, 0);
15576 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
15577 /*entering_scope=*/1);
15578 if (ctx
== error_mark_node
)
15579 return error_mark_node
;
15581 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
15582 complain
, in_decl
);
15583 if (f
== error_mark_node
)
15584 return error_mark_node
;
15586 if (!MAYBE_CLASS_TYPE_P (ctx
))
15588 if (complain
& tf_error
)
15589 error ("%qT is not a class, struct, or union type", ctx
);
15590 return error_mark_node
;
15592 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15594 /* Normally, make_typename_type does not require that the CTX
15595 have complete type in order to allow things like:
15597 template <class T> struct S { typename S<T>::X Y; };
15599 But, such constructs have already been resolved by this
15600 point, so here CTX really should have complete type, unless
15601 it's a partial instantiation. */
15602 ctx
= complete_type (ctx
);
15603 if (!COMPLETE_TYPE_P (ctx
))
15605 if (complain
& tf_error
)
15606 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15607 return error_mark_node
;
15611 f
= make_typename_type (ctx
, f
, typename_type
,
15612 complain
| tf_keep_type_decl
);
15613 if (f
== error_mark_node
)
15615 if (TREE_CODE (f
) == TYPE_DECL
)
15617 complain
|= tf_ignore_bad_quals
;
15621 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15623 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15625 if (complain
& tf_error
)
15626 error ("%qT resolves to %qT, which is not an enumeration type",
15629 return error_mark_node
;
15631 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15633 if (complain
& tf_error
)
15634 error ("%qT resolves to %qT, which is is not a class type",
15637 return error_mark_node
;
15641 return cp_build_qualified_type_real
15642 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15645 case UNBOUND_CLASS_TEMPLATE
:
15647 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15648 in_decl
, /*entering_scope=*/1);
15649 tree name
= TYPE_IDENTIFIER (t
);
15650 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15652 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15653 return error_mark_node
;
15656 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15657 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15664 ++cp_unevaluated_operand
;
15665 ++c_inhibit_evaluation_warnings
;
15667 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15669 /*integral_constant_expression_p=*/false);
15671 --cp_unevaluated_operand
;
15672 --c_inhibit_evaluation_warnings
;
15674 type
= finish_typeof (type
);
15675 return cp_build_qualified_type_real (type
,
15677 | cp_type_quals (type
),
15681 case DECLTYPE_TYPE
:
15685 ++cp_unevaluated_operand
;
15686 ++c_inhibit_evaluation_warnings
;
15688 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15689 complain
|tf_decltype
, in_decl
,
15690 /*function_p*/false,
15691 /*integral_constant_expression*/false);
15693 --cp_unevaluated_operand
;
15694 --c_inhibit_evaluation_warnings
;
15696 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15697 type
= lambda_capture_field_type (type
,
15698 false /*explicit_init*/,
15699 DECLTYPE_FOR_REF_CAPTURE (t
));
15700 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15701 type
= lambda_proxy_type (type
);
15704 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15705 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15707 /* In a template ~id could be either a complement expression
15708 or an unqualified-id naming a destructor; if instantiating
15709 it produces an expression, it's not an id-expression or
15712 type
= finish_decltype_type (type
, id
, complain
);
15714 return cp_build_qualified_type_real (type
,
15716 | cp_type_quals (type
),
15717 complain
| tf_ignore_bad_quals
);
15720 case UNDERLYING_TYPE
:
15722 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15723 complain
, in_decl
);
15724 return finish_underlying_type (type
);
15727 case TYPE_ARGUMENT_PACK
:
15728 case NONTYPE_ARGUMENT_PACK
:
15732 if (code
== NONTYPE_ARGUMENT_PACK
)
15733 r
= make_node (code
);
15735 r
= cxx_make_type (code
);
15737 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15738 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15739 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15757 /* We should use one of the expression tsubsts for these codes. */
15758 gcc_unreachable ();
15761 sorry ("use of %qs in template", get_tree_code_name (code
));
15762 return error_mark_node
;
15766 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15767 expression on the left-hand side of the "." or "->" operator. We
15768 only do the lookup if we had a dependent BASELINK. Otherwise we
15769 adjust it onto the instantiated heirarchy. */
15772 tsubst_baselink (tree baselink
, tree object_type
,
15773 tree args
, tsubst_flags_t complain
, tree in_decl
)
15775 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15776 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15777 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15779 tree optype
= BASELINK_OPTYPE (baselink
);
15780 optype
= tsubst (optype
, args
, complain
, in_decl
);
15782 tree template_args
= NULL_TREE
;
15783 bool template_id_p
= false;
15784 tree fns
= BASELINK_FUNCTIONS (baselink
);
15785 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15787 template_id_p
= true;
15788 template_args
= TREE_OPERAND (fns
, 1);
15789 fns
= TREE_OPERAND (fns
, 0);
15791 template_args
= tsubst_template_args (template_args
, args
,
15792 complain
, in_decl
);
15795 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15796 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15797 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15801 tree name
= OVL_NAME (fns
);
15802 if (IDENTIFIER_CONV_OP_P (name
))
15803 name
= make_conv_op_name (optype
);
15805 if (name
== complete_dtor_identifier
)
15806 /* Treat as-if non-dependent below. */
15807 dependent_p
= false;
15809 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15812 if ((complain
& tf_error
)
15813 && constructor_name_p (name
, qualifying_scope
))
15814 error ("cannot call constructor %<%T::%D%> directly",
15815 qualifying_scope
, name
);
15816 return error_mark_node
;
15819 if (BASELINK_P (baselink
))
15820 fns
= BASELINK_FUNCTIONS (baselink
);
15823 /* We're going to overwrite pieces below, make a duplicate. */
15824 baselink
= copy_node (baselink
);
15826 /* If lookup found a single function, mark it as used at this point.
15827 (If lookup found multiple functions the one selected later by
15828 overload resolution will be marked as used at that point.) */
15829 if (!template_id_p
&& !really_overloaded_fn (fns
))
15831 tree fn
= OVL_FIRST (fns
);
15832 bool ok
= mark_used (fn
, complain
);
15833 if (!ok
&& !(complain
& tf_error
))
15834 return error_mark_node
;
15835 if (ok
&& BASELINK_P (baselink
))
15836 /* We might have instantiated an auto function. */
15837 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15840 if (BASELINK_P (baselink
))
15842 /* Add back the template arguments, if present. */
15844 BASELINK_FUNCTIONS (baselink
)
15845 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15847 /* Update the conversion operator type. */
15848 BASELINK_OPTYPE (baselink
) = optype
;
15852 object_type
= current_class_type
;
15854 if (qualified_p
|| !dependent_p
)
15856 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15860 /* We need to call adjust_result_of_qualified_name_lookup in case the
15861 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15862 so that we still get virtual function binding. */
15863 BASELINK_QUALIFIED_P (baselink
) = false;
15869 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15870 true if the qualified-id will be a postfix-expression in-and-of
15871 itself; false if more of the postfix-expression follows the
15872 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15876 tsubst_qualified_id (tree qualified_id
, tree args
,
15877 tsubst_flags_t complain
, tree in_decl
,
15878 bool done
, bool address_p
)
15884 tree template_args
;
15885 location_t loc
= UNKNOWN_LOCATION
;
15887 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15889 /* Figure out what name to look up. */
15890 name
= TREE_OPERAND (qualified_id
, 1);
15891 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15893 is_template
= true;
15894 loc
= EXPR_LOCATION (name
);
15895 template_args
= TREE_OPERAND (name
, 1);
15897 template_args
= tsubst_template_args (template_args
, args
,
15898 complain
, in_decl
);
15899 if (template_args
== error_mark_node
)
15900 return error_mark_node
;
15901 name
= TREE_OPERAND (name
, 0);
15905 is_template
= false;
15906 template_args
= NULL_TREE
;
15909 /* Substitute into the qualifying scope. When there are no ARGS, we
15910 are just trying to simplify a non-dependent expression. In that
15911 case the qualifying scope may be dependent, and, in any case,
15912 substituting will not help. */
15913 scope
= TREE_OPERAND (qualified_id
, 0);
15916 scope
= tsubst (scope
, args
, complain
, in_decl
);
15917 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15922 if (dependent_scope_p (scope
))
15925 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15926 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15927 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15928 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15932 if (!BASELINK_P (name
) && !DECL_P (expr
))
15934 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15936 /* A BIT_NOT_EXPR is used to represent a destructor. */
15937 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15939 error ("qualifying type %qT does not match destructor name ~%qT",
15940 scope
, TREE_OPERAND (expr
, 0));
15941 expr
= error_mark_node
;
15944 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15945 /*is_type_p=*/0, false);
15948 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15949 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15950 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15952 if (complain
& tf_error
)
15954 error ("dependent-name %qE is parsed as a non-type, but "
15955 "instantiation yields a type", qualified_id
);
15956 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15958 return error_mark_node
;
15964 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15966 /* Remember that there was a reference to this entity. */
15967 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15968 return error_mark_node
;
15971 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15973 if (complain
& tf_error
)
15974 qualified_name_lookup_error (scope
,
15975 TREE_OPERAND (qualified_id
, 1),
15976 expr
, input_location
);
15977 return error_mark_node
;
15982 /* We may be repeating a check already done during parsing, but
15983 if it was well-formed and passed then, it will pass again
15984 now, and if it didn't, we wouldn't have got here. The case
15985 we want to catch is when we couldn't tell then, and can now,
15986 namely when templ prior to substitution was an
15988 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15989 return error_mark_node
;
15991 if (variable_template_p (expr
))
15992 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15995 expr
= lookup_template_function (expr
, template_args
);
15998 if (expr
== error_mark_node
&& complain
& tf_error
)
15999 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
16000 expr
, input_location
);
16001 else if (TYPE_P (scope
))
16003 expr
= (adjust_result_of_qualified_name_lookup
16004 (expr
, scope
, current_nonlambda_class_type ()));
16005 expr
= (finish_qualified_id_expr
16006 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
16007 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
16008 /*template_arg_p=*/false, complain
));
16011 /* Expressions do not generally have reference type. */
16012 if (TREE_CODE (expr
) != SCOPE_REF
16013 /* However, if we're about to form a pointer-to-member, we just
16014 want the referenced member referenced. */
16015 && TREE_CODE (expr
) != OFFSET_REF
)
16016 expr
= convert_from_reference (expr
);
16018 if (REF_PARENTHESIZED_P (qualified_id
))
16019 expr
= force_paren_expr (expr
);
16024 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16025 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16029 tsubst_init (tree init
, tree decl
, tree args
,
16030 tsubst_flags_t complain
, tree in_decl
)
16035 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
16037 tree type
= TREE_TYPE (decl
);
16039 if (!init
&& type
!= error_mark_node
)
16041 if (tree auto_node
= type_uses_auto (type
))
16043 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
16045 if (complain
& tf_error
)
16046 error ("initializer for %q#D expands to an empty list "
16047 "of expressions", decl
);
16048 return error_mark_node
;
16051 else if (!dependent_type_p (type
))
16053 /* If we had an initializer but it
16054 instantiated to nothing,
16055 value-initialize the object. This will
16056 only occur when the initializer was a
16057 pack expansion where the parameter packs
16058 used in that expansion were of length
16060 init
= build_value_init (type
, complain
);
16061 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
16062 init
= get_target_expr_sfinae (init
, complain
);
16063 if (TREE_CODE (init
) == TARGET_EXPR
)
16064 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
16071 /* Like tsubst, but deals with expressions. This function just replaces
16072 template parms; to finish processing the resultant expression, use
16073 tsubst_copy_and_build or tsubst_expr. */
16076 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16078 enum tree_code code
;
16081 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
16084 code
= TREE_CODE (t
);
16089 r
= retrieve_local_specialization (t
);
16091 if (r
== NULL_TREE
)
16093 /* We get here for a use of 'this' in an NSDMI. */
16094 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
16095 return current_class_ptr
;
16097 /* This can happen for a parameter name used later in a function
16098 declaration (such as in a late-specified return type). Just
16099 make a dummy decl, since it's only used for its type. */
16100 gcc_assert (cp_unevaluated_operand
!= 0);
16101 r
= tsubst_decl (t
, args
, complain
);
16102 /* Give it the template pattern as its context; its true context
16103 hasn't been instantiated yet and this is good enough for
16105 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
16108 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16109 r
= argument_pack_select_arg (r
);
16110 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
16111 return error_mark_node
;
16119 if (DECL_TEMPLATE_PARM_P (t
))
16120 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
16121 /* There is no need to substitute into namespace-scope
16123 if (DECL_NAMESPACE_SCOPE_P (t
))
16125 /* If ARGS is NULL, then T is known to be non-dependent. */
16126 if (args
== NULL_TREE
)
16127 return scalar_constant_value (t
);
16129 /* Unfortunately, we cannot just call lookup_name here.
16132 template <int I> int f() {
16134 struct S { void g() { E e = a; } };
16137 When we instantiate f<7>::S::g(), say, lookup_name is not
16138 clever enough to find f<7>::a. */
16140 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16141 /*entering_scope=*/0);
16143 for (v
= TYPE_VALUES (enum_type
);
16145 v
= TREE_CHAIN (v
))
16146 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
16147 return TREE_VALUE (v
);
16149 /* We didn't find the name. That should never happen; if
16150 name-lookup found it during preliminary parsing, we
16151 should find it again here during instantiation. */
16152 gcc_unreachable ();
16157 if (DECL_CONTEXT (t
))
16161 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
16162 /*entering_scope=*/1);
16163 if (ctx
!= DECL_CONTEXT (t
))
16165 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
16168 if (complain
& tf_error
)
16169 error ("using invalid field %qD", t
);
16170 return error_mark_node
;
16179 case FUNCTION_DECL
:
16180 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
16181 r
= tsubst (t
, args
, complain
, in_decl
);
16182 else if (local_variable_p (t
)
16183 && uses_template_parms (DECL_CONTEXT (t
)))
16185 r
= retrieve_local_specialization (t
);
16186 if (r
== NULL_TREE
)
16188 /* First try name lookup to find the instantiation. */
16189 r
= lookup_name (DECL_NAME (t
));
16194 /* During error-recovery we may find a non-variable,
16195 even an OVERLOAD: just bail out and avoid ICEs and
16196 duplicate diagnostics (c++/62207). */
16197 gcc_assert (seen_error ());
16198 return error_mark_node
;
16200 if (!is_capture_proxy (r
))
16202 /* Make sure the one we found is the one we want. */
16203 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
16204 if (ctx
!= DECL_CONTEXT (r
))
16213 /* This can happen for a variable used in a
16214 late-specified return type of a local lambda, or for a
16215 local static or constant. Building a new VAR_DECL
16216 should be OK in all those cases. */
16217 r
= tsubst_decl (t
, args
, complain
);
16218 if (local_specializations
)
16219 /* Avoid infinite recursion (79640). */
16220 register_local_specialization (r
, t
);
16221 if (decl_maybe_constant_var_p (r
))
16223 /* We can't call cp_finish_decl, so handle the
16224 initializer by hand. */
16225 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
16226 complain
, in_decl
);
16227 if (!processing_template_decl
)
16228 init
= maybe_constant_init (init
);
16229 if (processing_template_decl
16230 ? potential_constant_expression (init
)
16231 : reduced_constant_expression_p (init
))
16232 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
16233 = TREE_CONSTANT (r
) = true;
16234 DECL_INITIAL (r
) = init
;
16235 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
16237 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
16238 complain
, adc_variable_type
);
16240 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
16241 || decl_constant_var_p (r
)
16243 if (!processing_template_decl
16244 && !TREE_STATIC (r
))
16245 r
= process_outer_var_ref (r
, complain
);
16247 /* Remember this for subsequent uses. */
16248 if (local_specializations
)
16249 register_local_specialization (r
, t
);
16251 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
16252 r
= argument_pack_select_arg (r
);
16256 if (!mark_used (r
, complain
))
16257 return error_mark_node
;
16260 case NAMESPACE_DECL
:
16267 return tsubst_baselink (t
, current_nonlambda_class_type (),
16268 args
, complain
, in_decl
);
16270 case TEMPLATE_DECL
:
16271 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
16272 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
16273 args
, complain
, in_decl
);
16274 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
16275 return tsubst (t
, args
, complain
, in_decl
);
16276 else if (DECL_CLASS_SCOPE_P (t
)
16277 && uses_template_parms (DECL_CONTEXT (t
)))
16279 /* Template template argument like the following example need
16282 template <template <class> class TT> struct C {};
16283 template <class T> struct D {
16284 template <class U> struct E {};
16289 We are processing the template argument `E' in #1 for
16290 the template instantiation #2. Originally, `E' is a
16291 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16292 have to substitute this with one having context `D<int>'. */
16294 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
16295 if (dependent_scope_p (context
))
16297 /* When rewriting a constructor into a deduction guide, a
16298 non-dependent name can become dependent, so memtmpl<args>
16299 becomes context::template memtmpl<args>. */
16300 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16301 return build_qualified_name (type
, context
, DECL_NAME (t
),
16304 return lookup_field (context
, DECL_NAME(t
), 0, false);
16307 /* Ordinary template template argument. */
16310 case NON_LVALUE_EXPR
:
16311 case VIEW_CONVERT_EXPR
:
16313 /* Handle location wrappers by substituting the wrapped node
16314 first, *then* reusing the resulting type. Doing the type
16315 first ensures that we handle template parameters and
16316 parameter pack expansions. */
16317 if (location_wrapper_p (t
))
16319 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
16320 complain
, in_decl
);
16321 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
16323 tree op
= TREE_OPERAND (t
, 0);
16324 if (code
== VIEW_CONVERT_EXPR
16325 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16327 /* Wrapper to make a C++20 template parameter object const. */
16328 op
= tsubst_copy (op
, args
, complain
, in_decl
);
16329 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
16331 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16332 return build1 (code
, type
, op
);
16336 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
16340 /* We shouldn't see any other uses of these in templates. */
16341 gcc_unreachable ();
16345 case REINTERPRET_CAST_EXPR
:
16346 case CONST_CAST_EXPR
:
16347 case STATIC_CAST_EXPR
:
16348 case DYNAMIC_CAST_EXPR
:
16349 case IMPLICIT_CONV_EXPR
:
16353 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16354 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16355 return build1 (code
, type
, op0
);
16359 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16360 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16362 tree expanded
, op
= TREE_OPERAND (t
, 0);
16365 if (SIZEOF_EXPR_TYPE_P (t
))
16366 op
= TREE_TYPE (op
);
16368 ++cp_unevaluated_operand
;
16369 ++c_inhibit_evaluation_warnings
;
16370 /* We only want to compute the number of arguments. */
16371 if (PACK_EXPANSION_P (op
))
16372 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
16374 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
16375 args
, complain
, in_decl
);
16376 --cp_unevaluated_operand
;
16377 --c_inhibit_evaluation_warnings
;
16379 if (TREE_CODE (expanded
) == TREE_VEC
)
16381 len
= TREE_VEC_LENGTH (expanded
);
16382 /* Set TREE_USED for the benefit of -Wunused. */
16383 for (int i
= 0; i
< len
; i
++)
16384 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
16385 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
16388 if (expanded
== error_mark_node
)
16389 return error_mark_node
;
16390 else if (PACK_EXPANSION_P (expanded
)
16391 || (TREE_CODE (expanded
) == TREE_VEC
16392 && pack_expansion_args_count (expanded
)))
16395 if (PACK_EXPANSION_P (expanded
))
16397 else if (TREE_VEC_LENGTH (expanded
) == 1)
16398 expanded
= TREE_VEC_ELT (expanded
, 0);
16400 expanded
= make_argument_pack (expanded
);
16402 if (TYPE_P (expanded
))
16403 return cxx_sizeof_or_alignof_type (input_location
,
16404 expanded
, SIZEOF_EXPR
,
16406 complain
& tf_error
);
16408 return cxx_sizeof_or_alignof_expr (input_location
,
16409 expanded
, SIZEOF_EXPR
,
16410 complain
& tf_error
);
16413 return build_int_cst (size_type_node
, len
);
16415 if (SIZEOF_EXPR_TYPE_P (t
))
16417 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
16418 args
, complain
, in_decl
);
16419 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
16420 r
= build1 (SIZEOF_EXPR
,
16421 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
16422 SIZEOF_EXPR_TYPE_P (r
) = 1;
16429 case TRUTH_NOT_EXPR
:
16432 case UNARY_PLUS_EXPR
: /* Unary + */
16434 case AT_ENCODE_EXPR
:
16438 case REALPART_EXPR
:
16439 case IMAGPART_EXPR
:
16442 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16443 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16444 r
= build1 (code
, type
, op0
);
16445 if (code
== ALIGNOF_EXPR
)
16446 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
16450 case COMPONENT_REF
:
16455 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16456 name
= TREE_OPERAND (t
, 1);
16457 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
16459 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16460 complain
, in_decl
);
16461 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16463 else if (TREE_CODE (name
) == SCOPE_REF
16464 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
16466 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16467 complain
, in_decl
);
16468 name
= TREE_OPERAND (name
, 1);
16469 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
16470 complain
, in_decl
);
16471 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
16472 name
= build_qualified_name (/*type=*/NULL_TREE
,
16474 /*template_p=*/false);
16476 else if (BASELINK_P (name
))
16477 name
= tsubst_baselink (name
,
16478 non_reference (TREE_TYPE (object
)),
16482 name
= tsubst_copy (name
, args
, complain
, in_decl
);
16483 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
16489 case TRUNC_DIV_EXPR
:
16490 case CEIL_DIV_EXPR
:
16491 case FLOOR_DIV_EXPR
:
16492 case ROUND_DIV_EXPR
:
16493 case EXACT_DIV_EXPR
:
16497 case TRUNC_MOD_EXPR
:
16498 case FLOOR_MOD_EXPR
:
16499 case TRUTH_ANDIF_EXPR
:
16500 case TRUTH_ORIF_EXPR
:
16501 case TRUTH_AND_EXPR
:
16502 case TRUTH_OR_EXPR
:
16513 case COMPOUND_EXPR
:
16516 case PREDECREMENT_EXPR
:
16517 case PREINCREMENT_EXPR
:
16518 case POSTDECREMENT_EXPR
:
16519 case POSTINCREMENT_EXPR
:
16521 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16522 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16523 return build_nt (code
, op0
, op1
);
16528 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16529 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16530 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
16531 QUALIFIED_NAME_IS_TEMPLATE (t
));
16536 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16537 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16538 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
16543 int n
= VL_EXP_OPERAND_LENGTH (t
);
16544 tree result
= build_vl_exp (CALL_EXPR
, n
);
16546 for (i
= 0; i
< n
; i
++)
16547 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
16548 complain
, in_decl
);
16554 case PSEUDO_DTOR_EXPR
:
16555 case VEC_PERM_EXPR
:
16557 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16558 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16559 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16560 r
= build_nt (code
, op0
, op1
, op2
);
16561 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16567 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16568 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16569 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
16570 r
= build_nt (code
, op0
, op1
, op2
);
16571 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16577 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16578 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16579 r
= build_nt (code
, op0
, op1
);
16580 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16581 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16585 case TEMPLATE_ID_EXPR
:
16587 /* Substituted template arguments */
16588 tree fn
= TREE_OPERAND (t
, 0);
16589 tree targs
= TREE_OPERAND (t
, 1);
16591 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16593 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16595 return lookup_template_function (fn
, targs
);
16600 tree purpose
, value
, chain
;
16602 if (t
== void_list_node
)
16605 purpose
= TREE_PURPOSE (t
);
16607 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16608 value
= TREE_VALUE (t
);
16610 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16611 chain
= TREE_CHAIN (t
);
16612 if (chain
&& chain
!= void_type_node
)
16613 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16614 if (purpose
== TREE_PURPOSE (t
)
16615 && value
== TREE_VALUE (t
)
16616 && chain
== TREE_CHAIN (t
))
16618 return tree_cons (purpose
, value
, chain
);
16623 case ENUMERAL_TYPE
:
16625 case TEMPLATE_TYPE_PARM
:
16626 case TEMPLATE_TEMPLATE_PARM
:
16627 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16628 case TEMPLATE_PARM_INDEX
:
16630 case REFERENCE_TYPE
:
16632 case FUNCTION_TYPE
:
16635 case TYPENAME_TYPE
:
16636 case UNBOUND_CLASS_TEMPLATE
:
16638 case DECLTYPE_TYPE
:
16640 return tsubst (t
, args
, complain
, in_decl
);
16644 /* Fall through. */
16645 case IDENTIFIER_NODE
:
16646 if (IDENTIFIER_CONV_OP_P (t
))
16648 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16649 return make_conv_op_name (new_type
);
16655 /* This is handled by tsubst_copy_and_build. */
16656 gcc_unreachable ();
16660 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16661 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16662 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16665 case CLEANUP_POINT_EXPR
:
16666 /* We shouldn't have built any of these during initial template
16667 generation. Instead, they should be built during instantiation
16668 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16669 gcc_unreachable ();
16673 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16674 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16675 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16676 r
= build2 (code
, type
, op0
, op1
);
16677 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16678 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16679 && !(complain
& tf_error
))
16680 return error_mark_node
;
16684 case EXPR_PACK_EXPANSION
:
16685 error ("invalid use of pack expansion expression");
16686 return error_mark_node
;
16688 case NONTYPE_ARGUMENT_PACK
:
16689 error ("use %<...%> to expand argument pack");
16690 return error_mark_node
;
16693 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16701 /* Instantiate any typedefs in the type. */
16702 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16703 r
= fold_convert (type
, t
);
16704 gcc_assert (TREE_CODE (r
) == code
);
16709 /* These can sometimes show up in a partial instantiation, but never
16710 involve template parms. */
16711 gcc_assert (!uses_template_parms (t
));
16714 case UNARY_LEFT_FOLD_EXPR
:
16715 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16716 case UNARY_RIGHT_FOLD_EXPR
:
16717 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16718 case BINARY_LEFT_FOLD_EXPR
:
16719 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16720 case BINARY_RIGHT_FOLD_EXPR
:
16721 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16725 case DEBUG_BEGIN_STMT
:
16726 /* ??? There's no point in copying it for now, but maybe some
16727 day it will contain more information, such as a pointer back
16728 to the containing function, inlined copy or so. */
16732 /* We shouldn't get here, but keep going if !flag_checking. */
16734 gcc_unreachable ();
16739 /* Helper function for tsubst_omp_clauses, used for instantiation of
16740 OMP_CLAUSE_DECL of clauses. */
16743 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16744 tree in_decl
, tree
*iterator_cache
)
16746 if (decl
== NULL_TREE
)
16749 /* Handle OpenMP iterators. */
16750 if (TREE_CODE (decl
) == TREE_LIST
16751 && TREE_PURPOSE (decl
)
16752 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16755 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16756 ret
= iterator_cache
[1];
16760 begin_scope (sk_omp
, NULL
);
16761 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16763 *tp
= copy_node (it
);
16764 TREE_VEC_ELT (*tp
, 0)
16765 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16766 TREE_VEC_ELT (*tp
, 1)
16767 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16768 /*integral_constant_expression_p=*/false);
16769 TREE_VEC_ELT (*tp
, 2)
16770 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16771 /*integral_constant_expression_p=*/false);
16772 TREE_VEC_ELT (*tp
, 3)
16773 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16774 /*integral_constant_expression_p=*/false);
16775 TREE_CHAIN (*tp
) = NULL_TREE
;
16776 tp
= &TREE_CHAIN (*tp
);
16778 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16779 iterator_cache
[0] = TREE_PURPOSE (decl
);
16780 iterator_cache
[1] = ret
;
16782 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16787 /* Handle an OpenMP array section represented as a TREE_LIST (or
16788 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16789 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16790 TREE_LIST. We can handle it exactly the same as an array section
16791 (purpose, value, and a chain), even though the nomenclature
16792 (low_bound, length, etc) is different. */
16793 if (TREE_CODE (decl
) == TREE_LIST
)
16796 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16797 /*integral_constant_expression_p=*/false);
16798 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16799 /*integral_constant_expression_p=*/false);
16800 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16802 if (TREE_PURPOSE (decl
) == low_bound
16803 && TREE_VALUE (decl
) == length
16804 && TREE_CHAIN (decl
) == chain
)
16806 tree ret
= tree_cons (low_bound
, length
, chain
);
16807 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16808 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16811 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16812 /*integral_constant_expression_p=*/false);
16813 /* Undo convert_from_reference tsubst_expr could have called. */
16815 && REFERENCE_REF_P (ret
)
16816 && !REFERENCE_REF_P (decl
))
16817 ret
= TREE_OPERAND (ret
, 0);
16821 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16824 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16825 tree args
, tsubst_flags_t complain
, tree in_decl
)
16827 tree new_clauses
= NULL_TREE
, nc
, oc
;
16828 tree linear_no_step
= NULL_TREE
;
16829 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16831 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16833 nc
= copy_node (oc
);
16834 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16837 switch (OMP_CLAUSE_CODE (nc
))
16839 case OMP_CLAUSE_LASTPRIVATE
:
16840 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16842 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16843 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16844 in_decl
, /*integral_constant_expression_p=*/false);
16845 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16846 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16849 case OMP_CLAUSE_PRIVATE
:
16850 case OMP_CLAUSE_SHARED
:
16851 case OMP_CLAUSE_FIRSTPRIVATE
:
16852 case OMP_CLAUSE_COPYIN
:
16853 case OMP_CLAUSE_COPYPRIVATE
:
16854 case OMP_CLAUSE_UNIFORM
:
16855 case OMP_CLAUSE_DEPEND
:
16856 case OMP_CLAUSE_FROM
:
16857 case OMP_CLAUSE_TO
:
16858 case OMP_CLAUSE_MAP
:
16859 case OMP_CLAUSE_NONTEMPORAL
:
16860 case OMP_CLAUSE_USE_DEVICE_PTR
:
16861 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16862 case OMP_CLAUSE_IS_DEVICE_PTR
:
16863 case OMP_CLAUSE_INCLUSIVE
:
16864 case OMP_CLAUSE_EXCLUSIVE
:
16865 OMP_CLAUSE_DECL (nc
)
16866 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16867 in_decl
, iterator_cache
);
16869 case OMP_CLAUSE_TILE
:
16870 case OMP_CLAUSE_IF
:
16871 case OMP_CLAUSE_NUM_THREADS
:
16872 case OMP_CLAUSE_SCHEDULE
:
16873 case OMP_CLAUSE_COLLAPSE
:
16874 case OMP_CLAUSE_FINAL
:
16875 case OMP_CLAUSE_DEVICE
:
16876 case OMP_CLAUSE_DIST_SCHEDULE
:
16877 case OMP_CLAUSE_NUM_TEAMS
:
16878 case OMP_CLAUSE_THREAD_LIMIT
:
16879 case OMP_CLAUSE_SAFELEN
:
16880 case OMP_CLAUSE_SIMDLEN
:
16881 case OMP_CLAUSE_NUM_TASKS
:
16882 case OMP_CLAUSE_GRAINSIZE
:
16883 case OMP_CLAUSE_PRIORITY
:
16884 case OMP_CLAUSE_ORDERED
:
16885 case OMP_CLAUSE_HINT
:
16886 case OMP_CLAUSE_NUM_GANGS
:
16887 case OMP_CLAUSE_NUM_WORKERS
:
16888 case OMP_CLAUSE_VECTOR_LENGTH
:
16889 case OMP_CLAUSE_WORKER
:
16890 case OMP_CLAUSE_VECTOR
:
16891 case OMP_CLAUSE_ASYNC
:
16892 case OMP_CLAUSE_WAIT
:
16893 OMP_CLAUSE_OPERAND (nc
, 0)
16894 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16895 in_decl
, /*integral_constant_expression_p=*/false);
16897 case OMP_CLAUSE_REDUCTION
:
16898 case OMP_CLAUSE_IN_REDUCTION
:
16899 case OMP_CLAUSE_TASK_REDUCTION
:
16900 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16902 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16903 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16905 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16906 complain
, in_decl
);
16907 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16908 = build_qualified_name (NULL_TREE
, scope
,
16909 TREE_OPERAND (placeholder
, 1),
16913 gcc_assert (identifier_p (placeholder
));
16915 OMP_CLAUSE_DECL (nc
)
16916 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16919 case OMP_CLAUSE_GANG
:
16920 case OMP_CLAUSE_ALIGNED
:
16921 OMP_CLAUSE_DECL (nc
)
16922 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16924 OMP_CLAUSE_OPERAND (nc
, 1)
16925 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16926 in_decl
, /*integral_constant_expression_p=*/false);
16928 case OMP_CLAUSE_LINEAR
:
16929 OMP_CLAUSE_DECL (nc
)
16930 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16932 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16934 gcc_assert (!linear_no_step
);
16935 linear_no_step
= nc
;
16937 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16938 OMP_CLAUSE_LINEAR_STEP (nc
)
16939 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16940 complain
, in_decl
, NULL
);
16942 OMP_CLAUSE_LINEAR_STEP (nc
)
16943 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16945 /*integral_constant_expression_p=*/false);
16947 case OMP_CLAUSE_NOWAIT
:
16948 case OMP_CLAUSE_DEFAULT
:
16949 case OMP_CLAUSE_UNTIED
:
16950 case OMP_CLAUSE_MERGEABLE
:
16951 case OMP_CLAUSE_INBRANCH
:
16952 case OMP_CLAUSE_NOTINBRANCH
:
16953 case OMP_CLAUSE_PROC_BIND
:
16954 case OMP_CLAUSE_FOR
:
16955 case OMP_CLAUSE_PARALLEL
:
16956 case OMP_CLAUSE_SECTIONS
:
16957 case OMP_CLAUSE_TASKGROUP
:
16958 case OMP_CLAUSE_NOGROUP
:
16959 case OMP_CLAUSE_THREADS
:
16960 case OMP_CLAUSE_SIMD
:
16961 case OMP_CLAUSE_DEFAULTMAP
:
16962 case OMP_CLAUSE_ORDER
:
16963 case OMP_CLAUSE_BIND
:
16964 case OMP_CLAUSE_INDEPENDENT
:
16965 case OMP_CLAUSE_AUTO
:
16966 case OMP_CLAUSE_SEQ
:
16967 case OMP_CLAUSE_IF_PRESENT
:
16968 case OMP_CLAUSE_FINALIZE
:
16971 gcc_unreachable ();
16973 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16974 switch (OMP_CLAUSE_CODE (nc
))
16976 case OMP_CLAUSE_SHARED
:
16977 case OMP_CLAUSE_PRIVATE
:
16978 case OMP_CLAUSE_FIRSTPRIVATE
:
16979 case OMP_CLAUSE_LASTPRIVATE
:
16980 case OMP_CLAUSE_COPYPRIVATE
:
16981 case OMP_CLAUSE_LINEAR
:
16982 case OMP_CLAUSE_REDUCTION
:
16983 case OMP_CLAUSE_IN_REDUCTION
:
16984 case OMP_CLAUSE_TASK_REDUCTION
:
16985 case OMP_CLAUSE_USE_DEVICE_PTR
:
16986 case OMP_CLAUSE_USE_DEVICE_ADDR
:
16987 case OMP_CLAUSE_IS_DEVICE_PTR
:
16988 case OMP_CLAUSE_INCLUSIVE
:
16989 case OMP_CLAUSE_EXCLUSIVE
:
16990 /* tsubst_expr on SCOPE_REF results in returning
16991 finish_non_static_data_member result. Undo that here. */
16992 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16993 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16994 == IDENTIFIER_NODE
))
16996 tree t
= OMP_CLAUSE_DECL (nc
);
16999 switch (TREE_CODE (v
))
17001 case COMPONENT_REF
:
17005 case POINTER_PLUS_EXPR
:
17006 v
= TREE_OPERAND (v
, 0);
17009 if (DECL_CONTEXT (v
) == current_function_decl
17010 && DECL_ARTIFICIAL (v
)
17011 && DECL_NAME (v
) == this_identifier
)
17012 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
17019 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
17020 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
17021 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
17022 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
17023 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
17025 tree decl
= OMP_CLAUSE_DECL (nc
);
17028 retrofit_lang_decl (decl
);
17029 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
17038 new_clauses
= nreverse (new_clauses
);
17039 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
17041 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
17042 if (linear_no_step
)
17043 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
17044 if (nc
== linear_no_step
)
17046 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
17050 return new_clauses
;
17053 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17056 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
17059 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17061 tree purpose
, value
, chain
;
17066 if (TREE_CODE (t
) != TREE_LIST
)
17067 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17068 /*function_p=*/false,
17069 /*integral_constant_expression_p=*/false);
17071 if (t
== void_list_node
)
17074 purpose
= TREE_PURPOSE (t
);
17076 purpose
= RECUR (purpose
);
17077 value
= TREE_VALUE (t
);
17080 if (TREE_CODE (value
) != LABEL_DECL
)
17081 value
= RECUR (value
);
17084 value
= lookup_label (DECL_NAME (value
));
17085 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
17086 TREE_USED (value
) = 1;
17089 chain
= TREE_CHAIN (t
);
17090 if (chain
&& chain
!= void_type_node
)
17091 chain
= RECUR (chain
);
17092 return tree_cons (purpose
, value
, chain
);
17096 /* Used to temporarily communicate the list of #pragma omp parallel
17097 clauses to #pragma omp for instantiation if they are combined
17100 static tree
*omp_parallel_combined_clauses
;
17102 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
17103 tree
*, unsigned int *);
17105 /* Substitute one OMP_FOR iterator. */
17108 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
17109 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
17110 tree args
, tsubst_flags_t complain
, tree in_decl
,
17111 bool integral_constant_expression_p
)
17113 #define RECUR(NODE) \
17114 tsubst_expr ((NODE), args, complain, in_decl, \
17115 integral_constant_expression_p)
17116 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
17119 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
17120 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
17122 decl
= TREE_OPERAND (init
, 0);
17123 init
= TREE_OPERAND (init
, 1);
17124 tree decl_expr
= NULL_TREE
;
17125 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
17128 bool decomp
= false;
17129 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
17131 tree v
= DECL_VALUE_EXPR (decl
);
17132 if (TREE_CODE (v
) == ARRAY_REF
17133 && VAR_P (TREE_OPERAND (v
, 0))
17134 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
17136 tree decomp_first
= NULL_TREE
;
17137 unsigned decomp_cnt
= 0;
17138 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
17139 maybe_push_decl (d
);
17140 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
17141 in_decl
, &decomp_first
, &decomp_cnt
);
17143 if (d
== error_mark_node
)
17144 decl
= error_mark_node
;
17146 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
17148 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
17150 tree v
= build_nt (ARRAY_REF
, d
,
17151 size_int (decomp_cnt
- i
- 1),
17152 NULL_TREE
, NULL_TREE
);
17153 SET_DECL_VALUE_EXPR (decomp_first
, v
);
17154 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
17156 fit_decomposition_lang_decl (decomp_first
, d
);
17157 decomp_first
= DECL_CHAIN (decomp_first
);
17161 decl
= tsubst_decl (decl
, args
, complain
);
17163 maybe_push_decl (decl
);
17165 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
17167 /* We need to jump through some hoops to handle declarations in the
17168 init-statement, since we might need to handle auto deduction,
17169 but we need to keep control of initialization. */
17171 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
17172 decl
= tsubst_decl (decl
, args
, complain
);
17176 if (TREE_CODE (decl
) == SCOPE_REF
)
17178 decl
= RECUR (decl
);
17179 if (TREE_CODE (decl
) == COMPONENT_REF
)
17183 switch (TREE_CODE (v
))
17185 case COMPONENT_REF
:
17189 case POINTER_PLUS_EXPR
:
17190 v
= TREE_OPERAND (v
, 0);
17193 if (DECL_CONTEXT (v
) == current_function_decl
17194 && DECL_ARTIFICIAL (v
)
17195 && DECL_NAME (v
) == this_identifier
)
17197 decl
= TREE_OPERAND (decl
, 1);
17198 decl
= omp_privatize_field (decl
, false);
17208 decl
= RECUR (decl
);
17210 init
= RECUR (init
);
17212 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
17214 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
17215 if (TREE_CODE (o
) == TREE_LIST
)
17216 TREE_VEC_ELT (orig_declv
, i
)
17217 = tree_cons (RECUR (TREE_PURPOSE (o
)),
17218 RECUR (TREE_VALUE (o
)),
17221 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
17226 tree this_pre_body
= NULL_TREE
;
17227 tree orig_init
= NULL_TREE
;
17228 tree orig_decl
= NULL_TREE
;
17229 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
17230 orig_init
, cond
, incr
);
17233 if (orig_declv
== NULL_TREE
)
17234 orig_declv
= copy_node (declv
);
17235 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
17238 else if (orig_declv
)
17239 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17242 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
17243 if (!range_for
&& auto_node
&& init
)
17245 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
17247 gcc_assert (!type_dependent_expression_p (decl
));
17249 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
17253 /* Declare the variable, but don't let that initialize it. */
17254 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
17255 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
17257 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
17262 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
17263 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17264 if (TREE_CODE (incr
) == MODIFY_EXPR
)
17266 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17267 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
17268 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
17269 NOP_EXPR
, rhs
, complain
);
17272 incr
= RECUR (incr
);
17273 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17274 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17276 TREE_VEC_ELT (declv
, i
) = decl
;
17277 TREE_VEC_ELT (initv
, i
) = init
;
17278 TREE_VEC_ELT (condv
, i
) = cond
;
17279 TREE_VEC_ELT (incrv
, i
) = incr
;
17285 /* Declare and initialize the variable. */
17293 for (j
= ((omp_parallel_combined_clauses
== NULL
17294 || TREE_CODE (t
) == OMP_LOOP
) ? 1 : 0); j
< 2; j
++)
17296 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
17298 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
17299 && OMP_CLAUSE_DECL (*pc
) == decl
)
17301 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
17302 && OMP_CLAUSE_DECL (*pc
) == decl
)
17306 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17308 *pc
= OMP_CLAUSE_CHAIN (c
);
17309 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17312 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
17313 && OMP_CLAUSE_DECL (*pc
) == decl
)
17315 error ("iteration variable %qD should not be firstprivate",
17317 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17319 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
17320 && OMP_CLAUSE_DECL (*pc
) == decl
)
17322 error ("iteration variable %qD should not be reduction",
17324 *pc
= OMP_CLAUSE_CHAIN (*pc
);
17327 pc
= &OMP_CLAUSE_CHAIN (*pc
);
17332 if (*pc
== NULL_TREE
)
17334 tree c
= build_omp_clause (input_location
,
17335 TREE_CODE (t
) == OMP_LOOP
17336 ? OMP_CLAUSE_LASTPRIVATE
17337 : OMP_CLAUSE_PRIVATE
);
17338 OMP_CLAUSE_DECL (c
) = decl
;
17339 c
= finish_omp_clauses (c
, C_ORT_OMP
);
17342 OMP_CLAUSE_CHAIN (c
) = *clauses
;
17347 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
17348 if (COMPARISON_CLASS_P (cond
))
17350 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
17351 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
17352 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
17355 cond
= RECUR (cond
);
17356 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
17357 switch (TREE_CODE (incr
))
17359 case PREINCREMENT_EXPR
:
17360 case PREDECREMENT_EXPR
:
17361 case POSTINCREMENT_EXPR
:
17362 case POSTDECREMENT_EXPR
:
17363 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
17364 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
17367 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17368 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17370 tree rhs
= TREE_OPERAND (incr
, 1);
17371 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17372 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17373 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17374 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17375 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17379 incr
= RECUR (incr
);
17382 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
17383 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
17385 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17386 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17387 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
17388 TREE_TYPE (decl
), lhs
,
17389 RECUR (TREE_OPERAND (incr
, 2))));
17391 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
17392 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
17393 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
17395 tree rhs
= TREE_OPERAND (incr
, 2);
17396 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
17397 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
17398 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
17399 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
17400 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
17404 incr
= RECUR (incr
);
17407 incr
= RECUR (incr
);
17411 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
17412 TREE_VEC_ELT (orig_declv
, i
) = decl
;
17413 TREE_VEC_ELT (declv
, i
) = decl
;
17414 TREE_VEC_ELT (initv
, i
) = init
;
17415 TREE_VEC_ELT (condv
, i
) = cond
;
17416 TREE_VEC_ELT (incrv
, i
) = incr
;
17421 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17422 of OMP_TARGET's body. */
17425 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
17427 *walk_subtrees
= 0;
17428 switch (TREE_CODE (*tp
))
17433 case STATEMENT_LIST
:
17434 *walk_subtrees
= 1;
17442 /* Helper function for tsubst_expr. For decomposition declaration
17443 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17444 also the corresponding decls representing the identifiers
17445 of the decomposition declaration. Return DECL if successful
17446 or error_mark_node otherwise, set *FIRST to the first decl
17447 in the list chained through DECL_CHAIN and *CNT to the number
17451 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
17452 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
17455 tree decl2
, decl3
, prev
= decl
;
17457 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
17458 for (decl2
= DECL_CHAIN (pattern_decl
);
17461 && DECL_DECOMPOSITION_P (decl2
)
17462 && DECL_NAME (decl2
);
17463 decl2
= DECL_CHAIN (decl2
))
17465 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
17467 gcc_assert (errorcount
);
17468 return error_mark_node
;
17471 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
17472 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
17473 tree v
= DECL_VALUE_EXPR (decl2
);
17474 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
17475 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
17476 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
17477 SET_DECL_VALUE_EXPR (decl2
, v
);
17478 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
17480 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
17483 gcc_assert (errorcount
);
17484 decl
= error_mark_node
;
17487 maybe_push_decl (decl3
);
17488 if (error_operand_p (decl3
))
17489 decl
= error_mark_node
;
17490 else if (decl
!= error_mark_node
17491 && DECL_CHAIN (decl3
) != prev
17494 gcc_assert (errorcount
);
17495 decl
= error_mark_node
;
17504 /* Return the proper local_specialization for init-capture pack DECL. */
17507 lookup_init_capture_pack (tree decl
)
17509 /* We handle normal pack captures by forwarding to the specialization of the
17510 captured parameter. We can't do that for pack init-captures; we need them
17511 to have their own local_specialization. We created the individual
17512 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17513 when we process the DECL_EXPR for the pack init-capture in the template.
17514 So, how do we find them? We don't know the capture proxy pack when
17515 building the individual resulting proxies, and we don't know the
17516 individual proxies when instantiating the pack. What we have in common is
17519 So...when we instantiate the FIELD_DECL, we stick the result in
17520 local_specializations. Then at the DECL_EXPR we look up that result, see
17521 how many elements it has, synthesize the names, and look them up. */
17523 tree cname
= DECL_NAME (decl
);
17524 tree val
= DECL_VALUE_EXPR (decl
);
17525 tree field
= TREE_OPERAND (val
, 1);
17526 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
17527 tree fpack
= retrieve_local_specialization (field
);
17528 if (fpack
== error_mark_node
)
17529 return error_mark_node
;
17532 tree vec
= NULL_TREE
;
17533 tree r
= NULL_TREE
;
17534 if (TREE_CODE (fpack
) == TREE_VEC
)
17536 len
= TREE_VEC_LENGTH (fpack
);
17537 vec
= make_tree_vec (len
);
17538 r
= make_node (NONTYPE_ARGUMENT_PACK
);
17539 SET_ARGUMENT_PACK_ARGS (r
, vec
);
17541 for (int i
= 0; i
< len
; ++i
)
17543 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
17544 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
17546 TREE_VEC_ELT (vec
, i
) = elt
;
17553 /* Like tsubst_copy for expressions, etc. but also does semantic
17557 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
17558 bool integral_constant_expression_p
)
17560 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17561 #define RECUR(NODE) \
17562 tsubst_expr ((NODE), args, complain, in_decl, \
17563 integral_constant_expression_p)
17569 if (t
== NULL_TREE
|| t
== error_mark_node
)
17572 loc
= input_location
;
17573 if (location_t eloc
= cp_expr_location (t
))
17574 input_location
= eloc
;
17575 if (STATEMENT_CODE_P (TREE_CODE (t
)))
17576 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
17578 switch (TREE_CODE (t
))
17580 case STATEMENT_LIST
:
17582 tree_stmt_iterator i
;
17583 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
17584 RECUR (tsi_stmt (i
));
17588 case CTOR_INITIALIZER
:
17589 finish_mem_initializers (tsubst_initializer_list
17590 (TREE_OPERAND (t
, 0), args
));
17594 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17598 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17599 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17600 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17602 finish_expr_stmt (tmp
);
17606 finish_using_directive (USING_STMT_NAMESPACE (t
), /*attribs=*/NULL_TREE
);
17611 tree decl
, pattern_decl
;
17614 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17615 if (TREE_CODE (decl
) == LABEL_DECL
)
17616 finish_label_decl (DECL_NAME (decl
));
17617 else if (TREE_CODE (decl
) == USING_DECL
)
17619 tree scope
= USING_DECL_SCOPE (decl
);
17620 tree name
= DECL_NAME (decl
);
17622 scope
= tsubst (scope
, args
, complain
, in_decl
);
17623 finish_nonmember_using_decl (scope
, name
);
17625 else if (is_capture_proxy (decl
)
17626 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17628 /* We're in tsubst_lambda_expr, we've already inserted a new
17629 capture proxy, so look it up and register it. */
17631 if (!DECL_PACK_P (decl
))
17633 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17634 /*nonclass*/1, /*block_p=*/true,
17635 /*ns_only*/0, LOOKUP_HIDDEN
);
17636 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17638 else if (is_normal_capture_proxy (decl
))
17640 inst
= (retrieve_local_specialization
17641 (DECL_CAPTURED_VARIABLE (decl
)));
17642 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
17645 inst
= lookup_init_capture_pack (decl
);
17647 register_local_specialization (inst
, decl
);
17650 else if (DECL_PRETTY_FUNCTION_P (decl
))
17651 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17653 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17654 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17655 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17656 /* Don't copy the old closure; we'll create a new one in
17657 tsubst_lambda_expr. */
17661 init
= DECL_INITIAL (decl
);
17662 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17663 for local variables, so save if DECL was declared constinit. */
17664 const bool constinit_p
17666 && DECL_LANG_SPECIFIC (decl
)
17667 && DECL_TEMPLATE_INFO (decl
)
17668 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl
)));
17669 decl
= tsubst (decl
, args
, complain
, in_decl
);
17670 if (decl
!= error_mark_node
)
17672 /* By marking the declaration as instantiated, we avoid
17673 trying to instantiate it. Since instantiate_decl can't
17674 handle local variables, and since we've already done
17675 all that needs to be done, that's the right thing to
17678 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17679 if (VAR_P (decl
) && !DECL_NAME (decl
)
17680 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17681 /* Anonymous aggregates are a special case. */
17682 finish_anon_union (decl
);
17683 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17685 DECL_CONTEXT (decl
) = current_function_decl
;
17686 if (DECL_NAME (decl
) == this_identifier
)
17688 tree lam
= DECL_CONTEXT (current_function_decl
);
17689 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17690 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17692 insert_capture_proxy (decl
);
17694 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17695 /* We already did a pushtag. */;
17696 else if (TREE_CODE (decl
) == FUNCTION_DECL
17697 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17698 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17700 DECL_CONTEXT (decl
) = NULL_TREE
;
17702 DECL_CONTEXT (decl
) = current_function_decl
;
17703 cp_check_omp_declare_reduction (decl
);
17707 bool const_init
= false;
17708 unsigned int cnt
= 0;
17709 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17710 maybe_push_decl (decl
);
17713 && DECL_DECOMPOSITION_P (decl
)
17714 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17715 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17716 complain
, in_decl
, &first
,
17719 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17722 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17725 if (ndecl
!= error_mark_node
)
17726 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17728 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
,
17729 constinit_p
? LOOKUP_CONSTINIT
: 0);
17731 if (ndecl
!= error_mark_node
)
17732 cp_finish_decomp (ndecl
, first
, cnt
);
17741 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17742 RECUR (FOR_INIT_STMT (t
));
17743 finish_init_stmt (stmt
);
17744 tmp
= RECUR (FOR_COND (t
));
17745 finish_for_cond (tmp
, stmt
, false, 0);
17746 tmp
= RECUR (FOR_EXPR (t
));
17747 finish_for_expr (tmp
, stmt
);
17749 bool prev
= note_iteration_stmt_body_start ();
17750 RECUR (FOR_BODY (t
));
17751 note_iteration_stmt_body_end (prev
);
17753 finish_for_stmt (stmt
);
17756 case RANGE_FOR_STMT
:
17758 /* Construct another range_for, if this is not a final
17759 substitution (for inside inside a generic lambda of a
17760 template). Otherwise convert to a regular for. */
17762 stmt
= (processing_template_decl
17763 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17764 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17765 RECUR (RANGE_FOR_INIT_STMT (t
));
17766 decl
= RANGE_FOR_DECL (t
);
17767 decl
= tsubst (decl
, args
, complain
, in_decl
);
17768 maybe_push_decl (decl
);
17769 expr
= RECUR (RANGE_FOR_EXPR (t
));
17771 tree decomp_first
= NULL_TREE
;
17772 unsigned decomp_cnt
= 0;
17773 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17774 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17776 &decomp_first
, &decomp_cnt
);
17778 if (processing_template_decl
)
17780 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17781 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17782 finish_range_for_decl (stmt
, decl
, expr
);
17783 if (decomp_first
&& decl
!= error_mark_node
)
17784 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17788 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17789 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17790 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17791 decomp_first
, decomp_cnt
,
17792 RANGE_FOR_IVDEP (t
), unroll
);
17795 bool prev
= note_iteration_stmt_body_start ();
17796 RECUR (RANGE_FOR_BODY (t
));
17797 note_iteration_stmt_body_end (prev
);
17798 finish_for_stmt (stmt
);
17803 stmt
= begin_while_stmt ();
17804 tmp
= RECUR (WHILE_COND (t
));
17805 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17807 bool prev
= note_iteration_stmt_body_start ();
17808 RECUR (WHILE_BODY (t
));
17809 note_iteration_stmt_body_end (prev
);
17811 finish_while_stmt (stmt
);
17815 stmt
= begin_do_stmt ();
17817 bool prev
= note_iteration_stmt_body_start ();
17818 RECUR (DO_BODY (t
));
17819 note_iteration_stmt_body_end (prev
);
17821 finish_do_body (stmt
);
17822 tmp
= RECUR (DO_COND (t
));
17823 finish_do_stmt (tmp
, stmt
, false, 0);
17827 stmt
= begin_if_stmt ();
17828 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17829 if (IF_STMT_CONSTEXPR_P (t
))
17830 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17831 tmp
= RECUR (IF_COND (t
));
17832 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17833 if (IF_STMT_CONSTEXPR_P (t
)
17834 && instantiation_dependent_expression_p (tmp
))
17836 /* We're partially instantiating a generic lambda, but the condition
17837 of the constexpr if is still dependent. Don't substitute into the
17838 branches now, just remember the template arguments. */
17839 do_poplevel (IF_SCOPE (stmt
));
17840 IF_COND (stmt
) = IF_COND (t
);
17841 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17842 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17843 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17847 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17848 /* Don't instantiate the THEN_CLAUSE. */;
17851 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17852 bool inhibit
= integer_zerop (folded
);
17854 ++c_inhibit_evaluation_warnings
;
17855 RECUR (THEN_CLAUSE (t
));
17857 --c_inhibit_evaluation_warnings
;
17859 finish_then_clause (stmt
);
17861 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17862 /* Don't instantiate the ELSE_CLAUSE. */;
17863 else if (ELSE_CLAUSE (t
))
17865 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17866 bool inhibit
= integer_nonzerop (folded
);
17867 begin_else_clause (stmt
);
17869 ++c_inhibit_evaluation_warnings
;
17870 RECUR (ELSE_CLAUSE (t
));
17872 --c_inhibit_evaluation_warnings
;
17873 finish_else_clause (stmt
);
17876 finish_if_stmt (stmt
);
17880 if (BIND_EXPR_BODY_BLOCK (t
))
17881 stmt
= begin_function_body ();
17883 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17884 ? BCS_TRY_BLOCK
: 0);
17886 RECUR (BIND_EXPR_BODY (t
));
17888 if (BIND_EXPR_BODY_BLOCK (t
))
17889 finish_function_body (stmt
);
17891 finish_compound_stmt (stmt
);
17895 finish_break_stmt ();
17898 case CONTINUE_STMT
:
17899 finish_continue_stmt ();
17903 stmt
= begin_switch_stmt ();
17904 tmp
= RECUR (SWITCH_STMT_COND (t
));
17905 finish_switch_cond (tmp
, stmt
);
17906 RECUR (SWITCH_STMT_BODY (t
));
17907 finish_switch_stmt (stmt
);
17910 case CASE_LABEL_EXPR
:
17912 tree decl
= CASE_LABEL (t
);
17913 tree low
= RECUR (CASE_LOW (t
));
17914 tree high
= RECUR (CASE_HIGH (t
));
17915 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17916 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17918 tree label
= CASE_LABEL (l
);
17919 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17920 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17921 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17928 tree decl
= LABEL_EXPR_LABEL (t
);
17931 label
= finish_label_stmt (DECL_NAME (decl
));
17932 if (TREE_CODE (label
) == LABEL_DECL
)
17933 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17934 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17935 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17940 tmp
= GOTO_DESTINATION (t
);
17941 if (TREE_CODE (tmp
) != LABEL_DECL
)
17942 /* Computed goto's must be tsubst'd into. On the other hand,
17943 non-computed gotos must not be; the identifier in question
17944 will have no binding. */
17947 tmp
= DECL_NAME (tmp
);
17948 finish_goto_stmt (tmp
);
17953 tree string
= RECUR (ASM_STRING (t
));
17954 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17955 complain
, in_decl
);
17956 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17957 complain
, in_decl
);
17958 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17959 complain
, in_decl
);
17960 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17961 complain
, in_decl
);
17962 tmp
= finish_asm_stmt (EXPR_LOCATION (t
), ASM_VOLATILE_P (t
), string
,
17963 outputs
, inputs
, clobbers
, labels
,
17965 tree asm_expr
= tmp
;
17966 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17967 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17968 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17975 stmt
= begin_try_block ();
17976 RECUR (TRY_STMTS (t
));
17977 finish_cleanup_try_block (stmt
);
17978 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17982 tree compound_stmt
= NULL_TREE
;
17984 if (FN_TRY_BLOCK_P (t
))
17985 stmt
= begin_function_try_block (&compound_stmt
);
17987 stmt
= begin_try_block ();
17989 RECUR (TRY_STMTS (t
));
17991 if (FN_TRY_BLOCK_P (t
))
17992 finish_function_try_block (stmt
);
17994 finish_try_block (stmt
);
17996 RECUR (TRY_HANDLERS (t
));
17997 if (FN_TRY_BLOCK_P (t
))
17998 finish_function_handler_sequence (stmt
, compound_stmt
);
18000 finish_handler_sequence (stmt
);
18006 tree decl
= HANDLER_PARMS (t
);
18010 decl
= tsubst (decl
, args
, complain
, in_decl
);
18011 /* Prevent instantiate_decl from trying to instantiate
18012 this variable. We've already done all that needs to be
18014 if (decl
!= error_mark_node
)
18015 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
18017 stmt
= begin_handler ();
18018 finish_handler_parms (decl
, stmt
);
18019 RECUR (HANDLER_BODY (t
));
18020 finish_handler (stmt
);
18025 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
18026 if (CLASS_TYPE_P (tmp
))
18028 /* Local classes are not independent templates; they are
18029 instantiated along with their containing function. And this
18030 way we don't have to deal with pushing out of one local class
18031 to instantiate a member of another local class. */
18032 /* Closures are handled by the LAMBDA_EXPR. */
18033 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
18034 complete_type (tmp
);
18035 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
18037 || (TREE_CODE (fld
) == FUNCTION_DECL
18038 && !DECL_ARTIFICIAL (fld
)))
18039 && DECL_TEMPLATE_INSTANTIATION (fld
))
18040 instantiate_decl (fld
, /*defer_ok=*/false,
18041 /*expl_inst_class=*/false);
18045 case STATIC_ASSERT
:
18049 ++c_inhibit_evaluation_warnings
;
18051 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
18054 /*integral_constant_expression_p=*/true);
18055 --c_inhibit_evaluation_warnings
;
18057 finish_static_assert (condition
,
18058 STATIC_ASSERT_MESSAGE (t
),
18059 STATIC_ASSERT_SOURCE_LOCATION (t
),
18060 /*member_p=*/false);
18065 case OACC_PARALLEL
:
18067 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
18069 stmt
= begin_omp_parallel ();
18070 RECUR (OMP_BODY (t
));
18071 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
18075 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
18076 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
18077 complain
, in_decl
);
18078 if (OMP_PARALLEL_COMBINED (t
))
18079 omp_parallel_combined_clauses
= &tmp
;
18080 stmt
= begin_omp_parallel ();
18081 RECUR (OMP_PARALLEL_BODY (t
));
18082 gcc_assert (omp_parallel_combined_clauses
== NULL
);
18083 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
18084 = OMP_PARALLEL_COMBINED (t
);
18085 pop_omp_privatization_clauses (r
);
18089 if (OMP_TASK_BODY (t
) == NULL_TREE
)
18091 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18092 complain
, in_decl
);
18094 OMP_TASK_CLAUSES (t
) = tmp
;
18098 r
= push_omp_privatization_clauses (false);
18099 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
18100 complain
, in_decl
);
18101 stmt
= begin_omp_task ();
18102 RECUR (OMP_TASK_BODY (t
));
18103 finish_omp_task (tmp
, stmt
);
18104 pop_omp_privatization_clauses (r
);
18110 case OMP_DISTRIBUTE
:
18114 tree clauses
, body
, pre_body
;
18115 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
18116 tree orig_declv
= NULL_TREE
;
18117 tree incrv
= NULL_TREE
;
18118 enum c_omp_region_type ort
= C_ORT_OMP
;
18119 bool any_range_for
= false;
18122 if (TREE_CODE (t
) == OACC_LOOP
)
18125 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
18126 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
18128 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18130 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18131 if (OMP_FOR_ORIG_DECLS (t
))
18132 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18133 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18134 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18135 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
18138 keep_next_level (true);
18139 stmt
= begin_omp_structured_block ();
18141 pre_body
= push_stmt_list ();
18142 RECUR (OMP_FOR_PRE_BODY (t
));
18143 pre_body
= pop_stmt_list (pre_body
);
18145 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18146 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18148 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
18149 condv
, incrv
, &clauses
, args
,
18151 integral_constant_expression_p
);
18152 omp_parallel_combined_clauses
= NULL
;
18156 gcc_assert (orig_declv
);
18157 body
= begin_omp_structured_block ();
18158 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
18159 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
18160 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
18161 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
18162 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
18163 TREE_VEC_ELT (declv
, i
));
18166 body
= push_stmt_list ();
18167 RECUR (OMP_FOR_BODY (t
));
18169 body
= finish_omp_structured_block (body
);
18171 body
= pop_stmt_list (body
);
18173 if (OMP_FOR_INIT (t
) != NULL_TREE
)
18174 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
18175 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
18179 t
= make_node (TREE_CODE (t
));
18180 TREE_TYPE (t
) = void_type_node
;
18181 OMP_FOR_BODY (t
) = body
;
18182 OMP_FOR_PRE_BODY (t
) = pre_body
;
18183 OMP_FOR_CLAUSES (t
) = clauses
;
18184 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
18188 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
18190 pop_omp_privatization_clauses (r
);
18195 omp_parallel_combined_clauses
= NULL
;
18200 case OMP_TASKGROUP
:
18202 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
18203 && OMP_TEAMS_COMBINED (t
));
18204 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
18206 if (TREE_CODE (t
) == OMP_TEAMS
)
18208 keep_next_level (true);
18209 stmt
= begin_omp_structured_block ();
18210 RECUR (OMP_BODY (t
));
18211 stmt
= finish_omp_structured_block (stmt
);
18215 stmt
= push_stmt_list ();
18216 RECUR (OMP_BODY (t
));
18217 stmt
= pop_stmt_list (stmt
);
18221 OMP_BODY (t
) = stmt
;
18222 OMP_CLAUSES (t
) = tmp
;
18224 pop_omp_privatization_clauses (r
);
18228 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
18229 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
18231 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
18232 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
18234 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
18235 args
, complain
, in_decl
);
18236 if (tmp
== NULL_TREE
)
18237 tmp
= error_mark_node
;
18241 kind
= (enum omp_clause_depend_kind
)
18242 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
18245 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
18248 finish_omp_depobj (EXPR_LOCATION (t
), r
,
18249 OMP_CLAUSE_DEPEND_SOURCE
,
18250 OMP_DEPOBJ_CLAUSES (t
));
18254 case OMP_TARGET_DATA
:
18256 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
18257 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
18259 keep_next_level (true);
18260 stmt
= begin_omp_structured_block ();
18262 RECUR (OMP_BODY (t
));
18263 stmt
= finish_omp_structured_block (stmt
);
18266 OMP_BODY (t
) = stmt
;
18267 OMP_CLAUSES (t
) = tmp
;
18268 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
18270 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
18273 /* For combined target teams, ensure the num_teams and
18274 thread_limit clause expressions are evaluated on the host,
18275 before entering the target construct. */
18277 for (c
= OMP_TEAMS_CLAUSES (teams
);
18278 c
; c
= OMP_CLAUSE_CHAIN (c
))
18279 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
18280 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
18281 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
18283 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
18284 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
18285 if (expr
== error_mark_node
)
18287 tmp
= TARGET_EXPR_SLOT (expr
);
18289 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
18290 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
18291 OMP_CLAUSE_FIRSTPRIVATE
);
18292 OMP_CLAUSE_DECL (tc
) = tmp
;
18293 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
18294 OMP_TARGET_CLAUSES (t
) = tc
;
18303 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
18304 complain
, in_decl
);
18305 OACC_DECLARE_CLAUSES (t
) = tmp
;
18309 case OMP_TARGET_UPDATE
:
18310 case OMP_TARGET_ENTER_DATA
:
18311 case OMP_TARGET_EXIT_DATA
:
18312 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
18313 complain
, in_decl
);
18315 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18319 case OACC_ENTER_DATA
:
18320 case OACC_EXIT_DATA
:
18322 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
18323 complain
, in_decl
);
18325 OMP_STANDALONE_CLAUSES (t
) = tmp
;
18330 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
18331 complain
, in_decl
);
18332 stmt
= push_stmt_list ();
18333 RECUR (OMP_BODY (t
));
18334 stmt
= pop_stmt_list (stmt
);
18337 OMP_BODY (t
) = stmt
;
18338 OMP_ORDERED_CLAUSES (t
) = tmp
;
18344 stmt
= push_stmt_list ();
18345 RECUR (OMP_BODY (t
));
18346 stmt
= pop_stmt_list (stmt
);
18349 OMP_BODY (t
) = stmt
;
18354 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
18356 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
18357 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
18358 complain
, in_decl
);
18359 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
18361 tree op1
= TREE_OPERAND (t
, 1);
18362 tree rhs1
= NULL_TREE
;
18364 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
18366 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
18367 op1
= TREE_OPERAND (op1
, 1);
18369 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18370 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18371 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
18372 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
18373 OMP_ATOMIC_MEMORY_ORDER (t
));
18377 tree op1
= TREE_OPERAND (t
, 1);
18378 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
18379 tree rhs1
= NULL_TREE
;
18380 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
18381 enum tree_code opcode
= NOP_EXPR
;
18382 if (code
== OMP_ATOMIC_READ
)
18384 v
= RECUR (TREE_OPERAND (op1
, 0));
18385 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18387 else if (code
== OMP_ATOMIC_CAPTURE_OLD
18388 || code
== OMP_ATOMIC_CAPTURE_NEW
)
18390 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
18391 v
= RECUR (TREE_OPERAND (op1
, 0));
18392 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
18393 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
18395 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
18396 op11
= TREE_OPERAND (op11
, 1);
18398 lhs
= RECUR (TREE_OPERAND (op11
, 0));
18399 rhs
= RECUR (TREE_OPERAND (op11
, 1));
18400 opcode
= TREE_CODE (op11
);
18401 if (opcode
== MODIFY_EXPR
)
18407 lhs
= RECUR (TREE_OPERAND (op1
, 0));
18408 rhs
= RECUR (TREE_OPERAND (op1
, 1));
18410 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
18411 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
18415 case TRANSACTION_EXPR
:
18418 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
18419 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
18421 if (TRANSACTION_EXPR_IS_STMT (t
))
18423 tree body
= TRANSACTION_EXPR_BODY (t
);
18424 tree noex
= NULL_TREE
;
18425 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
18427 noex
= MUST_NOT_THROW_COND (body
);
18428 if (noex
== NULL_TREE
)
18429 noex
= boolean_true_node
;
18430 body
= TREE_OPERAND (body
, 0);
18432 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
18434 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
18438 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
18439 RECUR (TRANSACTION_EXPR_BODY (t
)),
18446 case MUST_NOT_THROW_EXPR
:
18448 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18449 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
18450 RETURN (build_must_not_throw_expr (op0
, cond
));
18453 case EXPR_PACK_EXPANSION
:
18454 error ("invalid use of pack expansion expression");
18455 RETURN (error_mark_node
);
18457 case NONTYPE_ARGUMENT_PACK
:
18458 error ("use %<...%> to expand argument pack");
18459 RETURN (error_mark_node
);
18461 case COMPOUND_EXPR
:
18462 tmp
= RECUR (TREE_OPERAND (t
, 0));
18463 if (tmp
== NULL_TREE
)
18464 /* If the first operand was a statement, we're done with it. */
18465 RETURN (RECUR (TREE_OPERAND (t
, 1)));
18466 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
18467 RECUR (TREE_OPERAND (t
, 1)),
18470 case ANNOTATE_EXPR
:
18471 tmp
= RECUR (TREE_OPERAND (t
, 0));
18472 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
18473 TREE_TYPE (tmp
), tmp
,
18474 RECUR (TREE_OPERAND (t
, 1)),
18475 RECUR (TREE_OPERAND (t
, 2))));
18478 RETURN (add_stmt (copy_node (t
)));
18481 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
18483 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18484 /*function_p=*/false,
18485 integral_constant_expression_p
));
18488 RETURN (NULL_TREE
);
18490 input_location
= loc
;
18496 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18497 function. For description of the body see comment above
18498 cp_parser_omp_declare_reduction_exprs. */
18501 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18503 if (t
== NULL_TREE
|| t
== error_mark_node
)
18506 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
18508 tree_stmt_iterator tsi
;
18511 memset (stmts
, 0, sizeof stmts
);
18512 for (i
= 0, tsi
= tsi_start (t
);
18513 i
< 7 && !tsi_end_p (tsi
);
18514 i
++, tsi_next (&tsi
))
18515 stmts
[i
] = tsi_stmt (tsi
);
18516 gcc_assert (tsi_end_p (tsi
));
18520 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
18521 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
18522 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
18523 args
, complain
, in_decl
);
18524 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
18525 args
, complain
, in_decl
);
18526 DECL_CONTEXT (omp_out
) = current_function_decl
;
18527 DECL_CONTEXT (omp_in
) = current_function_decl
;
18528 keep_next_level (true);
18529 tree block
= begin_omp_structured_block ();
18530 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
18531 block
= finish_omp_structured_block (block
);
18532 block
= maybe_cleanup_point_expr_void (block
);
18533 add_decl_expr (omp_out
);
18534 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
18535 TREE_NO_WARNING (omp_out
) = 1;
18536 add_decl_expr (omp_in
);
18537 finish_expr_stmt (block
);
18541 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
18542 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
18543 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
18544 args
, complain
, in_decl
);
18545 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
18546 args
, complain
, in_decl
);
18547 DECL_CONTEXT (omp_priv
) = current_function_decl
;
18548 DECL_CONTEXT (omp_orig
) = current_function_decl
;
18549 keep_next_level (true);
18550 tree block
= begin_omp_structured_block ();
18551 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
18552 block
= finish_omp_structured_block (block
);
18553 block
= maybe_cleanup_point_expr_void (block
);
18554 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
18555 add_decl_expr (omp_priv
);
18556 add_decl_expr (omp_orig
);
18557 finish_expr_stmt (block
);
18559 add_decl_expr (omp_orig
);
18563 /* T is a postfix-expression that is not being used in a function
18564 call. Return the substituted version of T. */
18567 tsubst_non_call_postfix_expression (tree t
, tree args
,
18568 tsubst_flags_t complain
,
18571 if (TREE_CODE (t
) == SCOPE_REF
)
18572 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
18573 /*done=*/false, /*address_p=*/false);
18575 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
18576 /*function_p=*/false,
18577 /*integral_constant_expression_p=*/false);
18582 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18583 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18584 dependent init-capture. */
18587 prepend_one_capture (tree field
, tree init
, tree
&list
,
18588 tsubst_flags_t complain
)
18590 if (tree auto_node
= type_uses_auto (TREE_TYPE (field
)))
18592 tree type
= NULL_TREE
;
18595 if (complain
& tf_error
)
18596 error ("empty initializer in lambda init-capture");
18597 init
= error_mark_node
;
18599 else if (TREE_CODE (init
) == TREE_LIST
)
18600 init
= build_x_compound_expr_from_list (init
, ELK_INIT
, complain
);
18602 type
= do_auto_deduction (TREE_TYPE (field
), init
, auto_node
, complain
);
18603 TREE_TYPE (field
) = type
;
18604 cp_apply_type_quals_to_decl (cp_type_quals (type
), field
);
18606 list
= tree_cons (field
, init
, list
);
18609 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18610 instantiation context. Instantiating a pack expansion containing a lambda
18611 might result in multiple lambdas all based on the same lambda in the
18615 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18617 tree oldfn
= lambda_function (t
);
18620 tree r
= build_lambda_expr ();
18622 LAMBDA_EXPR_LOCATION (r
)
18623 = LAMBDA_EXPR_LOCATION (t
);
18624 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18625 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18626 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18627 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18629 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18630 /* A lambda in a default argument outside a class gets no
18631 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18632 tsubst_default_argument calls start_lambda_scope, so we need to
18633 specifically ignore it here, and use the global scope. */
18634 record_null_lambda_scope (r
);
18636 record_lambda_scope (r
);
18638 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18639 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18641 vec
<tree
,va_gc
>* field_packs
= NULL
;
18643 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18644 cap
= TREE_CHAIN (cap
))
18646 tree ofield
= TREE_PURPOSE (cap
);
18647 if (PACK_EXPANSION_P (ofield
))
18648 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18649 tree field
= tsubst_decl (ofield
, args
, complain
);
18651 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18653 /* Remember these for when we've pushed local_specializations. */
18654 vec_safe_push (field_packs
, ofield
);
18655 vec_safe_push (field_packs
, field
);
18658 if (field
== error_mark_node
)
18659 return error_mark_node
;
18661 tree init
= TREE_VALUE (cap
);
18662 if (PACK_EXPANSION_P (init
))
18663 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18665 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18666 /*fn*/false, /*constexpr*/false);
18668 if (TREE_CODE (field
) == TREE_VEC
)
18670 int len
= TREE_VEC_LENGTH (field
);
18671 gcc_assert (TREE_CODE (init
) == TREE_VEC
18672 && TREE_VEC_LENGTH (init
) == len
);
18673 for (int i
= 0; i
< len
; ++i
)
18674 prepend_one_capture (TREE_VEC_ELT (field
, i
),
18675 TREE_VEC_ELT (init
, i
),
18676 LAMBDA_EXPR_CAPTURE_LIST (r
),
18681 prepend_one_capture (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
),
18684 if (id_equal (DECL_NAME (field
), "__this"))
18685 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18689 tree type
= begin_lambda_type (r
);
18690 if (type
== error_mark_node
)
18691 return error_mark_node
;
18693 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18694 determine_visibility (TYPE_NAME (type
));
18696 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18698 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18699 ? DECL_TI_TEMPLATE (oldfn
)
18702 tree fntype
= static_fn_type (oldfn
);
18704 ++processing_template_decl
;
18705 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18707 --processing_template_decl
;
18709 if (fntype
== error_mark_node
)
18710 r
= error_mark_node
;
18713 /* The body of a lambda-expression is not a subexpression of the
18714 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18715 which would be skipped if cp_unevaluated_operand. */
18718 /* Fix the type of 'this'. */
18719 fntype
= build_memfn_type (fntype
, type
,
18720 type_memfn_quals (fntype
),
18721 type_memfn_rqual (fntype
));
18725 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18726 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18727 finish_member_declaration (tmpl
);
18732 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18733 finish_member_declaration (fn
);
18736 /* Let finish_function set this. */
18737 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18739 bool nested
= cfun
;
18741 push_function_context ();
18743 /* Still increment function_depth so that we don't GC in the
18744 middle of an expression. */
18747 local_specialization_stack
s (lss_copy
);
18749 tree body
= start_lambda_function (fn
, r
);
18751 /* Now record them for lookup_init_capture_pack. */
18752 int fplen
= vec_safe_length (field_packs
);
18753 for (int i
= 0; i
< fplen
; )
18755 tree pack
= (*field_packs
)[i
++];
18756 tree inst
= (*field_packs
)[i
++];
18757 register_local_specialization (inst
, pack
);
18759 release_tree_vector (field_packs
);
18761 register_parameter_specializations (oldfn
, fn
);
18765 /* We might not partially instantiate some parts of the function, so
18766 copy these flags from the original template. */
18767 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18768 current_function_returns_value
= ol
->returns_value
;
18769 current_function_returns_null
= ol
->returns_null
;
18770 current_function_returns_abnormally
= ol
->returns_abnormally
;
18771 current_function_infinite_loop
= ol
->infinite_loop
;
18774 /* [temp.deduct] A lambda-expression appearing in a function type or a
18775 template parameter is not considered part of the immediate context for
18776 the purposes of template argument deduction. */
18777 complain
= tf_warning_or_error
;
18779 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18780 /*constexpr*/false);
18782 finish_lambda_function (body
);
18785 pop_function_context ();
18789 /* The capture list was built up in reverse order; fix that now. */
18790 LAMBDA_EXPR_CAPTURE_LIST (r
)
18791 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18793 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18795 maybe_add_lambda_conv_op (type
);
18798 finish_struct (type
, /*attr*/NULL_TREE
);
18800 insert_pending_capture_proxies ();
18805 /* Like tsubst but deals with expressions and performs semantic
18806 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18809 tsubst_copy_and_build (tree t
,
18811 tsubst_flags_t complain
,
18814 bool integral_constant_expression_p
)
18816 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18817 #define RECUR(NODE) \
18818 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18819 /*function_p=*/false, \
18820 integral_constant_expression_p)
18825 if (t
== NULL_TREE
|| t
== error_mark_node
)
18828 loc
= input_location
;
18829 if (location_t eloc
= cp_expr_location (t
))
18830 input_location
= eloc
;
18832 /* N3276 decltype magic only applies to calls at the top level or on the
18833 right side of a comma. */
18834 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18835 complain
&= ~tf_decltype
;
18837 switch (TREE_CODE (t
))
18841 /* Fall through. */
18842 case IDENTIFIER_NODE
:
18846 bool non_integral_constant_expression_p
;
18847 const char *error_msg
;
18849 if (IDENTIFIER_CONV_OP_P (t
))
18851 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18852 t
= make_conv_op_name (new_type
);
18855 /* Look up the name. */
18856 decl
= lookup_name (t
);
18858 /* By convention, expressions use ERROR_MARK_NODE to indicate
18859 failure, not NULL_TREE. */
18860 if (decl
== NULL_TREE
)
18861 decl
= error_mark_node
;
18863 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18865 integral_constant_expression_p
,
18866 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18867 &non_integral_constant_expression_p
,
18868 /*template_p=*/false,
18870 /*address_p=*/false,
18871 /*template_arg_p=*/false,
18876 if (!function_p
&& identifier_p (decl
))
18878 if (complain
& tf_error
)
18879 unqualified_name_lookup_error (decl
);
18880 decl
= error_mark_node
;
18885 case TEMPLATE_ID_EXPR
:
18888 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18889 tree targs
= TREE_OPERAND (t
, 1);
18892 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18893 if (targs
== error_mark_node
)
18894 RETURN (error_mark_node
);
18896 if (TREE_CODE (templ
) == SCOPE_REF
)
18898 tree name
= TREE_OPERAND (templ
, 1);
18899 tree tid
= lookup_template_function (name
, targs
);
18900 TREE_OPERAND (templ
, 1) = tid
;
18904 if (concept_definition_p (templ
))
18906 tree check
= build_concept_check (templ
, targs
, complain
);
18907 if (check
== error_mark_node
)
18908 RETURN (error_mark_node
);
18910 tree id
= unpack_concept_check (check
);
18912 /* If we built a function concept check, return the underlying
18913 template-id. So we can evaluate it as a function call. */
18914 if (function_concept_p (TREE_OPERAND (id
, 0)))
18920 if (variable_template_p (templ
))
18922 tree r
= lookup_and_finish_template_variable (templ
, targs
,
18924 r
= maybe_wrap_with_location (r
, EXPR_LOCATION (t
));
18928 if (TREE_CODE (templ
) == COMPONENT_REF
)
18930 object
= TREE_OPERAND (templ
, 0);
18931 templ
= TREE_OPERAND (templ
, 1);
18934 object
= NULL_TREE
;
18935 templ
= lookup_template_function (templ
, targs
);
18938 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18939 object
, templ
, NULL_TREE
));
18941 RETURN (baselink_for_fns (templ
));
18946 tree r
= RECUR (TREE_OPERAND (t
, 0));
18948 if (REFERENCE_REF_P (t
))
18950 /* A type conversion to reference type will be enclosed in
18951 such an indirect ref, but the substitution of the cast
18952 will have also added such an indirect ref. */
18953 r
= convert_from_reference (r
);
18956 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18957 complain
|decltype_flag
);
18959 if (REF_PARENTHESIZED_P (t
))
18960 r
= force_paren_expr (r
);
18967 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18968 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18969 RETURN (build_nop (type
, op0
));
18972 case IMPLICIT_CONV_EXPR
:
18974 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18975 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18976 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18978 retval
= copy_node (t
);
18979 TREE_TYPE (retval
) = type
;
18980 TREE_OPERAND (retval
, 0) = expr
;
18983 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18984 /* We'll pass this to convert_nontype_argument again, we don't need
18985 to actually perform any conversion here. */
18987 int flags
= LOOKUP_IMPLICIT
;
18988 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18989 flags
= LOOKUP_NORMAL
;
18990 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
18991 flags
|= LOOKUP_NO_NARROWING
;
18992 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18998 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18999 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19000 if (op0
== error_mark_node
)
19001 RETURN (error_mark_node
);
19002 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
19006 case REINTERPRET_CAST_EXPR
:
19007 case CONST_CAST_EXPR
:
19008 case DYNAMIC_CAST_EXPR
:
19009 case STATIC_CAST_EXPR
:
19012 tree op
, r
= NULL_TREE
;
19014 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19015 if (integral_constant_expression_p
19016 && !cast_valid_in_integral_constant_expression_p (type
))
19018 if (complain
& tf_error
)
19019 error ("a cast to a type other than an integral or "
19020 "enumeration type cannot appear in a constant-expression");
19021 RETURN (error_mark_node
);
19024 op
= RECUR (TREE_OPERAND (t
, 0));
19026 warning_sentinel
s(warn_useless_cast
);
19027 warning_sentinel
s2(warn_ignored_qualifiers
);
19028 switch (TREE_CODE (t
))
19031 r
= build_functional_cast (input_location
, type
, op
, complain
);
19033 case REINTERPRET_CAST_EXPR
:
19034 r
= build_reinterpret_cast (input_location
, type
, op
, complain
);
19036 case CONST_CAST_EXPR
:
19037 r
= build_const_cast (input_location
, type
, op
, complain
);
19039 case DYNAMIC_CAST_EXPR
:
19040 r
= build_dynamic_cast (input_location
, type
, op
, complain
);
19042 case STATIC_CAST_EXPR
:
19043 r
= build_static_cast (input_location
, type
, op
, complain
);
19046 gcc_unreachable ();
19052 case POSTDECREMENT_EXPR
:
19053 case POSTINCREMENT_EXPR
:
19054 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19055 args
, complain
, in_decl
);
19056 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
19057 complain
|decltype_flag
));
19059 case PREDECREMENT_EXPR
:
19060 case PREINCREMENT_EXPR
:
19064 case TRUTH_NOT_EXPR
:
19065 case UNARY_PLUS_EXPR
: /* Unary + */
19066 case REALPART_EXPR
:
19067 case IMAGPART_EXPR
:
19068 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
19069 RECUR (TREE_OPERAND (t
, 0)),
19070 complain
|decltype_flag
));
19072 case FIX_TRUNC_EXPR
:
19073 gcc_unreachable ();
19076 op1
= TREE_OPERAND (t
, 0);
19077 if (TREE_CODE (op1
) == LABEL_DECL
)
19078 RETURN (finish_label_address_expr (DECL_NAME (op1
),
19079 EXPR_LOCATION (op1
)));
19080 if (TREE_CODE (op1
) == SCOPE_REF
)
19081 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
19082 /*done=*/true, /*address_p=*/true);
19084 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
19086 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
19087 complain
|decltype_flag
));
19092 case TRUNC_DIV_EXPR
:
19093 case CEIL_DIV_EXPR
:
19094 case FLOOR_DIV_EXPR
:
19095 case ROUND_DIV_EXPR
:
19096 case EXACT_DIV_EXPR
:
19100 case TRUNC_MOD_EXPR
:
19101 case FLOOR_MOD_EXPR
:
19102 case TRUTH_ANDIF_EXPR
:
19103 case TRUTH_ORIF_EXPR
:
19104 case TRUTH_AND_EXPR
:
19105 case TRUTH_OR_EXPR
:
19116 case SPACESHIP_EXPR
:
19120 warning_sentinel
s1(warn_type_limits
);
19121 warning_sentinel
s2(warn_div_by_zero
);
19122 warning_sentinel
s3(warn_logical_op
);
19123 warning_sentinel
s4(warn_tautological_compare
);
19124 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19125 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19126 tree r
= build_x_binary_op
19127 (input_location
, TREE_CODE (t
),
19129 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
19131 : TREE_CODE (TREE_OPERAND (t
, 0))),
19133 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
19135 : TREE_CODE (TREE_OPERAND (t
, 1))),
19137 complain
|decltype_flag
);
19138 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
19139 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19144 case POINTER_PLUS_EXPR
:
19146 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19147 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19148 RETURN (fold_build_pointer_plus (op0
, op1
));
19152 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
19153 /*address_p=*/false));
19155 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19156 args
, complain
, in_decl
);
19157 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
19158 RECUR (TREE_OPERAND (t
, 1)),
19159 complain
|decltype_flag
));
19162 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
19163 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
19164 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19171 op1
= TREE_OPERAND (t
, 0);
19172 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
19173 op1
= TREE_TYPE (op1
);
19174 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
19175 && ALIGNOF_EXPR_STD_P (t
));
19178 /* When there are no ARGS, we are trying to evaluate a
19179 non-dependent expression from the parser. Trying to do
19180 the substitutions may not work. */
19182 op1
= TREE_TYPE (op1
);
19186 ++cp_unevaluated_operand
;
19187 ++c_inhibit_evaluation_warnings
;
19189 op1
= tsubst (op1
, args
, complain
, in_decl
);
19191 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19192 /*function_p=*/false,
19193 /*integral_constant_expression_p=*/
19195 --cp_unevaluated_operand
;
19196 --c_inhibit_evaluation_warnings
;
19199 r
= cxx_sizeof_or_alignof_type (input_location
,
19200 op1
, TREE_CODE (t
), std_alignof
,
19201 complain
& tf_error
);
19203 r
= cxx_sizeof_or_alignof_expr (input_location
,
19204 op1
, TREE_CODE (t
),
19205 complain
& tf_error
);
19206 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
19208 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
19210 if (!processing_template_decl
&& TYPE_P (op1
))
19212 r
= build_min (SIZEOF_EXPR
, size_type_node
,
19213 build1 (NOP_EXPR
, op1
, error_mark_node
));
19214 SIZEOF_EXPR_TYPE_P (r
) = 1;
19217 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
19218 TREE_SIDE_EFFECTS (r
) = 0;
19219 TREE_READONLY (r
) = 1;
19221 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
19226 case AT_ENCODE_EXPR
:
19228 op1
= TREE_OPERAND (t
, 0);
19229 ++cp_unevaluated_operand
;
19230 ++c_inhibit_evaluation_warnings
;
19231 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19232 /*function_p=*/false,
19233 /*integral_constant_expression_p=*/false);
19234 --cp_unevaluated_operand
;
19235 --c_inhibit_evaluation_warnings
;
19236 RETURN (objc_build_encode_expr (op1
));
19239 case NOEXCEPT_EXPR
:
19240 op1
= TREE_OPERAND (t
, 0);
19241 ++cp_unevaluated_operand
;
19242 ++c_inhibit_evaluation_warnings
;
19243 ++cp_noexcept_operand
;
19244 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
19245 /*function_p=*/false,
19246 /*integral_constant_expression_p=*/false);
19247 --cp_unevaluated_operand
;
19248 --c_inhibit_evaluation_warnings
;
19249 --cp_noexcept_operand
;
19250 RETURN (finish_noexcept_expr (op1
, complain
));
19254 warning_sentinel
s(warn_div_by_zero
);
19255 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
19256 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
19257 tree r
= build_x_modify_expr
19258 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
19259 complain
|decltype_flag
);
19260 /* TREE_NO_WARNING must be set if either the expression was
19261 parenthesized or it uses an operator such as >>= rather
19262 than plain assignment. In the former case, it was already
19263 set and must be copied. In the latter case,
19264 build_x_modify_expr sets it and it must not be reset
19266 if (TREE_NO_WARNING (t
))
19267 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
19273 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19274 args
, complain
, in_decl
);
19275 /* Remember that there was a reference to this entity. */
19277 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
19278 RETURN (error_mark_node
);
19279 RETURN (build_x_arrow (input_location
, op1
, complain
));
19283 tree placement
= RECUR (TREE_OPERAND (t
, 0));
19284 tree init
= RECUR (TREE_OPERAND (t
, 3));
19285 vec
<tree
, va_gc
> *placement_vec
;
19286 vec
<tree
, va_gc
> *init_vec
;
19289 if (placement
== NULL_TREE
)
19290 placement_vec
= NULL
;
19293 placement_vec
= make_tree_vector ();
19294 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
19295 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
19298 /* If there was an initializer in the original tree, but it
19299 instantiated to an empty list, then we should pass a
19300 non-NULL empty vector to tell build_new that it was an
19301 empty initializer() rather than no initializer. This can
19302 only happen when the initializer is a pack expansion whose
19303 parameter packs are of length zero. */
19304 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
19308 init_vec
= make_tree_vector ();
19309 if (init
== void_node
)
19310 gcc_assert (init_vec
!= NULL
);
19313 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
19314 vec_safe_push (init_vec
, TREE_VALUE (init
));
19318 /* Avoid passing an enclosing decl to valid_array_size_p. */
19319 in_decl
= NULL_TREE
;
19321 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
19322 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19323 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
19324 NEW_EXPR_USE_GLOBAL (t
),
19327 if (placement_vec
!= NULL
)
19328 release_tree_vector (placement_vec
);
19329 if (init_vec
!= NULL
)
19330 release_tree_vector (init_vec
);
19337 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19338 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19339 RETURN (delete_sanity (op0
, op1
,
19340 DELETE_EXPR_USE_VEC (t
),
19341 DELETE_EXPR_USE_GLOBAL (t
),
19345 case COMPOUND_EXPR
:
19347 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
19348 complain
& ~tf_decltype
, in_decl
,
19349 /*function_p=*/false,
19350 integral_constant_expression_p
);
19351 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
19353 RECUR (TREE_OPERAND (t
, 1)),
19354 complain
|decltype_flag
));
19360 unsigned int nargs
, i
;
19365 function
= CALL_EXPR_FN (t
);
19366 /* Internal function with no arguments. */
19367 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
19370 /* When we parsed the expression, we determined whether or
19371 not Koenig lookup should be performed. */
19372 koenig_p
= KOENIG_LOOKUP_P (t
);
19373 if (function
== NULL_TREE
)
19376 qualified_p
= false;
19378 else if (TREE_CODE (function
) == SCOPE_REF
)
19380 qualified_p
= true;
19381 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
19383 /*address_p=*/false);
19385 else if (koenig_p
&& identifier_p (function
))
19387 /* Do nothing; calling tsubst_copy_and_build on an identifier
19388 would incorrectly perform unqualified lookup again.
19390 Note that we can also have an IDENTIFIER_NODE if the earlier
19391 unqualified lookup found a member function; in that case
19392 koenig_p will be false and we do want to do the lookup
19393 again to find the instantiated member function.
19395 FIXME but doing that causes c++/15272, so we need to stop
19396 using IDENTIFIER_NODE in that situation. */
19397 qualified_p
= false;
19401 if (TREE_CODE (function
) == COMPONENT_REF
)
19403 tree op
= TREE_OPERAND (function
, 1);
19405 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
19406 || (BASELINK_P (op
)
19407 && BASELINK_QUALIFIED_P (op
)));
19410 qualified_p
= false;
19412 if (TREE_CODE (function
) == ADDR_EXPR
19413 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
19414 /* Avoid error about taking the address of a constructor. */
19415 function
= TREE_OPERAND (function
, 0);
19417 function
= tsubst_copy_and_build (function
, args
, complain
,
19420 integral_constant_expression_p
);
19422 if (BASELINK_P (function
))
19423 qualified_p
= true;
19426 nargs
= call_expr_nargs (t
);
19427 releasing_vec call_args
;
19428 for (i
= 0; i
< nargs
; ++i
)
19430 tree arg
= CALL_EXPR_ARG (t
, i
);
19432 if (!PACK_EXPANSION_P (arg
))
19433 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
19436 /* Expand the pack expansion and push each entry onto
19438 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
19439 if (TREE_CODE (arg
) == TREE_VEC
)
19441 unsigned int len
, j
;
19443 len
= TREE_VEC_LENGTH (arg
);
19444 for (j
= 0; j
< len
; ++j
)
19446 tree value
= TREE_VEC_ELT (arg
, j
);
19447 if (value
!= NULL_TREE
)
19448 value
= convert_from_reference (value
);
19449 vec_safe_push (call_args
, value
);
19454 /* A partial substitution. Add one entry. */
19455 vec_safe_push (call_args
, arg
);
19460 /* Stripped-down processing for a call in a thunk. Specifically, in
19461 the thunk template for a generic lambda. */
19462 if (CALL_FROM_THUNK_P (t
))
19464 /* Now that we've expanded any packs, the number of call args
19465 might be different. */
19466 unsigned int cargs
= call_args
->length ();
19467 tree thisarg
= NULL_TREE
;
19468 if (TREE_CODE (function
) == COMPONENT_REF
)
19470 thisarg
= TREE_OPERAND (function
, 0);
19471 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
19472 thisarg
= TREE_OPERAND (thisarg
, 0);
19473 function
= TREE_OPERAND (function
, 1);
19474 if (TREE_CODE (function
) == BASELINK
)
19475 function
= BASELINK_FUNCTIONS (function
);
19477 /* We aren't going to do normal overload resolution, so force the
19478 template-id to resolve. */
19479 function
= resolve_nondeduced_context (function
, complain
);
19480 for (unsigned i
= 0; i
< cargs
; ++i
)
19482 /* In a thunk, pass through args directly, without any
19484 tree arg
= (*call_args
)[i
];
19485 while (TREE_CODE (arg
) != PARM_DECL
)
19486 arg
= TREE_OPERAND (arg
, 0);
19487 (*call_args
)[i
] = arg
;
19491 /* If there are no other args, just push 'this'. */
19493 vec_safe_push (call_args
, thisarg
);
19496 /* Otherwise, shift the other args over to make room. */
19497 tree last
= (*call_args
)[cargs
- 1];
19498 vec_safe_push (call_args
, last
);
19499 for (int i
= cargs
- 1; i
> 0; --i
)
19500 (*call_args
)[i
] = (*call_args
)[i
- 1];
19501 (*call_args
)[0] = thisarg
;
19504 ret
= build_call_a (function
, call_args
->length (),
19505 call_args
->address ());
19506 /* The thunk location is not interesting. */
19507 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
19508 CALL_FROM_THUNK_P (ret
) = true;
19509 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
19510 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
19515 /* We do not perform argument-dependent lookup if normal
19516 lookup finds a non-function, in accordance with the
19517 expected resolution of DR 218. */
19519 && ((is_overloaded_fn (function
)
19520 /* If lookup found a member function, the Koenig lookup is
19521 not appropriate, even if an unqualified-name was used
19522 to denote the function. */
19523 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
19524 || identifier_p (function
))
19525 /* Only do this when substitution turns a dependent call
19526 into a non-dependent call. */
19527 && type_dependent_expression_p_push (t
)
19528 && !any_type_dependent_arguments_p (call_args
))
19529 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
19531 if (function
!= NULL_TREE
19532 && identifier_p (function
)
19533 && !any_type_dependent_arguments_p (call_args
))
19535 if (koenig_p
&& (complain
& tf_warning_or_error
))
19537 /* For backwards compatibility and good diagnostics, try
19538 the unqualified lookup again if we aren't in SFINAE
19540 tree unq
= (tsubst_copy_and_build
19541 (function
, args
, complain
, in_decl
, true,
19542 integral_constant_expression_p
));
19543 if (unq
== error_mark_node
)
19544 RETURN (error_mark_node
);
19546 if (unq
!= function
)
19548 /* In a lambda fn, we have to be careful to not
19549 introduce new this captures. Legacy code can't
19550 be using lambdas anyway, so it's ok to be
19552 bool in_lambda
= (current_class_type
19553 && LAMBDA_TYPE_P (current_class_type
));
19554 char const *const msg
19555 = G_("%qD was not declared in this scope, "
19556 "and no declarations were found by "
19557 "argument-dependent lookup at the point "
19558 "of instantiation");
19562 error_at (cp_expr_loc_or_input_loc (t
),
19565 diag
= permerror (cp_expr_loc_or_input_loc (t
),
19571 if (INDIRECT_REF_P (fn
))
19572 fn
= TREE_OPERAND (fn
, 0);
19573 if (is_overloaded_fn (fn
))
19574 fn
= get_first_fn (fn
);
19577 /* Can't say anything more. */;
19578 else if (DECL_CLASS_SCOPE_P (fn
))
19580 location_t loc
= cp_expr_loc_or_input_loc (t
);
19582 "declarations in dependent base %qT are "
19583 "not found by unqualified lookup",
19584 DECL_CLASS_CONTEXT (fn
));
19585 if (current_class_ptr
)
19587 "use %<this->%D%> instead", function
);
19590 "use %<%T::%D%> instead",
19591 current_class_name
, function
);
19594 inform (DECL_SOURCE_LOCATION (fn
),
19595 "%qD declared here, later in the "
19596 "translation unit", fn
);
19598 RETURN (error_mark_node
);
19604 if (identifier_p (function
))
19606 if (complain
& tf_error
)
19607 unqualified_name_lookup_error (function
);
19608 RETURN (error_mark_node
);
19612 /* Remember that there was a reference to this entity. */
19613 if (function
!= NULL_TREE
19614 && DECL_P (function
)
19615 && !mark_used (function
, complain
) && !(complain
& tf_error
))
19616 RETURN (error_mark_node
);
19618 /* Put back tf_decltype for the actual call. */
19619 complain
|= decltype_flag
;
19621 if (function
== NULL_TREE
)
19622 switch (CALL_EXPR_IFN (t
))
19625 gcc_assert (nargs
== 1);
19626 if (vec_safe_length (call_args
) != 1)
19628 error_at (cp_expr_loc_or_input_loc (t
),
19629 "wrong number of arguments to "
19630 "%<__builtin_launder%>");
19631 ret
= error_mark_node
;
19634 ret
= finish_builtin_launder (cp_expr_loc_or_input_loc (t
),
19635 (*call_args
)[0], complain
);
19638 case IFN_VEC_CONVERT
:
19639 gcc_assert (nargs
== 1);
19640 if (vec_safe_length (call_args
) != 1)
19642 error_at (cp_expr_loc_or_input_loc (t
),
19643 "wrong number of arguments to "
19644 "%<__builtin_convertvector%>");
19645 ret
= error_mark_node
;
19648 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19649 tsubst (TREE_TYPE (t
), args
,
19650 complain
, in_decl
),
19652 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19657 /* Unsupported internal function with arguments. */
19658 gcc_unreachable ();
19660 else if (TREE_CODE (function
) == OFFSET_REF
19661 || TREE_CODE (function
) == DOTSTAR_EXPR
19662 || TREE_CODE (function
) == MEMBER_REF
)
19663 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19665 else if (TREE_CODE (function
) == COMPONENT_REF
)
19667 tree instance
= TREE_OPERAND (function
, 0);
19668 tree fn
= TREE_OPERAND (function
, 1);
19670 if (processing_template_decl
19671 && (type_dependent_expression_p (instance
)
19672 || (!BASELINK_P (fn
)
19673 && TREE_CODE (fn
) != FIELD_DECL
)
19674 || type_dependent_expression_p (fn
)
19675 || any_type_dependent_arguments_p (call_args
)))
19676 ret
= build_min_nt_call_vec (function
, call_args
);
19677 else if (!BASELINK_P (fn
))
19678 ret
= finish_call_expr (function
, &call_args
,
19679 /*disallow_virtual=*/false,
19680 /*koenig_p=*/false,
19683 ret
= (build_new_method_call
19685 &call_args
, NULL_TREE
,
19686 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19690 else if (concept_check_p (function
))
19692 /* FUNCTION is a template-id referring to a concept definition. */
19693 tree id
= unpack_concept_check (function
);
19694 tree tmpl
= TREE_OPERAND (id
, 0);
19695 tree args
= TREE_OPERAND (id
, 1);
19697 /* Calls to standard and variable concepts should have been
19698 previously diagnosed. */
19699 gcc_assert (function_concept_p (tmpl
));
19701 /* Ensure the result is wrapped as a call expression. */
19702 ret
= build_concept_check (tmpl
, args
, tf_warning_or_error
);
19705 ret
= finish_call_expr (function
, &call_args
,
19706 /*disallow_virtual=*/qualified_p
,
19710 if (ret
!= error_mark_node
)
19712 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
19713 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
19714 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
19715 if (op
|| ord
|| rev
)
19717 function
= extract_call_expr (ret
);
19718 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
19719 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
19720 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
19729 tree cond
= RECUR (TREE_OPERAND (t
, 0));
19730 cond
= mark_rvalue_use (cond
);
19731 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19734 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19736 if (integer_zerop (folded_cond
))
19738 ++c_inhibit_evaluation_warnings
;
19739 exp1
= RECUR (TREE_OPERAND (t
, 1));
19740 --c_inhibit_evaluation_warnings
;
19741 exp2
= RECUR (TREE_OPERAND (t
, 2));
19745 exp1
= RECUR (TREE_OPERAND (t
, 1));
19746 ++c_inhibit_evaluation_warnings
;
19747 exp2
= RECUR (TREE_OPERAND (t
, 2));
19748 --c_inhibit_evaluation_warnings
;
19750 cond
= folded_cond
;
19754 exp1
= RECUR (TREE_OPERAND (t
, 1));
19755 exp2
= RECUR (TREE_OPERAND (t
, 2));
19758 warning_sentinel
s(warn_duplicated_branches
);
19759 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19760 cond
, exp1
, exp2
, complain
));
19763 case PSEUDO_DTOR_EXPR
:
19765 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19766 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19767 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19768 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19774 tree purpose
, value
, chain
;
19776 if (t
== void_list_node
)
19779 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19780 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19782 /* We have pack expansions, so expand those and
19783 create a new list out of it. */
19784 tree purposevec
= NULL_TREE
;
19785 tree valuevec
= NULL_TREE
;
19789 /* Expand the argument expressions. */
19790 if (TREE_PURPOSE (t
))
19791 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19792 complain
, in_decl
);
19793 if (TREE_VALUE (t
))
19794 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19795 complain
, in_decl
);
19797 /* Build the rest of the list. */
19798 chain
= TREE_CHAIN (t
);
19799 if (chain
&& chain
!= void_type_node
)
19800 chain
= RECUR (chain
);
19802 /* Determine the number of arguments. */
19803 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19805 len
= TREE_VEC_LENGTH (purposevec
);
19806 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19808 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19809 len
= TREE_VEC_LENGTH (valuevec
);
19812 /* Since we only performed a partial substitution into
19813 the argument pack, we only RETURN (a single list
19815 if (purposevec
== TREE_PURPOSE (t
)
19816 && valuevec
== TREE_VALUE (t
)
19817 && chain
== TREE_CHAIN (t
))
19820 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19823 /* Convert the argument vectors into a TREE_LIST */
19827 /* Grab the Ith values. */
19829 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19832 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19835 /* Build the list (backwards). */
19836 chain
= tree_cons (purpose
, value
, chain
);
19842 purpose
= TREE_PURPOSE (t
);
19844 purpose
= RECUR (purpose
);
19845 value
= TREE_VALUE (t
);
19847 value
= RECUR (value
);
19848 chain
= TREE_CHAIN (t
);
19849 if (chain
&& chain
!= void_type_node
)
19850 chain
= RECUR (chain
);
19851 if (purpose
== TREE_PURPOSE (t
)
19852 && value
== TREE_VALUE (t
)
19853 && chain
== TREE_CHAIN (t
))
19855 RETURN (tree_cons (purpose
, value
, chain
));
19858 case COMPONENT_REF
:
19865 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19866 args
, complain
, in_decl
);
19867 /* Remember that there was a reference to this entity. */
19868 if (DECL_P (object
)
19869 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19870 RETURN (error_mark_node
);
19871 object_type
= TREE_TYPE (object
);
19873 member
= TREE_OPERAND (t
, 1);
19874 if (BASELINK_P (member
))
19875 member
= tsubst_baselink (member
,
19876 non_reference (TREE_TYPE (object
)),
19877 args
, complain
, in_decl
);
19879 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19880 if (member
== error_mark_node
)
19881 RETURN (error_mark_node
);
19883 if (TREE_CODE (member
) == FIELD_DECL
)
19885 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19886 if (TREE_CODE (r
) == COMPONENT_REF
)
19887 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19890 else if (type_dependent_expression_p (object
))
19891 /* We can't do much here. */;
19892 else if (!CLASS_TYPE_P (object_type
))
19894 if (scalarish_type_p (object_type
))
19896 tree s
= NULL_TREE
;
19897 tree dtor
= member
;
19899 if (TREE_CODE (dtor
) == SCOPE_REF
)
19901 s
= TREE_OPERAND (dtor
, 0);
19902 dtor
= TREE_OPERAND (dtor
, 1);
19904 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19906 dtor
= TREE_OPERAND (dtor
, 0);
19908 RETURN (finish_pseudo_destructor_expr
19909 (object
, s
, dtor
, input_location
));
19913 else if (TREE_CODE (member
) == SCOPE_REF
19914 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19916 /* Lookup the template functions now that we know what the
19918 tree scope
= TREE_OPERAND (member
, 0);
19919 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19920 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19921 member
= lookup_qualified_name (scope
, tmpl
,
19922 /*is_type_p=*/false,
19923 /*complain=*/false);
19924 if (BASELINK_P (member
))
19926 BASELINK_FUNCTIONS (member
)
19927 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19929 member
= (adjust_result_of_qualified_name_lookup
19930 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19935 qualified_name_lookup_error (scope
, tmpl
, member
,
19937 RETURN (error_mark_node
);
19940 else if (TREE_CODE (member
) == SCOPE_REF
19941 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19942 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19944 if (complain
& tf_error
)
19946 if (TYPE_P (TREE_OPERAND (member
, 0)))
19947 error ("%qT is not a class or namespace",
19948 TREE_OPERAND (member
, 0));
19950 error ("%qD is not a class or namespace",
19951 TREE_OPERAND (member
, 0));
19953 RETURN (error_mark_node
);
19956 r
= finish_class_member_access_expr (object
, member
,
19957 /*template_p=*/false,
19959 if (TREE_CODE (r
) == COMPONENT_REF
)
19960 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19965 RETURN (build_throw
19966 (input_location
, RECUR (TREE_OPERAND (t
, 0))));
19970 vec
<constructor_elt
, va_gc
> *n
;
19971 constructor_elt
*ce
;
19972 unsigned HOST_WIDE_INT idx
;
19973 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19974 bool process_index_p
;
19976 bool need_copy_p
= false;
19979 if (type
== error_mark_node
)
19980 RETURN (error_mark_node
);
19982 /* We do not want to process the index of aggregate
19983 initializers as they are identifier nodes which will be
19984 looked up by digest_init. */
19985 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19987 if (null_member_pointer_value_p (t
))
19989 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
19993 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19994 newlen
= vec_safe_length (n
);
19995 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19997 if (ce
->index
&& process_index_p
19998 /* An identifier index is looked up in the type
19999 being initialized, not the current scope. */
20000 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
20001 ce
->index
= RECUR (ce
->index
);
20003 if (PACK_EXPANSION_P (ce
->value
))
20005 /* Substitute into the pack expansion. */
20006 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
20009 if (ce
->value
== error_mark_node
20010 || PACK_EXPANSION_P (ce
->value
))
20012 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
20013 /* Just move the argument into place. */
20014 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
20017 /* Update the length of the final CONSTRUCTOR
20018 arguments vector, and note that we will need to
20020 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
20021 need_copy_p
= true;
20025 ce
->value
= RECUR (ce
->value
);
20030 vec
<constructor_elt
, va_gc
> *old_n
= n
;
20032 vec_alloc (n
, newlen
);
20033 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
20035 if (TREE_CODE (ce
->value
) == TREE_VEC
)
20037 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
20038 for (i
= 0; i
< len
; ++i
)
20039 CONSTRUCTOR_APPEND_ELT (n
, 0,
20040 TREE_VEC_ELT (ce
->value
, i
));
20043 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
20047 r
= build_constructor (init_list_type_node
, n
);
20048 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
20049 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
20050 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
20052 if (TREE_HAS_CONSTRUCTOR (t
))
20054 fcl_t cl
= fcl_functional
;
20055 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
20057 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
20060 TREE_TYPE (r
) = type
;
20066 tree operand_0
= TREE_OPERAND (t
, 0);
20067 if (TYPE_P (operand_0
))
20069 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
20070 RETURN (get_typeid (operand_0
, complain
));
20074 operand_0
= RECUR (operand_0
);
20075 RETURN (build_typeid (operand_0
, complain
));
20086 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
20087 /* ??? We're doing a subset of finish_id_expression here. */
20088 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
20089 /* Replace an evaluated use of the thread_local variable with
20090 a call to its wrapper. */
20092 else if (outer_automatic_var_p (r
))
20093 r
= process_outer_var_ref (r
, complain
);
20095 if (!TYPE_REF_P (TREE_TYPE (t
)))
20096 /* If the original type was a reference, we'll be wrapped in
20097 the appropriate INDIRECT_REF. */
20098 r
= convert_from_reference (r
);
20104 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20105 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
20106 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
20109 case OFFSETOF_EXPR
:
20112 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
20113 in_decl
, /*function_p=*/false,
20114 /*integral_constant_expression_p=*/false);
20115 RETURN (finish_offsetof (object_ptr
,
20116 RECUR (TREE_OPERAND (t
, 0)),
20117 EXPR_LOCATION (t
)));
20120 case ADDRESSOF_EXPR
:
20121 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
20122 RECUR (TREE_OPERAND (t
, 0)), complain
));
20126 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
20127 complain
, in_decl
);
20129 tree type2
= TRAIT_EXPR_TYPE2 (t
);
20130 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
20131 type2
= RECUR (type2
);
20133 type2
= tsubst (type2
, args
, complain
, in_decl
);
20135 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t
),
20136 TRAIT_EXPR_KIND (t
), type1
, type2
));
20141 tree old_stmt_expr
= cur_stmt_expr
;
20142 tree stmt_expr
= begin_stmt_expr ();
20144 cur_stmt_expr
= stmt_expr
;
20145 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
20146 integral_constant_expression_p
);
20147 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
20148 cur_stmt_expr
= old_stmt_expr
;
20150 /* If the resulting list of expression statement is empty,
20151 fold it further into void_node. */
20152 if (empty_expr_stmt_p (stmt_expr
))
20153 stmt_expr
= void_node
;
20155 RETURN (stmt_expr
);
20160 if (complain
& tf_partial
)
20162 /* We don't have a full set of template arguments yet; don't touch
20163 the lambda at all. */
20164 gcc_assert (processing_template_decl
);
20167 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
20169 RETURN (build_lambda_object (r
));
20173 /* We can get here for a constant initializer of non-dependent type.
20174 FIXME stop folding in cp_parser_initializer_clause. */
20176 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
20181 case TRANSACTION_EXPR
:
20182 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
20183 integral_constant_expression_p
));
20186 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
20188 case VEC_PERM_EXPR
:
20190 tree op0
= RECUR (TREE_OPERAND (t
, 0));
20191 tree op1
= RECUR (TREE_OPERAND (t
, 1));
20192 tree op2
= RECUR (TREE_OPERAND (t
, 2));
20193 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
20197 case REQUIRES_EXPR
:
20199 tree r
= tsubst_requires_expr (t
, args
, tf_none
, in_decl
);
20200 if (r
== error_mark_node
&& (complain
& tf_error
))
20201 tsubst_requires_expr (t
, args
, complain
, in_decl
);
20206 /* No need to substitute further, a RANGE_EXPR will always be built
20207 with constant operands. */
20210 case NON_LVALUE_EXPR
:
20211 case VIEW_CONVERT_EXPR
:
20212 if (location_wrapper_p (t
))
20213 /* We need to do this here as well as in tsubst_copy so we get the
20214 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20215 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
20216 EXPR_LOCATION (t
)));
20220 /* Handle Objective-C++ constructs, if appropriate. */
20223 = objcp_tsubst_copy_and_build (t
, args
, complain
,
20224 in_decl
, /*function_p=*/false);
20228 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
20234 input_location
= loc
;
20238 /* Verify that the instantiated ARGS are valid. For type arguments,
20239 make sure that the type's linkage is ok. For non-type arguments,
20240 make sure they are constants if they are integral or enumerations.
20241 Emit an error under control of COMPLAIN, and return TRUE on error. */
20244 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
20246 if (dependent_template_arg_p (t
))
20248 if (ARGUMENT_PACK_P (t
))
20250 tree vec
= ARGUMENT_PACK_ARGS (t
);
20251 int len
= TREE_VEC_LENGTH (vec
);
20252 bool result
= false;
20255 for (i
= 0; i
< len
; ++i
)
20256 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
20260 else if (TYPE_P (t
))
20262 /* [basic.link]: A name with no linkage (notably, the name
20263 of a class or enumeration declared in a local scope)
20264 shall not be used to declare an entity with linkage.
20265 This implies that names with no linkage cannot be used as
20268 DR 757 relaxes this restriction for C++0x. */
20269 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
20270 : no_linkage_check (t
, /*relaxed_p=*/false));
20274 /* DR 488 makes use of a type with no linkage cause
20275 type deduction to fail. */
20276 if (complain
& tf_error
)
20278 if (TYPE_UNNAMED_P (nt
))
20279 error ("%qT is/uses unnamed type", t
);
20281 error ("template argument for %qD uses local type %qT",
20286 /* In order to avoid all sorts of complications, we do not
20287 allow variably-modified types as template arguments. */
20288 else if (variably_modified_type_p (t
, NULL_TREE
))
20290 if (complain
& tf_error
)
20291 error ("%qT is a variably modified type", t
);
20295 /* Class template and alias template arguments should be OK. */
20296 else if (DECL_TYPE_TEMPLATE_P (t
))
20298 /* A non-type argument of integral or enumerated type must be a
20300 else if (TREE_TYPE (t
)
20301 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
20302 && !REFERENCE_REF_P (t
)
20303 && !TREE_CONSTANT (t
))
20305 if (complain
& tf_error
)
20306 error ("integral expression %qE is not constant", t
);
20313 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
20315 int ix
, len
= DECL_NTPARMS (tmpl
);
20316 bool result
= false;
20318 for (ix
= 0; ix
!= len
; ix
++)
20320 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
20323 if (result
&& (complain
& tf_error
))
20324 error (" trying to instantiate %qD", tmpl
);
20328 /* We're out of SFINAE context now, so generate diagnostics for the access
20329 errors we saw earlier when instantiating D from TMPL and ARGS. */
20332 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
20334 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
20335 tree type
= TREE_TYPE (pattern
);
20336 location_t loc
= input_location
;
20338 push_access_scope (d
);
20339 push_deferring_access_checks (dk_no_deferred
);
20340 input_location
= DECL_SOURCE_LOCATION (pattern
);
20341 tsubst (type
, args
, tf_warning_or_error
, d
);
20342 input_location
= loc
;
20343 pop_deferring_access_checks ();
20344 pop_access_scope (d
);
20347 /* Instantiate the indicated variable, function, or alias template TMPL with
20348 the template arguments in TARG_PTR. */
20351 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20353 tree targ_ptr
= orig_args
;
20357 bool access_ok
= true;
20359 if (tmpl
== error_mark_node
)
20360 return error_mark_node
;
20362 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
20364 /* If this function is a clone, handle it specially. */
20365 if (DECL_CLONED_FUNCTION_P (tmpl
))
20370 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20371 DECL_CLONED_FUNCTION. */
20372 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
20373 targ_ptr
, complain
);
20374 if (spec
== error_mark_node
)
20375 return error_mark_node
;
20377 /* Look for the clone. */
20378 FOR_EACH_CLONE (clone
, spec
)
20379 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
20381 /* We should always have found the clone by now. */
20382 gcc_unreachable ();
20386 if (targ_ptr
== error_mark_node
)
20387 return error_mark_node
;
20389 /* Check to see if we already have this specialization. */
20390 gen_tmpl
= most_general_template (tmpl
);
20391 if (TMPL_ARGS_DEPTH (targ_ptr
)
20392 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
20393 /* targ_ptr only has the innermost template args, so add the outer ones
20394 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20395 the case of a non-dependent call within a template definition). */
20396 targ_ptr
= (add_outermost_template_args
20397 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
20400 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20401 but it doesn't seem to be on the hot path. */
20402 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
20404 gcc_assert (tmpl
== gen_tmpl
20405 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
20407 || fndecl
== NULL_TREE
);
20409 if (spec
!= NULL_TREE
)
20411 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
20413 if (complain
& tf_error
)
20414 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
20415 return error_mark_node
;
20420 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
20422 return error_mark_node
;
20424 /* We are building a FUNCTION_DECL, during which the access of its
20425 parameters and return types have to be checked. However this
20426 FUNCTION_DECL which is the desired context for access checking
20427 is not built yet. We solve this chicken-and-egg problem by
20428 deferring all checks until we have the FUNCTION_DECL. */
20429 push_deferring_access_checks (dk_deferred
);
20431 /* Instantiation of the function happens in the context of the function
20432 template, not the context of the overload resolution we're doing. */
20433 push_to_top_level ();
20434 /* If there are dependent arguments, e.g. because we're doing partial
20435 ordering, make sure processing_template_decl stays set. */
20436 if (uses_template_parms (targ_ptr
))
20437 ++processing_template_decl
;
20438 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20440 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
20441 complain
, gen_tmpl
, true);
20442 push_nested_class (ctx
);
20445 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
20447 fndecl
= NULL_TREE
;
20448 if (VAR_P (pattern
))
20450 /* We need to determine if we're using a partial or explicit
20451 specialization now, because the type of the variable could be
20453 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
20454 tree elt
= most_specialized_partial_spec (tid
, complain
);
20455 if (elt
== error_mark_node
)
20456 pattern
= error_mark_node
;
20459 tree partial_tmpl
= TREE_VALUE (elt
);
20460 tree partial_args
= TREE_PURPOSE (elt
);
20461 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
20462 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
20466 /* Substitute template parameters to obtain the specialization. */
20467 if (fndecl
== NULL_TREE
)
20468 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
20469 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
20470 pop_nested_class ();
20471 pop_from_top_level ();
20473 if (fndecl
== error_mark_node
)
20475 pop_deferring_access_checks ();
20476 return error_mark_node
;
20479 /* The DECL_TI_TEMPLATE should always be the immediate parent
20480 template, not the most general template. */
20481 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
20482 DECL_TI_ARGS (fndecl
) = targ_ptr
;
20484 /* Now we know the specialization, compute access previously
20485 deferred. Do no access control for inheriting constructors,
20486 as we already checked access for the inherited constructor. */
20487 if (!(flag_new_inheriting_ctors
20488 && DECL_INHERITED_CTOR (fndecl
)))
20490 push_access_scope (fndecl
);
20491 if (!perform_deferred_access_checks (complain
))
20493 pop_access_scope (fndecl
);
20495 pop_deferring_access_checks ();
20497 /* If we've just instantiated the main entry point for a function,
20498 instantiate all the alternate entry points as well. We do this
20499 by cloning the instantiation of the main entry point, not by
20500 instantiating the template clones. */
20501 if (tree chain
= DECL_CHAIN (gen_tmpl
))
20502 if (DECL_P (chain
) && DECL_CLONED_FUNCTION_P (chain
))
20503 clone_function_decl (fndecl
, /*update_methods=*/false);
20507 if (!(complain
& tf_error
))
20509 /* Remember to reinstantiate when we're out of SFINAE so the user
20510 can see the errors. */
20511 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
20513 return error_mark_node
;
20518 /* Wrapper for instantiate_template_1. */
20521 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
20524 timevar_push (TV_TEMPLATE_INST
);
20525 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
20526 timevar_pop (TV_TEMPLATE_INST
);
20530 /* Instantiate the alias template TMPL with ARGS. Also push a template
20531 instantiation level, which instantiate_template doesn't do because
20532 functions and variables have sufficient context established by the
20536 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
20538 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
20539 return error_mark_node
;
20542 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
20543 args
, tmpl
, complain
,
20544 /*require_all_args=*/true,
20545 /*use_default_args=*/true);
20547 /* FIXME check for satisfaction in check_instantiated_args. */
20549 && !any_dependent_template_arguments_p (args
)
20550 && !constraints_satisfied_p (tmpl
, args
))
20552 if (complain
& tf_error
)
20554 auto_diagnostic_group d
;
20555 error ("template constraint failure for %qD", tmpl
);
20556 diagnose_constraints (input_location
, tmpl
, args
);
20558 return error_mark_node
;
20561 if (!push_tinst_level (tmpl
, args
))
20562 return error_mark_node
;
20563 tree r
= instantiate_template (tmpl
, args
, complain
);
20564 pop_tinst_level ();
20569 /* PARM is a template parameter pack for FN. Returns true iff
20570 PARM is used in a deducible way in the argument list of FN. */
20573 pack_deducible_p (tree parm
, tree fn
)
20575 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
20576 for (; t
; t
= TREE_CHAIN (t
))
20578 tree type
= TREE_VALUE (t
);
20580 if (!PACK_EXPANSION_P (type
))
20582 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
20583 packs
; packs
= TREE_CHAIN (packs
))
20584 if (template_args_equal (TREE_VALUE (packs
), parm
))
20586 /* The template parameter pack is used in a function parameter
20587 pack. If this is the end of the parameter list, the
20588 template parameter pack is deducible. */
20589 if (TREE_CHAIN (t
) == void_list_node
)
20592 /* Otherwise, not. Well, it could be deduced from
20593 a non-pack parameter, but doing so would end up with
20594 a deduction mismatch, so don't bother. */
20598 /* The template parameter pack isn't used in any function parameter
20599 packs, but it might be used deeper, e.g. tuple<Args...>. */
20603 /* Subroutine of fn_type_unification: check non-dependent parms for
20607 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
20608 tree fn
, unification_kind_t strict
, int flags
,
20609 struct conversion
**convs
, bool explain_p
)
20611 /* Non-constructor methods need to leave a conversion for 'this', which
20612 isn't included in nargs here. */
20613 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20614 && !DECL_CONSTRUCTOR_P (fn
));
20616 for (unsigned ia
= 0;
20617 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20619 tree parm
= TREE_VALUE (parms
);
20621 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20622 && (!TREE_CHAIN (parms
)
20623 || TREE_CHAIN (parms
) == void_list_node
))
20624 /* For a function parameter pack that occurs at the end of the
20625 parameter-declaration-list, the type A of each remaining
20626 argument of the call is compared with the type P of the
20627 declarator-id of the function parameter pack. */
20630 parms
= TREE_CHAIN (parms
);
20632 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20633 /* For a function parameter pack that does not occur at the
20634 end of the parameter-declaration-list, the type of the
20635 parameter pack is a non-deduced context. */
20638 if (!uses_template_parms (parm
))
20640 tree arg
= args
[ia
];
20641 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
20642 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
20644 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
20645 conv_p
, explain_p
))
20655 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20656 NARGS elements of the arguments that are being used when calling
20657 it. TARGS is a vector into which the deduced template arguments
20660 Returns either a FUNCTION_DECL for the matching specialization of FN or
20661 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20662 true, diagnostics will be printed to explain why it failed.
20664 If FN is a conversion operator, or we are trying to produce a specific
20665 specialization, RETURN_TYPE is the return type desired.
20667 The EXPLICIT_TARGS are explicit template arguments provided via a
20670 The parameter STRICT is one of:
20673 We are deducing arguments for a function call, as in
20674 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20675 deducing arguments for a call to the result of a conversion
20676 function template, as in [over.call.object].
20679 We are deducing arguments for a conversion function, as in
20680 [temp.deduct.conv].
20683 We are deducing arguments when doing an explicit instantiation
20684 as in [temp.explicit], when determining an explicit specialization
20685 as in [temp.expl.spec], or when taking the address of a function
20686 template, as in [temp.deduct.funcaddr]. */
20689 fn_type_unification (tree fn
,
20690 tree explicit_targs
,
20693 unsigned int nargs
,
20695 unification_kind_t strict
,
20697 struct conversion
**convs
,
20703 tree decl
= NULL_TREE
;
20704 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20706 static int deduction_depth
;
20707 /* type_unification_real will pass back any access checks from default
20708 template argument substitution. */
20709 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20710 /* We don't have all the template args yet. */
20711 bool incomplete
= true;
20714 if (flag_new_inheriting_ctors
)
20715 fn
= strip_inheriting_ctors (fn
);
20717 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20718 tree r
= error_mark_node
;
20720 tree full_targs
= targs
;
20721 if (TMPL_ARGS_DEPTH (targs
)
20722 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20723 full_targs
= (add_outermost_template_args
20724 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20728 complain
|= tf_decltype
;
20730 /* In C++0x, it's possible to have a function template whose type depends
20731 on itself recursively. This is most obvious with decltype, but can also
20732 occur with enumeration scope (c++/48969). So we need to catch infinite
20733 recursion and reject the substitution at deduction time; this function
20734 will return error_mark_node for any repeated substitution.
20736 This also catches excessive recursion such as when f<N> depends on
20737 f<N-1> across all integers, and returns error_mark_node for all the
20738 substitutions back up to the initial one.
20740 This is, of course, not reentrant. */
20741 if (excessive_deduction_depth
)
20742 return error_mark_node
;
20745 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20747 fntype
= TREE_TYPE (fn
);
20748 if (explicit_targs
)
20752 The specified template arguments must match the template
20753 parameters in kind (i.e., type, nontype, template), and there
20754 must not be more arguments than there are parameters;
20755 otherwise type deduction fails.
20757 Nontype arguments must match the types of the corresponding
20758 nontype template parameters, or must be convertible to the
20759 types of the corresponding nontype parameters as specified in
20760 _temp.arg.nontype_, otherwise type deduction fails.
20762 All references in the function type of the function template
20763 to the corresponding template parameters are replaced by the
20764 specified template argument values. If a substitution in a
20765 template parameter or in the function type of the function
20766 template results in an invalid type, type deduction fails. */
20767 int i
, len
= TREE_VEC_LENGTH (tparms
);
20768 location_t loc
= input_location
;
20769 incomplete
= false;
20771 if (explicit_targs
== error_mark_node
)
20774 if (TMPL_ARGS_DEPTH (explicit_targs
)
20775 < TMPL_ARGS_DEPTH (full_targs
))
20776 explicit_targs
= add_outermost_template_args (full_targs
,
20779 /* Adjust any explicit template arguments before entering the
20780 substitution context. */
20782 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20783 complain
|tf_partial
,
20784 /*require_all_args=*/false,
20785 /*use_default_args=*/false));
20786 if (explicit_targs
== error_mark_node
)
20789 /* Substitute the explicit args into the function type. This is
20790 necessary so that, for instance, explicitly declared function
20791 arguments can match null pointed constants. If we were given
20792 an incomplete set of explicit args, we must not do semantic
20793 processing during substitution as we could create partial
20795 for (i
= 0; i
< len
; i
++)
20797 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20798 bool parameter_pack
= false;
20799 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20801 /* Dig out the actual parm. */
20802 if (TREE_CODE (parm
) == TYPE_DECL
20803 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20805 parm
= TREE_TYPE (parm
);
20806 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20808 else if (TREE_CODE (parm
) == PARM_DECL
)
20810 parm
= DECL_INITIAL (parm
);
20811 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20814 if (targ
== NULL_TREE
)
20815 /* No explicit argument for this template parameter. */
20817 else if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20819 /* Mark the argument pack as "incomplete". We could
20820 still deduce more arguments during unification.
20821 We remove this mark in type_unification_real. */
20822 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20823 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20824 = ARGUMENT_PACK_ARGS (targ
);
20826 /* We have some incomplete argument packs. */
20833 if (!push_tinst_level (fn
, explicit_targs
))
20835 excessive_deduction_depth
= true;
20838 ++processing_template_decl
;
20839 input_location
= DECL_SOURCE_LOCATION (fn
);
20840 /* Ignore any access checks; we'll see them again in
20841 instantiate_template and they might have the wrong
20842 access path at this point. */
20843 push_deferring_access_checks (dk_deferred
);
20844 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20845 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20846 pop_deferring_access_checks ();
20847 input_location
= loc
;
20848 --processing_template_decl
;
20849 pop_tinst_level ();
20851 if (fntype
== error_mark_node
)
20855 /* Place the explicitly specified arguments in TARGS. */
20856 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20857 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20858 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20859 if (!incomplete
&& CHECKING_P
20860 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20861 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20862 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20865 if (return_type
&& strict
!= DEDUCE_CALL
)
20867 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20868 new_args
[0] = return_type
;
20869 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20877 /* Never do unification on the 'this' parameter. */
20878 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20880 if (return_type
&& strict
== DEDUCE_CALL
)
20882 /* We're deducing for a call to the result of a template conversion
20883 function. The parms we really want are in return_type. */
20884 if (INDIRECT_TYPE_P (return_type
))
20885 return_type
= TREE_TYPE (return_type
);
20886 parms
= TYPE_ARG_TYPES (return_type
);
20888 else if (return_type
)
20890 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20893 /* We allow incomplete unification without an error message here
20894 because the standard doesn't seem to explicitly prohibit it. Our
20895 callers must be ready to deal with unification failures in any
20898 /* If we aren't explaining yet, push tinst context so we can see where
20899 any errors (e.g. from class instantiations triggered by instantiation
20900 of default template arguments) come from. If we are explaining, this
20901 context is redundant. */
20902 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20904 excessive_deduction_depth
= true;
20908 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20909 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20910 strict
, &checks
, explain_p
);
20912 pop_tinst_level ();
20916 /* Now that we have bindings for all of the template arguments,
20917 ensure that the arguments deduced for the template template
20918 parameters have compatible template parameter lists. We cannot
20919 check this property before we have deduced all template
20920 arguments, because the template parameter types of a template
20921 template parameter might depend on prior template parameters
20922 deduced after the template template parameter. The following
20923 ill-formed example illustrates this issue:
20925 template<typename T, template<T> class C> void f(C<5>, T);
20927 template<int N> struct X {};
20930 f(X<5>(), 5l); // error: template argument deduction fails
20933 The template parameter list of 'C' depends on the template type
20934 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20935 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20936 time that we deduce 'C'. */
20937 if (!template_template_parm_bindings_ok_p
20938 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20940 unify_inconsistent_template_template_parameters (explain_p
);
20944 /* DR 1391: All parameters have args, now check non-dependent parms for
20946 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20951 /* All is well so far. Now, check:
20955 When all template arguments have been deduced, all uses of
20956 template parameters in nondeduced contexts are replaced with
20957 the corresponding deduced argument values. If the
20958 substitution results in an invalid type, as described above,
20959 type deduction fails. */
20960 if (!push_tinst_level (fn
, targs
))
20962 excessive_deduction_depth
= true;
20966 /* Also collect access checks from the instantiation. */
20967 reopen_deferring_access_checks (checks
);
20969 decl
= instantiate_template (fn
, targs
, complain
);
20971 checks
= get_deferred_access_checks ();
20972 pop_deferring_access_checks ();
20974 pop_tinst_level ();
20976 if (decl
== error_mark_node
)
20979 /* Now perform any access checks encountered during substitution. */
20980 push_access_scope (decl
);
20981 ok
= perform_access_checks (checks
, complain
);
20982 pop_access_scope (decl
);
20986 /* If we're looking for an exact match, check that what we got
20987 is indeed an exact match. It might not be if some template
20988 parameters are used in non-deduced contexts. But don't check
20989 for an exact match if we have dependent template arguments;
20990 in that case we're doing partial ordering, and we already know
20991 that we have two candidates that will provide the actual type. */
20992 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20994 tree substed
= TREE_TYPE (decl
);
20998 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
21000 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
21001 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
21002 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
21004 unify_type_mismatch (explain_p
, args
[i
],
21005 TREE_VALUE (sarg
));
21010 /* After doing deduction with the inherited constructor, actually return an
21011 instantiation of the inheriting constructor. */
21013 decl
= instantiate_template (orig_fn
, targs
, complain
);
21019 if (excessive_deduction_depth
)
21021 if (deduction_depth
== 0)
21022 /* Reset once we're all the way out. */
21023 excessive_deduction_depth
= false;
21029 /* Adjust types before performing type deduction, as described in
21030 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21031 sections are symmetric. PARM is the type of a function parameter
21032 or the return type of the conversion function. ARG is the type of
21033 the argument passed to the call, or the type of the value
21034 initialized with the result of the conversion function.
21035 ARG_EXPR is the original argument expression, which may be null. */
21038 maybe_adjust_types_for_deduction (unification_kind_t strict
,
21051 /* Swap PARM and ARG throughout the remainder of this
21052 function; the handling is precisely symmetric since PARM
21053 will initialize ARG rather than vice versa. */
21054 std::swap (parm
, arg
);
21058 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21059 too, but here handle it by stripping the reference from PARM
21060 rather than by adding it to ARG. */
21061 if (TYPE_REF_P (*parm
)
21062 && TYPE_REF_IS_RVALUE (*parm
)
21063 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21064 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21065 && TYPE_REF_P (*arg
)
21066 && !TYPE_REF_IS_RVALUE (*arg
))
21067 *parm
= TREE_TYPE (*parm
);
21068 /* Nothing else to do in this case. */
21072 gcc_unreachable ();
21075 if (!TYPE_REF_P (*parm
))
21077 /* [temp.deduct.call]
21079 If P is not a reference type:
21081 --If A is an array type, the pointer type produced by the
21082 array-to-pointer standard conversion (_conv.array_) is
21083 used in place of A for type deduction; otherwise,
21085 --If A is a function type, the pointer type produced by
21086 the function-to-pointer standard conversion
21087 (_conv.func_) is used in place of A for type deduction;
21090 --If A is a cv-qualified type, the top level
21091 cv-qualifiers of A's type are ignored for type
21093 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
21094 *arg
= build_pointer_type (TREE_TYPE (*arg
));
21095 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
21096 *arg
= build_pointer_type (*arg
);
21098 *arg
= TYPE_MAIN_VARIANT (*arg
);
21101 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21102 reference to a cv-unqualified template parameter that does not represent a
21103 template parameter of a class template (during class template argument
21104 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21105 an lvalue, the type "lvalue reference to A" is used in place of A for type
21107 if (TYPE_REF_P (*parm
)
21108 && TYPE_REF_IS_RVALUE (*parm
)
21109 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
21110 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
21111 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
21112 && (arg_expr
? lvalue_p (arg_expr
)
21113 /* try_one_overload doesn't provide an arg_expr, but
21114 functions are always lvalues. */
21115 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
21116 *arg
= build_reference_type (*arg
);
21118 /* [temp.deduct.call]
21120 If P is a cv-qualified type, the top level cv-qualifiers
21121 of P's type are ignored for type deduction. If P is a
21122 reference type, the type referred to by P is used for
21124 *parm
= TYPE_MAIN_VARIANT (*parm
);
21125 if (TYPE_REF_P (*parm
))
21127 *parm
= TREE_TYPE (*parm
);
21128 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21131 /* DR 322. For conversion deduction, remove a reference type on parm
21132 too (which has been swapped into ARG). */
21133 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
21134 *arg
= TREE_TYPE (*arg
);
21139 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21140 template which doesn't contain any deducible template parameters; check if
21141 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21142 unify_one_argument. */
21145 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
21146 int flags
, struct conversion
**conv_p
,
21152 type
= TREE_TYPE (arg
);
21156 if (same_type_p (parm
, type
))
21157 return unify_success (explain_p
);
21159 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
21160 if (strict
== DEDUCE_CONV
)
21162 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
21163 return unify_success (explain_p
);
21165 else if (strict
!= DEDUCE_EXACT
)
21168 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
21170 /* Avoid recalculating this in add_function_candidate. */
21172 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
21174 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
21176 return unify_success (explain_p
);
21179 if (strict
== DEDUCE_EXACT
)
21180 return unify_type_mismatch (explain_p
, parm
, arg
);
21182 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
21185 static bool uses_deducible_template_parms (tree type
);
21187 /* Returns true iff the expression EXPR is one from which a template
21188 argument can be deduced. In other words, if it's an undecorated
21189 use of a template non-type parameter. */
21192 deducible_expression (tree expr
)
21194 /* Strip implicit conversions. */
21195 while (CONVERT_EXPR_P (expr
) || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
)
21196 expr
= TREE_OPERAND (expr
, 0);
21197 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
21200 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21201 deducible way; that is, if it has a max value of <PARM> - 1. */
21204 deducible_array_bound (tree domain
)
21206 if (domain
== NULL_TREE
)
21209 tree max
= TYPE_MAX_VALUE (domain
);
21210 if (TREE_CODE (max
) != MINUS_EXPR
)
21213 return deducible_expression (TREE_OPERAND (max
, 0));
21216 /* Returns true iff the template arguments ARGS use a template parameter
21217 in a deducible way. */
21220 deducible_template_args (tree args
)
21222 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
21225 tree elt
= TREE_VEC_ELT (args
, i
);
21226 if (ARGUMENT_PACK_P (elt
))
21227 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
21230 if (PACK_EXPANSION_P (elt
))
21231 elt
= PACK_EXPANSION_PATTERN (elt
);
21232 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
21234 else if (TYPE_P (elt
))
21235 deducible
= uses_deducible_template_parms (elt
);
21237 deducible
= deducible_expression (elt
);
21245 /* Returns true iff TYPE contains any deducible references to template
21246 parameters, as per 14.8.2.5. */
21249 uses_deducible_template_parms (tree type
)
21251 if (PACK_EXPANSION_P (type
))
21252 type
= PACK_EXPANSION_PATTERN (type
);
21259 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21260 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21266 if (INDIRECT_TYPE_P (type
))
21267 return uses_deducible_template_parms (TREE_TYPE (type
));
21269 /* T[integer-constant ]
21271 if (TREE_CODE (type
) == ARRAY_TYPE
)
21272 return (uses_deducible_template_parms (TREE_TYPE (type
))
21273 || deducible_array_bound (TYPE_DOMAIN (type
)));
21285 if (TYPE_PTRMEM_P (type
))
21286 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
21287 || (uses_deducible_template_parms
21288 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
21290 /* template-name <T> (where template-name refers to a class template)
21291 template-name <i> (where template-name refers to a class template) */
21292 if (CLASS_TYPE_P (type
)
21293 && CLASSTYPE_TEMPLATE_INFO (type
)
21294 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
21295 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21296 (CLASSTYPE_TI_ARGS (type
)));
21301 if (FUNC_OR_METHOD_TYPE_P (type
))
21303 if (uses_deducible_template_parms (TREE_TYPE (type
)))
21305 tree parm
= TYPE_ARG_TYPES (type
);
21306 if (TREE_CODE (type
) == METHOD_TYPE
)
21307 parm
= TREE_CHAIN (parm
);
21308 for (; parm
; parm
= TREE_CHAIN (parm
))
21309 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
21316 /* Subroutine of type_unification_real and unify_pack_expansion to
21317 handle unification of a single P/A pair. Parameters are as
21318 for those functions. */
21321 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
21322 int subr
, unification_kind_t strict
,
21325 tree arg_expr
= NULL_TREE
;
21328 if (arg
== error_mark_node
|| parm
== error_mark_node
)
21329 return unify_invalid (explain_p
);
21330 if (arg
== unknown_type_node
)
21331 /* We can't deduce anything from this, but we might get all the
21332 template args from other function args. */
21333 return unify_success (explain_p
);
21335 /* Implicit conversions (Clause 4) will be performed on a function
21336 argument to convert it to the type of the corresponding function
21337 parameter if the parameter type contains no template-parameters that
21338 participate in template argument deduction. */
21339 if (strict
!= DEDUCE_EXACT
21340 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
21341 /* For function parameters with no deducible template parameters,
21342 just return. We'll check non-dependent conversions later. */
21343 return unify_success (explain_p
);
21348 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
21349 | UNIFY_ALLOW_MORE_CV_QUAL
21350 | UNIFY_ALLOW_DERIVED
);
21354 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
21358 arg_strict
= UNIFY_ALLOW_NONE
;
21362 gcc_unreachable ();
21365 /* We only do these transformations if this is the top-level
21366 parameter_type_list in a call or declaration matching; in other
21367 situations (nested function declarators, template argument lists) we
21368 won't be comparing a type to an expression, and we don't do any type
21374 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
21375 if (type_unknown_p (arg
))
21377 /* [temp.deduct.type] A template-argument can be
21378 deduced from a pointer to function or pointer
21379 to member function argument if the set of
21380 overloaded functions does not contain function
21381 templates and at most one of a set of
21382 overloaded functions provides a unique
21384 resolve_overloaded_unification (tparms
, targs
, parm
,
21386 arg_strict
, explain_p
);
21387 /* If a unique match was not found, this is a
21388 non-deduced context, so we still succeed. */
21389 return unify_success (explain_p
);
21393 arg
= unlowered_expr_type (arg
);
21394 if (arg
== error_mark_node
)
21395 return unify_invalid (explain_p
);
21399 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
21402 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
21403 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
21404 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21406 /* For deduction from an init-list we need the actual list. */
21407 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
21409 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
21412 /* for_each_template_parm callback that always returns 0. */
21415 zero_r (tree
, void *)
21420 /* for_each_template_parm any_fn callback to handle deduction of a template
21421 type argument from the type of an array bound. */
21424 array_deduction_r (tree t
, void *data
)
21426 tree_pair_p d
= (tree_pair_p
)data
;
21427 tree
&tparms
= d
->purpose
;
21428 tree
&targs
= d
->value
;
21430 if (TREE_CODE (t
) == ARRAY_TYPE
)
21431 if (tree dom
= TYPE_DOMAIN (t
))
21432 if (tree max
= TYPE_MAX_VALUE (dom
))
21434 if (TREE_CODE (max
) == MINUS_EXPR
)
21435 max
= TREE_OPERAND (max
, 0);
21436 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
21437 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
21438 UNIFY_ALLOW_NONE
, /*explain*/false);
21441 /* Keep walking. */
21445 /* Try to deduce any not-yet-deduced template type arguments from the type of
21446 an array bound. This is handled separately from unify because 14.8.2.5 says
21447 "The type of a type parameter is only deduced from an array bound if it is
21448 not otherwise deduced." */
21451 try_array_deduction (tree tparms
, tree targs
, tree parm
)
21453 tree_pair_s data
= { tparms
, targs
};
21454 hash_set
<tree
> visited
;
21455 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
21456 /*nondeduced*/false, array_deduction_r
);
21459 /* Most parms like fn_type_unification.
21461 If SUBR is 1, we're being called recursively (to unify the
21462 arguments of a function or method parameter of a function
21465 CHECKS is a pointer to a vector of access checks encountered while
21466 substituting default template arguments. */
21469 type_unification_real (tree tparms
,
21473 unsigned int xnargs
,
21475 unification_kind_t strict
,
21476 vec
<deferred_access_check
, va_gc
> **checks
,
21481 int ntparms
= TREE_VEC_LENGTH (tparms
);
21482 int saw_undeduced
= 0;
21485 unsigned int nargs
;
21488 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
21489 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
21490 gcc_assert (ntparms
> 0);
21492 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
21494 /* Reset the number of non-defaulted template arguments contained
21496 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
21504 while (parms
&& parms
!= void_list_node
21507 parm
= TREE_VALUE (parms
);
21509 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
21510 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
21511 /* For a function parameter pack that occurs at the end of the
21512 parameter-declaration-list, the type A of each remaining
21513 argument of the call is compared with the type P of the
21514 declarator-id of the function parameter pack. */
21517 parms
= TREE_CHAIN (parms
);
21519 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
21520 /* For a function parameter pack that does not occur at the
21521 end of the parameter-declaration-list, the type of the
21522 parameter pack is a non-deduced context. */
21528 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
21534 && parms
!= void_list_node
21535 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
21537 /* Unify the remaining arguments with the pack expansion type. */
21539 tree parmvec
= make_tree_vec (1);
21541 /* Allocate a TREE_VEC and copy in all of the arguments */
21542 argvec
= make_tree_vec (nargs
- ia
);
21543 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
21544 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
21546 /* Copy the parameter into parmvec. */
21547 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
21548 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
21549 /*subr=*/subr
, explain_p
))
21552 /* Advance to the end of the list of parameters. */
21553 parms
= TREE_CHAIN (parms
);
21556 /* Fail if we've reached the end of the parm list, and more args
21557 are present, and the parm list isn't variadic. */
21558 if (ia
< nargs
&& parms
== void_list_node
)
21559 return unify_too_many_arguments (explain_p
, nargs
, ia
);
21560 /* Fail if parms are left and they don't have default values and
21561 they aren't all deduced as empty packs (c++/57397). This is
21562 consistent with sufficient_parms_p. */
21563 if (parms
&& parms
!= void_list_node
21564 && TREE_PURPOSE (parms
) == NULL_TREE
)
21566 unsigned int count
= nargs
;
21571 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
21574 p
= TREE_CHAIN (p
);
21576 while (p
&& p
!= void_list_node
);
21577 if (count
!= nargs
)
21578 return unify_too_few_arguments (explain_p
, ia
, count
,
21584 tsubst_flags_t complain
= (explain_p
21585 ? tf_warning_or_error
21587 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
21589 for (i
= 0; i
< ntparms
; i
++)
21591 tree targ
= TREE_VEC_ELT (targs
, i
);
21592 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21594 /* Clear the "incomplete" flags on all argument packs now so that
21595 substituting them into later default arguments works. */
21596 if (targ
&& ARGUMENT_PACK_P (targ
))
21598 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
21599 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
21602 if (targ
|| tparm
== error_mark_node
)
21604 tparm
= TREE_VALUE (tparm
);
21606 if (TREE_CODE (tparm
) == TYPE_DECL
21607 && !tried_array_deduction
)
21609 try_array_deduction (tparms
, targs
, xparms
);
21610 tried_array_deduction
= true;
21611 if (TREE_VEC_ELT (targs
, i
))
21615 /* If this is an undeduced nontype parameter that depends on
21616 a type parameter, try another pass; its type may have been
21617 deduced from a later argument than the one from which
21618 this parameter can be deduced. */
21619 if (TREE_CODE (tparm
) == PARM_DECL
21620 && uses_template_parms (TREE_TYPE (tparm
))
21621 && saw_undeduced
< 2)
21627 /* Core issue #226 (C++0x) [temp.deduct]:
21629 If a template argument has not been deduced, its
21630 default template argument, if any, is used.
21632 When we are in C++98 mode, TREE_PURPOSE will either
21633 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21634 to explicitly check cxx_dialect here. */
21635 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
21636 /* OK, there is a default argument. Wait until after the
21637 conversion check to do substitution. */
21640 /* If the type parameter is a parameter pack, then it will
21641 be deduced to an empty parameter pack. */
21642 if (template_parameter_pack_p (tparm
))
21646 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
21648 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
21649 TREE_CONSTANT (arg
) = 1;
21652 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21654 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21656 TREE_VEC_ELT (targs
, i
) = arg
;
21660 return unify_parameter_deduction_failure (explain_p
, tparm
);
21663 /* Now substitute into the default template arguments. */
21664 for (i
= 0; i
< ntparms
; i
++)
21666 tree targ
= TREE_VEC_ELT (targs
, i
);
21667 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21669 if (targ
|| tparm
== error_mark_node
)
21671 tree parm
= TREE_VALUE (tparm
);
21672 tree arg
= TREE_PURPOSE (tparm
);
21673 reopen_deferring_access_checks (*checks
);
21674 location_t save_loc
= input_location
;
21676 input_location
= DECL_SOURCE_LOCATION (parm
);
21678 if (saw_undeduced
== 1
21679 && TREE_CODE (parm
) == PARM_DECL
21680 && uses_template_parms (TREE_TYPE (parm
)))
21682 /* The type of this non-type parameter depends on undeduced
21683 parameters. Don't try to use its default argument yet,
21684 since we might deduce an argument for it on the next pass,
21685 but do check whether the arguments we already have cause
21686 substitution failure, so that that happens before we try
21687 later default arguments (78489). */
21688 ++processing_template_decl
;
21689 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21691 --processing_template_decl
;
21692 if (type
== error_mark_node
)
21693 arg
= error_mark_node
;
21699 /* Even if the call is happening in template context, getting
21700 here means it's non-dependent, and a default argument is
21701 considered a separate definition under [temp.decls], so we can
21702 do this substitution without processing_template_decl. This
21703 is important if the default argument contains something that
21704 might be instantiation-dependent like access (87480). */
21705 processing_template_decl_sentinel s
;
21706 tree substed
= NULL_TREE
;
21707 if (saw_undeduced
== 1)
21709 /* First instatiate in template context, in case we still
21710 depend on undeduced template parameters. */
21711 ++processing_template_decl
;
21712 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21714 --processing_template_decl
;
21715 if (substed
!= error_mark_node
21716 && !uses_template_parms (substed
))
21717 /* We replaced all the tparms, substitute again out of
21718 template context. */
21719 substed
= NULL_TREE
;
21722 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21725 if (!uses_template_parms (substed
))
21726 arg
= convert_template_argument (parm
, substed
, full_targs
,
21727 complain
, i
, NULL_TREE
);
21728 else if (saw_undeduced
== 1)
21731 arg
= error_mark_node
;
21734 input_location
= save_loc
;
21735 *checks
= get_deferred_access_checks ();
21736 pop_deferring_access_checks ();
21738 if (arg
== error_mark_node
)
21742 TREE_VEC_ELT (targs
, i
) = arg
;
21743 /* The position of the first default template argument,
21744 is also the number of non-defaulted arguments in TARGS.
21746 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21747 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21751 if (saw_undeduced
++ == 1)
21755 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21756 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21758 return unify_success (explain_p
);
21761 /* Subroutine of type_unification_real. Args are like the variables
21762 at the call site. ARG is an overloaded function (or template-id);
21763 we try deducing template args from each of the overloads, and if
21764 only one succeeds, we go with that. Modifies TARGS and returns
21765 true on success. */
21768 resolve_overloaded_unification (tree tparms
,
21772 unification_kind_t strict
,
21776 tree tempargs
= copy_node (targs
);
21778 tree goodfn
= NULL_TREE
;
21781 if (TREE_CODE (arg
) == ADDR_EXPR
)
21783 arg
= TREE_OPERAND (arg
, 0);
21789 if (TREE_CODE (arg
) == COMPONENT_REF
)
21790 /* Handle `&x' where `x' is some static or non-static member
21792 arg
= TREE_OPERAND (arg
, 1);
21794 if (TREE_CODE (arg
) == OFFSET_REF
)
21795 arg
= TREE_OPERAND (arg
, 1);
21797 /* Strip baselink information. */
21798 if (BASELINK_P (arg
))
21799 arg
= BASELINK_FUNCTIONS (arg
);
21801 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21803 /* If we got some explicit template args, we need to plug them into
21804 the affected templates before we try to unify, in case the
21805 explicit args will completely resolve the templates in question. */
21808 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21809 arg
= TREE_OPERAND (arg
, 0);
21811 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21814 tree subargs
, elem
;
21816 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21819 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21820 expl_subargs
, NULL_TREE
, tf_none
,
21821 /*require_all_args=*/true,
21822 /*use_default_args=*/true);
21823 if (subargs
!= error_mark_node
21824 && !any_dependent_template_arguments_p (subargs
))
21826 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21827 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21828 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21829 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21838 /* If no templates (or more than one) are fully resolved by the
21839 explicit arguments, this template-id is a non-deduced context; it
21840 could still be OK if we deduce all template arguments for the
21841 enclosing call through other arguments. */
21845 else if (!OVL_P (arg
))
21846 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21847 -- but the deduction does not succeed because the expression is
21848 not just the function on its own. */
21851 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21854 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21855 strict
, sub_strict
, addr_p
, explain_p
)
21856 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21863 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21864 to function or pointer to member function argument if the set of
21865 overloaded functions does not contain function templates and at most
21866 one of a set of overloaded functions provides a unique match.
21868 So if we found multiple possibilities, we return success but don't
21869 deduce anything. */
21873 int i
= TREE_VEC_LENGTH (targs
);
21875 if (TREE_VEC_ELT (tempargs
, i
))
21877 tree old
= TREE_VEC_ELT (targs
, i
);
21878 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21879 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21880 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21881 /* Don't forget explicit template arguments in a pack. */
21882 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21883 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21884 TREE_VEC_ELT (targs
, i
) = new_
;
21893 /* Core DR 115: In contexts where deduction is done and fails, or in
21894 contexts where deduction is not done, if a template argument list is
21895 specified and it, along with any default template arguments, identifies
21896 a single function template specialization, then the template-id is an
21897 lvalue for the function template specialization. */
21900 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21902 tree expr
, offset
, baselink
;
21905 if (!type_unknown_p (orig_expr
))
21910 offset
= NULL_TREE
;
21911 baselink
= NULL_TREE
;
21913 if (TREE_CODE (expr
) == ADDR_EXPR
)
21915 expr
= TREE_OPERAND (expr
, 0);
21918 if (TREE_CODE (expr
) == OFFSET_REF
)
21921 expr
= TREE_OPERAND (expr
, 1);
21923 if (BASELINK_P (expr
))
21926 expr
= BASELINK_FUNCTIONS (expr
);
21929 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21932 tree goodfn
= NULL_TREE
;
21934 /* If we got some explicit template args, we need to plug them into
21935 the affected templates before we try to unify, in case the
21936 explicit args will completely resolve the templates in question. */
21938 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21939 tree arg
= TREE_OPERAND (expr
, 0);
21940 tree badfn
= NULL_TREE
;
21941 tree badargs
= NULL_TREE
;
21943 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21946 tree subargs
, elem
;
21948 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21951 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21952 expl_subargs
, NULL_TREE
, tf_none
,
21953 /*require_all_args=*/true,
21954 /*use_default_args=*/true);
21955 if (subargs
!= error_mark_node
21956 && !any_dependent_template_arguments_p (subargs
))
21958 elem
= instantiate_template (fn
, subargs
, tf_none
);
21959 if (elem
== error_mark_node
)
21964 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21973 mark_used (goodfn
);
21976 expr
= build_baselink (BASELINK_BINFO (baselink
),
21977 BASELINK_ACCESS_BINFO (baselink
),
21978 expr
, BASELINK_OPTYPE (baselink
));
21982 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21983 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21986 expr
= cp_build_addr_expr (expr
, complain
);
21989 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21990 /* There were no good options and at least one bad one, so let the
21991 user know what the problem is. */
21992 instantiate_template (badfn
, badargs
, complain
);
21997 /* As above, but error out if the expression remains overloaded. */
22000 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
22002 exp
= resolve_nondeduced_context (exp
, complain
);
22003 if (type_unknown_p (exp
))
22005 if (complain
& tf_error
)
22006 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
22007 return error_mark_node
;
22012 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22013 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22014 different overloads deduce different arguments for a given parm.
22015 ADDR_P is true if the expression for which deduction is being
22016 performed was of the form "& fn" rather than simply "fn".
22018 Returns 1 on success. */
22021 try_one_overload (tree tparms
,
22026 unification_kind_t strict
,
22035 if (arg
== error_mark_node
)
22038 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22039 to function or pointer to member function argument if the set of
22040 overloaded functions does not contain function templates and at most
22041 one of a set of overloaded functions provides a unique match.
22043 So if this is a template, just return success. */
22045 if (uses_template_parms (arg
))
22048 if (TREE_CODE (arg
) == METHOD_TYPE
)
22049 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
22051 arg
= build_pointer_type (arg
);
22053 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
22055 /* We don't copy orig_targs for this because if we have already deduced
22056 some template args from previous args, unify would complain when we
22057 try to deduce a template parameter for the same argument, even though
22058 there isn't really a conflict. */
22059 nargs
= TREE_VEC_LENGTH (targs
);
22060 tempargs
= make_tree_vec (nargs
);
22062 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
22065 /* First make sure we didn't deduce anything that conflicts with
22066 explicitly specified args. */
22067 for (i
= nargs
; i
--; )
22069 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22070 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
22074 else if (uses_template_parms (elt
))
22075 /* Since we're unifying against ourselves, we will fill in
22076 template args used in the function parm list with our own
22077 template parms. Discard them. */
22078 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
22079 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
22081 /* Check that the argument at each index of the deduced argument pack
22082 is equivalent to the corresponding explicitly specified argument.
22083 We may have deduced more arguments than were explicitly specified,
22086 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22087 that's wrong if we deduce the same argument pack from multiple
22088 function arguments: it's only incomplete the first time. */
22090 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
22091 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
22093 if (TREE_VEC_LENGTH (deduced_pack
)
22094 < TREE_VEC_LENGTH (explicit_pack
))
22097 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
22098 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
22099 TREE_VEC_ELT (deduced_pack
, j
)))
22102 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
22106 for (i
= nargs
; i
--; )
22108 tree elt
= TREE_VEC_ELT (tempargs
, i
);
22111 TREE_VEC_ELT (targs
, i
) = elt
;
22117 /* PARM is a template class (perhaps with unbound template
22118 parameters). ARG is a fully instantiated type. If ARG can be
22119 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22120 TARGS are as for unify. */
22123 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
22126 tree copy_of_targs
;
22128 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22130 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22131 /* Matches anything. */;
22132 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
22133 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
22136 /* We need to make a new template argument vector for the call to
22137 unify. If we used TARGS, we'd clutter it up with the result of
22138 the attempted unification, even if this class didn't work out.
22139 We also don't want to commit ourselves to all the unifications
22140 we've already done, since unification is supposed to be done on
22141 an argument-by-argument basis. In other words, consider the
22142 following pathological case:
22144 template <int I, int J, int K>
22147 template <int I, int J>
22148 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22150 template <int I, int J, int K>
22151 void f(S<I, J, K>, S<I, I, I>);
22160 Now, by the time we consider the unification involving `s2', we
22161 already know that we must have `f<0, 0, 0>'. But, even though
22162 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22163 because there are two ways to unify base classes of S<0, 1, 2>
22164 with S<I, I, I>. If we kept the already deduced knowledge, we
22165 would reject the possibility I=1. */
22166 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
22168 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22170 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
22175 /* If unification failed, we're done. */
22176 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
22177 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
22183 /* Given a template type PARM and a class type ARG, find the unique
22184 base type in ARG that is an instance of PARM. We do not examine
22185 ARG itself; only its base-classes. If there is not exactly one
22186 appropriate base class, return NULL_TREE. PARM may be the type of
22187 a partial specialization, as well as a plain template type. Used
22190 static enum template_base_result
22191 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
22192 bool explain_p
, tree
*result
)
22194 tree rval
= NULL_TREE
;
22197 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
22199 binfo
= TYPE_BINFO (complete_type (arg
));
22202 /* The type could not be completed. */
22203 *result
= NULL_TREE
;
22204 return tbr_incomplete_type
;
22207 /* Walk in inheritance graph order. The search order is not
22208 important, and this avoids multiple walks of virtual bases. */
22209 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
22211 tree r
= try_class_unification (tparms
, targs
, parm
,
22212 BINFO_TYPE (binfo
), explain_p
);
22216 /* If there is more than one satisfactory baseclass, then:
22220 If they yield more than one possible deduced A, the type
22224 if (rval
&& !same_type_p (r
, rval
))
22226 *result
= NULL_TREE
;
22227 return tbr_ambiguous_baseclass
;
22235 return tbr_success
;
22238 /* Returns the level of DECL, which declares a template parameter. */
22241 template_decl_level (tree decl
)
22243 switch (TREE_CODE (decl
))
22246 case TEMPLATE_DECL
:
22247 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
22250 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
22253 gcc_unreachable ();
22258 /* Decide whether ARG can be unified with PARM, considering only the
22259 cv-qualifiers of each type, given STRICT as documented for unify.
22260 Returns nonzero iff the unification is OK on that basis. */
22263 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
22265 int arg_quals
= cp_type_quals (arg
);
22266 int parm_quals
= cp_type_quals (parm
);
22268 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22269 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22271 /* Although a CVR qualifier is ignored when being applied to a
22272 substituted template parameter ([8.3.2]/1 for example), that
22273 does not allow us to unify "const T" with "int&" because both
22274 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22275 It is ok when we're allowing additional CV qualifiers
22276 at the outer level [14.8.2.1]/3,1st bullet. */
22277 if ((TYPE_REF_P (arg
)
22278 || FUNC_OR_METHOD_TYPE_P (arg
))
22279 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
22282 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
22283 && (parm_quals
& TYPE_QUAL_RESTRICT
))
22287 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
22288 && (arg_quals
& parm_quals
) != parm_quals
)
22291 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
22292 && (parm_quals
& arg_quals
) != arg_quals
)
22298 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22300 template_parm_level_and_index (tree parm
, int* level
, int* index
)
22302 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22303 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22304 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22306 *index
= TEMPLATE_TYPE_IDX (parm
);
22307 *level
= TEMPLATE_TYPE_LEVEL (parm
);
22311 *index
= TEMPLATE_PARM_IDX (parm
);
22312 *level
= TEMPLATE_PARM_LEVEL (parm
);
22316 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22318 if (unify (TP, TA, P, A, S, EP)) \
22322 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22323 expansion at the end of PACKED_PARMS. Returns 0 if the type
22324 deduction succeeds, 1 otherwise. STRICT is the same as in
22325 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22326 function call argument list. We'll need to adjust the arguments to make them
22327 types. SUBR tells us if this is from a recursive call to
22328 type_unification_real, or for comparing two template argument
22332 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
22333 tree packed_args
, unification_kind_t strict
,
22334 bool subr
, bool explain_p
)
22337 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
22338 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
22339 tree pack
, packs
= NULL_TREE
;
22340 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
22342 /* Add in any args remembered from an earlier partial instantiation. */
22343 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
22344 int levels
= TMPL_ARGS_DEPTH (targs
);
22346 packed_args
= expand_template_argument_pack (packed_args
);
22348 int len
= TREE_VEC_LENGTH (packed_args
);
22350 /* Determine the parameter packs we will be deducing from the
22351 pattern, and record their current deductions. */
22352 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
22353 pack
; pack
= TREE_CHAIN (pack
))
22355 tree parm_pack
= TREE_VALUE (pack
);
22358 /* Only template parameter packs can be deduced, not e.g. function
22359 parameter packs or __bases or __integer_pack. */
22360 if (!TEMPLATE_PARM_P (parm_pack
))
22363 /* Determine the index and level of this parameter pack. */
22364 template_parm_level_and_index (parm_pack
, &level
, &idx
);
22365 if (level
< levels
)
22368 /* Keep track of the parameter packs and their corresponding
22370 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
22371 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
22374 /* Loop through all of the arguments that have not yet been
22375 unified and unify each with the pattern. */
22376 for (i
= start
; i
< len
; i
++)
22379 bool any_explicit
= false;
22380 tree arg
= TREE_VEC_ELT (packed_args
, i
);
22382 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22383 or the element of its argument pack at the current index if
22384 this argument was explicitly specified. */
22385 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22389 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22392 if (TREE_VALUE (pack
)
22393 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
22394 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
22396 any_explicit
= true;
22397 arg
= TREE_VEC_ELT (pargs
, i
- start
);
22399 TMPL_ARG (targs
, level
, idx
) = arg
;
22402 /* If we had explicit template arguments, substitute them into the
22403 pattern before deduction. */
22406 /* Some arguments might still be unspecified or dependent. */
22408 ++processing_template_decl
;
22409 dependent
= any_dependent_template_arguments_p (targs
);
22411 --processing_template_decl
;
22412 parm
= tsubst (pattern
, targs
,
22413 explain_p
? tf_warning_or_error
: tf_none
,
22416 --processing_template_decl
;
22417 if (parm
== error_mark_node
)
22423 /* Unify the pattern with the current argument. */
22424 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
22428 /* For each parameter pack, collect the deduced value. */
22429 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22432 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22434 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
22435 TMPL_ARG (targs
, level
, idx
);
22439 /* Verify that the results of unification with the parameter packs
22440 produce results consistent with what we've seen before, and make
22441 the deduced argument packs available. */
22442 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
22444 tree old_pack
= TREE_VALUE (pack
);
22445 tree new_args
= TREE_TYPE (pack
);
22446 int i
, len
= TREE_VEC_LENGTH (new_args
);
22448 bool nondeduced_p
= false;
22450 /* By default keep the original deduced argument pack.
22451 If necessary, more specific code is going to update the
22452 resulting deduced argument later down in this function. */
22453 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
22454 TMPL_ARG (targs
, level
, idx
) = old_pack
;
22456 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22457 actually deduce anything. */
22458 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
22459 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
22460 nondeduced_p
= true;
22464 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
22466 /* If we had fewer function args than explicit template args,
22467 just use the explicits. */
22468 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22469 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
22470 if (len
< explicit_len
)
22471 new_args
= explicit_args
;
22477 /* Build the deduced *_ARGUMENT_PACK. */
22478 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
22480 result
= make_node (NONTYPE_ARGUMENT_PACK
);
22481 TREE_CONSTANT (result
) = 1;
22484 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
22486 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
22488 /* Note the deduced argument packs for this parameter
22490 TMPL_ARG (targs
, level
, idx
) = result
;
22492 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
22493 && (ARGUMENT_PACK_ARGS (old_pack
)
22494 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
22496 /* We only had the explicitly-provided arguments before, but
22497 now we have a complete set of arguments. */
22498 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
22500 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
22501 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
22502 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
22506 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
22507 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
22509 if (!comp_template_args (old_args
, new_args
,
22510 &bad_old_arg
, &bad_new_arg
))
22511 /* Inconsistent unification of this parameter pack. */
22512 return unify_parameter_pack_inconsistent (explain_p
,
22518 return unify_success (explain_p
);
22521 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22522 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22523 parameters and return value are as for unify. */
22526 unify_array_domain (tree tparms
, tree targs
,
22527 tree parm_dom
, tree arg_dom
,
22535 /* Our representation of array types uses "N - 1" as the
22536 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22537 not an integer constant. We cannot unify arbitrarily
22538 complex expressions, so we eliminate the MINUS_EXPRs
22540 parm_max
= TYPE_MAX_VALUE (parm_dom
);
22541 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
22544 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
22545 parm_max
= TREE_OPERAND (parm_max
, 0);
22547 arg_max
= TYPE_MAX_VALUE (arg_dom
);
22548 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
22551 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22552 trying to unify the type of a variable with the type
22553 of a template parameter. For example:
22555 template <unsigned int N>
22556 void f (char (&) [N]);
22563 Here, the type of the ARG will be "int [g(i)]", and
22564 may be a SAVE_EXPR, etc. */
22565 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
22566 return unify_vla_arg (explain_p
, arg_dom
);
22567 arg_max
= TREE_OPERAND (arg_max
, 0);
22570 /* If only one of the bounds used a MINUS_EXPR, compensate
22571 by adding one to the other bound. */
22572 if (parm_cst
&& !arg_cst
)
22573 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22577 else if (arg_cst
&& !parm_cst
)
22578 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
22583 return unify (tparms
, targs
, parm_max
, arg_max
,
22584 UNIFY_ALLOW_INTEGER
, explain_p
);
22587 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22589 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
22594 if (PACK_EXPANSION_P (t
))
22595 t
= PACK_EXPANSION_PATTERN (t
);
22596 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
22597 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
22598 || DECL_TYPE_TEMPLATE_P (t
))
22600 else if (TYPE_P (t
))
22606 /* Deduce the value of template parameters. TPARMS is the (innermost)
22607 set of template parameters to a template. TARGS is the bindings
22608 for those template parameters, as determined thus far; TARGS may
22609 include template arguments for outer levels of template parameters
22610 as well. PARM is a parameter to a template function, or a
22611 subcomponent of that parameter; ARG is the corresponding argument.
22612 This function attempts to match PARM with ARG in a manner
22613 consistent with the existing assignments in TARGS. If more values
22614 are deduced, then TARGS is updated.
22616 Returns 0 if the type deduction succeeds, 1 otherwise. The
22617 parameter STRICT is a bitwise or of the following flags:
22620 Require an exact match between PARM and ARG.
22621 UNIFY_ALLOW_MORE_CV_QUAL:
22622 Allow the deduced ARG to be more cv-qualified (by qualification
22623 conversion) than ARG.
22624 UNIFY_ALLOW_LESS_CV_QUAL:
22625 Allow the deduced ARG to be less cv-qualified than ARG.
22626 UNIFY_ALLOW_DERIVED:
22627 Allow the deduced ARG to be a template base class of ARG,
22628 or a pointer to a template base class of the type pointed to by
22630 UNIFY_ALLOW_INTEGER:
22631 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22632 case for more information.
22633 UNIFY_ALLOW_OUTER_LEVEL:
22634 This is the outermost level of a deduction. Used to determine validity
22635 of qualification conversions. A valid qualification conversion must
22636 have const qualified pointers leading up to the inner type which
22637 requires additional CV quals, except at the outer level, where const
22638 is not required [conv.qual]. It would be normal to set this flag in
22639 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22640 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22641 This is the outermost level of a deduction, and PARM can be more CV
22642 qualified at this point.
22643 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22644 This is the outermost level of a deduction, and PARM can be less CV
22645 qualified at this point. */
22648 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22654 int strict_in
= strict
;
22655 tsubst_flags_t complain
= (explain_p
22656 ? tf_warning_or_error
22659 /* I don't think this will do the right thing with respect to types.
22660 But the only case I've seen it in so far has been array bounds, where
22661 signedness is the only information lost, and I think that will be
22662 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22663 finish_id_expression_1, and are also OK. */
22664 while (CONVERT_EXPR_P (parm
) || TREE_CODE (parm
) == VIEW_CONVERT_EXPR
)
22665 parm
= TREE_OPERAND (parm
, 0);
22667 if (arg
== error_mark_node
)
22668 return unify_invalid (explain_p
);
22669 if (arg
== unknown_type_node
22670 || arg
== init_list_type_node
)
22671 /* We can't deduce anything from this, but we might get all the
22672 template args from other function args. */
22673 return unify_success (explain_p
);
22675 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22676 return unify_success (explain_p
);
22678 /* If PARM uses template parameters, then we can't bail out here,
22679 even if ARG == PARM, since we won't record unifications for the
22680 template parameters. We might need them if we're trying to
22681 figure out which of two things is more specialized. */
22682 if (arg
== parm
&& !uses_template_parms (parm
))
22683 return unify_success (explain_p
);
22685 /* Handle init lists early, so the rest of the function can assume
22686 we're dealing with a type. */
22687 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22691 tree orig_parm
= parm
;
22693 if (!is_std_init_list (parm
)
22694 && TREE_CODE (parm
) != ARRAY_TYPE
)
22695 /* We can only deduce from an initializer list argument if the
22696 parameter is std::initializer_list or an array; otherwise this
22697 is a non-deduced context. */
22698 return unify_success (explain_p
);
22700 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22701 elttype
= TREE_TYPE (parm
);
22704 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22705 /* Deduction is defined in terms of a single type, so just punt
22706 on the (bizarre) std::initializer_list<T...>. */
22707 if (PACK_EXPANSION_P (elttype
))
22708 return unify_success (explain_p
);
22711 if (strict
!= DEDUCE_EXACT
22712 && TYPE_P (elttype
)
22713 && !uses_deducible_template_parms (elttype
))
22714 /* If ELTTYPE has no deducible template parms, skip deduction from
22715 the list elements. */;
22717 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22719 int elt_strict
= strict
;
22721 if (elt
== error_mark_node
)
22722 return unify_invalid (explain_p
);
22724 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22726 tree type
= TREE_TYPE (elt
);
22727 if (type
== error_mark_node
)
22728 return unify_invalid (explain_p
);
22729 /* It should only be possible to get here for a call. */
22730 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22731 elt_strict
|= maybe_adjust_types_for_deduction
22732 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22736 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22740 if (TREE_CODE (parm
) == ARRAY_TYPE
22741 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22743 /* Also deduce from the length of the initializer list. */
22744 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22745 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22746 if (idx
== error_mark_node
)
22747 return unify_invalid (explain_p
);
22748 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22752 /* If the std::initializer_list<T> deduction worked, replace the
22753 deduced A with std::initializer_list<A>. */
22754 if (orig_parm
!= parm
)
22756 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22757 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22758 targ
= listify (targ
);
22759 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22761 return unify_success (explain_p
);
22764 /* If parm and arg aren't the same kind of thing (template, type, or
22765 expression), fail early. */
22766 if (pa_kind (parm
) != pa_kind (arg
))
22767 return unify_invalid (explain_p
);
22769 /* Immediately reject some pairs that won't unify because of
22770 cv-qualification mismatches. */
22771 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22773 /* It is the elements of the array which hold the cv quals of an array
22774 type, and the elements might be template type parms. We'll check
22775 when we recurse. */
22776 && TREE_CODE (arg
) != ARRAY_TYPE
22777 /* We check the cv-qualifiers when unifying with template type
22778 parameters below. We want to allow ARG `const T' to unify with
22779 PARM `T' for example, when computing which of two templates
22780 is more specialized, for example. */
22781 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22782 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22783 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22785 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22786 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22787 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22788 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22789 strict
&= ~UNIFY_ALLOW_DERIVED
;
22790 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22791 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22793 switch (TREE_CODE (parm
))
22795 case TYPENAME_TYPE
:
22797 case UNBOUND_CLASS_TEMPLATE
:
22798 /* In a type which contains a nested-name-specifier, template
22799 argument values cannot be deduced for template parameters used
22800 within the nested-name-specifier. */
22801 return unify_success (explain_p
);
22803 case TEMPLATE_TYPE_PARM
:
22804 case TEMPLATE_TEMPLATE_PARM
:
22805 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22806 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22807 if (error_operand_p (tparm
))
22808 return unify_invalid (explain_p
);
22810 if (TEMPLATE_TYPE_LEVEL (parm
)
22811 != template_decl_level (tparm
))
22812 /* The PARM is not one we're trying to unify. Just check
22813 to see if it matches ARG. */
22815 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22816 && (is_auto (parm
) ? is_auto (arg
)
22817 : same_type_p (parm
, arg
)))
22818 return unify_success (explain_p
);
22820 return unify_type_mismatch (explain_p
, parm
, arg
);
22822 idx
= TEMPLATE_TYPE_IDX (parm
);
22823 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22824 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22825 if (error_operand_p (tparm
))
22826 return unify_invalid (explain_p
);
22828 /* Check for mixed types and values. */
22829 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22830 && TREE_CODE (tparm
) != TYPE_DECL
)
22831 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22832 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22833 gcc_unreachable ();
22835 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22837 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22838 && CLASS_TYPE_P (arg
))
22840 /* First try to match ARG directly. */
22841 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22845 /* Otherwise, look for a suitable base of ARG, as below. */
22846 enum template_base_result r
;
22847 r
= get_template_base (tparms
, targs
, parm
, arg
,
22850 return unify_no_common_base (explain_p
, r
, parm
, arg
);
22854 /* ARG must be constructed from a template class or a template
22855 template parameter. */
22856 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
22857 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22858 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22860 /* Deduce arguments T, i from TT<T> or TT<i>. */
22861 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22864 arg
= TYPE_TI_TEMPLATE (arg
);
22866 /* Fall through to deduce template name. */
22869 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22870 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22872 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22874 /* Simple cases: Value already set, does match or doesn't. */
22875 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22876 return unify_success (explain_p
);
22878 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22882 /* If PARM is `const T' and ARG is only `int', we don't have
22883 a match unless we are allowing additional qualification.
22884 If ARG is `const int' and PARM is just `T' that's OK;
22885 that binds `const int' to `T'. */
22886 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22888 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22890 /* Consider the case where ARG is `const volatile int' and
22891 PARM is `const T'. Then, T should be `volatile int'. */
22892 arg
= cp_build_qualified_type_real
22893 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22894 if (arg
== error_mark_node
)
22895 return unify_invalid (explain_p
);
22897 /* Simple cases: Value already set, does match or doesn't. */
22898 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22899 return unify_success (explain_p
);
22901 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22903 /* Make sure that ARG is not a variable-sized array. (Note
22904 that were talking about variable-sized arrays (like
22905 `int[n]'), rather than arrays of unknown size (like
22906 `int[]').) We'll get very confused by such a type since
22907 the bound of the array is not constant, and therefore
22908 not mangleable. Besides, such types are not allowed in
22909 ISO C++, so we can do as we please here. We do allow
22910 them for 'auto' deduction, since that isn't ABI-exposed. */
22911 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22912 return unify_vla_arg (explain_p
, arg
);
22914 /* Strip typedefs as in convert_template_argument. */
22915 arg
= canonicalize_type_argument (arg
, tf_none
);
22918 /* If ARG is a parameter pack or an expansion, we cannot unify
22919 against it unless PARM is also a parameter pack. */
22920 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22921 && !template_parameter_pack_p (parm
))
22922 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22924 /* If the argument deduction results is a METHOD_TYPE,
22925 then there is a problem.
22926 METHOD_TYPE doesn't map to any real C++ type the result of
22927 the deduction cannot be of that type. */
22928 if (TREE_CODE (arg
) == METHOD_TYPE
)
22929 return unify_method_type_error (explain_p
, arg
);
22931 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22932 return unify_success (explain_p
);
22934 case TEMPLATE_PARM_INDEX
:
22935 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22936 if (error_operand_p (tparm
))
22937 return unify_invalid (explain_p
);
22939 if (TEMPLATE_PARM_LEVEL (parm
)
22940 != template_decl_level (tparm
))
22942 /* The PARM is not one we're trying to unify. Just check
22943 to see if it matches ARG. */
22944 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22945 && cp_tree_equal (parm
, arg
));
22947 unify_expression_unequal (explain_p
, parm
, arg
);
22951 idx
= TEMPLATE_PARM_IDX (parm
);
22952 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22956 if ((strict
& UNIFY_ALLOW_INTEGER
)
22957 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22958 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22959 /* We're deducing from an array bound, the type doesn't matter. */
22960 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22961 int x
= !cp_tree_equal (targ
, arg
);
22963 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22967 /* [temp.deduct.type] If, in the declaration of a function template
22968 with a non-type template-parameter, the non-type
22969 template-parameter is used in an expression in the function
22970 parameter-list and, if the corresponding template-argument is
22971 deduced, the template-argument type shall match the type of the
22972 template-parameter exactly, except that a template-argument
22973 deduced from an array bound may be of any integral type.
22974 The non-type parameter might use already deduced type parameters. */
22975 tparm
= TREE_TYPE (parm
);
22976 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22977 /* We don't have enough levels of args to do any substitution. This
22978 can happen in the context of -fnew-ttp-matching. */;
22981 ++processing_template_decl
;
22982 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22983 --processing_template_decl
;
22985 if (tree a
= type_uses_auto (tparm
))
22987 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22988 if (tparm
== error_mark_node
)
22993 if (!TREE_TYPE (arg
))
22994 /* Template-parameter dependent expression. Just accept it for now.
22995 It will later be processed in convert_template_argument. */
22997 else if (same_type_ignoring_top_level_qualifiers_p
22998 (non_reference (TREE_TYPE (arg
)),
22999 non_reference (tparm
)))
23000 /* OK. Ignore top-level quals here because a class-type template
23001 parameter object is const. */;
23002 else if ((strict
& UNIFY_ALLOW_INTEGER
)
23003 && CP_INTEGRAL_TYPE_P (tparm
))
23004 /* Convert the ARG to the type of PARM; the deduced non-type
23005 template argument must exactly match the types of the
23006 corresponding parameter. */
23007 arg
= fold (build_nop (tparm
, arg
));
23008 else if (uses_template_parms (tparm
))
23010 /* We haven't deduced the type of this parameter yet. */
23011 if (cxx_dialect
>= cxx17
23012 /* We deduce from array bounds in try_array_deduction. */
23013 && !(strict
& UNIFY_ALLOW_INTEGER
))
23015 /* Deduce it from the non-type argument. */
23016 tree atype
= TREE_TYPE (arg
);
23017 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23019 UNIFY_ALLOW_NONE
, explain_p
);
23022 /* Try again later. */
23023 return unify_success (explain_p
);
23026 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
23028 /* If ARG is a parameter pack or an expansion, we cannot unify
23029 against it unless PARM is also a parameter pack. */
23030 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
23031 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
23032 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
23035 bool removed_attr
= false;
23036 arg
= strip_typedefs_expr (arg
, &removed_attr
);
23038 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
23039 return unify_success (explain_p
);
23043 /* A pointer-to-member constant can be unified only with
23044 another constant. */
23045 if (TREE_CODE (arg
) != PTRMEM_CST
)
23046 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
23048 /* Just unify the class member. It would be useless (and possibly
23049 wrong, depending on the strict flags) to unify also
23050 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23051 arg refer to the same variable, even if through different
23052 classes. For instance:
23054 struct A { int x; };
23057 Unification of &A::x and &B::x must succeed. */
23058 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
23059 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
23064 if (!TYPE_PTR_P (arg
))
23065 return unify_type_mismatch (explain_p
, parm
, arg
);
23067 /* [temp.deduct.call]
23069 A can be another pointer or pointer to member type that can
23070 be converted to the deduced A via a qualification
23071 conversion (_conv.qual_).
23073 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23074 This will allow for additional cv-qualification of the
23075 pointed-to types if appropriate. */
23077 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
23078 /* The derived-to-base conversion only persists through one
23079 level of pointers. */
23080 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
23082 return unify (tparms
, targs
, TREE_TYPE (parm
),
23083 TREE_TYPE (arg
), strict
, explain_p
);
23086 case REFERENCE_TYPE
:
23087 if (!TYPE_REF_P (arg
))
23088 return unify_type_mismatch (explain_p
, parm
, arg
);
23089 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23090 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23093 if (TREE_CODE (arg
) != ARRAY_TYPE
)
23094 return unify_type_mismatch (explain_p
, parm
, arg
);
23095 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
23096 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
23097 return unify_type_mismatch (explain_p
, parm
, arg
);
23098 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23099 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
23100 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
23101 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
23102 TYPE_DOMAIN (arg
), explain_p
);
23103 return unify_success (explain_p
);
23110 case ENUMERAL_TYPE
:
23113 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23114 return unify_type_mismatch (explain_p
, parm
, arg
);
23116 /* We have already checked cv-qualification at the top of the
23118 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
23119 return unify_type_mismatch (explain_p
, parm
, arg
);
23121 /* As far as unification is concerned, this wins. Later checks
23122 will invalidate it if necessary. */
23123 return unify_success (explain_p
);
23125 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23126 /* Type INTEGER_CST can come from ordinary constant template args. */
23128 while (CONVERT_EXPR_P (arg
))
23129 arg
= TREE_OPERAND (arg
, 0);
23131 if (TREE_CODE (arg
) != INTEGER_CST
)
23132 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23133 return (tree_int_cst_equal (parm
, arg
)
23134 ? unify_success (explain_p
)
23135 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
23139 int i
, len
, argslen
;
23140 int parm_variadic_p
= 0;
23142 if (TREE_CODE (arg
) != TREE_VEC
)
23143 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23145 len
= TREE_VEC_LENGTH (parm
);
23146 argslen
= TREE_VEC_LENGTH (arg
);
23148 /* Check for pack expansions in the parameters. */
23149 for (i
= 0; i
< len
; ++i
)
23151 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
23154 /* We can unify against something with a trailing
23156 parm_variadic_p
= 1;
23158 /* [temp.deduct.type]/9: If the template argument list of
23159 P contains a pack expansion that is not the last
23160 template argument, the entire template argument list
23161 is a non-deduced context. */
23162 return unify_success (explain_p
);
23166 /* If we don't have enough arguments to satisfy the parameters
23167 (not counting the pack expression at the end), or we have
23168 too many arguments for a parameter list that doesn't end in
23169 a pack expression, we can't unify. */
23170 if (parm_variadic_p
23171 ? argslen
< len
- parm_variadic_p
23173 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
23175 /* Unify all of the parameters that precede the (optional)
23176 pack expression. */
23177 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
23179 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
23180 TREE_VEC_ELT (parm
, i
),
23181 TREE_VEC_ELT (arg
, i
),
23182 UNIFY_ALLOW_NONE
, explain_p
);
23184 if (parm_variadic_p
)
23185 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
23187 /*subr=*/true, explain_p
);
23188 return unify_success (explain_p
);
23193 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23194 return unify_type_mismatch (explain_p
, parm
, arg
);
23196 if (TYPE_PTRMEMFUNC_P (parm
))
23198 if (!TYPE_PTRMEMFUNC_P (arg
))
23199 return unify_type_mismatch (explain_p
, parm
, arg
);
23201 return unify (tparms
, targs
,
23202 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
23203 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
23204 strict
, explain_p
);
23206 else if (TYPE_PTRMEMFUNC_P (arg
))
23207 return unify_type_mismatch (explain_p
, parm
, arg
);
23209 if (CLASSTYPE_TEMPLATE_INFO (parm
))
23211 tree t
= NULL_TREE
;
23213 if (strict_in
& UNIFY_ALLOW_DERIVED
)
23215 /* First, we try to unify the PARM and ARG directly. */
23216 t
= try_class_unification (tparms
, targs
,
23217 parm
, arg
, explain_p
);
23221 /* Fallback to the special case allowed in
23222 [temp.deduct.call]:
23224 If P is a class, and P has the form
23225 template-id, then A can be a derived class of
23226 the deduced A. Likewise, if P is a pointer to
23227 a class of the form template-id, A can be a
23228 pointer to a derived class pointed to by the
23230 enum template_base_result r
;
23231 r
= get_template_base (tparms
, targs
, parm
, arg
,
23236 /* Don't give the derived diagnostic if we're
23237 already dealing with the same template. */
23239 = (CLASSTYPE_TEMPLATE_INFO (arg
)
23240 && (CLASSTYPE_TI_TEMPLATE (parm
)
23241 == CLASSTYPE_TI_TEMPLATE (arg
)));
23242 return unify_no_common_base (explain_p
&& !same_template
,
23247 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
23248 && (CLASSTYPE_TI_TEMPLATE (parm
)
23249 == CLASSTYPE_TI_TEMPLATE (arg
)))
23250 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23251 Then, we should unify `int' and `U'. */
23254 /* There's no chance of unification succeeding. */
23255 return unify_type_mismatch (explain_p
, parm
, arg
);
23257 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
23258 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
23260 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
23261 return unify_type_mismatch (explain_p
, parm
, arg
);
23262 return unify_success (explain_p
);
23265 case FUNCTION_TYPE
:
23267 unsigned int nargs
;
23272 if (TREE_CODE (arg
) != TREE_CODE (parm
))
23273 return unify_type_mismatch (explain_p
, parm
, arg
);
23275 /* CV qualifications for methods can never be deduced, they must
23276 match exactly. We need to check them explicitly here,
23277 because type_unification_real treats them as any other
23278 cv-qualified parameter. */
23279 if (TREE_CODE (parm
) == METHOD_TYPE
23280 && (!check_cv_quals_for_unify
23282 class_of_this_parm (arg
),
23283 class_of_this_parm (parm
))))
23284 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23285 if (TREE_CODE (arg
) == FUNCTION_TYPE
23286 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
23287 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23288 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
23289 return unify_type_mismatch (explain_p
, parm
, arg
);
23291 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
23292 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
23294 nargs
= list_length (TYPE_ARG_TYPES (arg
));
23295 args
= XALLOCAVEC (tree
, nargs
);
23296 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
23297 a
!= NULL_TREE
&& a
!= void_list_node
;
23298 a
= TREE_CHAIN (a
), ++i
)
23299 args
[i
] = TREE_VALUE (a
);
23302 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
23303 args
, nargs
, 1, DEDUCE_EXACT
,
23307 if (flag_noexcept_type
)
23309 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
23310 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
23311 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
23312 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
23313 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
23314 && uses_template_parms (TREE_PURPOSE (pspec
)))
23315 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
23316 TREE_PURPOSE (aspec
),
23317 UNIFY_ALLOW_NONE
, explain_p
);
23318 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
23319 return unify_type_mismatch (explain_p
, parm
, arg
);
23326 /* Unify a pointer to member with a pointer to member function, which
23327 deduces the type of the member as a function type. */
23328 if (TYPE_PTRMEMFUNC_P (arg
))
23330 /* Check top-level cv qualifiers */
23331 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
23332 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
23334 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23335 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
23336 UNIFY_ALLOW_NONE
, explain_p
);
23338 /* Determine the type of the function we are unifying against. */
23339 tree fntype
= static_fn_type (arg
);
23341 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
23344 if (TREE_CODE (arg
) != OFFSET_TYPE
)
23345 return unify_type_mismatch (explain_p
, parm
, arg
);
23346 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
23347 TYPE_OFFSET_BASETYPE (arg
),
23348 UNIFY_ALLOW_NONE
, explain_p
);
23349 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
23350 strict
, explain_p
);
23353 if (DECL_TEMPLATE_PARM_P (parm
))
23354 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
23355 if (arg
!= scalar_constant_value (parm
))
23356 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23357 return unify_success (explain_p
);
23360 case TEMPLATE_DECL
:
23361 /* Matched cases are handled by the ARG == PARM test above. */
23362 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
23365 /* We might get a variable as a non-type template argument in parm if the
23366 corresponding parameter is type-dependent. Make any necessary
23367 adjustments based on whether arg is a reference. */
23368 if (CONSTANT_CLASS_P (arg
))
23369 parm
= fold_non_dependent_expr (parm
, complain
);
23370 else if (REFERENCE_REF_P (arg
))
23372 tree sub
= TREE_OPERAND (arg
, 0);
23374 if (TREE_CODE (sub
) == ADDR_EXPR
)
23375 arg
= TREE_OPERAND (sub
, 0);
23377 /* Now use the normal expression code to check whether they match. */
23380 case TYPE_ARGUMENT_PACK
:
23381 case NONTYPE_ARGUMENT_PACK
:
23382 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
23383 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
23386 case DECLTYPE_TYPE
:
23387 case UNDERLYING_TYPE
:
23388 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23389 or UNDERLYING_TYPE nodes. */
23390 return unify_success (explain_p
);
23393 /* Unification fails if we hit an error node. */
23394 return unify_invalid (explain_p
);
23397 if (REFERENCE_REF_P (parm
))
23399 bool pexp
= PACK_EXPANSION_P (arg
);
23401 arg
= PACK_EXPANSION_PATTERN (arg
);
23402 if (REFERENCE_REF_P (arg
))
23403 arg
= TREE_OPERAND (arg
, 0);
23405 arg
= make_pack_expansion (arg
, complain
);
23406 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
23407 strict
, explain_p
);
23412 /* An unresolved overload is a nondeduced context. */
23413 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
23414 return unify_success (explain_p
);
23415 gcc_assert (EXPR_P (parm
)
23416 || COMPOUND_LITERAL_P (parm
)
23417 || TREE_CODE (parm
) == TRAIT_EXPR
);
23419 /* We must be looking at an expression. This can happen with
23423 void foo(S<I>, S<I + 2>);
23427 template<typename T>
23428 void foo(A<T, T{}>);
23430 This is a "non-deduced context":
23434 The non-deduced contexts are:
23436 --A non-type template argument or an array bound in which
23437 a subexpression references a template parameter.
23439 In these cases, we assume deduction succeeded, but don't
23440 actually infer any unifications. */
23442 if (!uses_template_parms (parm
)
23443 && !template_args_equal (parm
, arg
))
23444 return unify_expression_unequal (explain_p
, parm
, arg
);
23446 return unify_success (explain_p
);
23449 #undef RECUR_AND_CHECK_FAILURE
23451 /* Note that DECL can be defined in this translation unit, if
23455 mark_definable (tree decl
)
23458 DECL_NOT_REALLY_EXTERN (decl
) = 1;
23459 FOR_EACH_CLONE (clone
, decl
)
23460 DECL_NOT_REALLY_EXTERN (clone
) = 1;
23463 /* Called if RESULT is explicitly instantiated, or is a member of an
23464 explicitly instantiated class. */
23467 mark_decl_instantiated (tree result
, int extern_p
)
23469 SET_DECL_EXPLICIT_INSTANTIATION (result
);
23471 /* If this entity has already been written out, it's too late to
23472 make any modifications. */
23473 if (TREE_ASM_WRITTEN (result
))
23476 /* For anonymous namespace we don't need to do anything. */
23477 if (decl_anon_ns_mem_p (result
))
23479 gcc_assert (!TREE_PUBLIC (result
));
23483 if (TREE_CODE (result
) != FUNCTION_DECL
)
23484 /* The TREE_PUBLIC flag for function declarations will have been
23485 set correctly by tsubst. */
23486 TREE_PUBLIC (result
) = 1;
23488 /* This might have been set by an earlier implicit instantiation. */
23489 DECL_COMDAT (result
) = 0;
23492 DECL_NOT_REALLY_EXTERN (result
) = 0;
23495 mark_definable (result
);
23496 mark_needed (result
);
23497 /* Always make artificials weak. */
23498 if (DECL_ARTIFICIAL (result
) && flag_weak
)
23499 comdat_linkage (result
);
23500 /* For WIN32 we also want to put explicit instantiations in
23501 linkonce sections. */
23502 else if (TREE_PUBLIC (result
))
23503 maybe_make_one_only (result
);
23504 if (TREE_CODE (result
) == FUNCTION_DECL
23505 && DECL_TEMPLATE_INSTANTIATED (result
))
23506 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23507 since start_preparsed_function wouldn't have if we had an earlier
23508 extern explicit instantiation. */
23509 DECL_EXTERNAL (result
) = 0;
23512 /* If EXTERN_P, then this function will not be emitted -- unless
23513 followed by an explicit instantiation, at which point its linkage
23514 will be adjusted. If !EXTERN_P, then this function will be
23515 emitted here. In neither circumstance do we want
23516 import_export_decl to adjust the linkage. */
23517 DECL_INTERFACE_KNOWN (result
) = 1;
23520 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23521 important template arguments. If any are missing, we check whether
23522 they're important by using error_mark_node for substituting into any
23523 args that were used for partial ordering (the ones between ARGS and END)
23524 and seeing if it bubbles up. */
23527 check_undeduced_parms (tree targs
, tree args
, tree end
)
23529 bool found
= false;
23531 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
23532 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
23535 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
23539 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
23540 if (substed
== error_mark_node
)
23546 /* Given two function templates PAT1 and PAT2, return:
23548 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23549 -1 if PAT2 is more specialized than PAT1.
23550 0 if neither is more specialized.
23552 LEN indicates the number of parameters we should consider
23553 (defaulted parameters should not be considered).
23555 The 1998 std underspecified function template partial ordering, and
23556 DR214 addresses the issue. We take pairs of arguments, one from
23557 each of the templates, and deduce them against each other. One of
23558 the templates will be more specialized if all the *other*
23559 template's arguments deduce against its arguments and at least one
23560 of its arguments *does* *not* deduce against the other template's
23561 corresponding argument. Deduction is done as for class templates.
23562 The arguments used in deduction have reference and top level cv
23563 qualifiers removed. Iff both arguments were originally reference
23564 types *and* deduction succeeds in both directions, an lvalue reference
23565 wins against an rvalue reference and otherwise the template
23566 with the more cv-qualified argument wins for that pairing (if
23567 neither is more cv-qualified, they both are equal). Unlike regular
23568 deduction, after all the arguments have been deduced in this way,
23569 we do *not* verify the deduced template argument values can be
23570 substituted into non-deduced contexts.
23572 The logic can be a bit confusing here, because we look at deduce1 and
23573 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23574 can find template arguments for pat1 to make arg1 look like arg2, that
23575 means that arg2 is at least as specialized as arg1. */
23578 more_specialized_fn (tree pat1
, tree pat2
, int len
)
23580 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
23581 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
23582 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
23583 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
23584 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
23585 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
23586 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
23587 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
23588 tree origs1
, origs2
;
23589 bool lose1
= false;
23590 bool lose2
= false;
23592 /* Remove the this parameter from non-static member functions. If
23593 one is a non-static member function and the other is not a static
23594 member function, remove the first parameter from that function
23595 also. This situation occurs for operator functions where we
23596 locate both a member function (with this pointer) and non-member
23597 operator (with explicit first operand). */
23598 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
23600 len
--; /* LEN is the number of significant arguments for DECL1 */
23601 args1
= TREE_CHAIN (args1
);
23602 if (!DECL_STATIC_FUNCTION_P (decl2
))
23603 args2
= TREE_CHAIN (args2
);
23605 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
23607 args2
= TREE_CHAIN (args2
);
23608 if (!DECL_STATIC_FUNCTION_P (decl1
))
23611 args1
= TREE_CHAIN (args1
);
23615 /* If only one is a conversion operator, they are unordered. */
23616 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
23619 /* Consider the return type for a conversion function */
23620 if (DECL_CONV_FN_P (decl1
))
23622 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
23623 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
23627 processing_template_decl
++;
23633 /* Stop when an ellipsis is seen. */
23634 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
23636 tree arg1
= TREE_VALUE (args1
);
23637 tree arg2
= TREE_VALUE (args2
);
23638 int deduce1
, deduce2
;
23644 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23645 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23647 /* When both arguments are pack expansions, we need only
23648 unify the patterns themselves. */
23649 arg1
= PACK_EXPANSION_PATTERN (arg1
);
23650 arg2
= PACK_EXPANSION_PATTERN (arg2
);
23652 /* This is the last comparison we need to do. */
23656 /* DR 1847: If a particular P contains no template-parameters that
23657 participate in template argument deduction, that P is not used to
23658 determine the ordering. */
23659 if (!uses_deducible_template_parms (arg1
)
23660 && !uses_deducible_template_parms (arg2
))
23663 if (TYPE_REF_P (arg1
))
23665 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23666 arg1
= TREE_TYPE (arg1
);
23667 quals1
= cp_type_quals (arg1
);
23670 if (TYPE_REF_P (arg2
))
23672 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23673 arg2
= TREE_TYPE (arg2
);
23674 quals2
= cp_type_quals (arg2
);
23677 arg1
= TYPE_MAIN_VARIANT (arg1
);
23678 arg2
= TYPE_MAIN_VARIANT (arg2
);
23680 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23682 int i
, len2
= remaining_arguments (args2
);
23683 tree parmvec
= make_tree_vec (1);
23684 tree argvec
= make_tree_vec (len2
);
23687 /* Setup the parameter vector, which contains only ARG1. */
23688 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23690 /* Setup the argument vector, which contains the remaining
23692 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23693 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23695 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23696 argvec
, DEDUCE_EXACT
,
23697 /*subr=*/true, /*explain_p=*/false)
23700 /* We cannot deduce in the other direction, because ARG1 is
23701 a pack expansion but ARG2 is not. */
23704 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23706 int i
, len1
= remaining_arguments (args1
);
23707 tree parmvec
= make_tree_vec (1);
23708 tree argvec
= make_tree_vec (len1
);
23711 /* Setup the parameter vector, which contains only ARG1. */
23712 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23714 /* Setup the argument vector, which contains the remaining
23716 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23717 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23719 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23720 argvec
, DEDUCE_EXACT
,
23721 /*subr=*/true, /*explain_p=*/false)
23724 /* We cannot deduce in the other direction, because ARG2 is
23725 a pack expansion but ARG1 is not.*/
23731 /* The normal case, where neither argument is a pack
23733 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23734 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23736 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23737 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23741 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23742 arg2, then arg2 is not as specialized as arg1. */
23748 /* "If, for a given type, deduction succeeds in both directions
23749 (i.e., the types are identical after the transformations above)
23750 and both P and A were reference types (before being replaced with
23751 the type referred to above):
23752 - if the type from the argument template was an lvalue reference and
23753 the type from the parameter template was not, the argument type is
23754 considered to be more specialized than the other; otherwise,
23755 - if the type from the argument template is more cv-qualified
23756 than the type from the parameter template (as described above),
23757 the argument type is considered to be more specialized than the other;
23759 - neither type is more specialized than the other." */
23761 if (deduce1
&& deduce2
)
23763 if (ref1
&& ref2
&& ref1
!= ref2
)
23770 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23772 if ((quals1
& quals2
) == quals2
)
23774 if ((quals1
& quals2
) == quals1
)
23779 if (lose1
&& lose2
)
23780 /* We've failed to deduce something in either direction.
23781 These must be unordered. */
23786 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23787 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23788 /* We have already processed all of the arguments in our
23789 handing of the pack expansion type. */
23792 args1
= TREE_CHAIN (args1
);
23793 args2
= TREE_CHAIN (args2
);
23796 /* "In most cases, all template parameters must have values in order for
23797 deduction to succeed, but for partial ordering purposes a template
23798 parameter may remain without a value provided it is not used in the
23799 types being used for partial ordering."
23801 Thus, if we are missing any of the targs1 we need to substitute into
23802 origs1, then pat2 is not as specialized as pat1. This can happen when
23803 there is a nondeduced context. */
23804 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23806 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23809 processing_template_decl
--;
23811 /* If both deductions succeed, the partial ordering selects the more
23812 constrained template. */
23813 if (!lose1
&& !lose2
)
23815 int winner
= more_constrained (decl1
, decl2
);
23818 else if (winner
< 0)
23822 /* All things being equal, if the next argument is a pack expansion
23823 for one function but not for the other, prefer the
23824 non-variadic function. FIXME this is bogus; see c++/41958. */
23826 && args1
&& TREE_VALUE (args1
)
23827 && args2
&& TREE_VALUE (args2
))
23829 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23830 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23833 if (lose1
== lose2
)
23841 /* Determine which of two partial specializations of TMPL is more
23844 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23845 to the first partial specialization. The TREE_PURPOSE is the
23846 innermost set of template parameters for the partial
23847 specialization. PAT2 is similar, but for the second template.
23849 Return 1 if the first partial specialization is more specialized;
23850 -1 if the second is more specialized; 0 if neither is more
23853 See [temp.class.order] for information about determining which of
23854 two templates is more specialized. */
23857 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
23861 bool any_deductions
= false;
23863 tree tmpl1
= TREE_VALUE (pat1
);
23864 tree tmpl2
= TREE_VALUE (pat2
);
23865 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
23866 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23868 /* Just like what happens for functions, if we are ordering between
23869 different template specializations, we may encounter dependent
23870 types in the arguments, and we need our dependency check functions
23871 to behave correctly. */
23872 ++processing_template_decl
;
23873 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23877 any_deductions
= true;
23880 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23884 any_deductions
= true;
23886 --processing_template_decl
;
23888 /* If both deductions succeed, the partial ordering selects the more
23889 constrained template. */
23890 if (!winner
&& any_deductions
)
23891 winner
= more_constrained (tmpl1
, tmpl2
);
23893 /* In the case of a tie where at least one of the templates
23894 has a parameter pack at the end, the template with the most
23895 non-packed parameters wins. */
23898 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23899 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23901 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23902 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23903 int len1
= TREE_VEC_LENGTH (args1
);
23904 int len2
= TREE_VEC_LENGTH (args2
);
23906 /* We don't count the pack expansion at the end. */
23907 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23909 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23914 else if (len1
< len2
)
23921 /* Return the template arguments that will produce the function signature
23922 DECL from the function template FN, with the explicit template
23923 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23924 also match. Return NULL_TREE if no satisfactory arguments could be
23928 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23930 int ntparms
= DECL_NTPARMS (fn
);
23931 tree targs
= make_tree_vec (ntparms
);
23932 tree decl_type
= TREE_TYPE (decl
);
23933 tree decl_arg_types
;
23935 unsigned int nargs
, ix
;
23938 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23940 /* Never do unification on the 'this' parameter. */
23941 decl_arg_types
= skip_artificial_parms_for (decl
,
23942 TYPE_ARG_TYPES (decl_type
));
23944 nargs
= list_length (decl_arg_types
);
23945 args
= XALLOCAVEC (tree
, nargs
);
23946 for (arg
= decl_arg_types
, ix
= 0;
23947 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23948 arg
= TREE_CHAIN (arg
), ++ix
)
23949 args
[ix
] = TREE_VALUE (arg
);
23951 if (fn_type_unification (fn
, explicit_args
, targs
,
23953 (check_rettype
|| DECL_CONV_FN_P (fn
)
23954 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23955 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23956 /*explain_p=*/false,
23958 == error_mark_node
)
23964 /* Return the innermost template arguments that, when applied to a partial
23965 specialization SPEC_TMPL of TMPL, yield the ARGS.
23967 For example, suppose we have:
23969 template <class T, class U> struct S {};
23970 template <class T> struct S<T*, int> {};
23972 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23973 partial specialization and the ARGS will be {double*, int}. The resulting
23974 vector will be {double}, indicating that `T' is bound to `double'. */
23977 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23979 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23981 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23982 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23984 tree innermost_deduced_args
;
23986 innermost_deduced_args
= make_tree_vec (ntparms
);
23987 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23989 deduced_args
= copy_node (args
);
23990 SET_TMPL_ARGS_LEVEL (deduced_args
,
23991 TMPL_ARGS_DEPTH (deduced_args
),
23992 innermost_deduced_args
);
23995 deduced_args
= innermost_deduced_args
;
23997 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23999 if (unify (tparms
, deduced_args
,
24000 INNERMOST_TEMPLATE_ARGS (spec_args
),
24001 INNERMOST_TEMPLATE_ARGS (args
),
24002 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
24005 for (i
= 0; i
< ntparms
; ++i
)
24006 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
24008 if (!tried_array_deduction
)
24010 try_array_deduction (tparms
, innermost_deduced_args
,
24011 INNERMOST_TEMPLATE_ARGS (spec_args
));
24012 tried_array_deduction
= true;
24013 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
24019 if (!push_tinst_level (spec_tmpl
, deduced_args
))
24021 excessive_deduction_depth
= true;
24025 /* Verify that nondeduced template arguments agree with the type
24026 obtained from argument deduction.
24030 struct A { typedef int X; };
24031 template <class T, class U> struct C {};
24032 template <class T> struct C<T, typename T::X> {};
24034 Then with the instantiation `C<A, int>', we can deduce that
24035 `T' is `A' but unify () does not check whether `typename T::X'
24037 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
24039 if (spec_args
!= error_mark_node
)
24040 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
24041 INNERMOST_TEMPLATE_ARGS (spec_args
),
24042 tmpl
, tf_none
, false, false);
24044 pop_tinst_level ();
24046 if (spec_args
== error_mark_node
24047 /* We only need to check the innermost arguments; the other
24048 arguments will always agree. */
24049 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
24050 INNERMOST_TEMPLATE_ARGS (args
)))
24053 /* Now that we have bindings for all of the template arguments,
24054 ensure that the arguments deduced for the template template
24055 parameters have compatible template parameter lists. See the use
24056 of template_template_parm_bindings_ok_p in fn_type_unification
24057 for more information. */
24058 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
24061 return deduced_args
;
24064 // Compare two function templates T1 and T2 by deducing bindings
24065 // from one against the other. If both deductions succeed, compare
24066 // constraints to see which is more constrained.
24068 more_specialized_inst (tree t1
, tree t2
)
24073 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
24079 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
24085 // If both deductions succeed, then one may be more constrained.
24086 if (count
== 2 && fate
== 0)
24087 fate
= more_constrained (t1
, t2
);
24092 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24093 Return the TREE_LIST node with the most specialized template, if
24094 any. If there is no most specialized template, the error_mark_node
24097 Note that this function does not look at, or modify, the
24098 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24099 returned is one of the elements of INSTANTIATIONS, callers may
24100 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24101 and retrieve it from the value returned. */
24104 most_specialized_instantiation (tree templates
)
24108 ++processing_template_decl
;
24111 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
24113 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
24114 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
24119 /* Equally specialized, move to next function. If there
24120 is no next function, nothing's most specialized. */
24121 fn
= TREE_CHAIN (fn
);
24129 /* Now verify that champ is better than everything earlier in the
24130 instantiation list. */
24131 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
24132 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
24139 processing_template_decl
--;
24142 return error_mark_node
;
24147 /* If DECL is a specialization of some template, return the most
24148 general such template. Otherwise, returns NULL_TREE.
24150 For example, given:
24152 template <class T> struct S { template <class U> void f(U); };
24154 if TMPL is `template <class U> void S<int>::f(U)' this will return
24155 the full template. This function will not trace past partial
24156 specializations, however. For example, given in addition:
24158 template <class T> struct S<T*> { template <class U> void f(U); };
24160 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24161 `template <class T> template <class U> S<T*>::f(U)'. */
24164 most_general_template (tree decl
)
24166 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24168 if (tree tinfo
= get_template_info (decl
))
24169 decl
= TI_TEMPLATE (tinfo
);
24170 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24171 template friend, or a FIELD_DECL for a capture pack. */
24172 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
24176 /* Look for more and more general templates. */
24177 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
24179 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24180 (See cp-tree.h for details.) */
24181 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
24184 if (CLASS_TYPE_P (TREE_TYPE (decl
))
24185 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
24186 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
24189 /* Stop if we run into an explicitly specialized class template. */
24190 if (!DECL_NAMESPACE_SCOPE_P (decl
)
24191 && DECL_CONTEXT (decl
)
24192 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
24195 decl
= DECL_TI_TEMPLATE (decl
);
24201 /* Return the most specialized of the template partial specializations
24202 which can produce TARGET, a specialization of some class or variable
24203 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24204 a TEMPLATE_DECL node corresponding to the partial specialization, while
24205 the TREE_PURPOSE is the set of template arguments that must be
24206 substituted into the template pattern in order to generate TARGET.
24208 If the choice of partial specialization is ambiguous, a diagnostic
24209 is issued, and the error_mark_node is returned. If there are no
24210 partial specializations matching TARGET, then NULL_TREE is
24211 returned, indicating that the primary template should be used. */
24214 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
24216 tree list
= NULL_TREE
;
24221 tree outer_args
= NULL_TREE
;
24224 if (TYPE_P (target
))
24226 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
24227 tmpl
= TI_TEMPLATE (tinfo
);
24228 args
= TI_ARGS (tinfo
);
24230 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
24232 tmpl
= TREE_OPERAND (target
, 0);
24233 args
= TREE_OPERAND (target
, 1);
24235 else if (VAR_P (target
))
24237 tree tinfo
= DECL_TEMPLATE_INFO (target
);
24238 tmpl
= TI_TEMPLATE (tinfo
);
24239 args
= TI_ARGS (tinfo
);
24242 gcc_unreachable ();
24244 tree main_tmpl
= most_general_template (tmpl
);
24246 /* For determining which partial specialization to use, only the
24247 innermost args are interesting. */
24248 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
24250 outer_args
= strip_innermost_template_args (args
, 1);
24251 args
= INNERMOST_TEMPLATE_ARGS (args
);
24254 /* The caller hasn't called push_to_top_level yet, but we need
24255 get_partial_spec_bindings to be done in non-template context so that we'll
24256 fully resolve everything. */
24257 processing_template_decl_sentinel ptds
;
24259 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
24262 tree spec_tmpl
= TREE_VALUE (t
);
24266 /* Substitute in the template args from the enclosing class. */
24267 ++processing_template_decl
;
24268 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
24269 --processing_template_decl
;
24272 if (spec_tmpl
== error_mark_node
)
24273 return error_mark_node
;
24275 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
24279 spec_args
= add_to_template_args (outer_args
, spec_args
);
24281 /* Keep the candidate only if the constraints are satisfied,
24282 or if we're not compiling with concepts. */
24284 || constraints_satisfied_p (spec_tmpl
, spec_args
))
24286 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
24287 TREE_TYPE (list
) = TREE_TYPE (t
);
24295 ambiguous_p
= false;
24298 t
= TREE_CHAIN (t
);
24299 for (; t
; t
= TREE_CHAIN (t
))
24301 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24308 t
= TREE_CHAIN (t
);
24311 ambiguous_p
= true;
24320 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
24322 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
24325 ambiguous_p
= true;
24333 char *spaces
= NULL
;
24334 if (!(complain
& tf_error
))
24335 return error_mark_node
;
24336 if (TYPE_P (target
))
24337 error ("ambiguous template instantiation for %q#T", target
);
24339 error ("ambiguous template instantiation for %q#D", target
);
24340 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
24341 for (t
= list
; t
; t
= TREE_CHAIN (t
))
24343 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
24344 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
24345 "%s %#qS", spaces
? spaces
: str
, subst
);
24346 spaces
= spaces
? spaces
: get_spaces (str
);
24349 return error_mark_node
;
24355 /* Explicitly instantiate DECL. */
24358 do_decl_instantiation (tree decl
, tree storage
)
24360 tree result
= NULL_TREE
;
24363 if (!decl
|| decl
== error_mark_node
)
24364 /* An error occurred, for which grokdeclarator has already issued
24365 an appropriate message. */
24367 else if (! DECL_LANG_SPECIFIC (decl
))
24369 error ("explicit instantiation of non-template %q#D", decl
);
24372 else if (DECL_DECLARED_CONCEPT_P (decl
))
24375 error ("explicit instantiation of variable concept %q#D", decl
);
24377 error ("explicit instantiation of function concept %q#D", decl
);
24381 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
24382 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
24384 if (VAR_P (decl
) && !var_templ
)
24386 /* There is an asymmetry here in the way VAR_DECLs and
24387 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24388 the latter, the DECL we get back will be marked as a
24389 template instantiation, and the appropriate
24390 DECL_TEMPLATE_INFO will be set up. This does not happen for
24391 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24392 should handle VAR_DECLs as it currently handles
24394 if (!DECL_CLASS_SCOPE_P (decl
))
24396 error ("%qD is not a static data member of a class template", decl
);
24399 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
24400 if (!result
|| !VAR_P (result
))
24402 error ("no matching template for %qD found", decl
);
24405 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
24407 error ("type %qT for explicit instantiation %qD does not match "
24408 "declared type %qT", TREE_TYPE (result
), decl
,
24413 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
24415 error ("explicit instantiation of %q#D", decl
);
24421 /* Check for various error cases. Note that if the explicit
24422 instantiation is valid the RESULT will currently be marked as an
24423 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24424 until we get here. */
24426 if (DECL_TEMPLATE_SPECIALIZATION (result
))
24428 /* DR 259 [temp.spec].
24430 Both an explicit instantiation and a declaration of an explicit
24431 specialization shall not appear in a program unless the explicit
24432 instantiation follows a declaration of the explicit specialization.
24434 For a given set of template parameters, if an explicit
24435 instantiation of a template appears after a declaration of an
24436 explicit specialization for that template, the explicit
24437 instantiation has no effect. */
24440 else if (DECL_EXPLICIT_INSTANTIATION (result
))
24444 No program shall explicitly instantiate any template more
24447 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24448 the first instantiation was `extern' and the second is not,
24449 and EXTERN_P for the opposite case. */
24450 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
24451 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
24452 /* If an "extern" explicit instantiation follows an ordinary
24453 explicit instantiation, the template is instantiated. */
24457 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
24459 error ("no matching template for %qD found", result
);
24462 else if (!DECL_TEMPLATE_INFO (result
))
24464 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
24468 if (storage
== NULL_TREE
)
24470 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24472 if (cxx_dialect
== cxx98
)
24473 pedwarn (input_location
, OPT_Wpedantic
,
24474 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24479 error ("storage class %qD applied to template instantiation", storage
);
24481 check_explicit_instantiation_namespace (result
);
24482 mark_decl_instantiated (result
, extern_p
);
24484 instantiate_decl (result
, /*defer_ok=*/true,
24485 /*expl_inst_class_mem_p=*/false);
24489 mark_class_instantiated (tree t
, int extern_p
)
24491 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
24492 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
24493 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
24494 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
24497 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
24498 rest_of_type_compilation (t
, 1);
24502 /* Called from do_type_instantiation through binding_table_foreach to
24503 do recursive instantiation for the type bound in ENTRY. */
24505 bt_instantiate_type_proc (binding_entry entry
, void *data
)
24507 tree storage
= *(tree
*) data
;
24509 if (MAYBE_CLASS_TYPE_P (entry
->type
)
24510 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
24511 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
24512 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
24515 /* Perform an explicit instantiation of template class T. STORAGE, if
24516 non-null, is the RID for extern, inline or static. COMPLAIN is
24517 nonzero if this is called from the parser, zero if called recursively,
24518 since the standard is unclear (as detailed below). */
24521 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
24526 int previous_instantiation_extern_p
= 0;
24528 if (TREE_CODE (t
) == TYPE_DECL
)
24531 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
24534 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
24536 error ("explicit instantiation of non-class template %qD", tmpl
);
24538 error ("explicit instantiation of non-template type %qT", t
);
24544 if (!COMPLETE_TYPE_P (t
))
24546 if (complain
& tf_error
)
24547 error ("explicit instantiation of %q#T before definition of template",
24552 if (storage
!= NULL_TREE
)
24554 if (storage
== ridpointers
[(int) RID_EXTERN
])
24556 if (cxx_dialect
== cxx98
)
24557 pedwarn (input_location
, OPT_Wpedantic
,
24558 "ISO C++ 1998 forbids the use of %<extern%> on "
24559 "explicit instantiations");
24562 pedwarn (input_location
, OPT_Wpedantic
,
24563 "ISO C++ forbids the use of %qE"
24564 " on explicit instantiations", storage
);
24566 if (storage
== ridpointers
[(int) RID_INLINE
])
24568 else if (storage
== ridpointers
[(int) RID_EXTERN
])
24570 else if (storage
== ridpointers
[(int) RID_STATIC
])
24574 error ("storage class %qD applied to template instantiation",
24580 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
24582 /* DR 259 [temp.spec].
24584 Both an explicit instantiation and a declaration of an explicit
24585 specialization shall not appear in a program unless the explicit
24586 instantiation follows a declaration of the explicit specialization.
24588 For a given set of template parameters, if an explicit
24589 instantiation of a template appears after a declaration of an
24590 explicit specialization for that template, the explicit
24591 instantiation has no effect. */
24594 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
24598 No program shall explicitly instantiate any template more
24601 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24602 instantiation was `extern'. If EXTERN_P then the second is.
24603 These cases are OK. */
24604 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
24606 if (!previous_instantiation_extern_p
&& !extern_p
24607 && (complain
& tf_error
))
24608 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
24610 /* If we've already instantiated the template, just return now. */
24611 if (!CLASSTYPE_INTERFACE_ONLY (t
))
24615 check_explicit_instantiation_namespace (TYPE_NAME (t
));
24616 mark_class_instantiated (t
, extern_p
);
24621 /* In contrast to implicit instantiation, where only the
24622 declarations, and not the definitions, of members are
24623 instantiated, we have here:
24627 The explicit instantiation of a class template specialization
24628 implies the instantiation of all of its members not
24629 previously explicitly specialized in the translation unit
24630 containing the explicit instantiation.
24632 Of course, we can't instantiate member template classes, since we
24633 don't have any arguments for them. Note that the standard is
24634 unclear on whether the instantiation of the members are
24635 *explicit* instantiations or not. However, the most natural
24636 interpretation is that it should be an explicit
24638 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
24640 || (TREE_CODE (fld
) == FUNCTION_DECL
24642 && user_provided_p (fld
)))
24643 && DECL_TEMPLATE_INSTANTIATION (fld
))
24645 mark_decl_instantiated (fld
, extern_p
);
24647 instantiate_decl (fld
, /*defer_ok=*/true,
24648 /*expl_inst_class_mem_p=*/true);
24651 if (CLASSTYPE_NESTED_UTDS (t
))
24652 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24653 bt_instantiate_type_proc
, &storage
);
24656 /* Given a function DECL, which is a specialization of TMPL, modify
24657 DECL to be a re-instantiation of TMPL with the same template
24658 arguments. TMPL should be the template into which tsubst'ing
24659 should occur for DECL, not the most general template.
24661 One reason for doing this is a scenario like this:
24664 void f(const T&, int i);
24666 void g() { f(3, 7); }
24669 void f(const T& t, const int i) { }
24671 Note that when the template is first instantiated, with
24672 instantiate_template, the resulting DECL will have no name for the
24673 first parameter, and the wrong type for the second. So, when we go
24674 to instantiate the DECL, we regenerate it. */
24677 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24679 /* The arguments used to instantiate DECL, from the most general
24683 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24685 /* Make sure that we can see identifiers, and compute access
24687 push_access_scope (decl
);
24689 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24697 args_depth
= TMPL_ARGS_DEPTH (args
);
24698 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24699 if (args_depth
> parms_depth
)
24700 args
= get_innermost_template_args (args
, parms_depth
);
24702 /* Instantiate a dynamic exception-specification. noexcept will be
24704 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24705 if (TREE_VALUE (raises
))
24707 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24708 args
, tf_error
, NULL_TREE
,
24709 /*defer_ok*/false);
24710 if (specs
&& specs
!= error_mark_node
)
24711 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24715 /* Merge parameter declarations. */
24716 decl_parm
= skip_artificial_parms_for (decl
,
24717 DECL_ARGUMENTS (decl
));
24719 = skip_artificial_parms_for (code_pattern
,
24720 DECL_ARGUMENTS (code_pattern
));
24721 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24726 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24727 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24728 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24730 parm_type
= type_decays_to (parm_type
);
24731 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24732 TREE_TYPE (decl_parm
) = parm_type
;
24733 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24734 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24736 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24737 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24739 decl_parm
= DECL_CHAIN (decl_parm
);
24740 pattern_parm
= DECL_CHAIN (pattern_parm
);
24742 /* Merge any parameters that match with the function parameter
24744 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24747 tree expanded_types
;
24748 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24749 the parameters in this function parameter pack. */
24750 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24751 args
, tf_error
, NULL_TREE
);
24752 len
= TREE_VEC_LENGTH (expanded_types
);
24753 for (i
= 0; i
< len
; i
++)
24758 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24759 /* Rename the parameter to include the index. */
24760 DECL_NAME (decl_parm
) =
24761 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24762 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24763 parm_type
= type_decays_to (parm_type
);
24764 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24765 TREE_TYPE (decl_parm
) = parm_type
;
24766 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24767 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24769 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24770 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24772 decl_parm
= DECL_CHAIN (decl_parm
);
24775 /* Merge additional specifiers from the CODE_PATTERN. */
24776 if (DECL_DECLARED_INLINE_P (code_pattern
)
24777 && !DECL_DECLARED_INLINE_P (decl
))
24778 DECL_DECLARED_INLINE_P (decl
) = 1;
24780 maybe_instantiate_noexcept (decl
, tf_error
);
24782 else if (VAR_P (decl
))
24784 start_lambda_scope (decl
);
24785 DECL_INITIAL (decl
) =
24786 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
24787 tf_error
, DECL_TI_TEMPLATE (decl
));
24788 finish_lambda_scope ();
24789 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24790 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24791 tf_error
, DECL_TI_TEMPLATE (decl
));
24794 gcc_unreachable ();
24796 pop_access_scope (decl
);
24799 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24800 substituted to get DECL. */
24803 template_for_substitution (tree decl
)
24805 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24807 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24808 for the instantiation. This is not always the most general
24809 template. Consider, for example:
24812 struct S { template <class U> void f();
24813 template <> void f<int>(); };
24815 and an instantiation of S<double>::f<int>. We want TD to be the
24816 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24817 while (/* An instantiation cannot have a definition, so we need a
24818 more general template. */
24819 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24820 /* We must also deal with friend templates. Given:
24822 template <class T> struct S {
24823 template <class U> friend void f() {};
24826 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24827 so far as the language is concerned, but that's still
24828 where we get the pattern for the instantiation from. On
24829 other hand, if the definition comes outside the class, say:
24831 template <class T> struct S {
24832 template <class U> friend void f();
24834 template <class U> friend void f() {}
24836 we don't need to look any further. That's what the check for
24837 DECL_INITIAL is for. */
24838 || (TREE_CODE (decl
) == FUNCTION_DECL
24839 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24840 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24842 /* The present template, TD, should not be a definition. If it
24843 were a definition, we should be using it! Note that we
24844 cannot restructure the loop to just keep going until we find
24845 a template with a definition, since that might go too far if
24846 a specialization was declared, but not defined. */
24848 /* Fetch the more general template. */
24849 tmpl
= DECL_TI_TEMPLATE (tmpl
);
24855 /* Returns true if we need to instantiate this template instance even if we
24856 know we aren't going to emit it. */
24859 always_instantiate_p (tree decl
)
24861 /* We always instantiate inline functions so that we can inline them. An
24862 explicit instantiation declaration prohibits implicit instantiation of
24863 non-inline functions. With high levels of optimization, we would
24864 normally inline non-inline functions -- but we're not allowed to do
24865 that for "extern template" functions. Therefore, we check
24866 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24867 return ((TREE_CODE (decl
) == FUNCTION_DECL
24868 && (DECL_DECLARED_INLINE_P (decl
)
24869 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
24870 /* And we need to instantiate static data members so that
24871 their initializers are available in integral constant
24874 && decl_maybe_constant_var_p (decl
)));
24877 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24878 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24879 error, true otherwise. */
24882 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24884 tree fntype
, spec
, noex
, clone
;
24886 /* Don't instantiate a noexcept-specification from template context. */
24887 if (processing_template_decl
24888 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24891 if (DECL_CLONED_FUNCTION_P (fn
))
24892 fn
= DECL_CLONED_FUNCTION (fn
);
24894 tree orig_fn
= NULL_TREE
;
24895 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24896 its FUNCTION_DECL for the rest of this function -- push_access_scope
24897 doesn't accept TEMPLATE_DECLs. */
24898 if (DECL_FUNCTION_TEMPLATE_P (fn
))
24901 fn
= DECL_TEMPLATE_RESULT (fn
);
24904 fntype
= TREE_TYPE (fn
);
24905 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24907 if (!spec
|| !TREE_PURPOSE (spec
))
24910 noex
= TREE_PURPOSE (spec
);
24912 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24914 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24915 bool added
= false;
24916 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24918 spec
= get_defaulted_eh_spec (fn
, complain
);
24919 if (spec
== error_mark_node
)
24920 /* This might have failed because of an unparsed DMI, so
24921 let's try again later. */
24924 else if (!(added
= !fns
->add (fn
)))
24926 /* If hash_set::add returns true, the element was already there. */
24927 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24928 DECL_SOURCE_LOCATION (fn
));
24930 "exception specification of %qD depends on itself",
24932 spec
= noexcept_false_spec
;
24934 else if (push_tinst_level (fn
))
24936 push_to_top_level ();
24937 push_access_scope (fn
);
24938 push_deferring_access_checks (dk_no_deferred
);
24939 input_location
= DECL_SOURCE_LOCATION (fn
);
24941 /* If needed, set current_class_ptr for the benefit of
24942 tsubst_copy/PARM_DECL. */
24943 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
24944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
24946 tree this_parm
= DECL_ARGUMENTS (tdecl
);
24947 current_class_ptr
= NULL_TREE
;
24948 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
24949 current_class_ptr
= this_parm
;
24952 /* If this function is represented by a TEMPLATE_DECL, then
24953 the deferred noexcept-specification might still contain
24954 dependent types, even after substitution. And we need the
24955 dependency check functions to work in build_noexcept_spec. */
24957 ++processing_template_decl
;
24959 /* Do deferred instantiation of the noexcept-specifier. */
24960 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24961 DEFERRED_NOEXCEPT_ARGS (noex
),
24962 tf_warning_or_error
, fn
,
24963 /*function_p=*/false,
24966 /* Build up the noexcept-specification. */
24967 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24970 --processing_template_decl
;
24972 pop_deferring_access_checks ();
24973 pop_access_scope (fn
);
24974 pop_tinst_level ();
24975 pop_from_top_level ();
24978 spec
= noexcept_false_spec
;
24983 if (spec
== error_mark_node
)
24985 /* This failed with a hard error, so let's go with false. */
24986 gcc_assert (seen_error ());
24987 spec
= noexcept_false_spec
;
24990 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24992 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
24994 FOR_EACH_CLONE (clone
, fn
)
24996 if (TREE_TYPE (clone
) == fntype
)
24997 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24999 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
25006 /* We're starting to process the function INST, an instantiation of PATTERN;
25007 add their parameters to local_specializations. */
25010 register_parameter_specializations (tree pattern
, tree inst
)
25012 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
25013 tree spec_parm
= DECL_ARGUMENTS (inst
);
25014 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
25016 register_local_specialization (spec_parm
, tmpl_parm
);
25017 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
25018 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
25020 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
25022 if (!DECL_PACK_P (tmpl_parm
))
25024 register_local_specialization (spec_parm
, tmpl_parm
);
25025 spec_parm
= DECL_CHAIN (spec_parm
);
25029 /* Register the (value) argument pack as a specialization of
25030 TMPL_PARM, then move on. */
25031 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
25032 register_local_specialization (argpack
, tmpl_parm
);
25035 gcc_assert (!spec_parm
);
25038 /* Produce the definition of D, a _DECL generated from a template. If
25039 DEFER_OK is true, then we don't have to actually do the
25040 instantiation now; we just have to do it sometime. Normally it is
25041 an error if this is an explicit instantiation but D is undefined.
25042 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25043 instantiated class template. */
25046 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
25048 tree tmpl
= DECL_TI_TEMPLATE (d
);
25055 bool pattern_defined
;
25056 location_t saved_loc
= input_location
;
25057 int saved_unevaluated_operand
= cp_unevaluated_operand
;
25058 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25062 /* This function should only be used to instantiate templates for
25063 functions and static member variables. */
25064 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
25066 /* A concept is never instantiated. */
25067 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
25069 /* Variables are never deferred; if instantiation is required, they
25070 are instantiated right away. That allows for better code in the
25071 case that an expression refers to the value of the variable --
25072 if the variable has a constant value the referring expression can
25073 take advantage of that fact. */
25077 /* Don't instantiate cloned functions. Instead, instantiate the
25078 functions they cloned. */
25079 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
25080 d
= DECL_CLONED_FUNCTION (d
);
25082 if (DECL_TEMPLATE_INSTANTIATED (d
)
25083 || (TREE_CODE (d
) == FUNCTION_DECL
25084 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
25085 || DECL_TEMPLATE_SPECIALIZATION (d
))
25086 /* D has already been instantiated or explicitly specialized, so
25087 there's nothing for us to do here.
25089 It might seem reasonable to check whether or not D is an explicit
25090 instantiation, and, if so, stop here. But when an explicit
25091 instantiation is deferred until the end of the compilation,
25092 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25093 the instantiation. */
25096 /* Check to see whether we know that this template will be
25097 instantiated in some other file, as with "extern template"
25099 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
25101 /* In general, we do not instantiate such templates. */
25102 if (external_p
&& !always_instantiate_p (d
))
25105 gen_tmpl
= most_general_template (tmpl
);
25106 gen_args
= DECL_TI_ARGS (d
);
25108 if (tmpl
!= gen_tmpl
)
25109 /* We should already have the extra args. */
25110 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
25111 == TMPL_ARGS_DEPTH (gen_args
));
25112 /* And what's in the hash table should match D. */
25113 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
25114 || spec
== NULL_TREE
);
25116 /* This needs to happen before any tsubsting. */
25117 if (! push_tinst_level (d
))
25120 timevar_push (TV_TEMPLATE_INST
);
25122 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25123 for the instantiation. */
25124 td
= template_for_substitution (d
);
25129 /* Look up an explicit specialization, if any. */
25130 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
25131 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
25132 if (elt
&& elt
!= error_mark_node
)
25134 td
= TREE_VALUE (elt
);
25135 args
= TREE_PURPOSE (elt
);
25139 code_pattern
= DECL_TEMPLATE_RESULT (td
);
25141 /* We should never be trying to instantiate a member of a class
25142 template or partial specialization. */
25143 gcc_assert (d
!= code_pattern
);
25145 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
25146 || DECL_TEMPLATE_SPECIALIZATION (td
))
25147 /* In the case of a friend template whose definition is provided
25148 outside the class, we may have too many arguments. Drop the
25149 ones we don't need. The same is true for specializations. */
25150 args
= get_innermost_template_args
25151 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
25153 if (TREE_CODE (d
) == FUNCTION_DECL
)
25155 deleted_p
= DECL_DELETED_FN (code_pattern
);
25156 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
25157 && DECL_INITIAL (code_pattern
) != error_mark_node
)
25158 || DECL_DEFAULTED_FN (code_pattern
)
25164 if (DECL_CLASS_SCOPE_P (code_pattern
))
25165 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
25167 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
25170 /* We may be in the middle of deferred access check. Disable it now. */
25171 push_deferring_access_checks (dk_no_deferred
);
25173 /* Unless an explicit instantiation directive has already determined
25174 the linkage of D, remember that a definition is available for
25176 if (pattern_defined
25177 && !DECL_INTERFACE_KNOWN (d
)
25178 && !DECL_NOT_REALLY_EXTERN (d
))
25179 mark_definable (d
);
25181 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
25182 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
25183 input_location
= DECL_SOURCE_LOCATION (d
);
25185 /* If D is a member of an explicitly instantiated class template,
25186 and no definition is available, treat it like an implicit
25188 if (!pattern_defined
&& expl_inst_class_mem_p
25189 && DECL_EXPLICIT_INSTANTIATION (d
))
25191 /* Leave linkage flags alone on instantiations with anonymous
25193 if (TREE_PUBLIC (d
))
25195 DECL_NOT_REALLY_EXTERN (d
) = 0;
25196 DECL_INTERFACE_KNOWN (d
) = 0;
25198 SET_DECL_IMPLICIT_INSTANTIATION (d
);
25201 /* Defer all other templates, unless we have been explicitly
25202 forbidden from doing so. */
25203 if (/* If there is no definition, we cannot instantiate the
25206 /* If it's OK to postpone instantiation, do so. */
25208 /* If this is a static data member that will be defined
25209 elsewhere, we don't want to instantiate the entire data
25210 member, but we do want to instantiate the initializer so that
25211 we can substitute that elsewhere. */
25212 || (external_p
&& VAR_P (d
))
25213 /* Handle here a deleted function too, avoid generating
25214 its body (c++/61080). */
25217 /* The definition of the static data member is now required so
25218 we must substitute the initializer. */
25220 && !DECL_INITIAL (d
)
25221 && DECL_INITIAL (code_pattern
))
25225 bool const_init
= false;
25226 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25228 ns
= decl_namespace_context (d
);
25229 push_nested_namespace (ns
);
25231 push_nested_class (DECL_CONTEXT (d
));
25232 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
25234 tf_warning_or_error
, NULL_TREE
,
25235 /*integral_constant_expression_p=*/false);
25236 /* If instantiating the initializer involved instantiating this
25237 again, don't call cp_finish_decl twice. */
25238 if (!DECL_INITIAL (d
))
25240 /* Make sure the initializer is still constant, in case of
25241 circular dependency (template/instantiate6.C). */
25243 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25244 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
25245 /*asmspec_tree=*/NULL_TREE
,
25246 LOOKUP_ONLYCONVERTING
);
25249 pop_nested_class ();
25250 pop_nested_namespace (ns
);
25253 /* We restore the source position here because it's used by
25254 add_pending_template. */
25255 input_location
= saved_loc
;
25257 if (at_eof
&& !pattern_defined
25258 && DECL_EXPLICIT_INSTANTIATION (d
)
25259 && DECL_NOT_REALLY_EXTERN (d
))
25262 The definition of a non-exported function template, a
25263 non-exported member function template, or a non-exported
25264 member function or static data member of a class template
25265 shall be present in every translation unit in which it is
25266 explicitly instantiated. */
25267 permerror (input_location
, "explicit instantiation of %qD "
25268 "but no definition available", d
);
25270 /* If we're in unevaluated context, we just wanted to get the
25271 constant value; this isn't an odr use, so don't queue
25272 a full instantiation. */
25273 if (cp_unevaluated_operand
!= 0)
25275 /* ??? Historically, we have instantiated inline functions, even
25276 when marked as "extern template". */
25277 if (!(external_p
&& VAR_P (d
)))
25278 add_pending_template (d
);
25282 bool push_to_top
, nested
;
25284 fn_context
= decl_function_context (d
);
25285 if (LAMBDA_FUNCTION_P (d
))
25286 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25287 fn_context
= NULL_TREE
;
25288 nested
= current_function_decl
!= NULL_TREE
;
25289 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
25291 vec
<tree
> omp_privatization_save
;
25293 save_omp_privatization_clauses (omp_privatization_save
);
25296 push_to_top_level ();
25299 gcc_assert (!processing_template_decl
);
25300 push_function_context ();
25301 cp_unevaluated_operand
= 0;
25302 c_inhibit_evaluation_warnings
= 0;
25305 /* Mark D as instantiated so that recursive calls to
25306 instantiate_decl do not try to instantiate it again. */
25307 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
25309 /* Regenerate the declaration in case the template has been modified
25310 by a subsequent redeclaration. */
25311 regenerate_decl_from_template (d
, td
, args
);
25313 /* We already set the file and line above. Reset them now in case
25314 they changed as a result of calling regenerate_decl_from_template. */
25315 input_location
= DECL_SOURCE_LOCATION (d
);
25320 bool const_init
= false;
25322 /* Clear out DECL_RTL; whatever was there before may not be right
25323 since we've reset the type of the declaration. */
25324 SET_DECL_RTL (d
, NULL
);
25325 DECL_IN_AGGR_P (d
) = 0;
25327 /* The initializer is placed in DECL_INITIAL by
25328 regenerate_decl_from_template so we don't need to
25329 push/pop_access_scope again here. Pull it out so that
25330 cp_finish_decl can process it. */
25331 init
= DECL_INITIAL (d
);
25332 DECL_INITIAL (d
) = NULL_TREE
;
25333 DECL_INITIALIZED_P (d
) = 0;
25335 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25336 initializer. That function will defer actual emission until
25337 we have a chance to determine linkage. */
25338 DECL_EXTERNAL (d
) = 0;
25340 /* Enter the scope of D so that access-checking works correctly. */
25341 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
25343 push_nested_class (DECL_CONTEXT (d
));
25345 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
25346 int flags
= (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d
))
25347 ? LOOKUP_CONSTINIT
: 0);
25348 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, flags
);
25351 pop_nested_class ();
25353 if (variable_template_p (gen_tmpl
))
25354 note_variable_template_instantiation (d
);
25356 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
25357 synthesize_method (d
);
25358 else if (TREE_CODE (d
) == FUNCTION_DECL
)
25360 /* Set up the list of local specializations. */
25361 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
25362 tree block
= NULL_TREE
;
25364 /* Set up context. */
25365 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25366 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25367 block
= push_stmt_list ();
25369 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
25371 /* Some typedefs referenced from within the template code need to be
25372 access checked at template instantiation time, i.e now. These
25373 types were added to the template at parsing time. Let's get those
25374 and perform the access checks then. */
25375 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
25378 /* Create substitution entries for the parameters. */
25379 register_parameter_specializations (code_pattern
, d
);
25381 /* Substitute into the body of the function. */
25382 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25383 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
25384 tf_warning_or_error
, tmpl
);
25387 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
25388 tf_warning_or_error
, tmpl
,
25389 /*integral_constant_expression_p=*/false);
25391 /* Set the current input_location to the end of the function
25392 so that finish_function knows where we are. */
25394 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
25396 /* Remember if we saw an infinite loop in the template. */
25397 current_function_infinite_loop
25398 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
25401 /* Finish the function. */
25402 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
25403 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
25404 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
25407 d
= finish_function (/*inline_p=*/false);
25408 expand_or_defer_fn (d
);
25411 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
25412 cp_check_omp_declare_reduction (d
);
25415 /* We're not deferring instantiation any more. */
25416 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
25419 pop_from_top_level ();
25421 pop_function_context ();
25424 restore_omp_privatization_clauses (omp_privatization_save
);
25427 pop_deferring_access_checks ();
25428 timevar_pop (TV_TEMPLATE_INST
);
25429 pop_tinst_level ();
25430 input_location
= saved_loc
;
25431 cp_unevaluated_operand
= saved_unevaluated_operand
;
25432 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25437 /* Run through the list of templates that we wish we could
25438 instantiate, and instantiate any we can. RETRIES is the
25439 number of times we retry pending template instantiation. */
25442 instantiate_pending_templates (int retries
)
25445 location_t saved_loc
= input_location
;
25447 /* Instantiating templates may trigger vtable generation. This in turn
25448 may require further template instantiations. We place a limit here
25449 to avoid infinite loop. */
25450 if (pending_templates
&& retries
>= max_tinst_depth
)
25452 tree decl
= pending_templates
->tinst
->maybe_get_node ();
25454 fatal_error (input_location
,
25455 "template instantiation depth exceeds maximum of %d"
25456 " instantiating %q+D, possibly from virtual table generation"
25457 " (use %<-ftemplate-depth=%> to increase the maximum)",
25458 max_tinst_depth
, decl
);
25459 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25460 /* Pretend that we defined it. */
25461 DECL_INITIAL (decl
) = error_mark_node
;
25467 struct pending_template
**t
= &pending_templates
;
25468 struct pending_template
*last
= NULL
;
25472 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
25473 bool complete
= false;
25475 if (TYPE_P (instantiation
))
25477 if (!COMPLETE_TYPE_P (instantiation
))
25479 instantiate_class_template (instantiation
);
25480 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
25481 for (tree fld
= TYPE_FIELDS (instantiation
);
25482 fld
; fld
= TREE_CHAIN (fld
))
25484 || (TREE_CODE (fld
) == FUNCTION_DECL
25485 && !DECL_ARTIFICIAL (fld
)))
25486 && DECL_TEMPLATE_INSTANTIATION (fld
))
25487 instantiate_decl (fld
,
25488 /*defer_ok=*/false,
25489 /*expl_inst_class_mem_p=*/false);
25491 if (COMPLETE_TYPE_P (instantiation
))
25495 complete
= COMPLETE_TYPE_P (instantiation
);
25499 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25500 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
25503 = instantiate_decl (instantiation
,
25504 /*defer_ok=*/false,
25505 /*expl_inst_class_mem_p=*/false);
25506 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
25510 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
25511 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
25516 /* If INSTANTIATION has been instantiated, then we don't
25517 need to consider it again in the future. */
25518 struct pending_template
*drop
= *t
;
25520 set_refcount_ptr (drop
->tinst
);
25521 pending_template_freelist ().free (drop
);
25529 set_refcount_ptr (current_tinst_level
);
25531 last_pending_template
= last
;
25533 while (reconsider
);
25535 input_location
= saved_loc
;
25538 /* Substitute ARGVEC into T, which is a list of initializers for
25539 either base class or a non-static data member. The TREE_PURPOSEs
25540 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25541 instantiate_decl. */
25544 tsubst_initializer_list (tree t
, tree argvec
)
25546 tree inits
= NULL_TREE
;
25547 tree target_ctor
= error_mark_node
;
25549 for (; t
; t
= TREE_CHAIN (t
))
25553 tree expanded_bases
= NULL_TREE
;
25554 tree expanded_arguments
= NULL_TREE
;
25557 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
25562 /* Expand the base class expansion type into separate base
25564 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
25565 tf_warning_or_error
,
25567 if (expanded_bases
== error_mark_node
)
25570 /* We'll be building separate TREE_LISTs of arguments for
25572 len
= TREE_VEC_LENGTH (expanded_bases
);
25573 expanded_arguments
= make_tree_vec (len
);
25574 for (i
= 0; i
< len
; i
++)
25575 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
25577 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25578 expand each argument in the TREE_VALUE of t. */
25579 expr
= make_node (EXPR_PACK_EXPANSION
);
25580 PACK_EXPANSION_LOCAL_P (expr
) = true;
25581 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
25582 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
25584 if (TREE_VALUE (t
) == void_type_node
)
25585 /* VOID_TYPE_NODE is used to indicate
25586 value-initialization. */
25588 for (i
= 0; i
< len
; i
++)
25589 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
25593 /* Substitute parameter packs into each argument in the
25595 in_base_initializer
= 1;
25596 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
25598 tree expanded_exprs
;
25600 /* Expand the argument. */
25601 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
25603 = tsubst_pack_expansion (expr
, argvec
,
25604 tf_warning_or_error
,
25606 if (expanded_exprs
== error_mark_node
)
25609 /* Prepend each of the expanded expressions to the
25610 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25611 for (i
= 0; i
< len
; i
++)
25613 TREE_VEC_ELT (expanded_arguments
, i
) =
25614 tree_cons (NULL_TREE
,
25615 TREE_VEC_ELT (expanded_exprs
, i
),
25616 TREE_VEC_ELT (expanded_arguments
, i
));
25619 in_base_initializer
= 0;
25621 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25622 since we built them backwards. */
25623 for (i
= 0; i
< len
; i
++)
25625 TREE_VEC_ELT (expanded_arguments
, i
) =
25626 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
25631 for (i
= 0; i
< len
; ++i
)
25633 if (expanded_bases
)
25635 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25636 decl
= expand_member_init (decl
);
25637 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25642 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25643 tf_warning_or_error
, NULL_TREE
);
25645 decl
= expand_member_init (decl
);
25646 if (decl
&& !DECL_P (decl
))
25647 in_base_initializer
= 1;
25649 init
= TREE_VALUE (t
);
25651 if (init
!= void_type_node
)
25652 init
= tsubst_expr (init
, argvec
,
25653 tf_warning_or_error
, NULL_TREE
,
25654 /*integral_constant_expression_p=*/false);
25655 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25656 /* If we had an initializer but it instantiated to nothing,
25657 value-initialize the object. This will only occur when
25658 the initializer was a pack expansion where the parameter
25659 packs used in that expansion were of length zero. */
25660 init
= void_type_node
;
25661 in_base_initializer
= 0;
25664 if (target_ctor
!= error_mark_node
25665 && init
!= error_mark_node
)
25667 error ("mem-initializer for %qD follows constructor delegation",
25671 /* Look for a target constructor. */
25672 if (init
!= error_mark_node
25673 && decl
&& CLASS_TYPE_P (decl
)
25674 && same_type_p (decl
, current_class_type
))
25676 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25679 error ("constructor delegation follows mem-initializer for %qD",
25680 TREE_PURPOSE (inits
));
25683 target_ctor
= init
;
25688 init
= build_tree_list (decl
, init
);
25689 TREE_CHAIN (init
) = inits
;
25697 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25700 set_current_access_from_decl (tree decl
)
25702 if (TREE_PRIVATE (decl
))
25703 current_access_specifier
= access_private_node
;
25704 else if (TREE_PROTECTED (decl
))
25705 current_access_specifier
= access_protected_node
;
25707 current_access_specifier
= access_public_node
;
25710 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25711 is the instantiation (which should have been created with
25712 start_enum) and ARGS are the template arguments to use. */
25715 tsubst_enum (tree tag
, tree newtag
, tree args
)
25719 if (SCOPED_ENUM_P (newtag
))
25720 begin_scope (sk_scoped_enum
, newtag
);
25722 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25727 decl
= TREE_VALUE (e
);
25728 /* Note that in a template enum, the TREE_VALUE is the
25729 CONST_DECL, not the corresponding INTEGER_CST. */
25730 value
= tsubst_expr (DECL_INITIAL (decl
),
25731 args
, tf_warning_or_error
, NULL_TREE
,
25732 /*integral_constant_expression_p=*/true);
25734 /* Give this enumeration constant the correct access. */
25735 set_current_access_from_decl (decl
);
25737 /* Actually build the enumerator itself. Here we're assuming that
25738 enumerators can't have dependent attributes. */
25739 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25740 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25743 if (SCOPED_ENUM_P (newtag
))
25746 finish_enum_value_list (newtag
);
25747 finish_enum (newtag
);
25749 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25750 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25753 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25754 its type -- but without substituting the innermost set of template
25755 arguments. So, innermost set of template parameters will appear in
25759 get_mostly_instantiated_function_type (tree decl
)
25761 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25762 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
25765 /* Return truthvalue if we're processing a template different from
25766 the last one involved in diagnostics. */
25768 problematic_instantiation_changed (void)
25770 return current_tinst_level
!= last_error_tinst_level
;
25773 /* Remember current template involved in diagnostics. */
25775 record_last_problematic_instantiation (void)
25777 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
25780 struct tinst_level
*
25781 current_instantiation (void)
25783 return current_tinst_level
;
25786 /* Return TRUE if current_function_decl is being instantiated, false
25790 instantiating_current_function_p (void)
25792 return (current_instantiation ()
25793 && (current_instantiation ()->maybe_get_node ()
25794 == current_function_decl
));
25797 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25798 type. Return false for ok, true for disallowed. Issue error and
25799 inform messages under control of COMPLAIN. */
25802 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25804 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25806 else if (TYPE_PTR_P (type
))
25808 else if (TYPE_REF_P (type
)
25809 && !TYPE_REF_IS_RVALUE (type
))
25811 else if (TYPE_PTRMEM_P (type
))
25813 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25815 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25817 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25819 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25821 /* A bound template template parm could later be instantiated to have a valid
25822 nontype parm type via an alias template. */
25823 else if (cxx_dialect
>= cxx11
25824 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25826 else if (CLASS_TYPE_P (type
))
25828 if (cxx_dialect
< cxx2a
)
25830 if (complain
& tf_error
)
25831 error ("non-type template parameters of class type only available "
25832 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25835 if (dependent_type_p (type
))
25837 if (!complete_type_or_else (type
, NULL_TREE
))
25839 if (!structural_type_p (type
))
25841 if (complain
& tf_error
)
25843 auto_diagnostic_group d
;
25844 error ("%qT is not a valid type for a template non-type "
25845 "parameter because it is not structural", type
);
25846 structural_type_p (type
, true);
25853 if (complain
& tf_error
)
25855 if (type
== error_mark_node
)
25856 inform (input_location
, "invalid template non-type parameter");
25858 error ("%q#T is not a valid type for a template non-type parameter",
25864 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25865 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25868 dependent_type_p_r (tree type
)
25874 A type is dependent if it is:
25876 -- a template parameter. Template template parameters are types
25877 for us (since TYPE_P holds true for them) so we handle
25879 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25880 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
25882 /* -- a qualified-id with a nested-name-specifier which contains a
25883 class-name that names a dependent type or whose unqualified-id
25884 names a dependent type. */
25885 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25888 /* An alias template specialization can be dependent even if the
25889 resulting type is not. */
25890 if (dependent_alias_template_spec_p (type
, nt_transparent
))
25893 /* -- a cv-qualified type where the cv-unqualified type is
25895 No code is necessary for this bullet; the code below handles
25896 cv-qualified types, and we don't want to strip aliases with
25897 TYPE_MAIN_VARIANT because of DR 1558. */
25898 /* -- a compound type constructed from any dependent type. */
25899 if (TYPE_PTRMEM_P (type
))
25900 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
25901 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25903 else if (INDIRECT_TYPE_P (type
))
25904 return dependent_type_p (TREE_TYPE (type
));
25905 else if (FUNC_OR_METHOD_TYPE_P (type
))
25909 if (dependent_type_p (TREE_TYPE (type
)))
25911 for (arg_type
= TYPE_ARG_TYPES (type
);
25913 arg_type
= TREE_CHAIN (arg_type
))
25914 if (dependent_type_p (TREE_VALUE (arg_type
)))
25916 if (cxx_dialect
>= cxx17
)
25917 /* A value-dependent noexcept-specifier makes the type dependent. */
25918 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25919 if (tree noex
= TREE_PURPOSE (spec
))
25920 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25921 affect overload resolution and treating it as dependent breaks
25922 things. Same for an unparsed noexcept expression. */
25923 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25924 && TREE_CODE (noex
) != DEFERRED_PARSE
25925 && value_dependent_expression_p (noex
))
25929 /* -- an array type constructed from any dependent type or whose
25930 size is specified by a constant expression that is
25933 We checked for type- and value-dependence of the bounds in
25934 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25935 if (TREE_CODE (type
) == ARRAY_TYPE
)
25937 if (TYPE_DOMAIN (type
)
25938 && dependent_type_p (TYPE_DOMAIN (type
)))
25940 return dependent_type_p (TREE_TYPE (type
));
25943 /* -- a template-id in which either the template name is a template
25945 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25947 /* ... or any of the template arguments is a dependent type or
25948 an expression that is type-dependent or value-dependent. */
25949 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25950 && (any_dependent_template_arguments_p
25951 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25954 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25955 dependent; if the argument of the `typeof' expression is not
25956 type-dependent, then it should already been have resolved. */
25957 if (TREE_CODE (type
) == TYPEOF_TYPE
25958 || TREE_CODE (type
) == DECLTYPE_TYPE
25959 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25962 /* A template argument pack is dependent if any of its packed
25964 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25966 tree args
= ARGUMENT_PACK_ARGS (type
);
25967 int i
, len
= TREE_VEC_LENGTH (args
);
25968 for (i
= 0; i
< len
; ++i
)
25969 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25973 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25974 be template parameters. */
25975 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25978 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25981 /* The standard does not specifically mention types that are local
25982 to template functions or local classes, but they should be
25983 considered dependent too. For example:
25985 template <int I> void f() {
25990 The size of `E' cannot be known until the value of `I' has been
25991 determined. Therefore, `E' must be considered dependent. */
25992 scope
= TYPE_CONTEXT (type
);
25993 if (scope
&& TYPE_P (scope
))
25994 return dependent_type_p (scope
);
25995 /* Don't use type_dependent_expression_p here, as it can lead
25996 to infinite recursion trying to determine whether a lambda
25997 nested in a lambda is dependent (c++/47687). */
25998 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25999 && DECL_LANG_SPECIFIC (scope
)
26000 && DECL_TEMPLATE_INFO (scope
)
26001 && (any_dependent_template_arguments_p
26002 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
26005 /* Other types are non-dependent. */
26009 /* Returns TRUE if TYPE is dependent, in the sense of
26010 [temp.dep.type]. Note that a NULL type is considered dependent. */
26013 dependent_type_p (tree type
)
26015 /* If there are no template parameters in scope, then there can't be
26016 any dependent types. */
26017 if (!processing_template_decl
)
26019 /* If we are not processing a template, then nobody should be
26020 providing us with a dependent type. */
26022 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
26026 /* If the type is NULL, we have not computed a type for the entity
26027 in question; in that case, the type is dependent. */
26031 /* Erroneous types can be considered non-dependent. */
26032 if (type
== error_mark_node
)
26035 /* Getting here with global_type_node means we improperly called this
26036 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26037 gcc_checking_assert (type
!= global_type_node
);
26039 /* If we have not already computed the appropriate value for TYPE,
26041 if (!TYPE_DEPENDENT_P_VALID (type
))
26043 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
26044 TYPE_DEPENDENT_P_VALID (type
) = 1;
26047 return TYPE_DEPENDENT_P (type
);
26050 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26051 lookup. In other words, a dependent type that is not the current
26055 dependent_scope_p (tree scope
)
26057 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
26058 && !currently_open_class (scope
));
26061 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26062 an unknown base of 'this' (and is therefore instantiation-dependent). */
26065 unknown_base_ref_p (tree t
)
26067 if (!current_class_ptr
)
26070 tree mem
= TREE_OPERAND (t
, 1);
26071 if (shared_member_p (mem
))
26074 tree cur
= current_nonlambda_class_type ();
26075 if (!any_dependent_bases_p (cur
))
26078 tree ctx
= TREE_OPERAND (t
, 0);
26079 if (DERIVED_FROM_P (ctx
, cur
))
26085 /* T is a SCOPE_REF; return whether we need to consider it
26086 instantiation-dependent so that we can check access at instantiation
26087 time even though we know which member it resolves to. */
26090 instantiation_dependent_scope_ref_p (tree t
)
26092 if (DECL_P (TREE_OPERAND (t
, 1))
26093 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
26094 && !unknown_base_ref_p (t
)
26095 && accessible_in_template_p (TREE_OPERAND (t
, 0),
26096 TREE_OPERAND (t
, 1)))
26102 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26103 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26106 /* Note that this predicate is not appropriate for general expressions;
26107 only constant expressions (that satisfy potential_constant_expression)
26108 can be tested for value dependence. */
26111 value_dependent_expression_p (tree expression
)
26113 if (!processing_template_decl
|| expression
== NULL_TREE
)
26116 /* A type-dependent expression is also value-dependent. */
26117 if (type_dependent_expression_p (expression
))
26120 switch (TREE_CODE (expression
))
26123 /* A dependent member function of the current instantiation. */
26124 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
26126 case FUNCTION_DECL
:
26127 /* A dependent member function of the current instantiation. */
26128 if (DECL_CLASS_SCOPE_P (expression
)
26129 && dependent_type_p (DECL_CONTEXT (expression
)))
26133 case IDENTIFIER_NODE
:
26134 /* A name that has not been looked up -- must be dependent. */
26137 case TEMPLATE_PARM_INDEX
:
26138 /* A non-type template parm. */
26142 /* A non-type template parm. */
26143 if (DECL_TEMPLATE_PARM_P (expression
))
26145 return value_dependent_expression_p (DECL_INITIAL (expression
));
26148 /* A constant with literal type and is initialized
26149 with an expression that is value-dependent. */
26150 if (DECL_DEPENDENT_INIT_P (expression
)
26151 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26152 || TYPE_REF_P (TREE_TYPE (expression
)))
26154 if (DECL_HAS_VALUE_EXPR_P (expression
))
26156 tree value_expr
= DECL_VALUE_EXPR (expression
);
26157 if (value_dependent_expression_p (value_expr
)
26158 /* __PRETTY_FUNCTION__ inside a template function is dependent
26159 on the name of the function. */
26160 || (DECL_PRETTY_FUNCTION_P (expression
)
26161 /* It might be used in a template, but not a template
26162 function, in which case its DECL_VALUE_EXPR will be
26164 && value_expr
== error_mark_node
))
26169 case DYNAMIC_CAST_EXPR
:
26170 case STATIC_CAST_EXPR
:
26171 case CONST_CAST_EXPR
:
26172 case REINTERPRET_CAST_EXPR
:
26174 case IMPLICIT_CONV_EXPR
:
26175 /* These expressions are value-dependent if the type to which
26176 the cast occurs is dependent or the expression being casted
26177 is value-dependent. */
26179 tree type
= TREE_TYPE (expression
);
26181 if (dependent_type_p (type
))
26184 /* A functional cast has a list of operands. */
26185 expression
= TREE_OPERAND (expression
, 0);
26188 /* If there are no operands, it must be an expression such
26189 as "int()". This should not happen for aggregate types
26190 because it would form non-constant expressions. */
26191 gcc_assert (cxx_dialect
>= cxx11
26192 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
26197 if (TREE_CODE (expression
) == TREE_LIST
)
26198 return any_value_dependent_elements_p (expression
);
26200 return value_dependent_expression_p (expression
);
26204 if (SIZEOF_EXPR_TYPE_P (expression
))
26205 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
26209 /* A `sizeof' expression is value-dependent if the operand is
26210 type-dependent or is a pack expansion. */
26211 expression
= TREE_OPERAND (expression
, 0);
26212 if (PACK_EXPANSION_P (expression
))
26214 else if (TYPE_P (expression
))
26215 return dependent_type_p (expression
);
26216 return instantiation_dependent_uneval_expression_p (expression
);
26218 case AT_ENCODE_EXPR
:
26219 /* An 'encode' expression is value-dependent if the operand is
26221 expression
= TREE_OPERAND (expression
, 0);
26222 return dependent_type_p (expression
);
26224 case NOEXCEPT_EXPR
:
26225 expression
= TREE_OPERAND (expression
, 0);
26226 return instantiation_dependent_uneval_expression_p (expression
);
26229 /* All instantiation-dependent expressions should also be considered
26230 value-dependent. */
26231 return instantiation_dependent_scope_ref_p (expression
);
26233 case COMPONENT_REF
:
26234 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
26235 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
26237 case NONTYPE_ARGUMENT_PACK
:
26238 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26239 is value-dependent. */
26241 tree values
= ARGUMENT_PACK_ARGS (expression
);
26242 int i
, len
= TREE_VEC_LENGTH (values
);
26244 for (i
= 0; i
< len
; ++i
)
26245 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
26253 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
26255 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
26261 if (TREE_CODE (type2
) != TREE_LIST
)
26262 return dependent_type_p (type2
);
26264 for (; type2
; type2
= TREE_CHAIN (type2
))
26265 if (dependent_type_p (TREE_VALUE (type2
)))
26272 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26273 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
26276 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26277 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
26281 tree op
= TREE_OPERAND (expression
, 0);
26282 return (value_dependent_expression_p (op
)
26283 || has_value_dependent_address (op
));
26286 case REQUIRES_EXPR
:
26287 /* Treat all requires-expressions as value-dependent so
26288 we don't try to fold them. */
26292 return dependent_type_p (TREE_OPERAND (expression
, 0));
26296 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
26298 tree fn
= get_callee_fndecl (expression
);
26300 nargs
= call_expr_nargs (expression
);
26301 for (i
= 0; i
< nargs
; ++i
)
26303 tree op
= CALL_EXPR_ARG (expression
, i
);
26304 /* In a call to a constexpr member function, look through the
26305 implicit ADDR_EXPR on the object argument so that it doesn't
26306 cause the call to be considered value-dependent. We also
26307 look through it in potential_constant_expression. */
26308 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
26309 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
26310 && TREE_CODE (op
) == ADDR_EXPR
)
26311 op
= TREE_OPERAND (op
, 0);
26312 if (value_dependent_expression_p (op
))
26318 case TEMPLATE_ID_EXPR
:
26319 return concept_definition_p (TREE_OPERAND (expression
, 0));
26325 if (dependent_type_p (TREE_TYPE (expression
)))
26327 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
26328 if (value_dependent_expression_p (val
))
26334 /* Treat a GNU statement expression as dependent to avoid crashing
26335 under instantiate_non_dependent_expr; it can't be constant. */
26339 /* A constant expression is value-dependent if any subexpression is
26340 value-dependent. */
26341 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
26343 case tcc_reference
:
26345 case tcc_comparison
:
26347 case tcc_expression
:
26350 int i
, len
= cp_tree_operand_length (expression
);
26352 for (i
= 0; i
< len
; i
++)
26354 tree t
= TREE_OPERAND (expression
, i
);
26356 /* In some cases, some of the operands may be missing.
26357 (For example, in the case of PREDECREMENT_EXPR, the
26358 amount to increment by may be missing.) That doesn't
26359 make the expression dependent. */
26360 if (t
&& value_dependent_expression_p (t
))
26371 /* The expression is not value-dependent. */
26375 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26376 [temp.dep.expr]. Note that an expression with no type is
26377 considered dependent. Other parts of the compiler arrange for an
26378 expression with type-dependent subexpressions to have no type, so
26379 this function doesn't have to be fully recursive. */
26382 type_dependent_expression_p (tree expression
)
26384 if (!processing_template_decl
)
26387 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
26390 STRIP_ANY_LOCATION_WRAPPER (expression
);
26392 /* An unresolved name is always dependent. */
26393 if (identifier_p (expression
)
26394 || TREE_CODE (expression
) == USING_DECL
26395 || TREE_CODE (expression
) == WILDCARD_DECL
)
26398 /* A lambda-expression in template context is dependent. dependent_type_p is
26399 true for a lambda in the scope of a class or function template, but that
26400 doesn't cover all template contexts, like a default template argument. */
26401 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
26404 /* A fold expression is type-dependent. */
26405 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
26406 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
26407 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
26408 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
26411 /* Some expression forms are never type-dependent. */
26412 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
26413 || TREE_CODE (expression
) == SIZEOF_EXPR
26414 || TREE_CODE (expression
) == ALIGNOF_EXPR
26415 || TREE_CODE (expression
) == AT_ENCODE_EXPR
26416 || TREE_CODE (expression
) == NOEXCEPT_EXPR
26417 || TREE_CODE (expression
) == TRAIT_EXPR
26418 || TREE_CODE (expression
) == TYPEID_EXPR
26419 || TREE_CODE (expression
) == DELETE_EXPR
26420 || TREE_CODE (expression
) == VEC_DELETE_EXPR
26421 || TREE_CODE (expression
) == THROW_EXPR
26422 || TREE_CODE (expression
) == REQUIRES_EXPR
)
26425 /* The types of these expressions depends only on the type to which
26426 the cast occurs. */
26427 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
26428 || TREE_CODE (expression
) == STATIC_CAST_EXPR
26429 || TREE_CODE (expression
) == CONST_CAST_EXPR
26430 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
26431 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
26432 || TREE_CODE (expression
) == CAST_EXPR
)
26433 return dependent_type_p (TREE_TYPE (expression
));
26435 /* The types of these expressions depends only on the type created
26436 by the expression. */
26437 if (TREE_CODE (expression
) == NEW_EXPR
26438 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
26440 /* For NEW_EXPR tree nodes created inside a template, either
26441 the object type itself or a TREE_LIST may appear as the
26443 tree type
= TREE_OPERAND (expression
, 1);
26444 if (TREE_CODE (type
) == TREE_LIST
)
26445 /* This is an array type. We need to check array dimensions
26447 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
26448 || value_dependent_expression_p
26449 (TREE_OPERAND (TREE_VALUE (type
), 1));
26451 return dependent_type_p (type
);
26454 if (TREE_CODE (expression
) == SCOPE_REF
)
26456 tree scope
= TREE_OPERAND (expression
, 0);
26457 tree name
= TREE_OPERAND (expression
, 1);
26459 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26460 contains an identifier associated by name lookup with one or more
26461 declarations declared with a dependent type, or...a
26462 nested-name-specifier or qualified-id that names a member of an
26463 unknown specialization. */
26464 return (type_dependent_expression_p (name
)
26465 || dependent_scope_p (scope
));
26468 if (TREE_CODE (expression
) == TEMPLATE_DECL
26469 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
26470 return uses_outer_template_parms (expression
);
26472 if (TREE_CODE (expression
) == STMT_EXPR
)
26473 expression
= stmt_expr_value_expr (expression
);
26475 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
26480 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
26482 if (type_dependent_expression_p (elt
))
26488 /* A static data member of the current instantiation with incomplete
26489 array type is type-dependent, as the definition and specializations
26490 can have different bounds. */
26491 if (VAR_P (expression
)
26492 && DECL_CLASS_SCOPE_P (expression
)
26493 && dependent_type_p (DECL_CONTEXT (expression
))
26494 && VAR_HAD_UNKNOWN_BOUND (expression
))
26497 /* An array of unknown bound depending on a variadic parameter, eg:
26499 template<typename... Args>
26500 void foo (Args... args)
26502 int arr[] = { args... };
26505 template<int... vals>
26508 int arr[] = { vals... };
26511 If the array has no length and has an initializer, it must be that
26512 we couldn't determine its length in cp_complete_array_type because
26513 it is dependent. */
26514 if (VAR_P (expression
)
26515 && TREE_TYPE (expression
) != NULL_TREE
26516 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
26517 && !TYPE_DOMAIN (TREE_TYPE (expression
))
26518 && DECL_INITIAL (expression
))
26521 /* A function or variable template-id is type-dependent if it has any
26522 dependent template arguments. */
26523 if (VAR_OR_FUNCTION_DECL_P (expression
)
26524 && DECL_LANG_SPECIFIC (expression
)
26525 && DECL_TEMPLATE_INFO (expression
))
26527 /* Consider the innermost template arguments, since those are the ones
26528 that come from the template-id; the template arguments for the
26529 enclosing class do not make it type-dependent unless they are used in
26530 the type of the decl. */
26531 if (instantiates_primary_template_p (expression
)
26532 && (any_dependent_template_arguments_p
26533 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
26537 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26538 type-dependent. Checking this is important for functions with auto return
26539 type, which looks like a dependent type. */
26540 if (TREE_CODE (expression
) == FUNCTION_DECL
26541 && !(DECL_CLASS_SCOPE_P (expression
)
26542 && dependent_type_p (DECL_CONTEXT (expression
)))
26543 && !(DECL_LANG_SPECIFIC (expression
)
26544 && DECL_FRIEND_P (expression
)
26545 && (!DECL_FRIEND_CONTEXT (expression
)
26546 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
26547 && !DECL_LOCAL_FUNCTION_P (expression
))
26549 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
26550 || undeduced_auto_decl (expression
));
26554 /* Always dependent, on the number of arguments if nothing else. */
26555 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
26558 if (TREE_TYPE (expression
) == unknown_type_node
)
26560 if (TREE_CODE (expression
) == ADDR_EXPR
)
26561 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
26562 if (TREE_CODE (expression
) == COMPONENT_REF
26563 || TREE_CODE (expression
) == OFFSET_REF
)
26565 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
26567 expression
= TREE_OPERAND (expression
, 1);
26568 if (identifier_p (expression
))
26571 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26572 if (TREE_CODE (expression
) == SCOPE_REF
)
26575 if (BASELINK_P (expression
))
26577 if (BASELINK_OPTYPE (expression
)
26578 && dependent_type_p (BASELINK_OPTYPE (expression
)))
26580 expression
= BASELINK_FUNCTIONS (expression
);
26583 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
26585 if (any_dependent_template_arguments_p
26586 (TREE_OPERAND (expression
, 1)))
26588 expression
= TREE_OPERAND (expression
, 0);
26589 if (identifier_p (expression
))
26593 gcc_assert (OVL_P (expression
));
26595 for (lkp_iterator
iter (expression
); iter
; ++iter
)
26596 if (type_dependent_expression_p (*iter
))
26602 /* The type of a non-type template parm declared with a placeholder type
26603 depends on the corresponding template argument, even though
26604 placeholders are not normally considered dependent. */
26605 if (TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
26606 && is_auto (TREE_TYPE (expression
)))
26609 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
26611 /* Dependent type attributes might not have made it from the decl to
26613 if (DECL_P (expression
)
26614 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
26617 return (dependent_type_p (TREE_TYPE (expression
)));
26620 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26621 type-dependent if the expression refers to a member of the current
26622 instantiation and the type of the referenced member is dependent, or the
26623 class member access expression refers to a member of an unknown
26626 This function returns true if the OBJECT in such a class member access
26627 expression is of an unknown specialization. */
26630 type_dependent_object_expression_p (tree object
)
26632 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26634 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
26636 tree scope
= TREE_TYPE (object
);
26637 return (!scope
|| dependent_scope_p (scope
));
26640 /* walk_tree callback function for instantiation_dependent_expression_p,
26641 below. Returns non-zero if a dependent subexpression is found. */
26644 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26649 /* We don't have to worry about decltype currently because decltype
26650 of an instantiation-dependent expr is a dependent type. This
26651 might change depending on the resolution of DR 1172. */
26652 *walk_subtrees
= false;
26655 enum tree_code code
= TREE_CODE (*tp
);
26658 /* Don't treat an argument list as dependent just because it has no
26662 case NONTYPE_ARGUMENT_PACK
:
26665 case TEMPLATE_PARM_INDEX
:
26666 if (dependent_type_p (TREE_TYPE (*tp
)))
26668 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26670 /* We'll check value-dependence separately. */
26673 /* Handle expressions with type operands. */
26677 case AT_ENCODE_EXPR
:
26679 tree op
= TREE_OPERAND (*tp
, 0);
26680 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26681 op
= TREE_TYPE (op
);
26684 if (dependent_type_p (op
))
26688 *walk_subtrees
= false;
26695 case COMPONENT_REF
:
26696 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26697 /* In a template, finish_class_member_access_expr creates a
26698 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26699 type-dependent, so that we can check access control at
26700 instantiation time (PR 42277). See also Core issue 1273. */
26705 if (instantiation_dependent_scope_ref_p (*tp
))
26710 /* Treat statement-expressions as dependent. */
26714 /* Treat requires-expressions as dependent. */
26715 case REQUIRES_EXPR
:
26719 /* Treat concept checks as dependent. */
26720 if (concept_check_p (*tp
))
26724 case TEMPLATE_ID_EXPR
:
26725 /* Treat concept checks as dependent. */
26726 if (concept_check_p (*tp
))
26731 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26739 if (type_dependent_expression_p (*tp
))
26745 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26746 sense defined by the ABI:
26748 "An expression is instantiation-dependent if it is type-dependent
26749 or value-dependent, or it has a subexpression that is type-dependent
26750 or value-dependent."
26752 Except don't actually check value-dependence for unevaluated expressions,
26753 because in sizeof(i) we don't care about the value of i. Checking
26754 type-dependence will in turn check value-dependence of array bounds/template
26755 arguments as needed. */
26758 instantiation_dependent_uneval_expression_p (tree expression
)
26762 if (!processing_template_decl
)
26765 if (expression
== error_mark_node
)
26768 result
= cp_walk_tree_without_duplicates (&expression
,
26769 instantiation_dependent_r
, NULL
);
26770 return result
!= NULL_TREE
;
26773 /* As above, but also check value-dependence of the expression as a whole. */
26776 instantiation_dependent_expression_p (tree expression
)
26778 return (instantiation_dependent_uneval_expression_p (expression
)
26779 || value_dependent_expression_p (expression
));
26782 /* Like type_dependent_expression_p, but it also works while not processing
26783 a template definition, i.e. during substitution or mangling. */
26786 type_dependent_expression_p_push (tree expr
)
26789 ++processing_template_decl
;
26790 b
= type_dependent_expression_p (expr
);
26791 --processing_template_decl
;
26795 /* Returns TRUE if ARGS contains a type-dependent expression. */
26798 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
26803 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
26805 if (type_dependent_expression_p (arg
))
26811 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26812 expressions) contains any type-dependent expressions. */
26815 any_type_dependent_elements_p (const_tree list
)
26817 for (; list
; list
= TREE_CHAIN (list
))
26818 if (type_dependent_expression_p (TREE_VALUE (list
)))
26824 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26825 expressions) contains any value-dependent expressions. */
26828 any_value_dependent_elements_p (const_tree list
)
26830 for (; list
; list
= TREE_CHAIN (list
))
26831 if (value_dependent_expression_p (TREE_VALUE (list
)))
26837 /* Returns TRUE if the ARG (a template argument) is dependent. */
26840 dependent_template_arg_p (tree arg
)
26842 if (!processing_template_decl
)
26845 /* Assume a template argument that was wrongly written by the user
26846 is dependent. This is consistent with what
26847 any_dependent_template_arguments_p [that calls this function]
26849 if (!arg
|| arg
== error_mark_node
)
26852 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
26853 arg
= argument_pack_select_arg (arg
);
26855 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
26857 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26859 if (DECL_TEMPLATE_PARM_P (arg
))
26861 /* A member template of a dependent class is not necessarily
26862 type-dependent, but it is a dependent template argument because it
26863 will be a member of an unknown specialization to that template. */
26864 tree scope
= CP_DECL_CONTEXT (arg
);
26865 return TYPE_P (scope
) && dependent_type_p (scope
);
26867 else if (ARGUMENT_PACK_P (arg
))
26869 tree args
= ARGUMENT_PACK_ARGS (arg
);
26870 int i
, len
= TREE_VEC_LENGTH (args
);
26871 for (i
= 0; i
< len
; ++i
)
26873 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26879 else if (TYPE_P (arg
))
26880 return dependent_type_p (arg
);
26882 return (type_dependent_expression_p (arg
)
26883 || value_dependent_expression_p (arg
));
26886 /* Returns true if ARGS (a collection of template arguments) contains
26887 any types that require structural equality testing. */
26890 any_template_arguments_need_structural_equality_p (tree args
)
26897 if (args
== error_mark_node
)
26900 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26902 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26903 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26905 tree arg
= TREE_VEC_ELT (level
, j
);
26906 tree packed_args
= NULL_TREE
;
26909 if (ARGUMENT_PACK_P (arg
))
26911 /* Look inside the argument pack. */
26912 packed_args
= ARGUMENT_PACK_ARGS (arg
);
26913 len
= TREE_VEC_LENGTH (packed_args
);
26916 for (k
= 0; k
< len
; ++k
)
26919 arg
= TREE_VEC_ELT (packed_args
, k
);
26921 if (error_operand_p (arg
))
26923 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26925 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
26927 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
26928 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26937 /* Returns true if ARGS (a collection of template arguments) contains
26938 any dependent arguments. */
26941 any_dependent_template_arguments_p (const_tree args
)
26948 if (args
== error_mark_node
)
26951 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26953 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26954 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26955 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26962 /* Returns true if ARGS contains any errors. */
26965 any_erroneous_template_args_p (const_tree args
)
26970 if (args
== error_mark_node
)
26973 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26975 if (tree ti
= get_template_info (args
))
26976 args
= TI_ARGS (ti
);
26984 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26986 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26987 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26988 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26995 /* Returns TRUE if the template TMPL is type-dependent. */
26998 dependent_template_p (tree tmpl
)
27000 if (TREE_CODE (tmpl
) == OVERLOAD
)
27002 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
27003 if (dependent_template_p (*iter
))
27008 /* Template template parameters are dependent. */
27009 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
27010 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
27012 /* So are names that have not been looked up. */
27013 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
27018 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27021 dependent_template_id_p (tree tmpl
, tree args
)
27023 return (dependent_template_p (tmpl
)
27024 || any_dependent_template_arguments_p (args
));
27027 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27031 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
27035 if (!processing_template_decl
)
27038 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
27040 tree decl
= TREE_VEC_ELT (declv
, i
);
27041 tree init
= TREE_VEC_ELT (initv
, i
);
27042 tree cond
= TREE_VEC_ELT (condv
, i
);
27043 tree incr
= TREE_VEC_ELT (incrv
, i
);
27045 if (type_dependent_expression_p (decl
)
27046 || TREE_CODE (decl
) == SCOPE_REF
)
27049 if (init
&& type_dependent_expression_p (init
))
27052 if (cond
== global_namespace
)
27055 if (type_dependent_expression_p (cond
))
27058 if (COMPARISON_CLASS_P (cond
)
27059 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
27060 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
27063 if (TREE_CODE (incr
) == MODOP_EXPR
)
27065 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
27066 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
27069 else if (type_dependent_expression_p (incr
))
27071 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
27073 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
27075 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
27077 tree t
= TREE_OPERAND (incr
, 1);
27078 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
27079 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
27082 /* If this loop has a class iterator with != comparison
27083 with increment other than i++/++i/i--/--i, make sure the
27084 increment is constant. */
27085 if (CLASS_TYPE_P (TREE_TYPE (decl
))
27086 && TREE_CODE (cond
) == NE_EXPR
)
27088 if (TREE_OPERAND (t
, 0) == decl
)
27089 t
= TREE_OPERAND (t
, 1);
27091 t
= TREE_OPERAND (t
, 0);
27092 if (TREE_CODE (t
) != INTEGER_CST
)
27102 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27103 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27104 no such TYPE can be found. Note that this function peers inside
27105 uninstantiated templates and therefore should be used only in
27106 extremely limited situations. ONLY_CURRENT_P restricts this
27107 peering to the currently open classes hierarchy (which is required
27108 when comparing types). */
27111 resolve_typename_type (tree type
, bool only_current_p
)
27120 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
27122 scope
= TYPE_CONTEXT (type
);
27123 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27124 gcc_checking_assert (uses_template_parms (scope
));
27126 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27127 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27128 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27129 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27130 identifier of the TYPENAME_TYPE anymore.
27131 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27132 TYPENAME_TYPE instead, we avoid messing up with a possible
27133 typedef variant case. */
27134 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
27136 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27137 it first before we can figure out what NAME refers to. */
27138 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
27140 if (TYPENAME_IS_RESOLVING_P (scope
))
27141 /* Given a class template A with a dependent base with nested type C,
27142 typedef typename A::C::C C will land us here, as trying to resolve
27143 the initial A::C leads to the local C typedef, which leads back to
27144 A::C::C. So we break the recursion now. */
27147 scope
= resolve_typename_type (scope
, only_current_p
);
27149 /* If we don't know what SCOPE refers to, then we cannot resolve the
27151 if (!CLASS_TYPE_P (scope
))
27153 /* If this is a typedef, we don't want to look inside (c++/11987). */
27154 if (typedef_variant_p (type
))
27156 /* If SCOPE isn't the template itself, it will not have a valid
27157 TYPE_FIELDS list. */
27158 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
27159 /* scope is either the template itself or a compatible instantiation
27160 like X<T>, so look up the name in the original template. */
27161 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
27162 /* If scope has no fields, it can't be a current instantiation. Check this
27163 before currently_open_class to avoid infinite recursion (71515). */
27164 if (!TYPE_FIELDS (scope
))
27166 /* If the SCOPE is not the current instantiation, there's no reason
27167 to look inside it. */
27168 if (only_current_p
&& !currently_open_class (scope
))
27170 /* Enter the SCOPE so that name lookup will be resolved as if we
27171 were in the class definition. In particular, SCOPE will no
27172 longer be considered a dependent type. */
27173 pushed_scope
= push_scope (scope
);
27174 /* Look up the declaration. */
27175 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
27176 tf_warning_or_error
);
27178 result
= NULL_TREE
;
27180 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27181 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27182 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
27185 else if (identifier_p (fullname
)
27186 && TREE_CODE (decl
) == TYPE_DECL
)
27188 result
= TREE_TYPE (decl
);
27189 if (result
== error_mark_node
)
27190 result
= NULL_TREE
;
27192 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
27193 && DECL_CLASS_TEMPLATE_P (decl
))
27195 /* Obtain the template and the arguments. */
27196 tree tmpl
= TREE_OPERAND (fullname
, 0);
27197 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
27199 /* We get here with a plain identifier because a previous tentative
27200 parse of the nested-name-specifier as part of a ptr-operator saw
27201 ::template X<A>. The use of ::template is necessary in a
27202 ptr-operator, but wrong in a declarator-id.
27204 [temp.names]: In a qualified-id of a declarator-id, the keyword
27205 template shall not appear at the top level. */
27206 pedwarn (cp_expr_loc_or_input_loc (fullname
), OPT_Wpedantic
,
27207 "keyword %<template%> not allowed in declarator-id");
27210 tree args
= TREE_OPERAND (fullname
, 1);
27211 /* Instantiate the template. */
27212 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
27213 /*entering_scope=*/true,
27214 tf_error
| tf_user
);
27215 if (result
== error_mark_node
)
27216 result
= NULL_TREE
;
27219 /* Leave the SCOPE. */
27221 pop_scope (pushed_scope
);
27223 /* If we failed to resolve it, return the original typename. */
27227 /* If lookup found a typename type, resolve that too. */
27228 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
27230 /* Ill-formed programs can cause infinite recursion here, so we
27231 must catch that. */
27232 TYPENAME_IS_RESOLVING_P (result
) = 1;
27233 result
= resolve_typename_type (result
, only_current_p
);
27234 TYPENAME_IS_RESOLVING_P (result
) = 0;
27237 /* Qualify the resulting type. */
27238 quals
= cp_type_quals (type
);
27240 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
27245 /* EXPR is an expression which is not type-dependent. Return a proxy
27246 for EXPR that can be used to compute the types of larger
27247 expressions containing EXPR. */
27250 build_non_dependent_expr (tree expr
)
27252 tree orig_expr
= expr
;
27255 /* When checking, try to get a constant value for all non-dependent
27256 expressions in order to expose bugs in *_dependent_expression_p
27257 and constexpr. This can affect code generation, see PR70704, so
27258 only do this for -fchecking=2. */
27259 if (flag_checking
> 1
27260 && cxx_dialect
>= cxx11
27261 /* Don't do this during nsdmi parsing as it can lead to
27262 unexpected recursive instantiations. */
27263 && !parsing_nsdmi ()
27264 /* Don't do this during concept processing either and for
27265 the same reason. */
27266 && !processing_constraint_expression_p ())
27267 fold_non_dependent_expr (expr
, tf_none
);
27269 STRIP_ANY_LOCATION_WRAPPER (expr
);
27271 /* Preserve OVERLOADs; the functions must be available to resolve
27274 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
27275 inner_expr
= stmt_expr_value_expr (inner_expr
);
27276 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
27277 inner_expr
= TREE_OPERAND (inner_expr
, 0);
27278 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
27279 inner_expr
= TREE_OPERAND (inner_expr
, 1);
27280 if (is_overloaded_fn (inner_expr
)
27281 || TREE_CODE (inner_expr
) == OFFSET_REF
)
27283 /* There is no need to return a proxy for a variable or enumerator. */
27284 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
27286 /* Preserve string constants; conversions from string constants to
27287 "char *" are allowed, even though normally a "const char *"
27288 cannot be used to initialize a "char *". */
27289 if (TREE_CODE (expr
) == STRING_CST
)
27291 /* Preserve void and arithmetic constants, as an optimization -- there is no
27292 reason to create a new node. */
27293 if (TREE_CODE (expr
) == VOID_CST
27294 || TREE_CODE (expr
) == INTEGER_CST
27295 || TREE_CODE (expr
) == REAL_CST
)
27297 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27298 There is at least one place where we want to know that a
27299 particular expression is a throw-expression: when checking a ?:
27300 expression, there are special rules if the second or third
27301 argument is a throw-expression. */
27302 if (TREE_CODE (expr
) == THROW_EXPR
)
27305 /* Don't wrap an initializer list, we need to be able to look inside. */
27306 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
27309 /* Don't wrap a dummy object, we need to be able to test for it. */
27310 if (is_dummy_object (expr
))
27313 if (TREE_CODE (expr
) == COND_EXPR
)
27314 return build3 (COND_EXPR
,
27316 build_non_dependent_expr (TREE_OPERAND (expr
, 0)),
27317 (TREE_OPERAND (expr
, 1)
27318 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
27319 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
27320 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
27321 if (TREE_CODE (expr
) == COMPOUND_EXPR
27322 && !COMPOUND_EXPR_OVERLOADED (expr
))
27323 return build2 (COMPOUND_EXPR
,
27325 TREE_OPERAND (expr
, 0),
27326 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
27328 /* If the type is unknown, it can't really be non-dependent */
27329 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
27331 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27332 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
27333 TREE_TYPE (expr
), expr
);
27336 /* ARGS is a vector of expressions as arguments to a function call.
27337 Replace the arguments with equivalent non-dependent expressions.
27338 This modifies ARGS in place. */
27341 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
27346 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
27348 tree newarg
= build_non_dependent_expr (arg
);
27350 (*args
)[ix
] = newarg
;
27354 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27355 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27356 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27359 make_auto_1 (tree name
, bool set_canonical
)
27361 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
27362 TYPE_NAME (au
) = build_decl (input_location
, TYPE_DECL
, name
, au
);
27363 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
27364 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
27365 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
27366 TYPE_NAME (au
), NULL_TREE
);
27368 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
27369 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
27370 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
27376 make_decltype_auto (void)
27378 return make_auto_1 (decltype_auto_identifier
, true);
27384 return make_auto_1 (auto_identifier
, true);
27387 /* Return a C++17 deduction placeholder for class template TMPL. */
27390 make_template_placeholder (tree tmpl
)
27392 tree t
= make_auto_1 (auto_identifier
, false);
27393 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
27394 /* Our canonical type depends on the placeholder. */
27395 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27399 /* True iff T is a C++17 class template deduction placeholder. */
27402 template_placeholder_p (tree t
)
27404 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
27407 /* Make a "constrained auto" type-specifier. This is an auto or
27408 decltype(auto) type with constraints that must be associated after
27409 deduction. The constraint is formed from the given concept CON
27410 and its optional sequence of template arguments ARGS.
27412 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27415 make_constrained_placeholder_type (tree type
, tree con
, tree args
)
27417 /* Build the constraint. */
27418 tree tmpl
= DECL_TI_TEMPLATE (con
);
27420 if (TREE_CODE (con
) == FUNCTION_DECL
)
27421 expr
= ovl_make (tmpl
);
27422 expr
= build_concept_check (expr
, type
, args
, tf_warning_or_error
);
27424 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = expr
;
27426 /* Our canonical type depends on the constraint. */
27427 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
27429 /* Attach the constraint to the type declaration. */
27430 return TYPE_NAME (type
);
27433 /* Make a "constrained auto" type-specifier. */
27436 make_constrained_auto (tree con
, tree args
)
27438 tree type
= make_auto_1 (auto_identifier
, false);
27439 return make_constrained_placeholder_type (type
, con
, args
);
27442 /* Make a "constrained decltype(auto)" type-specifier. */
27445 make_constrained_decltype_auto (tree con
, tree args
)
27447 tree type
= make_auto_1 (decltype_auto_identifier
, false);
27448 /* FIXME: I don't know why this isn't done in make_auto_1. */
27449 AUTO_IS_DECLTYPE (type
) = true;
27450 return make_constrained_placeholder_type (type
, con
, args
);
27453 /* Build and return a concept definition. Like other templates, the
27454 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27455 the TEMPLATE_DECL. */
27458 finish_concept_definition (cp_expr id
, tree init
)
27460 gcc_assert (identifier_p (id
));
27461 gcc_assert (processing_template_decl
);
27463 location_t loc
= id
.get_location();
27465 /* A concept-definition shall not have associated constraints. */
27466 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
))
27468 error_at (loc
, "a concept cannot be constrained");
27469 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = NULL_TREE
;
27472 /* A concept-definition shall appear in namespace scope. Templates
27473 aren't allowed in block scope, so we only need to check for class
27475 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27477 error_at (loc
, "concept %qE not in namespace scope", *id
);
27478 return error_mark_node
;
27481 /* Initially build the concept declaration; it's type is bool. */
27482 tree decl
= build_lang_decl_loc (loc
, CONCEPT_DECL
, *id
, boolean_type_node
);
27483 DECL_CONTEXT (decl
) = current_scope ();
27484 DECL_INITIAL (decl
) = init
;
27486 /* Push the enclosing template. */
27487 return push_template_decl (decl
);
27490 /* Given type ARG, return std::initializer_list<ARG>. */
27495 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
27497 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
27499 gcc_rich_location
richloc (input_location
);
27500 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
27501 error_at (&richloc
,
27502 "deducing from brace-enclosed initializer list"
27503 " requires %<#include <initializer_list>%>");
27505 return error_mark_node
;
27507 tree argvec
= make_tree_vec (1);
27508 TREE_VEC_ELT (argvec
, 0) = arg
;
27510 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
27511 NULL_TREE
, 0, tf_warning_or_error
);
27514 /* Replace auto in TYPE with std::initializer_list<auto>. */
27517 listify_autos (tree type
, tree auto_node
)
27519 tree init_auto
= listify (strip_top_quals (auto_node
));
27520 tree argvec
= make_tree_vec (1);
27521 TREE_VEC_ELT (argvec
, 0) = init_auto
;
27522 if (processing_template_decl
)
27523 argvec
= add_to_template_args (current_template_args (), argvec
);
27524 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
27527 /* Hash traits for hashing possibly constrained 'auto'
27528 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27530 struct auto_hash
: default_hash_traits
<tree
>
27532 static inline hashval_t
hash (tree
);
27533 static inline bool equal (tree
, tree
);
27536 /* Hash the 'auto' T. */
27539 auto_hash::hash (tree t
)
27541 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
27542 /* Matching constrained-type-specifiers denote the same template
27543 parameter, so hash the constraint. */
27544 return hash_placeholder_constraint (c
);
27546 /* But unconstrained autos are all separate, so just hash the pointer. */
27547 return iterative_hash_object (t
, 0);
27550 /* Compare two 'auto's. */
27553 auto_hash::equal (tree t1
, tree t2
)
27558 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
27559 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
27561 /* Two unconstrained autos are distinct. */
27565 return equivalent_placeholder_constraints (c1
, c2
);
27568 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27569 constrained) auto, add it to the vector. */
27572 extract_autos_r (tree t
, void *data
)
27574 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
27577 /* All the autos were built with index 0; fix that up now. */
27578 tree
*p
= hash
.find_slot (t
, INSERT
);
27581 /* If this is a repeated constrained-type-specifier, use the index we
27583 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
27586 /* Otherwise this is new, so use the current count. */
27588 idx
= hash
.elements () - 1;
27590 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
27593 /* Always keep walking. */
27597 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27598 says they can appear anywhere in the type. */
27601 extract_autos (tree type
)
27603 hash_set
<tree
> visited
;
27604 hash_table
<auto_hash
> hash (2);
27606 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
27608 tree tree_vec
= make_tree_vec (hash
.elements());
27609 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
27610 iter
!= hash
.end(); ++iter
)
27613 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
27614 TREE_VEC_ELT (tree_vec
, i
)
27615 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
27621 /* The stem for deduction guide names. */
27622 const char *const dguide_base
= "__dguide_";
27624 /* Return the name for a deduction guide for class template TMPL. */
27627 dguide_name (tree tmpl
)
27629 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
27630 tree tname
= TYPE_IDENTIFIER (type
);
27631 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
27632 + IDENTIFIER_LENGTH (tname
));
27633 memcpy (buf
, dguide_base
, strlen (dguide_base
));
27634 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
27635 IDENTIFIER_LENGTH (tname
) + 1);
27636 tree dname
= get_identifier (buf
);
27637 TREE_TYPE (dname
) = type
;
27641 /* True if NAME is the name of a deduction guide. */
27644 dguide_name_p (tree name
)
27646 return (TREE_CODE (name
) == IDENTIFIER_NODE
27647 && TREE_TYPE (name
)
27648 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
27649 strlen (dguide_base
)));
27652 /* True if FN is a deduction guide. */
27655 deduction_guide_p (const_tree fn
)
27658 if (tree name
= DECL_NAME (fn
))
27659 return dguide_name_p (name
);
27663 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27666 copy_guide_p (const_tree fn
)
27668 gcc_assert (deduction_guide_p (fn
));
27669 if (!DECL_ARTIFICIAL (fn
))
27671 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
27672 return (TREE_CHAIN (parms
) == void_list_node
27673 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
27676 /* True if FN is a guide generated from a constructor template. */
27679 template_guide_p (const_tree fn
)
27681 gcc_assert (deduction_guide_p (fn
));
27682 if (!DECL_ARTIFICIAL (fn
))
27684 tree tmpl
= DECL_TI_TEMPLATE (fn
);
27685 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
27686 return PRIMARY_TEMPLATE_P (org
);
27690 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27691 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27692 template parameter types. Note that the handling of template template
27693 parameters relies on current_template_parms being set appropriately for the
27697 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
27698 tree tsubst_args
, tsubst_flags_t complain
)
27700 if (olddecl
== error_mark_node
)
27701 return error_mark_node
;
27703 tree oldidx
= get_template_parm_index (olddecl
);
27706 if (TREE_CODE (olddecl
) == TYPE_DECL
27707 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27709 tree oldtype
= TREE_TYPE (olddecl
);
27710 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27711 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27712 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27713 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27714 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27718 newtype
= TREE_TYPE (olddecl
);
27719 if (type_uses_auto (newtype
))
27721 // Substitute once to fix references to other template parameters.
27722 newtype
= tsubst (newtype
, tsubst_args
,
27723 complain
|tf_partial
, NULL_TREE
);
27724 // Now substitute again to reduce the level of the auto.
27725 newtype
= tsubst (newtype
, current_template_args (),
27726 complain
, NULL_TREE
);
27729 newtype
= tsubst (newtype
, tsubst_args
,
27730 complain
, NULL_TREE
);
27734 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27735 DECL_NAME (olddecl
), newtype
);
27736 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27739 if (TREE_CODE (olddecl
) == TYPE_DECL
27740 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27742 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27743 = build_template_parm_index (index
, level
, level
,
27745 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27746 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27747 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27748 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
27750 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27752 DECL_TEMPLATE_RESULT (newdecl
)
27753 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
27754 DECL_NAME (olddecl
), newtype
);
27755 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
27756 // First create a copy (ttargs) of tsubst_args with an
27757 // additional level for the template template parameter's own
27758 // template parameters (ttparms).
27759 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
27760 (DECL_TEMPLATE_PARMS (olddecl
)));
27761 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
27762 tree ttargs
= make_tree_vec (depth
+ 1);
27763 for (int i
= 0; i
< depth
; ++i
)
27764 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
27765 TREE_VEC_ELT (ttargs
, depth
)
27766 = template_parms_level_to_args (ttparms
);
27767 // Substitute ttargs into ttparms to fix references to
27768 // other template parameters.
27769 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27770 complain
|tf_partial
);
27771 // Now substitute again with args based on tparms, to reduce
27772 // the level of the ttparms.
27773 ttargs
= current_template_args ();
27774 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27776 // Finally, tack the adjusted parms onto tparms.
27777 ttparms
= tree_cons (size_int (depth
), ttparms
,
27778 current_template_parms
);
27779 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
27784 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
27786 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
27787 TREE_CODE (oldconst
),
27788 DECL_NAME (oldconst
), newtype
);
27789 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
27790 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
27791 SET_DECL_TEMPLATE_PARM_P (newconst
);
27792 newidx
= build_template_parm_index (index
, level
, level
,
27793 newconst
, newtype
);
27794 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27795 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27796 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
27802 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
27803 template parameter. */
27806 rewrite_tparm_list (tree oldelt
, unsigned index
, unsigned level
,
27807 tree targs
, unsigned targs_index
, tsubst_flags_t complain
)
27809 tree olddecl
= TREE_VALUE (oldelt
);
27810 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27812 if (newdecl
== error_mark_node
)
27813 return error_mark_node
;
27814 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27815 targs
, complain
, NULL_TREE
);
27816 tree list
= build_tree_list (newdef
, newdecl
);
27817 TEMPLATE_PARM_CONSTRAINTS (list
)
27818 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27819 targs
, complain
, NULL_TREE
);
27820 int depth
= TMPL_ARGS_DEPTH (targs
);
27821 TMPL_ARG (targs
, depth
, targs_index
) = template_parm_to_arg (list
);
27825 /* Returns a C++17 class deduction guide template based on the constructor
27826 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27827 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27828 aggregate initialization guide. */
27831 build_deduction_guide (tree type
, tree ctor
, tree outer_args
, tsubst_flags_t complain
)
27833 tree tparms
, targs
, fparms
, fargs
, ci
;
27834 bool memtmpl
= false;
27837 tree fn_tmpl
= NULL_TREE
;
27841 ++processing_template_decl
;
27842 type
= tsubst (type
, outer_args
, complain
, CLASSTYPE_TI_TEMPLATE (type
));
27843 --processing_template_decl
;
27846 if (!DECL_DECLARES_FUNCTION_P (ctor
))
27850 bool copy_p
= TYPE_REF_P (ctor
);
27852 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
27854 fparms
= void_list_node
;
27856 else if (TREE_CODE (ctor
) == TREE_LIST
)
27859 gcc_unreachable ();
27861 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
27862 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
27863 targs
= CLASSTYPE_TI_ARGS (type
);
27866 loc
= DECL_SOURCE_LOCATION (ctmpl
);
27867 explicit_p
= false;
27871 ++processing_template_decl
;
27875 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
27876 : DECL_TI_TEMPLATE (ctor
));
27878 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
27879 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
27881 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
27882 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27883 fully specialized args for the enclosing class. Strip those off, as
27884 the deduction guide won't have those template parameters. */
27885 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
27886 TMPL_PARMS_DEPTH (tparms
));
27887 /* Discard the 'this' parameter. */
27888 fparms
= FUNCTION_ARG_CHAIN (ctor
);
27889 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
27890 ci
= get_constraints (ctor
);
27891 loc
= DECL_SOURCE_LOCATION (ctor
);
27892 explicit_p
= DECL_NONCONVERTING_P (ctor
);
27894 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
27898 /* For a member template constructor, we need to flatten the two
27899 template parameter lists into one, and then adjust the function
27900 signature accordingly. This gets...complicated. */
27901 tree save_parms
= current_template_parms
;
27903 /* For a member template we should have two levels of parms/args, one
27904 for the class and one for the constructor. We stripped
27905 specialized args for further enclosing classes above. */
27906 const int depth
= 2;
27907 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
27909 /* Template args for translating references to the two-level template
27910 parameters into references to the one-level template parameters we
27912 tree tsubst_args
= copy_node (targs
);
27913 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
27914 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
27916 /* Template parms for the constructor template. */
27917 tree ftparms
= TREE_VALUE (tparms
);
27918 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
27919 /* Template parms for the class template. */
27920 tparms
= TREE_CHAIN (tparms
);
27921 tree ctparms
= TREE_VALUE (tparms
);
27922 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
27923 /* Template parms for the deduction guide start as a copy of the
27924 template parms for the class. We set current_template_parms for
27925 lookup_template_class_1. */
27926 current_template_parms
= tparms
= copy_node (tparms
);
27927 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
27928 for (unsigned i
= 0; i
< clen
; ++i
)
27929 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
27931 /* Now we need to rewrite the constructor parms to append them to the
27933 for (unsigned i
= 0; i
< flen
; ++i
)
27935 unsigned index
= i
+ clen
;
27936 unsigned level
= 1;
27937 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
27939 = rewrite_tparm_list (oldelt
, index
, level
,
27940 tsubst_args
, i
, complain
);
27941 if (newelt
== error_mark_node
)
27943 TREE_VEC_ELT (new_vec
, index
) = newelt
;
27946 /* Now we have a final set of template parms to substitute into the
27947 function signature. */
27948 targs
= template_parms_to_args (tparms
);
27949 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
27951 if (fparms
== error_mark_node
)
27953 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
27955 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
27957 current_template_parms
= save_parms
;
27960 --processing_template_decl
;
27962 return error_mark_node
;
27967 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27968 tparms
= copy_node (tparms
);
27969 INNERMOST_TEMPLATE_PARMS (tparms
)
27970 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
27973 tree fntype
= build_function_type (type
, fparms
);
27974 tree ded_fn
= build_lang_decl_loc (loc
,
27976 dguide_name (type
), fntype
);
27977 DECL_ARGUMENTS (ded_fn
) = fargs
;
27978 DECL_ARTIFICIAL (ded_fn
) = true;
27979 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
27980 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
27981 DECL_ARTIFICIAL (ded_tmpl
) = true;
27982 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
27983 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
27984 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
27985 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
27987 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
27989 set_constraints (ded_tmpl
, ci
);
27994 /* Add to LIST the member types for the reshaped initializer CTOR. */
27997 collect_ctor_idx_types (tree ctor
, tree list
)
27999 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (ctor
);
28000 tree idx
, val
; unsigned i
;
28001 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, idx
, val
)
28003 if (BRACE_ENCLOSED_INITIALIZER_P (val
)
28004 && CONSTRUCTOR_NELTS (val
))
28005 if (tree subidx
= CONSTRUCTOR_ELT (val
, 0)->index
)
28006 if (TREE_CODE (subidx
) == FIELD_DECL
)
28008 list
= collect_ctor_idx_types (val
, list
);
28011 tree ftype
= finish_decltype_type (idx
, true, tf_none
);
28012 list
= tree_cons (NULL_TREE
, ftype
, list
);
28018 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28021 is_spec_or_derived (tree etype
, tree tmpl
)
28023 if (!etype
|| !CLASS_TYPE_P (etype
))
28026 tree type
= TREE_TYPE (tmpl
);
28027 tree tparms
= (INNERMOST_TEMPLATE_PARMS
28028 (DECL_TEMPLATE_PARMS (tmpl
)));
28029 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28030 int err
= unify (tparms
, targs
, type
, etype
,
28031 UNIFY_ALLOW_DERIVED
, /*explain*/false);
28036 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28040 maybe_aggr_guide (tree tmpl
, tree init
, vec
<tree
,va_gc
> *args
)
28042 if (cxx_dialect
< cxx2a
)
28045 if (init
== NULL_TREE
)
28048 tree type
= TREE_TYPE (tmpl
);
28049 if (!CP_AGGREGATE_TYPE_P (type
))
28052 /* No aggregate candidate for copy-initialization. */
28053 if (args
->length() == 1)
28055 tree val
= (*args
)[0];
28056 if (is_spec_or_derived (tmpl
, TREE_TYPE (val
)))
28060 /* If we encounter a problem, we just won't add the candidate. */
28061 tsubst_flags_t complain
= tf_none
;
28063 tree parms
= NULL_TREE
;
28064 if (TREE_CODE (init
) == CONSTRUCTOR
)
28066 init
= reshape_init (type
, init
, complain
);
28067 if (init
== error_mark_node
)
28069 parms
= collect_ctor_idx_types (init
, parms
);
28071 else if (TREE_CODE (init
) == TREE_LIST
)
28073 int len
= list_length (init
);
28074 for (tree field
= TYPE_FIELDS (type
);
28076 --len
, field
= DECL_CHAIN (field
))
28078 field
= next_initializable_field (field
);
28081 tree ftype
= finish_decltype_type (field
, true, complain
);
28082 parms
= tree_cons (NULL_TREE
, ftype
, parms
);
28086 /* Aggregate initialization doesn't apply to an initializer expression. */
28092 parms
= nreverse (parms
);
28093 TREE_CHAIN (last
) = void_list_node
;
28094 tree guide
= build_deduction_guide (type
, parms
, NULL_TREE
, complain
);
28101 /* UGUIDES are the deduction guides for the underlying template of alias
28102 template TMPL; adjust them to be deduction guides for TMPL. */
28105 alias_ctad_tweaks (tree tmpl
, tree uguides
)
28107 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28108 class type (9.2.8.2) where the template-name names an alias template A,
28109 the defining-type-id of A must be of the form
28111 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28113 as specified in 9.2.8.2. The guides of A are the set of functions or
28114 function templates formed as follows. For each function or function
28115 template f in the guides of the template named by the simple-template-id
28116 of the defining-type-id, the template arguments of the return type of f
28117 are deduced from the defining-type-id of A according to the process in
28118 13.10.2.5 with the exception that deduction does not fail if not all
28119 template arguments are deduced. Let g denote the result of substituting
28120 these deductions into f. If substitution succeeds, form a function or
28121 function template f' with the following properties and add it to the set
28124 * The function type of f' is the function type of g.
28126 * If f is a function template, f' is a function template whose template
28127 parameter list consists of all the template parameters of A (including
28128 their default template arguments) that appear in the above deductions or
28129 (recursively) in their default template arguments, followed by the
28130 template parameters of f that were not deduced (including their default
28131 template arguments), otherwise f' is not a function template.
28133 * The associated constraints (13.5.2) are the conjunction of the
28134 associated constraints of g and a constraint that is satisfied if and only
28135 if the arguments of A are deducible (see below) from the return type.
28137 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28140 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28141 considered to be so as well.
28143 * The explicit-specifier of f' is the explicit-specifier of g (if
28146 /* This implementation differs from the above in two significant ways:
28148 1) We include all template parameters of A, not just some.
28149 2) The added constraint is same_type instead of deducible.
28151 I believe that while it's probably possible to construct a testcase that
28152 behaves differently with this simplification, it should have the same
28153 effect for real uses. Including all template parameters means that we
28154 deduce all parameters of A when resolving the call, so when we're in the
28155 constraint we don't need to deduce them again, we can just check whether
28156 the deduction produced the desired result. */
28158 tsubst_flags_t complain
= tf_warning_or_error
;
28159 tree atype
= TREE_TYPE (tmpl
);
28160 tree aguides
= NULL_TREE
;
28161 tree atparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
28162 unsigned natparms
= TREE_VEC_LENGTH (atparms
);
28163 tree utype
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28164 for (ovl_iterator
iter (uguides
); iter
; ++iter
)
28168 location_t loc
= DECL_SOURCE_LOCATION (f
);
28169 tree ret
= TREE_TYPE (TREE_TYPE (f
));
28171 if (TREE_CODE (f
) == TEMPLATE_DECL
)
28173 processing_template_decl_sentinel
ptds (/*reset*/false);
28174 ++processing_template_decl
;
28176 /* Deduce template arguments for f from the type-id of A. */
28177 tree ftparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f
));
28178 unsigned len
= TREE_VEC_LENGTH (ftparms
);
28179 tree targs
= make_tree_vec (len
);
28180 int err
= unify (ftparms
, targs
, ret
, utype
, UNIFY_ALLOW_NONE
, false);
28183 /* The number of parms for f' is the number of parms for A plus
28184 non-deduced parms of f. */
28185 unsigned ndlen
= 0;
28187 for (unsigned i
= 0; i
< len
; ++i
)
28188 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28190 tree gtparms
= make_tree_vec (natparms
+ ndlen
);
28192 /* First copy over the parms of A. */
28193 for (j
= 0; j
< natparms
; ++j
)
28194 TREE_VEC_ELT (gtparms
, j
) = TREE_VEC_ELT (atparms
, j
);
28195 /* Now rewrite the non-deduced parms of f. */
28196 for (unsigned i
= 0; ndlen
&& i
< len
; ++i
)
28197 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28200 unsigned index
= j
++;
28201 unsigned level
= 1;
28202 tree oldlist
= TREE_VEC_ELT (ftparms
, i
);
28203 tree list
= rewrite_tparm_list (oldlist
, index
, level
,
28204 targs
, i
, complain
);
28205 TREE_VEC_ELT (gtparms
, index
) = list
;
28207 gtparms
= build_tree_list (size_one_node
, gtparms
);
28209 /* Substitute the deduced arguments plus the rewritten template
28210 parameters into f to get g. This covers the type, copyness,
28211 guideness, and explicit-specifier. */
28212 tree g
= tsubst_decl (DECL_TEMPLATE_RESULT (f
), targs
, complain
);
28213 if (g
== error_mark_node
)
28214 return error_mark_node
;
28215 DECL_USE_TEMPLATE (g
) = 0;
28216 fprime
= build_template_decl (g
, gtparms
, false);
28217 DECL_TEMPLATE_RESULT (fprime
) = g
;
28218 TREE_TYPE (fprime
) = TREE_TYPE (g
);
28219 tree gtargs
= template_parms_to_args (gtparms
);
28220 DECL_TEMPLATE_INFO (g
) = build_template_info (fprime
, gtargs
);
28221 DECL_PRIMARY_TEMPLATE (fprime
) = fprime
;
28223 /* Substitute the associated constraints. */
28224 tree ci
= get_constraints (f
);
28226 ci
= tsubst_constraint_info (ci
, targs
, complain
, in_decl
);
28227 if (ci
== error_mark_node
)
28228 return error_mark_node
;
28230 /* Add a constraint that the return type matches the instantiation of
28231 A with the same template arguments. */
28232 ret
= TREE_TYPE (TREE_TYPE (fprime
));
28233 if (!same_type_p (atype
, ret
)
28234 /* FIXME this should mean they don't compare as equivalent. */
28235 || dependent_alias_template_spec_p (atype
, nt_opaque
))
28237 tree same
= finish_trait_expr (loc
, CPTK_IS_SAME_AS
, atype
, ret
);
28238 ci
= append_constraint (ci
, same
);
28242 set_constraints (fprime
, ci
);
28246 /* For a non-template deduction guide, if the arguments of A aren't
28247 deducible from the return type, don't add the candidate. */
28248 tree targs
= make_tree_vec (natparms
);
28249 int err
= unify (atparms
, targs
, utype
, ret
, UNIFY_ALLOW_NONE
, false);
28250 for (unsigned i
= 0; !err
&& i
< natparms
; ++i
)
28251 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
28257 aguides
= lookup_add (fprime
, aguides
);
28263 /* Return artificial deduction guides built from the constructors of class
28267 ctor_deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28269 tree type
= TREE_TYPE (tmpl
);
28270 tree outer_args
= NULL_TREE
;
28271 if (DECL_CLASS_SCOPE_P (tmpl
)
28272 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
28274 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
28275 type
= TREE_TYPE (most_general_template (tmpl
));
28278 tree cands
= NULL_TREE
;
28280 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
28282 /* Skip inherited constructors. */
28283 if (iter
.using_p ())
28286 tree guide
= build_deduction_guide (type
, *iter
, outer_args
, complain
);
28287 cands
= lookup_add (guide
, cands
);
28290 /* Add implicit default constructor deduction guide. */
28291 if (!TYPE_HAS_USER_CONSTRUCTOR (type
))
28293 tree guide
= build_deduction_guide (type
, type
, outer_args
,
28295 cands
= lookup_add (guide
, cands
);
28298 /* Add copy guide. */
28300 tree gtype
= build_reference_type (type
);
28301 tree guide
= build_deduction_guide (type
, gtype
, outer_args
,
28303 cands
= lookup_add (guide
, cands
);
28309 static GTY((deletable
)) hash_map
<tree
, tree_pair_p
> *dguide_cache
;
28311 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28312 aggregate candidate is added separately because it depends on the
28316 deduction_guides_for (tree tmpl
, tsubst_flags_t complain
)
28318 tree guides
= NULL_TREE
;
28319 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28321 tree under
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28322 tree tinfo
= get_template_info (under
);
28323 guides
= deduction_guides_for (TI_TEMPLATE (tinfo
), complain
);
28327 guides
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
),
28328 dguide_name (tmpl
),
28329 /*type*/false, /*complain*/false,
28331 if (guides
== error_mark_node
)
28332 guides
= NULL_TREE
;
28335 /* Cache the deduction guides for a template. We also remember the result of
28336 lookup, and rebuild everything if it changes; should be very rare. */
28337 tree_pair_p cache
= NULL
;
28339 = hash_map_safe_get_or_insert
<hm_ggc
> (dguide_cache
, tmpl
))
28342 if (cache
->purpose
== guides
)
28343 return cache
->value
;
28347 r
= cache
= ggc_cleared_alloc
<tree_pair_s
> ();
28348 cache
->purpose
= guides
;
28351 tree cands
= NULL_TREE
;
28352 if (DECL_ALIAS_TEMPLATE_P (tmpl
))
28353 cands
= alias_ctad_tweaks (tmpl
, guides
);
28356 cands
= ctor_deduction_guides_for (tmpl
, complain
);
28357 for (ovl_iterator
it (guides
); it
; ++it
)
28358 cands
= lookup_add (*it
, cands
);
28361 cache
->value
= cands
;
28365 /* Return whether TMPL is a (class template argument-) deducible template. */
28368 ctad_template_p (tree tmpl
)
28370 /* A deducible template is either a class template or is an alias template
28371 whose defining-type-id is of the form
28373 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28375 where the nested-name-specifier (if any) is non-dependent and the
28376 template-name of the simple-template-id names a deducible template. */
28378 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28379 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28381 if (!DECL_ALIAS_TEMPLATE_P (tmpl
))
28383 tree orig
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
28384 if (tree tinfo
= get_template_info (orig
))
28385 return ctad_template_p (TI_TEMPLATE (tinfo
));
28389 /* Deduce template arguments for the class template placeholder PTYPE for
28390 template TMPL based on the initializer INIT, and return the resulting
28394 do_class_deduction (tree ptype
, tree tmpl
, tree init
,
28395 int flags
, tsubst_flags_t complain
)
28397 /* We should have handled this in the caller. */
28398 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28401 /* Look through alias templates that just rename another template. */
28402 tmpl
= get_underlying_template (tmpl
);
28403 if (!ctad_template_p (tmpl
))
28405 if (complain
& tf_error
)
28406 error ("non-deducible template %qT used without template arguments", tmpl
);
28407 return error_mark_node
;
28409 else if (cxx_dialect
< cxx2a
&& DECL_ALIAS_TEMPLATE_P (tmpl
))
28411 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28412 if (complain
& tf_warning_or_error
)
28413 pedwarn (input_location
, 0, "alias template deduction only available "
28414 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28417 if (init
&& TREE_TYPE (init
) == ptype
)
28418 /* Using the template parm as its own argument. */
28421 tree type
= TREE_TYPE (tmpl
);
28423 bool try_list_ctor
= false;
28425 releasing_vec rv_args
= NULL
;
28426 vec
<tree
,va_gc
> *&args
= *&rv_args
;
28427 if (init
== NULL_TREE
)
28428 args
= make_tree_vector ();
28429 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28431 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
28432 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
28434 /* As an exception, the first phase in 16.3.1.7 (considering the
28435 initializer list as a single argument) is omitted if the
28436 initializer list consists of a single expression of type cv U,
28437 where U is a specialization of C or a class derived from a
28438 specialization of C. */
28439 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
28440 if (is_spec_or_derived (TREE_TYPE (elt
), tmpl
))
28441 try_list_ctor
= false;
28443 if (try_list_ctor
|| is_std_init_list (type
))
28444 args
= make_tree_vector_single (init
);
28446 args
= make_tree_vector_from_ctor (init
);
28448 else if (TREE_CODE (init
) == TREE_LIST
)
28449 args
= make_tree_vector_from_list (init
);
28451 args
= make_tree_vector_single (init
);
28453 /* Do this now to avoid problems with erroneous args later on. */
28454 args
= resolve_args (args
, complain
);
28456 return error_mark_node
;
28458 tree cands
= deduction_guides_for (tmpl
, complain
);
28459 if (cands
== error_mark_node
)
28460 return error_mark_node
;
28462 /* Prune explicit deduction guides in copy-initialization context. */
28463 bool elided
= false;
28464 if (flags
& LOOKUP_ONLYCONVERTING
)
28466 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
28467 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28472 /* Found a nonconverting guide, prune the candidates. */
28473 tree pruned
= NULL_TREE
;
28474 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28475 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
28476 pruned
= lookup_add (*iter
, pruned
);
28482 if (tree guide
= maybe_aggr_guide (tmpl
, init
, args
))
28483 cands
= lookup_add (guide
, cands
);
28485 tree call
= error_mark_node
;
28487 /* If this is list-initialization and the class has a list constructor, first
28488 try deducing from the list as a single argument, as [over.match.list]. */
28489 tree list_cands
= NULL_TREE
;
28490 if (try_list_ctor
&& cands
)
28491 for (lkp_iterator
iter (cands
); iter
; ++iter
)
28494 if (is_list_ctor (dg
))
28495 list_cands
= lookup_add (dg
, list_cands
);
28499 ++cp_unevaluated_operand
;
28500 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
28501 --cp_unevaluated_operand
;
28503 if (call
== error_mark_node
)
28505 /* That didn't work, now try treating the list as a sequence of
28507 release_tree_vector (args
);
28508 args
= make_tree_vector_from_ctor (init
);
28512 if (elided
&& !cands
)
28514 error ("cannot deduce template arguments for copy-initialization"
28515 " of %qT, as it has no non-explicit deduction guides or "
28516 "user-declared constructors", type
);
28517 return error_mark_node
;
28519 else if (!cands
&& call
== error_mark_node
)
28521 error ("cannot deduce template arguments of %qT, as it has no viable "
28522 "deduction guides", type
);
28523 return error_mark_node
;
28526 if (call
== error_mark_node
)
28528 ++cp_unevaluated_operand
;
28529 call
= build_new_function_call (cands
, &args
, tf_decltype
);
28530 --cp_unevaluated_operand
;
28533 if (call
== error_mark_node
28534 && (complain
& tf_warning_or_error
))
28536 error ("class template argument deduction failed:");
28538 ++cp_unevaluated_operand
;
28539 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
28540 --cp_unevaluated_operand
;
28543 inform (input_location
, "explicit deduction guides not considered "
28544 "for copy-initialization");
28547 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
28550 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28551 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28552 The CONTEXT determines the context in which auto deduction is performed
28553 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28554 OUTER_TARGS are used during template argument deduction
28555 (context == adc_unify) to properly substitute the result, and is ignored
28558 For partial-concept-ids, extra args may be appended to the list of deduced
28559 template arguments prior to determining constraint satisfaction. */
28562 do_auto_deduction (tree type
, tree init
, tree auto_node
,
28563 tsubst_flags_t complain
, auto_deduction_context context
,
28564 tree outer_targs
, int flags
)
28568 if (init
== error_mark_node
)
28569 return error_mark_node
;
28571 if (init
&& type_dependent_expression_p (init
)
28572 && context
!= adc_unify
)
28573 /* Defining a subset of type-dependent expressions that we can deduce
28574 from ahead of time isn't worth the trouble. */
28577 /* Similarly, we can't deduce from another undeduced decl. */
28578 if (init
&& undeduced_auto_decl (init
))
28581 /* We may be doing a partial substitution, but we still want to replace
28583 complain
&= ~tf_partial
;
28585 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
28586 /* C++17 class template argument deduction. */
28587 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
28589 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
28590 /* Nothing we can do with this, even in deduction context. */
28593 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28594 with either a new invented type template parameter U or, if the
28595 initializer is a braced-init-list (8.5.4), with
28596 std::initializer_list<U>. */
28597 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
28599 if (!DIRECT_LIST_INIT_P (init
))
28600 type
= listify_autos (type
, auto_node
);
28601 else if (CONSTRUCTOR_NELTS (init
) == 1)
28602 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
28605 if (complain
& tf_warning_or_error
)
28607 if (permerror (input_location
, "direct-list-initialization of "
28608 "%<auto%> requires exactly one element"))
28609 inform (input_location
,
28610 "for deduction to %<std::initializer_list%>, use copy-"
28611 "list-initialization (i.e. add %<=%> before the %<{%>)");
28613 type
= listify_autos (type
, auto_node
);
28617 if (type
== error_mark_node
)
28618 return error_mark_node
;
28620 init
= resolve_nondeduced_context (init
, complain
);
28622 if (context
== adc_decomp_type
28623 && auto_node
== type
28624 && init
!= error_mark_node
28625 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
28626 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28627 and initializer has array type, deduce cv-qualified array type. */
28628 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
28630 else if (AUTO_IS_DECLTYPE (auto_node
))
28632 tree stripped_init
= tree_strip_any_location_wrapper (init
);
28633 bool id
= (DECL_P (stripped_init
)
28634 || ((TREE_CODE (init
) == COMPONENT_REF
28635 || TREE_CODE (init
) == SCOPE_REF
)
28636 && !REF_PARENTHESIZED_P (init
)));
28637 targs
= make_tree_vec (1);
28638 TREE_VEC_ELT (targs
, 0)
28639 = finish_decltype_type (init
, id
, tf_warning_or_error
);
28640 if (type
!= auto_node
)
28642 if (complain
& tf_error
)
28643 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
28644 return error_mark_node
;
28649 if (error_operand_p (init
))
28650 return error_mark_node
;
28652 tree parms
= build_tree_list (NULL_TREE
, type
);
28656 tparms
= extract_autos (type
);
28659 tparms
= make_tree_vec (1);
28660 TREE_VEC_ELT (tparms
, 0)
28661 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
28664 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
28665 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28667 NULL
, /*explain_p=*/false);
28670 if (processing_template_decl
)
28671 /* Try again at instantiation time. */
28673 if (type
&& type
!= error_mark_node
28674 && (complain
& tf_error
))
28675 /* If type is error_mark_node a diagnostic must have been
28676 emitted by now. Also, having a mention to '<type error>'
28677 in the diagnostic is not really useful to the user. */
28680 && FNDECL_USED_AUTO (current_function_decl
)
28682 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
))
28683 && LAMBDA_FUNCTION_P (current_function_decl
))
28684 error ("unable to deduce lambda return type from %qE", init
);
28686 error ("unable to deduce %qT from %qE", type
, init
);
28687 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
28689 NULL
, /*explain_p=*/true);
28691 return error_mark_node
;
28695 /* Check any placeholder constraints against the deduced type. */
28696 if (flag_concepts
&& !processing_template_decl
)
28697 if (tree check
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
28699 /* Use the deduced type to check the associated constraints. If we
28700 have a partial-concept-id, rebuild the argument list so that
28701 we check using the extra arguments. */
28702 check
= unpack_concept_check (check
);
28703 gcc_assert (TREE_CODE (check
) == TEMPLATE_ID_EXPR
);
28704 tree
cdecl = TREE_OPERAND (check
, 0);
28706 cdecl = OVL_FIRST (cdecl);
28707 tree cargs
= TREE_OPERAND (check
, 1);
28708 if (TREE_VEC_LENGTH (cargs
) > 1)
28710 cargs
= copy_node (cargs
);
28711 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
28716 /* Rebuild the check using the deduced arguments. */
28717 check
= build_concept_check (cdecl, cargs
, tf_none
);
28719 if (!constraints_satisfied_p (check
))
28721 if (complain
& tf_warning_or_error
)
28723 auto_diagnostic_group d
;
28726 case adc_unspecified
:
28728 error("placeholder constraints not satisfied");
28730 case adc_variable_type
:
28731 case adc_decomp_type
:
28732 error ("deduced initializer does not satisfy "
28733 "placeholder constraints");
28735 case adc_return_type
:
28736 error ("deduced return type does not satisfy "
28737 "placeholder constraints");
28739 case adc_requirement
:
28740 error ("deduced expression type does not satisfy "
28741 "placeholder constraints");
28744 diagnose_constraints (input_location
, check
, targs
);
28746 return error_mark_node
;
28750 if (processing_template_decl
&& context
!= adc_unify
)
28751 outer_targs
= current_template_args ();
28752 targs
= add_to_template_args (outer_targs
, targs
);
28753 return tsubst (type
, targs
, complain
, NULL_TREE
);
28756 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28760 splice_late_return_type (tree type
, tree late_return_type
)
28762 if (is_auto (type
))
28764 if (late_return_type
)
28765 return late_return_type
;
28767 tree idx
= get_template_parm_index (type
);
28768 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
28769 /* In an abbreviated function template we didn't know we were dealing
28770 with a function template when we saw the auto return type, so update
28771 it to have the correct level. */
28772 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
28777 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28778 'decltype(auto)' or a deduced class template. */
28781 is_auto (const_tree type
)
28783 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
28784 && (TYPE_IDENTIFIER (type
) == auto_identifier
28785 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
28791 /* for_each_template_parm callback for type_uses_auto. */
28794 is_auto_r (tree tp
, void */
*data*/
)
28796 return is_auto (tp
);
28799 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28800 a use of `auto'. Returns NULL_TREE otherwise. */
28803 type_uses_auto (tree type
)
28805 if (type
== NULL_TREE
)
28807 else if (flag_concepts
)
28809 /* The Concepts TS allows multiple autos in one type-specifier; just
28810 return the first one we find, do_auto_deduction will collect all of
28812 if (uses_template_parms (type
))
28813 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
28814 /*visited*/NULL
, /*nondeduced*/false);
28819 return find_type_usage (type
, is_auto
);
28822 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28823 concepts are enabled, auto is acceptable in template arguments, but
28824 only when TEMPL identifies a template class. Return TRUE if any
28825 such errors were reported. */
28828 check_auto_in_tmpl_args (tree tmpl
, tree args
)
28830 /* If there were previous errors, nevermind. */
28831 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
28834 /* If TMPL is an identifier, we're parsing and we can't tell yet
28835 whether TMPL is supposed to be a type, a function or a variable.
28836 We'll only be able to tell during template substitution, so we
28837 expect to be called again then. If concepts are enabled and we
28838 know we have a type, we're ok. */
28840 && (identifier_p (tmpl
)
28842 && (DECL_TYPE_TEMPLATE_P (tmpl
)
28843 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
28846 /* Quickly search for any occurrences of auto; usually there won't
28847 be any, and then we'll avoid allocating the vector. */
28848 if (!type_uses_auto (args
))
28851 bool errors
= false;
28853 tree vec
= extract_autos (args
);
28854 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
28856 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
28857 error_at (DECL_SOURCE_LOCATION (xauto
),
28858 "invalid use of %qT in template argument", xauto
);
28865 /* For a given template T, return the vector of typedefs referenced
28866 in T for which access check is needed at T instantiation time.
28867 T is either a FUNCTION_DECL or a RECORD_TYPE.
28868 Those typedefs were added to T by the function
28869 append_type_to_template_for_access_check. */
28871 vec
<qualified_typedef_usage_t
, va_gc
> *
28872 get_types_needing_access_check (tree t
)
28875 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
28877 if (!t
|| t
== error_mark_node
)
28880 if (!(ti
= get_template_info (t
)))
28883 if (CLASS_TYPE_P (t
)
28884 || TREE_CODE (t
) == FUNCTION_DECL
)
28886 if (!TI_TEMPLATE (ti
))
28889 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
28895 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28896 tied to T. That list of typedefs will be access checked at
28897 T instantiation time.
28898 T is either a FUNCTION_DECL or a RECORD_TYPE.
28899 TYPE_DECL is a TYPE_DECL node representing a typedef.
28900 SCOPE is the scope through which TYPE_DECL is accessed.
28901 LOCATION is the location of the usage point of TYPE_DECL.
28903 This function is a subroutine of
28904 append_type_to_template_for_access_check. */
28907 append_type_to_template_for_access_check_1 (tree t
,
28910 location_t location
)
28912 qualified_typedef_usage_t typedef_usage
;
28915 if (!t
|| t
== error_mark_node
)
28918 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
28919 || CLASS_TYPE_P (t
))
28921 && TREE_CODE (type_decl
) == TYPE_DECL
28924 if (!(ti
= get_template_info (t
)))
28927 gcc_assert (TI_TEMPLATE (ti
));
28929 typedef_usage
.typedef_decl
= type_decl
;
28930 typedef_usage
.context
= scope
;
28931 typedef_usage
.locus
= location
;
28933 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
28936 /* Append TYPE_DECL to the template TEMPL.
28937 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28938 At TEMPL instanciation time, TYPE_DECL will be checked to see
28939 if it can be accessed through SCOPE.
28940 LOCATION is the location of the usage point of TYPE_DECL.
28942 e.g. consider the following code snippet:
28949 template<class U> struct S
28951 C::myint mi; // <-- usage point of the typedef C::myint
28956 At S<char> instantiation time, we need to check the access of C::myint
28957 In other words, we need to check the access of the myint typedef through
28958 the C scope. For that purpose, this function will add the myint typedef
28959 and the scope C through which its being accessed to a list of typedefs
28960 tied to the template S. That list will be walked at template instantiation
28961 time and access check performed on each typedefs it contains.
28962 Note that this particular code snippet should yield an error because
28963 myint is private to C. */
28966 append_type_to_template_for_access_check (tree templ
,
28969 location_t location
)
28971 qualified_typedef_usage_t
*iter
;
28974 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
28976 /* Make sure we don't append the type to the template twice. */
28977 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
28978 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
28981 append_type_to_template_for_access_check_1 (templ
, type_decl
,
28985 /* Recursively walk over && expressions searching for EXPR. Return a reference
28986 to that expression. */
28988 static tree
*find_template_requirement (tree
*t
, tree key
)
28992 if (TREE_CODE (*t
) == TRUTH_ANDIF_EXPR
)
28994 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 0), key
))
28996 if (tree
*p
= find_template_requirement (&TREE_OPERAND (*t
, 1), key
))
29002 /* Convert the generic type parameters in PARM that match the types given in the
29003 range [START_IDX, END_IDX) from the current_template_parms into generic type
29007 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
29009 tree current
= current_template_parms
;
29010 int depth
= TMPL_PARMS_DEPTH (current
);
29011 current
= INNERMOST_TEMPLATE_PARMS (current
);
29012 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
29014 for (int i
= 0; i
< start_idx
; ++i
)
29015 TREE_VEC_ELT (replacement
, i
)
29016 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29018 for (int i
= start_idx
; i
< end_idx
; ++i
)
29020 /* Create a distinct parameter pack type from the current parm and add it
29021 to the replacement args to tsubst below into the generic function
29023 tree node
= TREE_VEC_ELT (current
, i
);
29024 tree o
= TREE_TYPE (TREE_VALUE (node
));
29025 tree t
= copy_type (o
);
29026 TEMPLATE_TYPE_PARM_INDEX (t
)
29027 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
29029 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
29030 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
29031 TYPE_MAIN_VARIANT (t
) = t
;
29032 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
29033 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
29034 TREE_VEC_ELT (replacement
, i
) = t
;
29036 /* Replace the current template parameter with new pack. */
29037 TREE_VALUE (node
) = TREE_CHAIN (t
);
29039 /* Surgically adjust the associated constraint of adjusted parameter
29040 and it's corresponding contribution to the current template
29042 if (tree constr
= TEMPLATE_PARM_CONSTRAINTS (node
))
29044 tree id
= unpack_concept_check (constr
);
29045 TREE_VEC_ELT (TREE_OPERAND (id
, 1), 0) = template_parm_to_arg (t
);
29046 tree fold
= finish_left_unary_fold_expr (constr
, TRUTH_ANDIF_EXPR
);
29047 TEMPLATE_PARM_CONSTRAINTS (node
) = fold
;
29049 /* If there was a constraint, we also need to replace that in
29050 the template requirements, which we've already built. */
29051 tree
*reqs
= &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
29052 reqs
= find_template_requirement (reqs
, constr
);
29057 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
29058 TREE_VEC_ELT (replacement
, i
)
29059 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
29061 /* If there are more levels then build up the replacement with the outer
29064 replacement
= add_to_template_args (template_parms_to_args
29065 (TREE_CHAIN (current_template_parms
)),
29068 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
29071 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29075 declare_integer_pack (void)
29077 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
29078 build_function_type_list (integer_type_node
,
29081 NULL_TREE
, ECF_CONST
);
29082 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
29083 set_decl_built_in_function (ipfn
, BUILT_IN_FRONTEND
,
29084 CP_BUILT_IN_INTEGER_PACK
);
29087 /* Set up the hash tables for template instantiations. */
29090 init_template_processing (void)
29092 /* FIXME: enable sanitization (PR87847) */
29093 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
29094 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37, false);
29096 if (cxx_dialect
>= cxx11
)
29097 declare_integer_pack ();
29100 /* Print stats about the template hash tables for -fstats. */
29103 print_template_statistics (void)
29105 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
29106 "%f collisions\n", (long) decl_specializations
->size (),
29107 (long) decl_specializations
->elements (),
29108 decl_specializations
->collisions ());
29109 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
29110 "%f collisions\n", (long) type_specializations
->size (),
29111 (long) type_specializations
->elements (),
29112 type_specializations
->collisions ());
29117 namespace selftest
{
29119 /* Verify that build_non_dependent_expr () works, for various expressions,
29120 and that location wrappers don't affect the results. */
29123 test_build_non_dependent_expr ()
29125 location_t loc
= BUILTINS_LOCATION
;
29127 /* Verify constants, without and with location wrappers. */
29128 tree int_cst
= build_int_cst (integer_type_node
, 42);
29129 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
29131 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
29132 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
29133 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
29135 tree string_lit
= build_string (4, "foo");
29136 TREE_TYPE (string_lit
) = char_array_type_node
;
29137 string_lit
= fix_string_type (string_lit
);
29138 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
29140 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
29141 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
29142 ASSERT_EQ (wrapped_string_lit
,
29143 build_non_dependent_expr (wrapped_string_lit
));
29146 /* Verify that type_dependent_expression_p () works correctly, even
29147 in the presence of location wrapper nodes. */
29150 test_type_dependent_expression_p ()
29152 location_t loc
= BUILTINS_LOCATION
;
29154 tree name
= get_identifier ("foo");
29156 /* If no templates are involved, nothing is type-dependent. */
29157 gcc_assert (!processing_template_decl
);
29158 ASSERT_FALSE (type_dependent_expression_p (name
));
29160 ++processing_template_decl
;
29162 /* Within a template, an unresolved name is always type-dependent. */
29163 ASSERT_TRUE (type_dependent_expression_p (name
));
29165 /* Ensure it copes with NULL_TREE and errors. */
29166 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
29167 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
29169 /* A USING_DECL in a template should be type-dependent, even if wrapped
29170 with a location wrapper (PR c++/83799). */
29171 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
29172 TREE_TYPE (using_decl
) = integer_type_node
;
29173 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
29174 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
29175 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
29176 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
29178 --processing_template_decl
;
29181 /* Run all of the selftests within this file. */
29186 test_build_non_dependent_expr ();
29187 test_type_dependent_expression_p ();
29190 } // namespace selftest
29192 #endif /* #if CHECKING_P */
29194 #include "gt-cp-pt.h"