1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
81 local_specialization_stack::local_specialization_stack (lss_policy policy
)
82 : saved (local_specializations
)
84 if (policy
== lss_blank
|| !saved
)
85 local_specializations
= new hash_map
<tree
, tree
>;
87 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations
;
93 local_specializations
= saved
;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth
;
99 struct GTY((for_user
)) spec_entry
106 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
108 static hashval_t
hash (spec_entry
*);
109 static bool equal (spec_entry
*, spec_entry
*);
112 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
114 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result
{
133 tbr_ambiguous_baseclass
,
137 static void push_access_scope (tree
);
138 static void pop_access_scope (tree
);
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
153 static void tsubst_enum (tree
, tree
, tree
);
154 static tree
add_to_template_args (tree
, tree
);
155 static tree
add_outermost_template_args (tree
, tree
);
156 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
157 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
, int,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static void template_parm_level_and_index (tree
, int*, int*);
184 static int unify_pack_expansion (tree
, tree
, tree
,
185 tree
, unification_kind_t
, bool, bool);
186 static tree
copy_template_args (tree
);
187 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
189 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
190 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
191 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
192 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
193 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
194 static bool check_specialization_scope (void);
195 static tree
process_partial_specialization (tree
);
196 static void set_current_access_from_decl (tree
);
197 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
199 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
200 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
202 static bool template_template_parm_bindings_ok_p (tree
, tree
);
203 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
204 static tree
for_each_template_parm_r (tree
*, int *, void *);
205 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
206 static void copy_default_args_to_explicit_spec (tree
);
207 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
208 static bool dependent_template_arg_p (tree
);
209 static bool any_template_arguments_need_structural_equality_p (tree
);
210 static bool dependent_type_p_r (tree
);
211 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
212 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
213 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
214 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
216 static tree
listify (tree
);
217 static tree
listify_autos (tree
, tree
);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
220 static bool complex_alias_template_p (const_tree tmpl
);
221 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
222 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
223 static tree
make_argument_pack (tree
);
224 static void register_parameter_specializations (tree
, tree
);
225 static tree
enclosing_instantiation_of (tree tctx
);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
233 push_access_scope (tree t
)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
236 || TREE_CODE (t
) == TYPE_DECL
);
238 if (DECL_FRIEND_CONTEXT (t
))
239 push_nested_class (DECL_FRIEND_CONTEXT (t
));
240 else if (DECL_CLASS_SCOPE_P (t
))
241 push_nested_class (DECL_CONTEXT (t
));
243 push_to_top_level ();
245 if (TREE_CODE (t
) == FUNCTION_DECL
)
247 saved_access_scope
= tree_cons
248 (NULL_TREE
, current_function_decl
, saved_access_scope
);
249 current_function_decl
= t
;
253 /* Restore the scope set up by push_access_scope. T is the node we
257 pop_access_scope (tree t
)
259 if (TREE_CODE (t
) == FUNCTION_DECL
)
261 current_function_decl
= TREE_VALUE (saved_access_scope
);
262 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
265 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
277 finish_member_template_decl (tree decl
)
279 if (decl
== error_mark_node
)
280 return error_mark_node
;
282 gcc_assert (DECL_P (decl
));
284 if (TREE_CODE (decl
) == TYPE_DECL
)
288 type
= TREE_TYPE (decl
);
289 if (type
== error_mark_node
)
290 return error_mark_node
;
291 if (MAYBE_CLASS_TYPE_P (type
)
292 && CLASSTYPE_TEMPLATE_INFO (type
)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
295 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
296 check_member_template (tmpl
);
301 else if (TREE_CODE (decl
) == FIELD_DECL
)
302 error ("data member %qD cannot be a member template", decl
);
303 else if (DECL_TEMPLATE_INFO (decl
))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
307 check_member_template (DECL_TI_TEMPLATE (decl
));
308 return DECL_TI_TEMPLATE (decl
);
314 error ("invalid member template declaration %qD", decl
);
316 return error_mark_node
;
319 /* Create a template info node. */
322 build_template_info (tree template_decl
, tree template_args
)
324 tree result
= make_node (TEMPLATE_INFO
);
325 TI_TEMPLATE (result
) = template_decl
;
326 TI_ARGS (result
) = template_args
;
330 /* Return the template info node corresponding to T, whatever T is. */
333 get_template_info (const_tree t
)
335 tree tinfo
= NULL_TREE
;
337 if (!t
|| t
== error_mark_node
)
340 if (TREE_CODE (t
) == NAMESPACE_DECL
341 || TREE_CODE (t
) == PARM_DECL
)
344 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
345 tinfo
= DECL_TEMPLATE_INFO (t
);
347 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
350 if (OVERLOAD_TYPE_P (t
))
351 tinfo
= TYPE_TEMPLATE_INFO (t
);
352 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
353 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
358 /* Returns the template nesting level of the indicated class TYPE.
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
377 template_class_depth (tree type
)
381 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
383 tree tinfo
= get_template_info (type
);
385 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
390 type
= CP_DECL_CONTEXT (type
);
391 else if (LAMBDA_TYPE_P (type
))
392 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
394 type
= CP_TYPE_CONTEXT (type
);
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
404 inline_needs_template_parms (tree decl
, bool nsdmi
)
406 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
410 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
419 push_inline_template_parms_recursive (tree parmlist
, int levels
)
421 tree parms
= TREE_VALUE (parmlist
);
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
427 ++processing_template_decl
;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl
),
430 parms
, current_template_parms
);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
435 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
437 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
439 if (error_operand_p (parm
))
442 gcc_assert (DECL_P (parm
));
444 switch (TREE_CODE (parm
))
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
467 maybe_begin_member_template_processing (tree decl
)
471 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
475 tree ctx
= DECL_CONTEXT (decl
);
476 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx
)
479 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
482 if (inline_needs_template_parms (decl
, nsdmi
))
484 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
485 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
490 parms
= TREE_CHAIN (parms
);
493 push_inline_template_parms_recursive (parms
, levels
);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels
.safe_push (levels
);
501 /* Undo the effects of maybe_begin_member_template_processing. */
504 maybe_end_member_template_processing (void)
509 if (inline_parm_levels
.length () == 0)
512 last
= inline_parm_levels
.pop ();
513 for (i
= 0; i
< last
; ++i
)
515 --processing_template_decl
;
516 current_template_parms
= TREE_CHAIN (current_template_parms
);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
525 add_to_template_args (tree args
, tree extra_args
)
532 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
535 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
536 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
538 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
539 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
541 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
555 add_outermost_template_args (tree args
, tree extra_args
)
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
565 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
571 new_args
= add_to_template_args (args
, extra_args
);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
579 /* Return the N levels of innermost template arguments from the ARGS. */
582 get_innermost_template_args (tree args
, int n
)
590 /* If N is 1, just return the innermost set of template arguments. */
592 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
594 /* If we're not removing anything, just return the arguments we were
596 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
597 gcc_assert (extra_levels
>= 0);
598 if (extra_levels
== 0)
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args
= make_tree_vec (n
);
603 for (i
= 1; i
<= n
; ++i
)
604 SET_TMPL_ARGS_LEVEL (new_args
, i
,
605 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
614 strip_innermost_template_args (tree args
, int extra_levels
)
617 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
622 /* If N is 1, just return the outermost set of template arguments. */
624 return TMPL_ARGS_LEVEL (args
, 1);
626 /* If we're not removing anything, just return the arguments we were
628 gcc_assert (extra_levels
>= 0);
629 if (extra_levels
== 0)
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args
= make_tree_vec (n
);
634 for (i
= 1; i
<= n
; ++i
)
635 SET_TMPL_ARGS_LEVEL (new_args
, i
,
636 TMPL_ARGS_LEVEL (args
, i
));
641 /* We've got a template header coming up; push to a new level for storing
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms
, NULL
);
662 ++processing_template_decl
;
663 ++processing_template_parmlist
;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl
),
670 current_template_parms
);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
678 check_specialization_scope (void)
680 tree scope
= current_scope ();
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
691 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
693 error ("explicit specialization in non-namespace scope %qD", scope
);
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms
)
707 error ("enclosing class templates are not explicitly specialized");
714 /* We've just seen template <>. */
717 begin_specialization (void)
719 begin_scope (sk_template_spec
, NULL
);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
728 end_specialization (void)
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
738 reset_specialization (void)
740 processing_specialization
= 0;
741 template_header_count
= 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
748 note_template_header (int specialization
)
750 processing_specialization
= specialization
;
751 template_header_count
++;
754 /* We're beginning an explicit instantiation. */
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation
);
760 processing_explicit_instantiation
= true;
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation
);
768 processing_explicit_instantiation
= false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
777 check_specialization_namespace (tree tmpl
)
779 tree tpl_ns
= decl_namespace_context (tmpl
);
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl
)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl
);
795 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
796 /* Same or enclosing namespace. */
800 permerror (input_location
,
801 "specialization of %qD in different namespace", tmpl
);
802 inform (DECL_SOURCE_LOCATION (tmpl
),
803 " from definition of %q#D", tmpl
);
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
812 check_explicit_instantiation_namespace (tree spec
)
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns
= decl_namespace_context (spec
);
819 if (!is_nested_namespace (current_namespace
, ns
))
820 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec
, current_namespace
, ns
);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
829 maybe_new_partial_specialization (tree type
)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
837 // template<typename T>
840 // Here, S<T*> is an implicit instantiation of S whose type
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
863 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
864 tree args
= CLASSTYPE_TI_ARGS (type
);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms
)
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr
= current_template_constraints ();
879 if (type
== TREE_TYPE (tmpl
))
881 tree main_constr
= get_constraints (tmpl
);
882 if (equivalent_constraints (type_constr
, main_constr
))
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
891 tree spec_tmpl
= TREE_VALUE (specs
);
892 tree spec_args
= TREE_PURPOSE (specs
);
893 tree spec_constr
= get_constraints (spec_tmpl
);
894 if (comp_template_args (args
, spec_args
)
895 && equivalent_constraints (type_constr
, spec_constr
))
897 specs
= TREE_CHAIN (specs
);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t
= make_class_type (TREE_CODE (type
));
903 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
904 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
911 // Build the corresponding type decl.
912 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
913 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
914 DECL_SOURCE_LOCATION (d
) = input_location
;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
926 maybe_process_partial_specialization (tree type
)
930 if (type
== error_mark_node
)
931 return error_mark_node
;
933 /* A lambda that appears in specialization context is not itself a
935 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
938 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
940 error ("name of class shadows template template parameter %qD",
942 return error_mark_node
;
945 context
= TYPE_CONTEXT (type
);
947 if (TYPE_ALIAS_P (type
))
949 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
951 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo
));
955 error ("explicit specialization of non-template %qT", type
);
956 return error_mark_node
;
958 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t
= maybe_new_partial_specialization (type
))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
974 && !at_namespace_scope_p ())
975 return error_mark_node
;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
978 if (processing_template_decl
)
980 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
981 if (decl
== error_mark_node
)
982 return error_mark_node
;
983 return TREE_TYPE (decl
);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
987 error ("specialization of %qT after instantiation", type
);
988 else if (errorcount
&& !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node
;
996 else if (CLASS_TYPE_P (type
)
997 && !CLASSTYPE_USE_TEMPLATE (type
)
998 && CLASSTYPE_TEMPLATE_INFO (type
)
999 && context
&& CLASS_TYPE_P (context
)
1000 && CLASSTYPE_TEMPLATE_INFO (context
))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1019 && !COMPLETE_TYPE_P (type
))
1022 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl
))
1027 permerror (input_location
,
1028 "specializing %q#T in different namespace", type
);
1029 permerror (DECL_SOURCE_LOCATION (tmpl
),
1030 " from definition of %q#D", tmpl
);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1039 t
; t
= TREE_CHAIN (t
))
1041 tree inst
= TREE_VALUE (t
);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1052 elt
.tmpl
= most_general_template (tmpl
);
1053 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1056 type_specializations
->remove_elt (&elt
);
1059 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1062 = type_specializations
->find_slot (&elt
, INSERT
);
1063 entry
= ggc_alloc
<spec_entry
> ();
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1079 CLASSTYPE_TI_ARGS (type
)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1083 else if (processing_specialization
)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1088 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1089 "of %qD not allowed by ISO C++", type
);
1092 error ("explicit specialization of non-template %qT", type
);
1093 return error_mark_node
;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1105 optimize_specialization_lookup_p (tree tmpl
)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1108 && DECL_CLASS_SCOPE_P (tmpl
)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1119 && !DECL_CONV_FN_P (tmpl
)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1137 verify_unstripped_args (tree args
)
1139 ++processing_template_decl
;
1140 if (!any_dependent_template_arguments_p (args
))
1142 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1143 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1145 tree arg
= TREE_VEC_ELT (inner
, i
);
1146 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1148 else if (TYPE_P (arg
))
1149 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1150 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1154 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1157 --processing_template_decl
;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1175 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1177 if (tmpl
== NULL_TREE
)
1180 if (args
== error_mark_node
)
1183 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl
) == FIELD_DECL
);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args
)
1189 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1191 : template_class_depth (DECL_CONTEXT (tmpl
))));
1194 verify_unstripped_args (args
);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl
))
1201 if (optimize_specialization_lookup_p (tmpl
))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1206 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1207 tree class_specialization
1208 = retrieve_specialization (class_template
, args
, 0);
1209 if (!class_specialization
)
1212 /* Find the instance of TMPL. */
1213 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1214 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1217 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn
) == class_specialization
)
1229 hash_table
<spec_hasher
> *specializations
;
1233 elt
.spec
= NULL_TREE
;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1236 specializations
= type_specializations
;
1238 specializations
= decl_specializations
;
1241 hash
= spec_hasher::hash (&elt
);
1242 found
= specializations
->find_with_hash (&elt
, hash
);
1250 /* Like retrieve_specialization, but for local declarations. */
1253 retrieve_local_specialization (tree tmpl
)
1255 if (local_specializations
== NULL
)
1258 tree
*slot
= local_specializations
->get (tmpl
);
1259 return slot
? *slot
: NULL_TREE
;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl
, tree tmpl
)
1269 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1273 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1279 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1281 for (t
= TREE_TYPE (decl
);
1283 t
= CLASSTYPE_USE_TEMPLATE (t
)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1285 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1296 is_specialization_of_friend (tree decl
, tree friend_decl
)
1298 bool need_template
= true;
1301 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1302 || TREE_CODE (decl
) == TYPE_DECL
);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1307 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl
)
1309 && !DECL_USE_TEMPLATE (friend_decl
))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1313 need_template
= false;
1315 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl
))
1317 need_template
= false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1323 if (is_specialization_of (decl
, friend_decl
))
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1348 && DECL_CLASS_SCOPE_P (decl
)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context
= DECL_CONTEXT (decl
);
1369 tree args
= NULL_TREE
;
1370 int current_depth
= 0;
1372 while (current_depth
< template_depth
)
1374 if (CLASSTYPE_TEMPLATE_INFO (context
))
1376 if (current_depth
== 0)
1377 args
= TYPE_TI_ARGS (context
);
1379 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1382 context
= TYPE_CONTEXT (context
);
1385 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1390 tree friend_args_type
;
1391 tree decl_args_type
;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1395 is_template
= DECL_TEMPLATE_INFO (decl
)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1397 if (need_template
^ is_template
)
1399 else if (is_template
)
1401 /* If both are templates, check template parameter list. */
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1410 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1413 decl_type
= TREE_TYPE (decl
);
1415 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1416 tf_none
, NULL_TREE
);
1417 if (friend_type
== error_mark_node
)
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1427 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1429 friend_args_type
= TREE_CHAIN (friend_args_type
);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1431 decl_args_type
= TREE_CHAIN (decl_args_type
);
1433 return compparms (decl_args_type
, friend_args_type
);
1437 /* DECL is a TYPE_DECL */
1439 tree decl_type
= TREE_TYPE (decl
);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1447 if (need_template
^ is_template
)
1449 else if (is_template
)
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1455 != DECL_NAME (friend_decl
))
1458 /* Now check template parameter list. */
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1467 return (DECL_NAME (decl
)
1468 == DECL_NAME (friend_decl
));
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1483 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1487 spec_entry
**slot
= NULL
;
1490 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1491 || (TREE_CODE (tmpl
) == FIELD_DECL
1492 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1494 if (TREE_CODE (spec
) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec
)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1509 if (optimize_specialization_lookup_p (tmpl
))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn
= retrieve_specialization (tmpl
, args
, 0);
1520 hash
= spec_hasher::hash (&elt
);
1523 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1525 fn
= ((spec_entry
*) *slot
)->spec
;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1537 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1541 if (DECL_ODR_USED (fn
)
1542 || DECL_EXPLICIT_INSTANTIATION (fn
))
1544 error ("specialization of %qD after instantiation",
1546 return error_mark_node
;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn
) = NULL_TREE
;
1575 duplicate_decls (spec
, fn
, is_friend
);
1576 /* The call to duplicate_decls will have applied
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone
, fn
)
1588 DECL_DECLARED_INLINE_P (clone
)
1589 = DECL_DECLARED_INLINE_P (fn
);
1590 DECL_SOURCE_LOCATION (clone
)
1591 = DECL_SOURCE_LOCATION (fn
);
1592 DECL_DELETED_FN (clone
)
1593 = DECL_DELETED_FN (fn
);
1595 check_specialization_namespace (tmpl
);
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1602 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1603 if (dd
== error_mark_node
)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node
;
1607 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1611 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1617 return duplicate_decls (spec
, fn
, is_friend
);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1622 && !check_specialization_namespace (tmpl
))
1623 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1625 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1627 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1628 gcc_assert (tmpl
&& args
&& spec
);
1631 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1632 && PRIMARY_TEMPLATE_P (tmpl
)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1634 || variable_template_p (tmpl
))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1643 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1651 int comparing_specializations
;
1654 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1658 ++comparing_specializations
;
1659 equal
= (e1
->tmpl
== e2
->tmpl
1660 && comp_template_args (e1
->args
, e2
->args
));
1661 if (equal
&& flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1665 && uses_template_parms (e1
->args
))
1667 /* Partial specializations of a variable template can be distinguished by
1669 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1670 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1671 equal
= equivalent_constraints (c1
, c2
);
1673 --comparing_specializations
;
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1681 hash_tmpl_and_args (tree tmpl
, tree args
)
1683 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1684 return iterative_hash_template_arg (args
, val
);
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1691 spec_hasher::hash (spec_entry
*e
)
1693 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1700 iterative_hash_template_arg (tree arg
, hashval_t val
)
1702 unsigned HOST_WIDE_INT i
;
1703 enum tree_code code
;
1706 if (arg
== NULL_TREE
)
1707 return iterative_hash_object (arg
, val
);
1712 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1715 code
= TREE_CODE (arg
);
1716 tclass
= TREE_CODE_CLASS (code
);
1718 val
= iterative_hash_object (code
, val
);
1725 case IDENTIFIER_NODE
:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1730 int i
, len
= TREE_VEC_LENGTH (arg
);
1731 for (i
= 0; i
< len
; ++i
)
1732 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1736 case TYPE_PACK_EXPANSION
:
1737 case EXPR_PACK_EXPANSION
:
1738 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1741 case TYPE_ARGUMENT_PACK
:
1742 case NONTYPE_ARGUMENT_PACK
:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1746 for (; arg
; arg
= TREE_CHAIN (arg
))
1747 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1751 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1752 val
= iterative_hash_template_arg (*iter
, val
);
1758 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1761 val
= iterative_hash_template_arg (field
, val
);
1762 val
= iterative_hash_template_arg (value
, val
);
1768 if (!DECL_ARTIFICIAL (arg
))
1770 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1771 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1773 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1779 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1782 case TEMPLATE_PARM_INDEX
:
1783 val
= iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1785 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1789 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1790 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1794 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1800 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1801 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1802 val
= iterative_hash_object (code
, val
);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1806 /* A lambda can't appear in a template arg, but don't crash on
1808 gcc_assert (seen_error ());
1812 case IMPLICIT_CONV_EXPR
:
1813 case STATIC_CAST_EXPR
:
1814 case REINTERPRET_CAST_EXPR
:
1815 case CONST_CAST_EXPR
:
1816 case DYNAMIC_CAST_EXPR
:
1818 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1819 /* Now hash operands as usual. */
1829 if (alias_template_specialization_p (arg
))
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1841 if (TYPE_CANONICAL (arg
))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1844 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1846 /* Otherwise just compare the types during lookup. */
1849 case tcc_declaration
:
1851 return iterative_hash_expr (arg
, val
);
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1856 unsigned n
= cp_tree_operand_length (arg
);
1857 for (i
= 0; i
< n
; ++i
)
1858 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1873 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1878 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1879 elt
.args
= TI_ARGS (tinfo
);
1880 elt
.spec
= NULL_TREE
;
1882 entry
= decl_specializations
->find (&elt
);
1885 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1886 gcc_assert (new_spec
!= NULL_TREE
);
1887 entry
->spec
= new_spec
;
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1898 register_local_specialization (tree spec
, tree tmpl
)
1900 gcc_assert (tmpl
!= spec
);
1901 local_specializations
->put (tmpl
, spec
);
1904 /* TYPE is a class type. Returns true if TYPE is an explicitly
1905 specialized class. */
1908 explicit_class_specialization_p (tree type
)
1910 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1912 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1915 /* Print the list of functions at FNS, going through all the overloads
1916 for each element of the list. Alternatively, FNS can not be a
1917 TREE_LIST, in which case it will be printed together with all the
1920 MORE and *STR should respectively be FALSE and NULL when the function
1921 is called from the outside. They are used internally on recursive
1922 calls. print_candidates manages the two parameters and leaves NULL
1923 in *STR when it ends. */
1926 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1928 if (TREE_CODE (fns
) == TREE_LIST
)
1929 for (; fns
; fns
= TREE_CHAIN (fns
))
1930 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1932 for (lkp_iterator
iter (fns
); iter
;)
1937 const char *pfx
= *str
;
1941 pfx
= _("candidates are:");
1943 pfx
= _("candidate is:");
1944 *str
= get_spaces (pfx
);
1946 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1950 /* Print the list of candidate FNS in an error message. FNS can also
1951 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1954 print_candidates (tree fns
)
1957 print_candidates_1 (fns
, &str
);
1961 /* Get a (possibly) constrained template declaration for the
1962 purpose of ordering candidates. */
1964 get_template_for_ordering (tree list
)
1966 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1967 tree f
= TREE_VALUE (list
);
1968 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1969 return TI_TEMPLATE (ti
);
1973 /* Among candidates having the same signature, return the
1974 most constrained or NULL_TREE if there is no best candidate.
1975 If the signatures of candidates vary (e.g., template
1976 specialization vs. member function), then there can be no
1979 Note that we don't compare constraints on the functions
1980 themselves, but rather those of their templates. */
1982 most_constrained_function (tree candidates
)
1984 // Try to find the best candidate in a first pass.
1985 tree champ
= candidates
;
1986 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1988 int winner
= more_constrained (get_template_for_ordering (champ
),
1989 get_template_for_ordering (c
));
1991 champ
= c
; // The candidate is more constrained
1992 else if (winner
== 0)
1993 return NULL_TREE
; // Neither is more constrained
1996 // Verify that the champ is better than previous candidates.
1997 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
1998 if (!more_constrained (get_template_for_ordering (champ
),
1999 get_template_for_ordering (c
)))
2007 /* Returns the template (one of the functions given by TEMPLATE_ID)
2008 which can be specialized to match the indicated DECL with the
2009 explicit template args given in TEMPLATE_ID. The DECL may be
2010 NULL_TREE if none is available. In that case, the functions in
2011 TEMPLATE_ID are non-members.
2013 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2014 specialization of a member template.
2016 The TEMPLATE_COUNT is the number of references to qualifying
2017 template classes that appeared in the name of the function. See
2018 check_explicit_specialization for a more accurate description.
2020 TSK indicates what kind of template declaration (if any) is being
2021 declared. TSK_TEMPLATE indicates that the declaration given by
2022 DECL, though a FUNCTION_DECL, has template parameters, and is
2023 therefore a template function.
2025 The template args (those explicitly specified and those deduced)
2026 are output in a newly created vector *TARGS_OUT.
2028 If it is impossible to determine the result, an error message is
2029 issued. The error_mark_node is returned to indicate failure. */
2032 determine_specialization (tree template_id
,
2035 int need_member_template
,
2041 tree explicit_targs
;
2042 tree candidates
= NULL_TREE
;
2044 /* A TREE_LIST of templates of which DECL may be a specialization.
2045 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2046 corresponding TREE_PURPOSE is the set of template arguments that,
2047 when used to instantiate the template, would produce a function
2048 with the signature of DECL. */
2049 tree templates
= NULL_TREE
;
2051 cp_binding_level
*b
;
2053 *targs_out
= NULL_TREE
;
2055 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2056 return error_mark_node
;
2058 /* We shouldn't be specializing a member template of an
2059 unspecialized class template; we already gave an error in
2060 check_specialization_scope, now avoid crashing. */
2061 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2062 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2064 gcc_assert (errorcount
);
2065 return error_mark_node
;
2068 fns
= TREE_OPERAND (template_id
, 0);
2069 explicit_targs
= TREE_OPERAND (template_id
, 1);
2071 if (fns
== error_mark_node
)
2072 return error_mark_node
;
2074 /* Check for baselinks. */
2075 if (BASELINK_P (fns
))
2076 fns
= BASELINK_FUNCTIONS (fns
);
2078 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2080 error ("%qD is not a function template", fns
);
2081 return error_mark_node
;
2083 else if (VAR_P (decl
) && !variable_template_p (fns
))
2085 error ("%qD is not a variable template", fns
);
2086 return error_mark_node
;
2089 /* Count the number of template headers specified for this
2092 for (b
= current_binding_level
;
2093 b
->kind
== sk_template_parms
;
2097 tree orig_fns
= fns
;
2099 if (variable_template_p (fns
))
2101 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2102 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2103 tf_warning_or_error
,
2104 /*req_all*/true, /*use_defarg*/true);
2105 if (targs
!= error_mark_node
)
2106 templates
= tree_cons (targs
, fns
, templates
);
2108 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2112 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2114 tree decl_arg_types
;
2118 /* In case of explicit specialization, we need to check if
2119 the number of template headers appearing in the specialization
2120 is correct. This is usually done in check_explicit_specialization,
2121 but the check done there cannot be exhaustive when specializing
2122 member functions. Consider the following code:
2124 template <> void A<int>::f(int);
2125 template <> template <> void A<int>::f(int);
2127 Assuming that A<int> is not itself an explicit specialization
2128 already, the first line specializes "f" which is a non-template
2129 member function, whilst the second line specializes "f" which
2130 is a template member function. So both lines are syntactically
2131 correct, and check_explicit_specialization does not reject
2134 Here, we can do better, as we are matching the specialization
2135 against the declarations. We count the number of template
2136 headers, and we check if they match TEMPLATE_COUNT + 1
2137 (TEMPLATE_COUNT is the number of qualifying template classes,
2138 plus there must be another header for the member template
2141 Notice that if header_count is zero, this is not a
2142 specialization but rather a template instantiation, so there
2143 is no check we can perform here. */
2144 if (header_count
&& header_count
!= template_count
+ 1)
2147 /* Check that the number of template arguments at the
2148 innermost level for DECL is the same as for FN. */
2149 if (current_binding_level
->kind
== sk_template_parms
2150 && !current_binding_level
->explicit_spec_p
2151 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2152 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2153 (current_template_parms
))))
2156 /* DECL might be a specialization of FN. */
2157 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2158 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2160 /* For a non-static member function, we need to make sure
2161 that the const qualification is the same. Since
2162 get_bindings does not try to merge the "this" parameter,
2163 we must do the comparison explicitly. */
2164 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2165 && !same_type_p (TREE_VALUE (fn_arg_types
),
2166 TREE_VALUE (decl_arg_types
)))
2169 /* Skip the "this" parameter and, for constructors of
2170 classes with virtual bases, the VTT parameter. A
2171 full specialization of a constructor will have a VTT
2172 parameter, but a template never will. */
2174 = skip_artificial_parms_for (decl
, decl_arg_types
);
2176 = skip_artificial_parms_for (fn
, fn_arg_types
);
2178 /* Function templates cannot be specializations; there are
2179 no partial specializations of functions. Therefore, if
2180 the type of DECL does not match FN, there is no
2183 Note that it should never be the case that we have both
2184 candidates added here, and for regular member functions
2186 if (tsk
== tsk_template
)
2188 if (compparms (fn_arg_types
, decl_arg_types
))
2189 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2193 /* See whether this function might be a specialization of this
2194 template. Suppress access control because we might be trying
2195 to make this specialization a friend, and we have already done
2196 access control for the declaration of the specialization. */
2197 push_deferring_access_checks (dk_no_check
);
2198 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2199 pop_deferring_access_checks ();
2202 /* We cannot deduce template arguments that when used to
2203 specialize TMPL will produce DECL. */
2206 /* Remove, from the set of candidates, all those functions
2207 whose constraints are not satisfied. */
2208 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2211 // Then, try to form the new function type.
2212 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2213 if (insttype
== error_mark_node
)
2216 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2217 if (!compparms (fn_arg_types
, decl_arg_types
))
2220 /* Save this template, and the arguments deduced. */
2221 templates
= tree_cons (targs
, fn
, templates
);
2223 else if (need_member_template
)
2224 /* FN is an ordinary member function, and we need a
2225 specialization of a member template. */
2227 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2228 /* We can get IDENTIFIER_NODEs here in certain erroneous
2231 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2232 /* This is just an ordinary non-member function. Nothing can
2233 be a specialization of that. */
2235 else if (DECL_ARTIFICIAL (fn
))
2236 /* Cannot specialize functions that are created implicitly. */
2240 tree decl_arg_types
;
2242 /* This is an ordinary member function. However, since
2243 we're here, we can assume its enclosing class is a
2244 template class. For example,
2246 template <typename T> struct S { void f(); };
2247 template <> void S<int>::f() {}
2249 Here, S<int>::f is a non-template, but S<int> is a
2250 template class. If FN has the same type as DECL, we
2251 might be in business. */
2253 if (!DECL_TEMPLATE_INFO (fn
))
2254 /* Its enclosing class is an explicit specialization
2255 of a template class. This is not a candidate. */
2258 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2259 TREE_TYPE (TREE_TYPE (fn
))))
2260 /* The return types differ. */
2263 /* Adjust the type of DECL in case FN is a static member. */
2264 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2265 if (DECL_STATIC_FUNCTION_P (fn
)
2266 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2267 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2269 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2273 // If the deduced arguments do not satisfy the constraints,
2274 // this is not a candidate.
2275 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2278 // Add the candidate.
2279 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2283 if (templates
&& TREE_CHAIN (templates
))
2289 It is possible for a specialization with a given function
2290 signature to be instantiated from more than one function
2291 template. In such cases, explicit specification of the
2292 template arguments must be used to uniquely identify the
2293 function template specialization being specialized.
2295 Note that here, there's no suggestion that we're supposed to
2296 determine which of the candidate templates is most
2297 specialized. However, we, also have:
2301 Partial ordering of overloaded function template
2302 declarations is used in the following contexts to select
2303 the function template to which a function template
2304 specialization refers:
2306 -- when an explicit specialization refers to a function
2309 So, we do use the partial ordering rules, at least for now.
2310 This extension can only serve to make invalid programs valid,
2311 so it's safe. And, there is strong anecdotal evidence that
2312 the committee intended the partial ordering rules to apply;
2313 the EDG front end has that behavior, and John Spicer claims
2314 that the committee simply forgot to delete the wording in
2315 [temp.expl.spec]. */
2316 tree tmpl
= most_specialized_instantiation (templates
);
2317 if (tmpl
!= error_mark_node
)
2320 TREE_CHAIN (templates
) = NULL_TREE
;
2324 // Concepts allows multiple declarations of member functions
2325 // with the same signature. Like above, we need to rely on
2326 // on the partial ordering of those candidates to determine which
2328 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2330 if (tree cand
= most_constrained_function (candidates
))
2333 TREE_CHAIN (cand
) = NULL_TREE
;
2337 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2339 error ("template-id %qD for %q+D does not match any template "
2340 "declaration", template_id
, decl
);
2341 if (header_count
&& header_count
!= template_count
+ 1)
2342 inform (input_location
, "saw %d %<template<>%>, need %d for "
2343 "specializing a member function template",
2344 header_count
, template_count
+ 1);
2346 print_candidates (orig_fns
);
2347 return error_mark_node
;
2349 else if ((templates
&& TREE_CHAIN (templates
))
2350 || (candidates
&& TREE_CHAIN (candidates
))
2351 || (templates
&& candidates
))
2353 error ("ambiguous template specialization %qD for %q+D",
2355 candidates
= chainon (candidates
, templates
);
2356 print_candidates (candidates
);
2357 return error_mark_node
;
2360 /* We have one, and exactly one, match. */
2363 tree fn
= TREE_VALUE (candidates
);
2364 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2366 // Propagate the candidate's constraints to the declaration.
2367 set_constraints (decl
, get_constraints (fn
));
2369 /* DECL is a re-declaration or partial instantiation of a template
2371 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2373 /* It was a specialization of an ordinary member function in a
2375 return DECL_TI_TEMPLATE (fn
);
2378 /* It was a specialization of a template. */
2379 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2380 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2382 *targs_out
= copy_node (targs
);
2383 SET_TMPL_ARGS_LEVEL (*targs_out
,
2384 TMPL_ARGS_DEPTH (*targs_out
),
2385 TREE_PURPOSE (templates
));
2388 *targs_out
= TREE_PURPOSE (templates
);
2389 return TREE_VALUE (templates
);
2392 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2393 but with the default argument values filled in from those in the
2397 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2400 tree new_spec_types
;
2405 if (spec_types
== void_list_node
)
2406 return void_list_node
;
2408 /* Substitute into the rest of the list. */
2410 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2411 TREE_CHAIN (tmpl_types
));
2413 /* Add the default argument for this parameter. */
2414 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2415 TREE_VALUE (spec_types
),
2419 /* DECL is an explicit specialization. Replicate default arguments
2420 from the template it specializes. (That way, code like:
2422 template <class T> void f(T = 3);
2423 template <> void f(double);
2426 works, as required.) An alternative approach would be to look up
2427 the correct default arguments at the call-site, but this approach
2428 is consistent with how implicit instantiations are handled. */
2431 copy_default_args_to_explicit_spec (tree decl
)
2436 tree new_spec_types
;
2440 tree object_type
= NULL_TREE
;
2441 tree in_charge
= NULL_TREE
;
2442 tree vtt
= NULL_TREE
;
2444 /* See if there's anything we need to do. */
2445 tmpl
= DECL_TI_TEMPLATE (decl
);
2446 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2447 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2448 if (TREE_PURPOSE (t
))
2453 old_type
= TREE_TYPE (decl
);
2454 spec_types
= TYPE_ARG_TYPES (old_type
);
2456 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2458 /* Remove the this pointer, but remember the object's type for
2460 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2461 spec_types
= TREE_CHAIN (spec_types
);
2462 tmpl_types
= TREE_CHAIN (tmpl_types
);
2464 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2466 /* DECL may contain more parameters than TMPL due to the extra
2467 in-charge parameter in constructors and destructors. */
2468 in_charge
= spec_types
;
2469 spec_types
= TREE_CHAIN (spec_types
);
2471 if (DECL_HAS_VTT_PARM_P (decl
))
2474 spec_types
= TREE_CHAIN (spec_types
);
2478 /* Compute the merged default arguments. */
2480 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2482 /* Compute the new FUNCTION_TYPE. */
2486 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2491 /* Put the in-charge parameter back. */
2492 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2493 TREE_VALUE (in_charge
),
2496 new_type
= build_method_type_directly (object_type
,
2497 TREE_TYPE (old_type
),
2501 new_type
= build_function_type (TREE_TYPE (old_type
),
2503 new_type
= cp_build_type_attribute_variant (new_type
,
2504 TYPE_ATTRIBUTES (old_type
));
2505 new_type
= build_exception_variant (new_type
,
2506 TYPE_RAISES_EXCEPTIONS (old_type
));
2508 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2509 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2511 TREE_TYPE (decl
) = new_type
;
2514 /* Return the number of template headers we expect to see for a definition
2515 or specialization of CTYPE or one of its non-template members. */
2518 num_template_headers_for_class (tree ctype
)
2520 int num_templates
= 0;
2522 while (ctype
&& CLASS_TYPE_P (ctype
))
2524 /* You're supposed to have one `template <...>' for every
2525 template class, but you don't need one for a full
2526 specialization. For example:
2528 template <class T> struct S{};
2529 template <> struct S<int> { void f(); };
2530 void S<int>::f () {}
2532 is correct; there shouldn't be a `template <>' for the
2533 definition of `S<int>::f'. */
2534 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2535 /* If CTYPE does not have template information of any
2536 kind, then it is not a template, nor is it nested
2537 within a template. */
2539 if (explicit_class_specialization_p (ctype
))
2541 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2544 ctype
= TYPE_CONTEXT (ctype
);
2547 return num_templates
;
2550 /* Do a simple sanity check on the template headers that precede the
2551 variable declaration DECL. */
2554 check_template_variable (tree decl
)
2556 tree ctx
= CP_DECL_CONTEXT (decl
);
2557 int wanted
= num_template_headers_for_class (ctx
);
2558 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2559 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2561 if (cxx_dialect
< cxx14
)
2562 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2563 "variable templates only available with "
2564 "-std=c++14 or -std=gnu++14");
2566 // Namespace-scope variable templates should have a template header.
2569 if (template_header_count
> wanted
)
2571 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2572 "too many template headers for %qD "
2575 if (warned
&& CLASS_TYPE_P (ctx
)
2576 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2577 inform (DECL_SOURCE_LOCATION (decl
),
2578 "members of an explicitly specialized class are defined "
2579 "without a template header");
2583 /* An explicit specialization whose declarator-id or class-head-name is not
2584 qualified shall be declared in the nearest enclosing namespace of the
2585 template, or, if the namespace is inline (7.3.1), any namespace from its
2586 enclosing namespace set.
2588 If the name declared in the explicit instantiation is an unqualified name,
2589 the explicit instantiation shall appear in the namespace where its template
2590 is declared or, if that namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set. */
2594 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2596 tree tmpl
= most_general_template (t
);
2597 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2598 && !is_nested_namespace (current_namespace
,
2599 CP_DECL_CONTEXT (tmpl
), true))
2601 if (processing_specialization
)
2602 permerror (loc
, "explicit specialization of %qD outside its "
2603 "namespace must use a nested-name-specifier", tmpl
);
2604 else if (processing_explicit_instantiation
2605 && cxx_dialect
>= cxx11
)
2606 /* This was allowed in C++98, so only pedwarn. */
2607 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2608 "outside its namespace must use a nested-name-"
2613 /* Check to see if the function just declared, as indicated in
2614 DECLARATOR, and in DECL, is a specialization of a function
2615 template. We may also discover that the declaration is an explicit
2616 instantiation at this point.
2618 Returns DECL, or an equivalent declaration that should be used
2619 instead if all goes well. Issues an error message if something is
2620 amiss. Returns error_mark_node if the error is not easily
2623 FLAGS is a bitmask consisting of the following flags:
2625 2: The function has a definition.
2626 4: The function is a friend.
2628 The TEMPLATE_COUNT is the number of references to qualifying
2629 template classes that appeared in the name of the function. For
2632 template <class T> struct S { void f(); };
2635 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2636 classes are not counted in the TEMPLATE_COUNT, so that in
2638 template <class T> struct S {};
2639 template <> struct S<int> { void f(); }
2640 template <> void S<int>::f();
2642 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2643 invalid; there should be no template <>.)
2645 If the function is a specialization, it is marked as such via
2646 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2647 is set up correctly, and it is added to the list of specializations
2648 for that template. */
2651 check_explicit_specialization (tree declarator
,
2656 int have_def
= flags
& 2;
2657 int is_friend
= flags
& 4;
2658 bool is_concept
= flags
& 8;
2659 int specialization
= 0;
2660 int explicit_instantiation
= 0;
2661 int member_specialization
= 0;
2662 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2663 tree dname
= DECL_NAME (decl
);
2668 if (!processing_specialization
)
2671 tsk
= tsk_excessive_parms
;
2674 tsk
= current_tmpl_spec_kind (template_count
);
2679 if (processing_specialization
&& !VAR_P (decl
))
2682 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2684 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2687 /* This could be something like:
2689 template <class T> void f(T);
2690 class S { friend void f<>(int); } */
2694 /* This case handles bogus declarations like template <>
2695 template <class T> void f<int>(); */
2697 error ("template-id %qD in declaration of primary template",
2704 case tsk_invalid_member_spec
:
2705 /* The error has already been reported in
2706 check_specialization_scope. */
2707 return error_mark_node
;
2709 case tsk_invalid_expl_inst
:
2710 error ("template parameter list used in explicit instantiation");
2716 error ("definition provided for explicit instantiation");
2718 explicit_instantiation
= 1;
2721 case tsk_excessive_parms
:
2722 case tsk_insufficient_parms
:
2723 if (tsk
== tsk_excessive_parms
)
2724 error ("too many template parameter lists in declaration of %qD",
2726 else if (template_header_count
)
2727 error("too few template parameter lists in declaration of %qD", decl
);
2729 error("explicit specialization of %qD must be introduced by "
2730 "%<template <>%>", decl
);
2735 error ("explicit specialization declared %<concept%>");
2737 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2738 /* In cases like template<> constexpr bool v = true;
2739 We'll give an error in check_template_variable. */
2742 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2744 member_specialization
= 1;
2750 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2752 /* This case handles bogus declarations like template <>
2753 template <class T> void f<int>(); */
2755 if (!uses_template_parms (declarator
))
2756 error ("template-id %qD in declaration of primary template",
2758 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2760 /* Partial specialization of variable template. */
2761 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2765 else if (cxx_dialect
< cxx14
)
2766 error ("non-type partial specialization %qD "
2767 "is not allowed", declarator
);
2769 error ("non-class, non-variable partial specialization %qD "
2770 "is not allowed", declarator
);
2775 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2776 /* This is a specialization of a member template, without
2777 specialization the containing class. Something like:
2779 template <class T> struct S {
2780 template <class U> void f (U);
2782 template <> template <class U> void S<int>::f(U) {}
2784 That's a specialization -- but of the entire template. */
2792 if ((specialization
|| member_specialization
)
2793 /* This doesn't apply to variable templates. */
2794 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2795 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2797 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2798 for (; t
; t
= TREE_CHAIN (t
))
2799 if (TREE_PURPOSE (t
))
2801 permerror (input_location
,
2802 "default argument specified in explicit specialization");
2807 if (specialization
|| member_specialization
|| explicit_instantiation
)
2809 tree tmpl
= NULL_TREE
;
2810 tree targs
= NULL_TREE
;
2811 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2813 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2814 if (!was_template_id
)
2818 gcc_assert (identifier_p (declarator
));
2823 /* If there is no class context, the explicit instantiation
2824 must be at namespace scope. */
2825 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2827 /* Find the namespace binding, using the declaration
2829 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2831 if (fns
== error_mark_node
)
2832 /* If lookup fails, look for a friend declaration so we can
2833 give a better diagnostic. */
2834 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2835 /*type*/false, /*complain*/true,
2838 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2840 error ("%qD is not a template function", dname
);
2841 fns
= error_mark_node
;
2845 declarator
= lookup_template_function (fns
, NULL_TREE
);
2848 if (declarator
== error_mark_node
)
2849 return error_mark_node
;
2851 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2853 if (!explicit_instantiation
)
2854 /* A specialization in class scope. This is invalid,
2855 but the error will already have been flagged by
2856 check_specialization_scope. */
2857 return error_mark_node
;
2860 /* It's not valid to write an explicit instantiation in
2863 class C { template void f(); }
2865 This case is caught by the parser. However, on
2868 template class C { void f(); };
2870 (which is invalid) we can get here. The error will be
2877 else if (ctype
!= NULL_TREE
2878 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2880 // We'll match variable templates in start_decl.
2884 /* Find the list of functions in ctype that have the same
2885 name as the declared function. */
2886 tree name
= TREE_OPERAND (declarator
, 0);
2888 if (constructor_name_p (name
, ctype
))
2890 if (DECL_CONSTRUCTOR_P (decl
)
2891 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2892 : !CLASSTYPE_DESTRUCTOR (ctype
))
2894 /* From [temp.expl.spec]:
2896 If such an explicit specialization for the member
2897 of a class template names an implicitly-declared
2898 special member function (clause _special_), the
2899 program is ill-formed.
2901 Similar language is found in [temp.explicit]. */
2902 error ("specialization of implicitly-declared special member function");
2903 return error_mark_node
;
2906 name
= DECL_NAME (decl
);
2909 /* For a type-conversion operator, We might be looking for
2910 `operator int' which will be a specialization of
2911 `operator T'. Grab all the conversion operators, and
2912 then select from them. */
2913 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2914 ? conv_op_identifier
: name
);
2916 if (fns
== NULL_TREE
)
2918 error ("no member function %qD declared in %qT", name
, ctype
);
2919 return error_mark_node
;
2922 TREE_OPERAND (declarator
, 0) = fns
;
2925 /* Figure out what exactly is being specialized at this point.
2926 Note that for an explicit instantiation, even one for a
2927 member function, we cannot tell a priori whether the
2928 instantiation is for a member template, or just a member
2929 function of a template class. Even if a member template is
2930 being instantiated, the member template arguments may be
2931 elided if they can be deduced from the rest of the
2933 tmpl
= determine_specialization (declarator
, decl
,
2935 member_specialization
,
2939 if (!tmpl
|| tmpl
== error_mark_node
)
2940 /* We couldn't figure out what this declaration was
2942 return error_mark_node
;
2945 if (TREE_CODE (decl
) == FUNCTION_DECL
2946 && DECL_HIDDEN_FRIEND_P (tmpl
))
2948 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2949 "friend declaration %qD is not visible to "
2950 "explicit specialization", tmpl
))
2951 inform (DECL_SOURCE_LOCATION (tmpl
),
2952 "friend declaration here");
2954 else if (!ctype
&& !is_friend
2955 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2956 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2958 tree gen_tmpl
= most_general_template (tmpl
);
2960 if (explicit_instantiation
)
2962 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2963 is done by do_decl_instantiation later. */
2965 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2966 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2968 if (arg_depth
> parm_depth
)
2970 /* If TMPL is not the most general template (for
2971 example, if TMPL is a friend template that is
2972 injected into namespace scope), then there will
2973 be too many levels of TARGS. Remove some of them
2978 new_targs
= make_tree_vec (parm_depth
);
2979 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2980 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2981 = TREE_VEC_ELT (targs
, i
);
2985 return instantiate_template (tmpl
, targs
, tf_error
);
2988 /* If we thought that the DECL was a member function, but it
2989 turns out to be specializing a static member function,
2990 make DECL a static member function as well. */
2991 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
2992 && DECL_STATIC_FUNCTION_P (tmpl
)
2993 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2994 revert_static_member_fn (decl
);
2996 /* If this is a specialization of a member template of a
2997 template class, we want to return the TEMPLATE_DECL, not
2998 the specialization of it. */
2999 if (tsk
== tsk_template
&& !was_template_id
)
3001 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3002 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3003 DECL_INITIAL (result
) = NULL_TREE
;
3007 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3008 DECL_SOURCE_LOCATION (result
)
3009 = DECL_SOURCE_LOCATION (decl
);
3010 /* We want to use the argument list specified in the
3011 definition, not in the original declaration. */
3012 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3013 for (parm
= DECL_ARGUMENTS (result
); parm
;
3014 parm
= DECL_CHAIN (parm
))
3015 DECL_CONTEXT (parm
) = result
;
3017 return register_specialization (tmpl
, gen_tmpl
, targs
,
3021 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3022 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3024 if (was_template_id
)
3025 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3027 /* Inherit default function arguments from the template
3028 DECL is specializing. */
3029 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3030 copy_default_args_to_explicit_spec (decl
);
3032 /* This specialization has the same protection as the
3033 template it specializes. */
3034 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3035 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3037 /* 7.1.1-1 [dcl.stc]
3039 A storage-class-specifier shall not be specified in an
3040 explicit specialization...
3042 The parser rejects these, so unless action is taken here,
3043 explicit function specializations will always appear with
3046 The action recommended by the C++ CWG in response to C++
3047 defect report 605 is to make the storage class and linkage
3048 of the explicit specialization match the templated function:
3050 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3052 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3054 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3055 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3057 /* A concept cannot be specialized. */
3058 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3060 error ("explicit specialization of function concept %qD",
3062 return error_mark_node
;
3065 /* This specialization has the same linkage and visibility as
3066 the function template it specializes. */
3067 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3068 if (! TREE_PUBLIC (decl
))
3070 DECL_INTERFACE_KNOWN (decl
) = 1;
3071 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3073 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3074 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3076 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3077 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3081 /* If DECL is a friend declaration, declared using an
3082 unqualified name, the namespace associated with DECL may
3083 have been set incorrectly. For example, in:
3085 template <typename T> void f(T);
3087 struct S { friend void f<int>(int); }
3090 we will have set the DECL_CONTEXT for the friend
3091 declaration to N, rather than to the global namespace. */
3092 if (DECL_NAMESPACE_SCOPE_P (decl
))
3093 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3095 if (is_friend
&& !have_def
)
3096 /* This is not really a declaration of a specialization.
3097 It's just the name of an instantiation. But, it's not
3098 a request for an instantiation, either. */
3099 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3100 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3101 /* A specialization is not necessarily COMDAT. */
3102 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3103 && DECL_DECLARED_INLINE_P (decl
));
3104 else if (VAR_P (decl
))
3105 DECL_COMDAT (decl
) = false;
3107 /* If this is a full specialization, register it so that we can find
3108 it again. Partial specializations will be registered in
3109 process_partial_specialization. */
3110 if (!processing_template_decl
)
3111 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3114 /* A 'structor should already have clones. */
3115 gcc_assert (decl
== error_mark_node
3116 || variable_template_p (tmpl
)
3117 || !(DECL_CONSTRUCTOR_P (decl
)
3118 || DECL_DESTRUCTOR_P (decl
))
3119 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3126 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3127 parameters. These are represented in the same format used for
3128 DECL_TEMPLATE_PARMS. */
3131 comp_template_parms (const_tree parms1
, const_tree parms2
)
3136 if (parms1
== parms2
)
3139 for (p1
= parms1
, p2
= parms2
;
3140 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3141 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3143 tree t1
= TREE_VALUE (p1
);
3144 tree t2
= TREE_VALUE (p2
);
3147 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3148 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3150 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3153 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3155 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3156 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3158 /* If either of the template parameters are invalid, assume
3159 they match for the sake of error recovery. */
3160 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3163 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3166 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3167 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3168 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3170 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3175 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3176 /* One set of parameters has more parameters lists than the
3183 /* Determine whether PARM is a parameter pack. */
3186 template_parameter_pack_p (const_tree parm
)
3188 /* Determine if we have a non-type template parameter pack. */
3189 if (TREE_CODE (parm
) == PARM_DECL
)
3190 return (DECL_TEMPLATE_PARM_P (parm
)
3191 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3192 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3193 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3195 /* If this is a list of template parameters, we could get a
3196 TYPE_DECL or a TEMPLATE_DECL. */
3197 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3198 parm
= TREE_TYPE (parm
);
3200 /* Otherwise it must be a type template parameter. */
3201 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3202 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3203 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3206 /* Determine if T is a function parameter pack. */
3209 function_parameter_pack_p (const_tree t
)
3211 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3212 return DECL_PACK_P (t
);
3216 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3217 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3220 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3222 if (! primary_func_tmpl_inst
3223 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3224 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3227 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3230 /* Return true iff the function parameter PARAM_DECL was expanded
3231 from the function parameter pack PACK. */
3234 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3236 if (DECL_ARTIFICIAL (param_decl
)
3237 || !function_parameter_pack_p (pack
))
3240 /* The parameter pack and its pack arguments have the same
3242 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3245 /* Determine whether ARGS describes a variadic template args list,
3246 i.e., one that is terminated by a template argument pack. */
3249 template_args_variadic_p (tree args
)
3254 if (args
== NULL_TREE
)
3257 args
= INNERMOST_TEMPLATE_ARGS (args
);
3258 nargs
= TREE_VEC_LENGTH (args
);
3263 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3265 return ARGUMENT_PACK_P (last_parm
);
3268 /* Generate a new name for the parameter pack name NAME (an
3269 IDENTIFIER_NODE) that incorporates its */
3272 make_ith_pack_parameter_name (tree name
, int i
)
3274 /* Munge the name to include the parameter index. */
3275 #define NUMBUF_LEN 128
3276 char numbuf
[NUMBUF_LEN
];
3280 if (name
== NULL_TREE
)
3282 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3283 newname_len
= IDENTIFIER_LENGTH (name
)
3284 + strlen (numbuf
) + 2;
3285 newname
= (char*)alloca (newname_len
);
3286 snprintf (newname
, newname_len
,
3287 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3288 return get_identifier (newname
);
3291 /* Return true if T is a primary function, class or alias template
3292 specialization, not including the template pattern. */
3295 primary_template_specialization_p (const_tree t
)
3300 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3301 return (DECL_LANG_SPECIFIC (t
)
3302 && DECL_USE_TEMPLATE (t
)
3303 && DECL_TEMPLATE_INFO (t
)
3304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3305 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3306 return (CLASSTYPE_TEMPLATE_INFO (t
)
3307 && CLASSTYPE_USE_TEMPLATE (t
)
3308 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3309 else if (alias_template_specialization_p (t
))
3314 /* Return true if PARM is a template template parameter. */
3317 template_template_parameter_p (const_tree parm
)
3319 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3322 /* Return true iff PARM is a DECL representing a type template
3326 template_type_parameter_p (const_tree parm
)
3329 && (TREE_CODE (parm
) == TYPE_DECL
3330 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3331 && DECL_TEMPLATE_PARM_P (parm
));
3334 /* Return the template parameters of T if T is a
3335 primary template instantiation, NULL otherwise. */
3338 get_primary_template_innermost_parameters (const_tree t
)
3340 tree parms
= NULL
, template_info
= NULL
;
3342 if ((template_info
= get_template_info (t
))
3343 && primary_template_specialization_p (t
))
3344 parms
= INNERMOST_TEMPLATE_PARMS
3345 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3350 /* Return the template parameters of the LEVELth level from the full list
3351 of template parameters PARMS. */
3354 get_template_parms_at_level (tree parms
, int level
)
3358 || TREE_CODE (parms
) != TREE_LIST
3359 || level
> TMPL_PARMS_DEPTH (parms
))
3362 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3363 if (TMPL_PARMS_DEPTH (p
) == level
)
3369 /* Returns the template arguments of T if T is a template instantiation,
3373 get_template_innermost_arguments (const_tree t
)
3375 tree args
= NULL
, template_info
= NULL
;
3377 if ((template_info
= get_template_info (t
))
3378 && TI_ARGS (template_info
))
3379 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3384 /* Return the argument pack elements of T if T is a template argument pack,
3388 get_template_argument_pack_elems (const_tree t
)
3390 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3391 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3394 return ARGUMENT_PACK_ARGS (t
);
3397 /* True iff FN is a function representing a built-in variadic parameter
3401 builtin_pack_fn_p (tree fn
)
3404 || TREE_CODE (fn
) != FUNCTION_DECL
3405 || !DECL_IS_BUILTIN (fn
))
3408 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3414 /* True iff CALL is a call to a function representing a built-in variadic
3418 builtin_pack_call_p (tree call
)
3420 if (TREE_CODE (call
) != CALL_EXPR
)
3422 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3425 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3428 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3431 tree ohi
= CALL_EXPR_ARG (call
, 0);
3432 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3433 false/*fn*/, true/*int_cst*/);
3435 if (value_dependent_expression_p (hi
))
3439 call
= copy_node (call
);
3440 CALL_EXPR_ARG (call
, 0) = hi
;
3442 tree ex
= make_pack_expansion (call
, complain
);
3443 tree vec
= make_tree_vec (1);
3444 TREE_VEC_ELT (vec
, 0) = ex
;
3449 hi
= cxx_constant_value (hi
);
3450 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3452 /* Calculate the largest value of len that won't make the size of the vec
3453 overflow an int. The compiler will exceed resource limits long before
3454 this, but it seems a decent place to diagnose. */
3455 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3457 if (len
< 0 || len
> max
)
3459 if ((complain
& tf_error
)
3460 && hi
!= error_mark_node
)
3461 error ("argument to __integer_pack must be between 0 and %d", max
);
3462 return error_mark_node
;
3465 tree vec
= make_tree_vec (len
);
3467 for (int i
= 0; i
< len
; ++i
)
3468 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3474 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3478 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3481 if (!builtin_pack_call_p (call
))
3484 tree fn
= CALL_EXPR_FN (call
);
3486 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3487 return expand_integer_pack (call
, args
, complain
, in_decl
);
3492 /* Structure used to track the progress of find_parameter_packs_r. */
3493 struct find_parameter_pack_data
3495 /* TREE_LIST that will contain all of the parameter packs found by
3497 tree
* parameter_packs
;
3499 /* Set of AST nodes that have been visited by the traversal. */
3500 hash_set
<tree
> *visited
;
3502 /* True iff we're making a type pack expansion. */
3503 bool type_pack_expansion_p
;
3506 /* Identifies all of the argument packs that occur in a template
3507 argument and appends them to the TREE_LIST inside DATA, which is a
3508 find_parameter_pack_data structure. This is a subroutine of
3509 make_pack_expansion and uses_parameter_packs. */
3511 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3514 struct find_parameter_pack_data
* ppd
=
3515 (struct find_parameter_pack_data
*)data
;
3516 bool parameter_pack_p
= false;
3518 /* Handle type aliases/typedefs. */
3519 if (TYPE_ALIAS_P (t
))
3521 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3522 cp_walk_tree (&TI_ARGS (tinfo
),
3523 &find_parameter_packs_r
,
3529 /* Identify whether this is a parameter pack or not. */
3530 switch (TREE_CODE (t
))
3532 case TEMPLATE_PARM_INDEX
:
3533 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3534 parameter_pack_p
= true;
3537 case TEMPLATE_TYPE_PARM
:
3538 t
= TYPE_MAIN_VARIANT (t
);
3540 case TEMPLATE_TEMPLATE_PARM
:
3541 /* If the placeholder appears in the decl-specifier-seq of a function
3542 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3543 is a pack expansion, the invented template parameter is a template
3545 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3546 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3547 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3548 parameter_pack_p
= true;
3553 if (DECL_PACK_P (t
))
3555 /* We don't want to walk into the type of a PARM_DECL,
3556 because we don't want to see the type parameter pack. */
3558 parameter_pack_p
= true;
3562 /* Look through a lambda capture proxy to the field pack. */
3564 if (DECL_HAS_VALUE_EXPR_P (t
))
3566 tree v
= DECL_VALUE_EXPR (t
);
3568 &find_parameter_packs_r
,
3572 else if (variable_template_specialization_p (t
))
3574 cp_walk_tree (&DECL_TI_ARGS (t
),
3575 find_parameter_packs_r
,
3582 if (builtin_pack_call_p (t
))
3583 parameter_pack_p
= true;
3587 parameter_pack_p
= true;
3590 /* Not a parameter pack. */
3594 if (parameter_pack_p
)
3596 /* Add this parameter pack to the list. */
3597 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3601 cp_walk_tree (&TYPE_CONTEXT (t
),
3602 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3604 /* This switch statement will return immediately if we don't find a
3606 switch (TREE_CODE (t
))
3608 case TEMPLATE_PARM_INDEX
:
3611 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3612 /* Check the template itself. */
3613 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3614 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3615 /* Check the template arguments. */
3616 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3621 case TEMPLATE_TYPE_PARM
:
3622 case TEMPLATE_TEMPLATE_PARM
:
3629 /* Ignore the declaration of a capture proxy for a parameter pack. */
3630 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3635 if (TYPE_PTRMEMFUNC_P (t
))
3641 if (TYPE_TEMPLATE_INFO (t
))
3642 cp_walk_tree (&TYPE_TI_ARGS (t
),
3643 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3649 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3654 cp_walk_tree (&TREE_TYPE (t
),
3655 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3659 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3664 case TYPE_PACK_EXPANSION
:
3665 case EXPR_PACK_EXPANSION
:
3670 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3675 case IDENTIFIER_NODE
:
3676 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3683 tree fn
= lambda_function (t
);
3684 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3692 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3693 type_pack_expansion_p to false so that any placeholders
3694 within the expression don't get marked as parameter packs. */
3695 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3696 ppd
->type_pack_expansion_p
= false;
3697 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3699 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3711 /* Determines if the expression or type T uses any parameter packs. */
3713 uses_parameter_packs (tree t
)
3715 tree parameter_packs
= NULL_TREE
;
3716 struct find_parameter_pack_data ppd
;
3717 ppd
.parameter_packs
= ¶meter_packs
;
3718 ppd
.visited
= new hash_set
<tree
>;
3719 ppd
.type_pack_expansion_p
= false;
3720 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3722 return parameter_packs
!= NULL_TREE
;
3725 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3726 representation a base-class initializer into a parameter pack
3727 expansion. If all goes well, the resulting node will be an
3728 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3731 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3734 tree parameter_packs
= NULL_TREE
;
3735 bool for_types
= false;
3736 struct find_parameter_pack_data ppd
;
3738 if (!arg
|| arg
== error_mark_node
)
3741 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3743 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3744 class initializer. In this case, the TREE_PURPOSE will be a
3745 _TYPE node (representing the base class expansion we're
3746 initializing) and the TREE_VALUE will be a TREE_LIST
3747 containing the initialization arguments.
3749 The resulting expansion looks somewhat different from most
3750 expansions. Rather than returning just one _EXPANSION, we
3751 return a TREE_LIST whose TREE_PURPOSE is a
3752 TYPE_PACK_EXPANSION containing the bases that will be
3753 initialized. The TREE_VALUE will be identical to the
3754 original TREE_VALUE, which is a list of arguments that will
3755 be passed to each base. We do not introduce any new pack
3756 expansion nodes into the TREE_VALUE (although it is possible
3757 that some already exist), because the TREE_PURPOSE and
3758 TREE_VALUE all need to be expanded together with the same
3759 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3760 resulting TREE_PURPOSE will mention the parameter packs in
3761 both the bases and the arguments to the bases. */
3764 tree parameter_packs
= NULL_TREE
;
3766 /* Determine which parameter packs will be used by the base
3768 ppd
.visited
= new hash_set
<tree
>;
3769 ppd
.parameter_packs
= ¶meter_packs
;
3770 ppd
.type_pack_expansion_p
= true;
3771 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3772 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3775 if (parameter_packs
== NULL_TREE
)
3777 if (complain
& tf_error
)
3778 error ("base initializer expansion %qT contains no parameter packs",
3781 return error_mark_node
;
3784 if (TREE_VALUE (arg
) != void_type_node
)
3786 /* Collect the sets of parameter packs used in each of the
3787 initialization arguments. */
3788 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3790 /* Determine which parameter packs will be expanded in this
3792 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3799 /* Create the pack expansion type for the base type. */
3800 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3801 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3802 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3803 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3805 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3806 they will rarely be compared to anything. */
3807 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3809 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3812 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3815 /* Build the PACK_EXPANSION_* node. */
3817 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3818 : make_node (EXPR_PACK_EXPANSION
);
3819 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3820 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3822 /* Propagate type and const-expression information. */
3823 TREE_TYPE (result
) = TREE_TYPE (arg
);
3824 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3825 /* Mark this read now, since the expansion might be length 0. */
3826 mark_exp_read (arg
);
3829 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3830 they will rarely be compared to anything. */
3831 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3833 /* Determine which parameter packs will be expanded. */
3834 ppd
.parameter_packs
= ¶meter_packs
;
3835 ppd
.visited
= new hash_set
<tree
>;
3836 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3837 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3840 /* Make sure we found some parameter packs. */
3841 if (parameter_packs
== NULL_TREE
)
3843 if (complain
& tf_error
)
3846 error ("expansion pattern %qT contains no argument packs", arg
);
3848 error ("expansion pattern %qE contains no argument packs", arg
);
3850 return error_mark_node
;
3852 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3854 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3859 /* Checks T for any "bare" parameter packs, which have not yet been
3860 expanded, and issues an error if any are found. This operation can
3861 only be done on full expressions or types (e.g., an expression
3862 statement, "if" condition, etc.), because we could have expressions like:
3864 foo(f(g(h(args)))...)
3866 where "args" is a parameter pack. check_for_bare_parameter_packs
3867 should not be called for the subexpressions args, h(args),
3868 g(h(args)), or f(g(h(args))), because we would produce erroneous
3871 Returns TRUE and emits an error if there were bare parameter packs,
3872 returns FALSE otherwise. */
3874 check_for_bare_parameter_packs (tree t
)
3876 tree parameter_packs
= NULL_TREE
;
3877 struct find_parameter_pack_data ppd
;
3879 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3882 /* A lambda might use a parameter pack from the containing context. */
3883 if (current_function_decl
&& LAMBDA_FUNCTION_P (current_function_decl
))
3886 if (TREE_CODE (t
) == TYPE_DECL
)
3889 ppd
.parameter_packs
= ¶meter_packs
;
3890 ppd
.visited
= new hash_set
<tree
>;
3891 ppd
.type_pack_expansion_p
= false;
3892 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3895 if (parameter_packs
)
3897 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3898 error_at (loc
, "parameter packs not expanded with %<...%>:");
3899 while (parameter_packs
)
3901 tree pack
= TREE_VALUE (parameter_packs
);
3902 tree name
= NULL_TREE
;
3904 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3905 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3906 name
= TYPE_NAME (pack
);
3907 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3908 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3909 else if (TREE_CODE (pack
) == CALL_EXPR
)
3910 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3912 name
= DECL_NAME (pack
);
3915 inform (loc
, " %qD", name
);
3917 inform (loc
, " <anonymous>");
3919 parameter_packs
= TREE_CHAIN (parameter_packs
);
3928 /* Expand any parameter packs that occur in the template arguments in
3931 expand_template_argument_pack (tree args
)
3933 if (args
== error_mark_node
)
3934 return error_mark_node
;
3936 tree result_args
= NULL_TREE
;
3937 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3938 int num_result_args
= -1;
3939 int non_default_args_count
= -1;
3941 /* First, determine if we need to expand anything, and the number of
3942 slots we'll need. */
3943 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3945 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3946 if (arg
== NULL_TREE
)
3948 if (ARGUMENT_PACK_P (arg
))
3950 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3951 if (num_result_args
< 0)
3952 num_result_args
= in_arg
+ num_packed
;
3954 num_result_args
+= num_packed
;
3958 if (num_result_args
>= 0)
3963 /* If no expansion is necessary, we're done. */
3964 if (num_result_args
< 0)
3967 /* Expand arguments. */
3968 result_args
= make_tree_vec (num_result_args
);
3969 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3970 non_default_args_count
=
3971 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3972 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3974 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3975 if (ARGUMENT_PACK_P (arg
))
3977 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3978 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3979 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3980 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3981 if (non_default_args_count
> 0)
3982 non_default_args_count
+= num_packed
- 1;
3986 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3990 if (non_default_args_count
>= 0)
3991 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3995 /* Checks if DECL shadows a template parameter.
3997 [temp.local]: A template-parameter shall not be redeclared within its
3998 scope (including nested scopes).
4000 Emits an error and returns TRUE if the DECL shadows a parameter,
4001 returns FALSE otherwise. */
4004 check_template_shadow (tree decl
)
4008 /* If we're not in a template, we can't possibly shadow a template
4010 if (!current_template_parms
)
4013 /* Figure out what we're shadowing. */
4014 decl
= OVL_FIRST (decl
);
4015 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4017 /* If there's no previous binding for this name, we're not shadowing
4018 anything, let alone a template parameter. */
4022 /* If we're not shadowing a template parameter, we're done. Note
4023 that OLDDECL might be an OVERLOAD (or perhaps even an
4024 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4026 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4029 /* We check for decl != olddecl to avoid bogus errors for using a
4030 name inside a class. We check TPFI to avoid duplicate errors for
4031 inline member templates. */
4033 || (DECL_TEMPLATE_PARM_P (decl
)
4034 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4037 /* Don't complain about the injected class name, as we've already
4038 complained about the class itself. */
4039 if (DECL_SELF_REFERENCE_P (decl
))
4042 if (DECL_TEMPLATE_PARM_P (decl
))
4043 error ("declaration of template parameter %q+D shadows "
4044 "template parameter", decl
);
4046 error ("declaration of %q+#D shadows template parameter", decl
);
4047 inform (DECL_SOURCE_LOCATION (olddecl
),
4048 "template parameter %qD declared here", olddecl
);
4052 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4053 ORIG_LEVEL, DECL, and TYPE. */
4056 build_template_parm_index (int index
,
4062 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4063 TEMPLATE_PARM_IDX (t
) = index
;
4064 TEMPLATE_PARM_LEVEL (t
) = level
;
4065 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4066 TEMPLATE_PARM_DECL (t
) = decl
;
4067 TREE_TYPE (t
) = type
;
4068 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4069 TREE_READONLY (t
) = TREE_READONLY (decl
);
4074 /* Find the canonical type parameter for the given template type
4075 parameter. Returns the canonical type parameter, which may be TYPE
4076 if no such parameter existed. */
4079 canonical_type_parameter (tree type
)
4082 int idx
= TEMPLATE_TYPE_IDX (type
);
4083 if (!canonical_template_parms
)
4084 vec_alloc (canonical_template_parms
, idx
+ 1);
4086 if (canonical_template_parms
->length () <= (unsigned) idx
)
4087 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4089 list
= (*canonical_template_parms
)[idx
];
4090 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4091 list
= TREE_CHAIN (list
);
4094 return TREE_VALUE (list
);
4097 (*canonical_template_parms
)[idx
]
4098 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4103 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4104 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4105 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4106 new one is created. */
4109 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4110 tsubst_flags_t complain
)
4112 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4113 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4114 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4115 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4117 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4120 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4121 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4122 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4123 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4124 DECL_ARTIFICIAL (decl
) = 1;
4125 SET_DECL_TEMPLATE_PARM_P (decl
);
4127 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4128 TEMPLATE_PARM_LEVEL (index
) - levels
,
4129 TEMPLATE_PARM_ORIG_LEVEL (index
),
4131 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4132 TEMPLATE_PARM_PARAMETER_PACK (t
)
4133 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4135 /* Template template parameters need this. */
4136 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4138 DECL_TEMPLATE_RESULT (decl
)
4139 = build_decl (DECL_SOURCE_LOCATION (decl
),
4140 TYPE_DECL
, DECL_NAME (decl
), type
);
4141 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4142 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4143 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4147 return TEMPLATE_PARM_DESCENDANTS (index
);
4150 /* Process information from new template parameter PARM and append it
4151 to the LIST being built. This new parameter is a non-type
4152 parameter iff IS_NON_TYPE is true. This new parameter is a
4153 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4157 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4158 bool is_non_type
, bool is_parameter_pack
)
4163 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4164 tree defval
= TREE_PURPOSE (parm
);
4165 tree constr
= TREE_TYPE (parm
);
4169 tree p
= tree_last (list
);
4171 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4174 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4175 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4177 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4185 parm
= TREE_VALUE (parm
);
4187 SET_DECL_TEMPLATE_PARM_P (parm
);
4189 if (TREE_TYPE (parm
) != error_mark_node
)
4193 The top-level cv-qualifiers on the template-parameter are
4194 ignored when determining its type. */
4195 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4196 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4197 TREE_TYPE (parm
) = error_mark_node
;
4198 else if (uses_parameter_packs (TREE_TYPE (parm
))
4199 && !is_parameter_pack
4200 /* If we're in a nested template parameter list, the template
4201 template parameter could be a parameter pack. */
4202 && processing_template_parmlist
== 1)
4204 /* This template parameter is not a parameter pack, but it
4205 should be. Complain about "bare" parameter packs. */
4206 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4208 /* Recover by calling this a parameter pack. */
4209 is_parameter_pack
= true;
4213 /* A template parameter is not modifiable. */
4214 TREE_CONSTANT (parm
) = 1;
4215 TREE_READONLY (parm
) = 1;
4216 decl
= build_decl (parm_loc
,
4217 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4218 TREE_CONSTANT (decl
) = 1;
4219 TREE_READONLY (decl
) = 1;
4220 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4221 = build_template_parm_index (idx
, processing_template_decl
,
4222 processing_template_decl
,
4223 decl
, TREE_TYPE (parm
));
4225 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4226 = is_parameter_pack
;
4231 parm
= TREE_VALUE (TREE_VALUE (parm
));
4233 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4235 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4236 /* This is for distinguishing between real templates and template
4237 template parameters */
4238 TREE_TYPE (parm
) = t
;
4239 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4244 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4245 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4246 decl
= build_decl (parm_loc
,
4247 TYPE_DECL
, parm
, t
);
4250 TYPE_NAME (t
) = decl
;
4251 TYPE_STUB_DECL (t
) = decl
;
4253 TEMPLATE_TYPE_PARM_INDEX (t
)
4254 = build_template_parm_index (idx
, processing_template_decl
,
4255 processing_template_decl
,
4256 decl
, TREE_TYPE (parm
));
4257 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4258 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4260 DECL_ARTIFICIAL (decl
) = 1;
4261 SET_DECL_TEMPLATE_PARM_P (decl
);
4263 /* Build requirements for the type/template parameter.
4264 This must be done after SET_DECL_TEMPLATE_PARM_P or
4265 process_template_parm could fail. */
4266 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4270 /* Build the parameter node linking the parameter declaration,
4271 its default argument (if any), and its constraints (if any). */
4272 parm
= build_tree_list (defval
, parm
);
4273 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4275 return chainon (list
, parm
);
4278 /* The end of a template parameter list has been reached. Process the
4279 tree list into a parameter vector, converting each parameter into a more
4280 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4284 end_template_parm_list (tree parms
)
4288 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4290 /* Pop the dummy parameter level and add the real one. */
4291 current_template_parms
= TREE_CHAIN (current_template_parms
);
4293 current_template_parms
4294 = tree_cons (size_int (processing_template_decl
),
4295 saved_parmlist
, current_template_parms
);
4297 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4299 next
= TREE_CHAIN (parm
);
4300 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4301 TREE_CHAIN (parm
) = NULL_TREE
;
4304 --processing_template_parmlist
;
4306 return saved_parmlist
;
4309 // Explicitly indicate the end of the template parameter list. We assume
4310 // that the current template parameters have been constructed and/or
4311 // managed explicitly, as when creating new template template parameters
4312 // from a shorthand constraint.
4314 end_template_parm_list ()
4316 --processing_template_parmlist
;
4319 /* end_template_decl is called after a template declaration is seen. */
4322 end_template_decl (void)
4324 reset_specialization ();
4326 if (! processing_template_decl
)
4329 /* This matches the pushlevel in begin_template_parm_list. */
4332 --processing_template_decl
;
4333 current_template_parms
= TREE_CHAIN (current_template_parms
);
4336 /* Takes a TREE_LIST representing a template parameter and convert it
4337 into an argument suitable to be passed to the type substitution
4338 functions. Note that If the TREE_LIST contains an error_mark
4339 node, the returned argument is error_mark_node. */
4342 template_parm_to_arg (tree t
)
4346 || TREE_CODE (t
) != TREE_LIST
)
4349 if (error_operand_p (TREE_VALUE (t
)))
4350 return error_mark_node
;
4354 if (TREE_CODE (t
) == TYPE_DECL
4355 || TREE_CODE (t
) == TEMPLATE_DECL
)
4359 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4361 /* Turn this argument into a TYPE_ARGUMENT_PACK
4362 with a single element, which expands T. */
4363 tree vec
= make_tree_vec (1);
4365 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4367 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4369 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4370 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4375 t
= DECL_INITIAL (t
);
4377 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4379 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4380 with a single element, which expands T. */
4381 tree vec
= make_tree_vec (1);
4383 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4385 t
= convert_from_reference (t
);
4386 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4388 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4389 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4392 t
= convert_from_reference (t
);
4397 /* Given a single level of template parameters (a TREE_VEC), return it
4398 as a set of template arguments. */
4401 template_parms_level_to_args (tree parms
)
4403 tree a
= copy_node (parms
);
4404 TREE_TYPE (a
) = NULL_TREE
;
4405 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4406 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4409 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4414 /* Given a set of template parameters, return them as a set of template
4415 arguments. The template parameters are represented as a TREE_VEC, in
4416 the form documented in cp-tree.h for template arguments. */
4419 template_parms_to_args (tree parms
)
4422 tree args
= NULL_TREE
;
4423 int length
= TMPL_PARMS_DEPTH (parms
);
4426 /* If there is only one level of template parameters, we do not
4427 create a TREE_VEC of TREE_VECs. Instead, we return a single
4428 TREE_VEC containing the arguments. */
4430 args
= make_tree_vec (length
);
4432 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4434 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4437 TREE_VEC_ELT (args
, --l
) = a
;
4445 /* Within the declaration of a template, return the currently active
4446 template parameters as an argument TREE_VEC. */
4449 current_template_args (void)
4451 return template_parms_to_args (current_template_parms
);
4454 /* Update the declared TYPE by doing any lookups which were thought to be
4455 dependent, but are not now that we know the SCOPE of the declarator. */
4458 maybe_update_decl_type (tree orig_type
, tree scope
)
4460 tree type
= orig_type
;
4462 if (type
== NULL_TREE
)
4465 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4466 type
= TREE_TYPE (type
);
4468 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4469 && dependent_type_p (type
)
4470 /* Don't bother building up the args in this case. */
4471 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4473 /* tsubst in the args corresponding to the template parameters,
4474 including auto if present. Most things will be unchanged, but
4475 make_typename_type and tsubst_qualified_id will resolve
4476 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4477 tree args
= current_template_args ();
4478 tree auto_node
= type_uses_auto (type
);
4482 tree auto_vec
= make_tree_vec (1);
4483 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4484 args
= add_to_template_args (args
, auto_vec
);
4486 pushed
= push_scope (scope
);
4487 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4492 if (type
== error_mark_node
)
4495 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4497 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4500 type
= TYPE_NAME (type
);
4505 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4506 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4507 the new template is a member template. */
4510 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4512 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4513 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4514 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4515 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4516 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4521 struct template_parm_data
4523 /* The level of the template parameters we are currently
4527 /* The index of the specialization argument we are currently
4531 /* An array whose size is the number of template parameters. The
4532 elements are nonzero if the parameter has been used in any one
4533 of the arguments processed so far. */
4536 /* An array whose size is the number of template arguments. The
4537 elements are nonzero if the argument makes use of template
4538 parameters of this level. */
4539 int* arg_uses_template_parms
;
4542 /* Subroutine of push_template_decl used to see if each template
4543 parameter in a partial specialization is used in the explicit
4544 argument list. If T is of the LEVEL given in DATA (which is
4545 treated as a template_parm_data*), then DATA->PARMS is marked
4549 mark_template_parm (tree t
, void* data
)
4553 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4555 template_parm_level_and_index (t
, &level
, &idx
);
4557 if (level
== tpd
->level
)
4559 tpd
->parms
[idx
] = 1;
4560 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4563 /* In C++17 the type of a non-type argument is a deduced context. */
4564 if (cxx_dialect
>= cxx17
4565 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4566 for_each_template_parm (TREE_TYPE (t
),
4567 &mark_template_parm
,
4570 /*include_nondeduced_p=*/false);
4572 /* Return zero so that for_each_template_parm will continue the
4573 traversal of the tree; we want to mark *every* template parm. */
4577 /* Process the partial specialization DECL. */
4580 process_partial_specialization (tree decl
)
4582 tree type
= TREE_TYPE (decl
);
4583 tree tinfo
= get_template_info (decl
);
4584 tree maintmpl
= TI_TEMPLATE (tinfo
);
4585 tree specargs
= TI_ARGS (tinfo
);
4586 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4587 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4590 int nargs
= TREE_VEC_LENGTH (inner_args
);
4593 bool did_error_intro
= false;
4594 struct template_parm_data tpd
;
4595 struct template_parm_data tpd2
;
4597 gcc_assert (current_template_parms
);
4599 /* A concept cannot be specialized. */
4600 if (flag_concepts
&& variable_concept_p (maintmpl
))
4602 error ("specialization of variable concept %q#D", maintmpl
);
4603 return error_mark_node
;
4606 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4607 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4609 /* We check that each of the template parameters given in the
4610 partial specialization is used in the argument list to the
4611 specialization. For example:
4613 template <class T> struct S;
4614 template <class T> struct S<T*>;
4616 The second declaration is OK because `T*' uses the template
4617 parameter T, whereas
4619 template <class T> struct S<int>;
4621 is no good. Even trickier is:
4632 The S2<T> declaration is actually invalid; it is a
4633 full-specialization. Of course,
4636 struct S2<T (*)(U)>;
4638 or some such would have been OK. */
4639 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4640 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4641 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4643 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4644 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4645 for (i
= 0; i
< nargs
; ++i
)
4647 tpd
.current_arg
= i
;
4648 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4649 &mark_template_parm
,
4652 /*include_nondeduced_p=*/false);
4654 for (i
= 0; i
< ntparms
; ++i
)
4655 if (tpd
.parms
[i
] == 0)
4657 /* One of the template parms was not used in a deduced context in the
4659 if (!did_error_intro
)
4661 error ("template parameters not deducible in "
4662 "partial specialization:");
4663 did_error_intro
= true;
4666 inform (input_location
, " %qD",
4667 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4670 if (did_error_intro
)
4671 return error_mark_node
;
4673 /* [temp.class.spec]
4675 The argument list of the specialization shall not be identical to
4676 the implicit argument list of the primary template. */
4678 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4679 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4681 || !strictly_subsumes (current_template_constraints (),
4682 get_constraints (maintmpl
))))
4685 error ("partial specialization %q+D does not specialize "
4686 "any template arguments", decl
);
4688 error ("partial specialization %q+D does not specialize any "
4689 "template arguments and is not more constrained than", decl
);
4690 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4693 /* A partial specialization that replaces multiple parameters of the
4694 primary template with a pack expansion is less specialized for those
4696 if (nargs
< DECL_NTPARMS (maintmpl
))
4698 error ("partial specialization is not more specialized than the "
4699 "primary template because it replaces multiple parameters "
4700 "with a pack expansion");
4701 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4702 /* Avoid crash in process_partial_specialization. */
4706 /* If we aren't in a dependent class, we can actually try deduction. */
4707 else if (tpd
.level
== 1
4708 /* FIXME we should be able to handle a partial specialization of a
4709 partial instantiation, but currently we can't (c++/41727). */
4710 && TMPL_ARGS_DEPTH (specargs
) == 1
4711 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4713 if (permerror (input_location
, "partial specialization %qD is not "
4714 "more specialized than", decl
))
4715 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4719 /* [temp.class.spec]
4721 A partially specialized non-type argument expression shall not
4722 involve template parameters of the partial specialization except
4723 when the argument expression is a simple identifier.
4725 The type of a template parameter corresponding to a specialized
4726 non-type argument shall not be dependent on a parameter of the
4729 Also, we verify that pack expansions only occur at the
4730 end of the argument list. */
4731 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4733 for (i
= 0; i
< nargs
; ++i
)
4735 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4736 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4737 tree packed_args
= NULL_TREE
;
4740 if (ARGUMENT_PACK_P (arg
))
4742 /* Extract the arguments from the argument pack. We'll be
4743 iterating over these in the following loop. */
4744 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4745 len
= TREE_VEC_LENGTH (packed_args
);
4748 for (j
= 0; j
< len
; j
++)
4751 /* Get the Jth argument in the parameter pack. */
4752 arg
= TREE_VEC_ELT (packed_args
, j
);
4754 if (PACK_EXPANSION_P (arg
))
4756 /* Pack expansions must come at the end of the
4758 if ((packed_args
&& j
< len
- 1)
4759 || (!packed_args
&& i
< nargs
- 1))
4761 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4762 error ("parameter pack argument %qE must be at the "
4763 "end of the template argument list", arg
);
4765 error ("parameter pack argument %qT must be at the "
4766 "end of the template argument list", arg
);
4770 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4771 /* We only care about the pattern. */
4772 arg
= PACK_EXPANSION_PATTERN (arg
);
4774 if (/* These first two lines are the `non-type' bit. */
4776 && TREE_CODE (arg
) != TEMPLATE_DECL
4777 /* This next two lines are the `argument expression is not just a
4778 simple identifier' condition and also the `specialized
4779 non-type argument' bit. */
4780 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4781 && !(REFERENCE_REF_P (arg
)
4782 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4784 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4785 || (packed_args
&& uses_template_parms (arg
)))
4786 error ("template argument %qE involves template parameter(s)",
4790 /* Look at the corresponding template parameter,
4791 marking which template parameters its type depends
4793 tree type
= TREE_TYPE (parm
);
4797 /* We haven't yet initialized TPD2. Do so now. */
4798 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4799 /* The number of parameters here is the number in the
4800 main template, which, as checked in the assertion
4802 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4804 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4807 /* Mark the template parameters. But this time, we're
4808 looking for the template parameters of the main
4809 template, not in the specialization. */
4810 tpd2
.current_arg
= i
;
4811 tpd2
.arg_uses_template_parms
[i
] = 0;
4812 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4813 for_each_template_parm (type
,
4814 &mark_template_parm
,
4817 /*include_nondeduced_p=*/false);
4819 if (tpd2
.arg_uses_template_parms
[i
])
4821 /* The type depended on some template parameters.
4822 If they are fully specialized in the
4823 specialization, that's OK. */
4826 for (j
= 0; j
< nargs
; ++j
)
4827 if (tpd2
.parms
[j
] != 0
4828 && tpd
.arg_uses_template_parms
[j
])
4831 error_n (input_location
, count
,
4832 "type %qT of template argument %qE depends "
4833 "on a template parameter",
4834 "type %qT of template argument %qE depends "
4835 "on template parameters",
4844 /* We should only get here once. */
4845 if (TREE_CODE (decl
) == TYPE_DECL
)
4846 gcc_assert (!COMPLETE_TYPE_P (type
));
4848 // Build the template decl.
4849 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4850 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4851 TREE_TYPE (tmpl
) = type
;
4852 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4853 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4854 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4855 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4857 /* Give template template parms a DECL_CONTEXT of the template
4858 for which they are a parameter. */
4859 for (i
= 0; i
< ntparms
; ++i
)
4861 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4862 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4863 DECL_CONTEXT (parm
) = tmpl
;
4867 /* We didn't register this in check_explicit_specialization so we could
4868 wait until the constraints were set. */
4869 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4871 associate_classtype_constraints (type
);
4873 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4874 = tree_cons (specargs
, tmpl
,
4875 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4876 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4878 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4879 inst
= TREE_CHAIN (inst
))
4881 tree instance
= TREE_VALUE (inst
);
4882 if (TYPE_P (instance
)
4883 ? (COMPLETE_TYPE_P (instance
)
4884 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4885 : DECL_TEMPLATE_INSTANTIATION (instance
))
4887 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4888 tree inst_decl
= (DECL_P (instance
)
4889 ? instance
: TYPE_NAME (instance
));
4892 else if (spec
== error_mark_node
)
4893 permerror (input_location
,
4894 "declaration of %qD ambiguates earlier template "
4895 "instantiation for %qD", decl
, inst_decl
);
4896 else if (TREE_VALUE (spec
) == tmpl
)
4897 permerror (input_location
,
4898 "partial specialization of %qD after instantiation "
4899 "of %qD", decl
, inst_decl
);
4906 /* PARM is a template parameter of some form; return the corresponding
4907 TEMPLATE_PARM_INDEX. */
4910 get_template_parm_index (tree parm
)
4912 if (TREE_CODE (parm
) == PARM_DECL
4913 || TREE_CODE (parm
) == CONST_DECL
)
4914 parm
= DECL_INITIAL (parm
);
4915 else if (TREE_CODE (parm
) == TYPE_DECL
4916 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4917 parm
= TREE_TYPE (parm
);
4918 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4919 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4920 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4921 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4922 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4926 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4927 parameter packs used by the template parameter PARM. */
4930 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4932 /* A type parm can't refer to another parm. */
4933 if (TREE_CODE (parm
) == TYPE_DECL
)
4935 else if (TREE_CODE (parm
) == PARM_DECL
)
4937 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4942 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4944 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4945 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4946 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4949 /* PARM is a template parameter pack. Return any parameter packs used in
4950 its type or the type of any of its template parameters. If there are
4951 any such packs, it will be instantiated into a fixed template parameter
4952 list by partial instantiation rather than be fully deduced. */
4955 fixed_parameter_pack_p (tree parm
)
4957 /* This can only be true in a member template. */
4958 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4960 /* This can only be true for a parameter pack. */
4961 if (!template_parameter_pack_p (parm
))
4963 /* A type parm can't refer to another parm. */
4964 if (TREE_CODE (parm
) == TYPE_DECL
)
4967 tree parameter_packs
= NULL_TREE
;
4968 struct find_parameter_pack_data ppd
;
4969 ppd
.parameter_packs
= ¶meter_packs
;
4970 ppd
.visited
= new hash_set
<tree
>;
4971 ppd
.type_pack_expansion_p
= false;
4973 fixed_parameter_pack_p_1 (parm
, &ppd
);
4976 return parameter_packs
;
4979 /* Check that a template declaration's use of default arguments and
4980 parameter packs is not invalid. Here, PARMS are the template
4981 parameters. IS_PRIMARY is true if DECL is the thing declared by
4982 a primary template. IS_PARTIAL is true if DECL is a partial
4985 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
4986 function template declaration or a friend class template
4987 declaration. In the function case, 1 indicates a declaration, 2
4988 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4989 emitted for extraneous default arguments.
4991 Returns TRUE if there were no errors found, FALSE otherwise. */
4994 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4995 bool is_partial
, int is_friend_decl
)
4998 int last_level_to_check
;
5000 bool no_errors
= true;
5004 A default template-argument shall not be specified in a
5005 function template declaration or a function template definition, nor
5006 in the template-parameter-list of the definition of a member of a
5009 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5010 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5011 /* You can't have a function template declaration in a local
5012 scope, nor you can you define a member of a class template in a
5016 if ((TREE_CODE (decl
) == TYPE_DECL
5018 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5019 || (TREE_CODE (decl
) == FUNCTION_DECL
5020 && LAMBDA_FUNCTION_P (decl
)))
5021 /* A lambda doesn't have an explicit declaration; don't complain
5022 about the parms of the enclosing class. */
5025 if (current_class_type
5026 && !TYPE_BEING_DEFINED (current_class_type
)
5027 && DECL_LANG_SPECIFIC (decl
)
5028 && DECL_DECLARES_FUNCTION_P (decl
)
5029 /* If this is either a friend defined in the scope of the class
5030 or a member function. */
5031 && (DECL_FUNCTION_MEMBER_P (decl
)
5032 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5033 : DECL_FRIEND_CONTEXT (decl
)
5034 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5036 /* And, if it was a member function, it really was defined in
5037 the scope of the class. */
5038 && (!DECL_FUNCTION_MEMBER_P (decl
)
5039 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5040 /* We already checked these parameters when the template was
5041 declared, so there's no need to do it again now. This function
5042 was defined in class scope, but we're processing its body now
5043 that the class is complete. */
5046 /* Core issue 226 (C++0x only): the following only applies to class
5049 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5053 If a template-parameter has a default template-argument, all
5054 subsequent template-parameters shall have a default
5055 template-argument supplied. */
5056 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5058 tree inner_parms
= TREE_VALUE (parm_level
);
5059 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5060 int seen_def_arg_p
= 0;
5063 for (i
= 0; i
< ntparms
; ++i
)
5065 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5067 if (parm
== error_mark_node
)
5070 if (TREE_PURPOSE (parm
))
5072 else if (seen_def_arg_p
5073 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5075 error ("no default argument for %qD", TREE_VALUE (parm
));
5076 /* For better subsequent error-recovery, we indicate that
5077 there should have been a default argument. */
5078 TREE_PURPOSE (parm
) = error_mark_node
;
5081 else if (!is_partial
5083 /* Don't complain about an enclosing partial
5085 && parm_level
== parms
5086 && TREE_CODE (decl
) == TYPE_DECL
5088 && template_parameter_pack_p (TREE_VALUE (parm
))
5089 /* A fixed parameter pack will be partially
5090 instantiated into a fixed length list. */
5091 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5093 /* A primary class template can only have one
5094 parameter pack, at the end of the template
5097 error ("parameter pack %q+D must be at the end of the"
5098 " template parameter list", TREE_VALUE (parm
));
5100 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5108 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5112 /* For an ordinary class template, default template arguments are
5113 allowed at the innermost level, e.g.:
5114 template <class T = int>
5116 but, in a partial specialization, they're not allowed even
5117 there, as we have in [temp.class.spec]:
5119 The template parameter list of a specialization shall not
5120 contain default template argument values.
5122 So, for a partial specialization, or for a function template
5123 (in C++98/C++03), we look at all of them. */
5126 /* But, for a primary class template that is not a partial
5127 specialization we look at all template parameters except the
5129 parms
= TREE_CHAIN (parms
);
5131 /* Figure out what error message to issue. */
5132 if (is_friend_decl
== 2)
5133 msg
= G_("default template arguments may not be used in function template "
5134 "friend re-declaration");
5135 else if (is_friend_decl
)
5136 msg
= G_("default template arguments may not be used in template "
5137 "friend declarations");
5138 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5139 msg
= G_("default template arguments may not be used in function templates "
5140 "without -std=c++11 or -std=gnu++11");
5141 else if (is_partial
)
5142 msg
= G_("default template arguments may not be used in "
5143 "partial specializations");
5144 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5145 msg
= G_("default argument for template parameter for class enclosing %qD");
5147 /* Per [temp.param]/9, "A default template-argument shall not be
5148 specified in the template-parameter-lists of the definition of
5149 a member of a class template that appears outside of the member's
5150 class.", thus if we aren't handling a member of a class template
5151 there is no need to examine the parameters. */
5154 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5155 /* If we're inside a class definition, there's no need to
5156 examine the parameters to the class itself. On the one
5157 hand, they will be checked when the class is defined, and,
5158 on the other, default arguments are valid in things like:
5159 template <class T = double>
5160 struct S { template <class U> void f(U); };
5161 Here the default argument for `S' has no bearing on the
5162 declaration of `f'. */
5163 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5165 /* Check everything. */
5166 last_level_to_check
= 0;
5168 for (parm_level
= parms
;
5169 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5170 parm_level
= TREE_CHAIN (parm_level
))
5172 tree inner_parms
= TREE_VALUE (parm_level
);
5176 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5177 for (i
= 0; i
< ntparms
; ++i
)
5179 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5182 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5187 if (is_friend_decl
== 2)
5194 /* Clear out the default argument so that we are not
5196 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5200 /* At this point, if we're still interested in issuing messages,
5201 they must apply to classes surrounding the object declared. */
5203 msg
= G_("default argument for template parameter for class "
5210 /* Worker for push_template_decl_real, called via
5211 for_each_template_parm. DATA is really an int, indicating the
5212 level of the parameters we are interested in. If T is a template
5213 parameter of that level, return nonzero. */
5216 template_parm_this_level_p (tree t
, void* data
)
5218 int this_level
= *(int *)data
;
5221 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5222 level
= TEMPLATE_PARM_LEVEL (t
);
5224 level
= TEMPLATE_TYPE_LEVEL (t
);
5225 return level
== this_level
;
5228 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5229 DATA is really an int, indicating the innermost outer level of parameters.
5230 If T is a template parameter of that level or further out, return
5234 template_parm_outer_level (tree t
, void *data
)
5236 int this_level
= *(int *)data
;
5239 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5240 level
= TEMPLATE_PARM_LEVEL (t
);
5242 level
= TEMPLATE_TYPE_LEVEL (t
);
5243 return level
<= this_level
;
5246 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5247 parameters given by current_template_args, or reuses a
5248 previously existing one, if appropriate. Returns the DECL, or an
5249 equivalent one, if it is replaced via a call to duplicate_decls.
5251 If IS_FRIEND is true, DECL is a friend declaration. */
5254 push_template_decl_real (tree decl
, bool is_friend
)
5262 int new_template_p
= 0;
5263 /* True if the template is a member template, in the sense of
5265 bool member_template_p
= false;
5267 if (decl
== error_mark_node
|| !current_template_parms
)
5268 return error_mark_node
;
5270 /* See if this is a partial specialization. */
5271 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5272 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5273 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5275 && DECL_LANG_SPECIFIC (decl
)
5276 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5277 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5279 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5283 /* For a friend, we want the context of the friend, not
5284 the type of which it is a friend. */
5285 ctx
= CP_DECL_CONTEXT (decl
);
5286 else if (CP_DECL_CONTEXT (decl
)
5287 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5288 /* In the case of a virtual function, we want the class in which
5290 ctx
= CP_DECL_CONTEXT (decl
);
5292 /* Otherwise, if we're currently defining some class, the DECL
5293 is assumed to be a member of the class. */
5294 ctx
= current_scope ();
5296 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5299 if (!DECL_CONTEXT (decl
))
5300 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5302 /* See if this is a primary template. */
5303 if (is_friend
&& ctx
5304 && uses_template_parms_level (ctx
, processing_template_decl
))
5305 /* A friend template that specifies a class context, i.e.
5306 template <typename T> friend void A<T>::f();
5309 else if (TREE_CODE (decl
) == TYPE_DECL
5310 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5313 is_primary
= template_parm_scope_p ();
5317 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5319 if (DECL_CLASS_SCOPE_P (decl
))
5320 member_template_p
= true;
5321 if (TREE_CODE (decl
) == TYPE_DECL
5322 && anon_aggrname_p (DECL_NAME (decl
)))
5324 error ("template class without a name");
5325 return error_mark_node
;
5327 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5329 if (member_template_p
)
5331 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5332 error ("member template %qD may not have virt-specifiers", decl
);
5334 if (DECL_DESTRUCTOR_P (decl
))
5338 A destructor shall not be a member template. */
5339 error ("destructor %qD declared as member template", decl
);
5340 return error_mark_node
;
5342 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5343 && (!prototype_p (TREE_TYPE (decl
))
5344 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5345 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5346 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5347 == void_list_node
)))
5349 /* [basic.stc.dynamic.allocation]
5351 An allocation function can be a function
5352 template. ... Template allocation functions shall
5353 have two or more parameters. */
5354 error ("invalid template declaration of %qD", decl
);
5355 return error_mark_node
;
5358 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5359 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5361 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5362 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5363 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5365 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5366 if (TREE_CODE (t
) == TYPE_DECL
)
5368 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5369 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5372 else if (TREE_CODE (decl
) == TYPE_DECL
5373 && TYPE_DECL_ALIAS_P (decl
))
5374 /* alias-declaration */
5375 gcc_assert (!DECL_ARTIFICIAL (decl
));
5376 else if (VAR_P (decl
))
5377 /* C++14 variable template. */;
5380 error ("template declaration of %q#D", decl
);
5381 return error_mark_node
;
5385 /* Check to see that the rules regarding the use of default
5386 arguments are not being violated. We check args for a friend
5387 functions when we know whether it's a definition, introducing
5388 declaration or re-declaration. */
5389 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5390 check_default_tmpl_args (decl
, current_template_parms
,
5391 is_primary
, is_partial
, is_friend
);
5393 /* Ensure that there are no parameter packs in the type of this
5394 declaration that have not been expanded. */
5395 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5397 /* Check each of the arguments individually to see if there are
5398 any bare parameter packs. */
5399 tree type
= TREE_TYPE (decl
);
5400 tree arg
= DECL_ARGUMENTS (decl
);
5401 tree argtype
= TYPE_ARG_TYPES (type
);
5403 while (arg
&& argtype
)
5405 if (!DECL_PACK_P (arg
)
5406 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5408 /* This is a PARM_DECL that contains unexpanded parameter
5409 packs. We have already complained about this in the
5410 check_for_bare_parameter_packs call, so just replace
5411 these types with ERROR_MARK_NODE. */
5412 TREE_TYPE (arg
) = error_mark_node
;
5413 TREE_VALUE (argtype
) = error_mark_node
;
5416 arg
= DECL_CHAIN (arg
);
5417 argtype
= TREE_CHAIN (argtype
);
5420 /* Check for bare parameter packs in the return type and the
5421 exception specifiers. */
5422 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5423 /* Errors were already issued, set return type to int
5424 as the frontend doesn't expect error_mark_node as
5426 TREE_TYPE (type
) = integer_type_node
;
5427 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5428 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5430 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5431 && TYPE_DECL_ALIAS_P (decl
))
5432 ? DECL_ORIGINAL_TYPE (decl
)
5433 : TREE_TYPE (decl
)))
5435 TREE_TYPE (decl
) = error_mark_node
;
5436 return error_mark_node
;
5440 return process_partial_specialization (decl
);
5442 args
= current_template_args ();
5445 || TREE_CODE (ctx
) == FUNCTION_DECL
5446 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5447 || (TREE_CODE (decl
) == TYPE_DECL
5448 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5449 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5451 if (DECL_LANG_SPECIFIC (decl
)
5452 && DECL_TEMPLATE_INFO (decl
)
5453 && DECL_TI_TEMPLATE (decl
))
5454 tmpl
= DECL_TI_TEMPLATE (decl
);
5455 /* If DECL is a TYPE_DECL for a class-template, then there won't
5456 be DECL_LANG_SPECIFIC. The information equivalent to
5457 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5458 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5459 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5460 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5462 /* Since a template declaration already existed for this
5463 class-type, we must be redeclaring it here. Make sure
5464 that the redeclaration is valid. */
5465 redeclare_class_template (TREE_TYPE (decl
),
5466 current_template_parms
,
5467 current_template_constraints ());
5468 /* We don't need to create a new TEMPLATE_DECL; just use the
5469 one we already had. */
5470 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5474 tmpl
= build_template_decl (decl
, current_template_parms
,
5478 if (DECL_LANG_SPECIFIC (decl
)
5479 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5481 /* A specialization of a member template of a template
5483 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5484 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5485 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5491 tree a
, t
, current
, parms
;
5493 tree tinfo
= get_template_info (decl
);
5497 error ("template definition of non-template %q#D", decl
);
5498 return error_mark_node
;
5501 tmpl
= TI_TEMPLATE (tinfo
);
5503 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5504 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5505 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5506 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5510 /* The declaration is a specialization of a member
5511 template, declared outside the class. Therefore, the
5512 innermost template arguments will be NULL, so we
5513 replace them with the arguments determined by the
5514 earlier call to check_explicit_specialization. */
5515 args
= DECL_TI_ARGS (decl
);
5518 = build_template_decl (decl
, current_template_parms
,
5520 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5521 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5522 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5523 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5524 DECL_TEMPLATE_INFO (new_tmpl
)
5525 = build_template_info (tmpl
, args
);
5527 register_specialization (new_tmpl
,
5528 most_general_template (tmpl
),
5534 /* Make sure the template headers we got make sense. */
5536 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5537 i
= TMPL_PARMS_DEPTH (parms
);
5538 if (TMPL_ARGS_DEPTH (args
) != i
)
5540 error ("expected %d levels of template parms for %q#D, got %d",
5541 i
, decl
, TMPL_ARGS_DEPTH (args
));
5542 DECL_INTERFACE_KNOWN (decl
) = 1;
5543 return error_mark_node
;
5546 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5548 a
= TMPL_ARGS_LEVEL (args
, i
);
5549 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5551 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5553 if (current
== decl
)
5554 error ("got %d template parameters for %q#D",
5555 TREE_VEC_LENGTH (a
), decl
);
5557 error ("got %d template parameters for %q#T",
5558 TREE_VEC_LENGTH (a
), current
);
5559 error (" but %d required", TREE_VEC_LENGTH (t
));
5560 /* Avoid crash in import_export_decl. */
5561 DECL_INTERFACE_KNOWN (decl
) = 1;
5562 return error_mark_node
;
5565 if (current
== decl
)
5567 else if (current
== NULL_TREE
)
5568 /* Can happen in erroneous input. */
5571 current
= get_containing_scope (current
);
5574 /* Check that the parms are used in the appropriate qualifying scopes
5575 in the declarator. */
5576 if (!comp_template_args
5578 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5580 error ("template arguments to %qD do not match original "
5581 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5582 if (!uses_template_parms (TI_ARGS (tinfo
)))
5583 inform (input_location
, "use %<template<>%> for"
5584 " an explicit specialization");
5585 /* Avoid crash in import_export_decl. */
5586 DECL_INTERFACE_KNOWN (decl
) = 1;
5587 return error_mark_node
;
5591 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5592 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5594 /* Push template declarations for global functions and types. Note
5595 that we do not try to push a global template friend declared in a
5596 template class; such a thing may well depend on the template
5597 parameters of the class. */
5598 if (new_template_p
&& !ctx
5599 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5601 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5602 if (tmpl
== error_mark_node
)
5603 return error_mark_node
;
5605 /* Hide template friend classes that haven't been declared yet. */
5606 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5608 DECL_ANTICIPATED (tmpl
) = 1;
5609 DECL_FRIEND_P (tmpl
) = 1;
5615 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5617 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5619 /* Give template template parms a DECL_CONTEXT of the template
5620 for which they are a parameter. */
5621 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5622 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5624 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5625 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5626 DECL_CONTEXT (parm
) = tmpl
;
5629 if (TREE_CODE (decl
) == TYPE_DECL
5630 && TYPE_DECL_ALIAS_P (decl
)
5631 && complex_alias_template_p (tmpl
))
5632 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5635 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5636 back to its most general template. If TMPL is a specialization,
5637 ARGS may only have the innermost set of arguments. Add the missing
5638 argument levels if necessary. */
5639 if (DECL_TEMPLATE_INFO (tmpl
))
5640 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5642 info
= build_template_info (tmpl
, args
);
5644 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5645 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5649 retrofit_lang_decl (decl
);
5650 if (DECL_LANG_SPECIFIC (decl
))
5651 DECL_TEMPLATE_INFO (decl
) = info
;
5654 if (flag_implicit_templates
5656 && TREE_PUBLIC (decl
)
5657 && VAR_OR_FUNCTION_DECL_P (decl
))
5658 /* Set DECL_COMDAT on template instantiations; if we force
5659 them to be emitted by explicit instantiation or -frepo,
5660 mark_needed will tell cgraph to do the right thing. */
5661 DECL_COMDAT (decl
) = true;
5663 return DECL_TEMPLATE_RESULT (tmpl
);
5667 push_template_decl (tree decl
)
5669 return push_template_decl_real (decl
, false);
5672 /* FN is an inheriting constructor that inherits from the constructor
5673 template INHERITED; turn FN into a constructor template with a matching
5677 add_inherited_template_parms (tree fn
, tree inherited
)
5680 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5681 inner_parms
= copy_node (inner_parms
);
5683 = tree_cons (size_int (processing_template_decl
+ 1),
5684 inner_parms
, current_template_parms
);
5685 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5686 tree args
= template_parms_to_args (parms
);
5687 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5688 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5689 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5690 DECL_ARTIFICIAL (tmpl
) = true;
5691 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5695 /* Called when a class template TYPE is redeclared with the indicated
5696 template PARMS, e.g.:
5698 template <class T> struct S;
5699 template <class T> struct S {}; */
5702 redeclare_class_template (tree type
, tree parms
, tree cons
)
5708 if (!TYPE_TEMPLATE_INFO (type
))
5710 error ("%qT is not a template type", type
);
5714 tmpl
= TYPE_TI_TEMPLATE (type
);
5715 if (!PRIMARY_TEMPLATE_P (tmpl
))
5716 /* The type is nested in some template class. Nothing to worry
5717 about here; there are no new template parameters for the nested
5723 error ("template specifiers not specified in declaration of %qD",
5728 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5729 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5731 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5733 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5734 "redeclared with %d template parameter",
5735 "redeclared with %d template parameters",
5736 TREE_VEC_LENGTH (parms
));
5737 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5738 "previous declaration %qD used %d template parameter",
5739 "previous declaration %qD used %d template parameters",
5740 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5744 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5751 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5752 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5755 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5756 if (error_operand_p (tmpl_parm
))
5759 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5760 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5761 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5763 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5765 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5766 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5767 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5768 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5769 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5770 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5771 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5772 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5773 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5775 error ("template parameter %q+#D", tmpl_parm
);
5776 error ("redeclared here as %q#D", parm
);
5780 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5782 /* We have in [temp.param]:
5784 A template-parameter may not be given default arguments
5785 by two different declarations in the same scope. */
5786 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5787 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5788 "original definition appeared here");
5792 if (parm_default
!= NULL_TREE
)
5793 /* Update the previous template parameters (which are the ones
5794 that will really count) with the new default value. */
5795 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5796 else if (tmpl_default
!= NULL_TREE
)
5797 /* Update the new parameters, too; they'll be used as the
5798 parameters for any members. */
5799 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5801 /* Give each template template parm in this redeclaration a
5802 DECL_CONTEXT of the template for which they are a parameter. */
5803 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5805 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5806 DECL_CONTEXT (parm
) = tmpl
;
5809 if (TREE_CODE (parm
) == TYPE_DECL
)
5810 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5813 // Cannot redeclare a class template with a different set of constraints.
5814 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5816 error_at (input_location
, "redeclaration %q#D with different "
5817 "constraints", tmpl
);
5818 inform (DECL_SOURCE_LOCATION (tmpl
),
5819 "original declaration appeared here");
5825 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5826 to be used when the caller has already checked
5827 (processing_template_decl
5828 && !instantiation_dependent_expression_p (expr)
5829 && potential_constant_expression (expr))
5830 and cleared processing_template_decl. */
5833 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5835 return tsubst_copy_and_build (expr
,
5838 /*in_decl=*/NULL_TREE
,
5839 /*function_p=*/false,
5840 /*integral_constant_expression_p=*/true);
5843 /* Simplify EXPR if it is a non-dependent expression. Returns the
5844 (possibly simplified) expression. */
5847 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5849 if (expr
== NULL_TREE
)
5852 /* If we're in a template, but EXPR isn't value dependent, simplify
5853 it. We're supposed to treat:
5855 template <typename T> void f(T[1 + 1]);
5856 template <typename T> void f(T[2]);
5858 as two declarations of the same function, for example. */
5859 if (processing_template_decl
5860 && is_nondependent_constant_expression (expr
))
5862 processing_template_decl_sentinel s
;
5863 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5869 instantiate_non_dependent_expr (tree expr
)
5871 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5874 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5875 an uninstantiated expression. */
5878 instantiate_non_dependent_or_null (tree expr
)
5880 if (expr
== NULL_TREE
)
5882 if (processing_template_decl
)
5884 if (!is_nondependent_constant_expression (expr
))
5888 processing_template_decl_sentinel s
;
5889 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5895 /* True iff T is a specialization of a variable template. */
5898 variable_template_specialization_p (tree t
)
5900 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5902 tree tmpl
= DECL_TI_TEMPLATE (t
);
5903 return variable_template_p (tmpl
);
5906 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5907 template declaration, or a TYPE_DECL for an alias declaration. */
5910 alias_type_or_template_p (tree t
)
5914 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5917 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5918 || DECL_ALIAS_TEMPLATE_P (t
));
5921 /* Return TRUE iff T is a specialization of an alias template. */
5924 alias_template_specialization_p (const_tree t
)
5926 /* It's an alias template specialization if it's an alias and its
5927 TYPE_NAME is a specialization of a primary template. */
5928 if (TYPE_ALIAS_P (t
))
5929 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5930 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5935 /* An alias template is complex from a SFINAE perspective if a template-id
5936 using that alias can be ill-formed when the expansion is not, as with
5937 the void_t template. We determine this by checking whether the
5938 expansion for the alias template uses all its template parameters. */
5940 struct uses_all_template_parms_data
5947 uses_all_template_parms_r (tree t
, void *data_
)
5949 struct uses_all_template_parms_data
&data
5950 = *(struct uses_all_template_parms_data
*)data_
;
5951 tree idx
= get_template_parm_index (t
);
5953 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5954 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5959 complex_alias_template_p (const_tree tmpl
)
5961 struct uses_all_template_parms_data data
;
5962 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5963 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5964 data
.level
= TMPL_PARMS_DEPTH (parms
);
5965 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5966 data
.seen
= XALLOCAVEC (bool, len
);
5967 for (int i
= 0; i
< len
; ++i
)
5968 data
.seen
[i
] = false;
5970 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5971 for (int i
= 0; i
< len
; ++i
)
5977 /* Return TRUE iff T is a specialization of a complex alias template with
5978 dependent template-arguments. */
5981 dependent_alias_template_spec_p (const_tree t
)
5983 if (!alias_template_specialization_p (t
))
5986 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5987 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5990 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5991 if (!any_dependent_template_arguments_p (args
))
5997 /* Return the number of innermost template parameters in TMPL. */
6000 num_innermost_template_parms (tree tmpl
)
6002 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6003 return TREE_VEC_LENGTH (parms
);
6006 /* Return either TMPL or another template that it is equivalent to under DR
6007 1286: An alias that just changes the name of a template is equivalent to
6008 the other template. */
6011 get_underlying_template (tree tmpl
)
6013 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6014 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6016 /* Determine if the alias is equivalent to an underlying template. */
6017 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6018 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6022 tree underlying
= TI_TEMPLATE (tinfo
);
6023 if (!PRIMARY_TEMPLATE_P (underlying
)
6024 || (num_innermost_template_parms (tmpl
)
6025 != num_innermost_template_parms (underlying
)))
6028 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6029 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6030 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6033 /* Alias is equivalent. Strip it and repeat. */
6040 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6041 must be a reference-to-function or a pointer-to-function type, as specified
6042 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6043 and check that the resulting function has external linkage. */
6046 convert_nontype_argument_function (tree type
, tree expr
,
6047 tsubst_flags_t complain
)
6051 linkage_kind linkage
;
6053 fn
= instantiate_type (type
, fns
, tf_none
);
6054 if (fn
== error_mark_node
)
6055 return error_mark_node
;
6057 if (value_dependent_expression_p (fn
))
6060 fn_no_ptr
= strip_fnptr_conv (fn
);
6061 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6062 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6063 if (BASELINK_P (fn_no_ptr
))
6064 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6066 /* [temp.arg.nontype]/1
6068 A template-argument for a non-type, non-template template-parameter
6071 -- the address of an object or function with external [C++11: or
6072 internal] linkage. */
6074 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6076 if (complain
& tf_error
)
6078 error ("%qE is not a valid template argument for type %qT",
6080 if (TYPE_PTR_P (type
))
6081 inform (input_location
, "it must be the address of a function "
6082 "with external linkage");
6084 inform (input_location
, "it must be the name of a function with "
6085 "external linkage");
6090 linkage
= decl_linkage (fn_no_ptr
);
6091 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6093 if (complain
& tf_error
)
6095 if (cxx_dialect
>= cxx11
)
6096 error ("%qE is not a valid template argument for type %qT "
6097 "because %qD has no linkage",
6098 expr
, type
, fn_no_ptr
);
6100 error ("%qE is not a valid template argument for type %qT "
6101 "because %qD does not have external linkage",
6102 expr
, type
, fn_no_ptr
);
6108 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6109 fn
= build_address (fn
);
6110 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6111 fn
= build_nop (type
, fn
);
6116 /* Subroutine of convert_nontype_argument.
6117 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6118 Emit an error otherwise. */
6121 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6122 tsubst_flags_t complain
)
6124 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6125 tree orig_expr
= expr
;
6127 if (null_ptr_cst_p (expr
))
6129 if (TREE_CODE (expr
) == PTRMEM_CST
6130 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6131 PTRMEM_CST_CLASS (expr
)))
6133 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6135 if (processing_template_decl
6136 && TREE_CODE (expr
) == ADDR_EXPR
6137 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6139 if (complain
& tf_error
)
6141 error_at (loc
, "%qE is not a valid template argument for type %qT",
6143 if (TREE_CODE (expr
) != PTRMEM_CST
)
6144 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6146 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6151 /* Returns TRUE iff the address of OP is value-dependent.
6153 14.6.2.4 [temp.dep.temp]:
6154 A non-integral non-type template-argument is dependent if its type is
6155 dependent or it has either of the following forms
6158 and contains a nested-name-specifier which specifies a class-name that
6159 names a dependent type.
6161 We generalize this to just say that the address of a member of a
6162 dependent class is value-dependent; the above doesn't cover the
6163 address of a static data member named with an unqualified-id. */
6166 has_value_dependent_address (tree op
)
6168 /* We could use get_inner_reference here, but there's no need;
6169 this is only relevant for template non-type arguments, which
6170 can only be expressed as &id-expression. */
6173 tree ctx
= CP_DECL_CONTEXT (op
);
6174 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6181 /* The next set of functions are used for providing helpful explanatory
6182 diagnostics for failed overload resolution. Their messages should be
6183 indented by two spaces for consistency with the messages in
6187 unify_success (bool /*explain_p*/)
6192 /* Other failure functions should call this one, to provide a single function
6193 for setting a breakpoint on. */
6196 unify_invalid (bool /*explain_p*/)
6202 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6205 inform (input_location
,
6206 " couldn't deduce template parameter %qD", parm
);
6207 return unify_invalid (explain_p
);
6211 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6214 inform (input_location
,
6215 " types %qT and %qT have incompatible cv-qualifiers",
6217 return unify_invalid (explain_p
);
6221 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6224 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6225 return unify_invalid (explain_p
);
6229 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6232 inform (input_location
,
6233 " template parameter %qD is not a parameter pack, but "
6236 return unify_invalid (explain_p
);
6240 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6243 inform (input_location
,
6244 " template argument %qE does not match "
6245 "pointer-to-member constant %qE",
6247 return unify_invalid (explain_p
);
6251 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6254 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6255 return unify_invalid (explain_p
);
6259 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6262 inform (input_location
,
6263 " inconsistent parameter pack deduction with %qT and %qT",
6265 return unify_invalid (explain_p
);
6269 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6274 inform (input_location
,
6275 " deduced conflicting types for parameter %qT (%qT and %qT)",
6276 parm
, first
, second
);
6278 inform (input_location
,
6279 " deduced conflicting values for non-type parameter "
6280 "%qE (%qE and %qE)", parm
, first
, second
);
6282 return unify_invalid (explain_p
);
6286 unify_vla_arg (bool explain_p
, tree arg
)
6289 inform (input_location
,
6290 " variable-sized array type %qT is not "
6291 "a valid template argument",
6293 return unify_invalid (explain_p
);
6297 unify_method_type_error (bool explain_p
, tree arg
)
6300 inform (input_location
,
6301 " member function type %qT is not a valid template argument",
6303 return unify_invalid (explain_p
);
6307 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6312 inform_n (input_location
, wanted
,
6313 " candidate expects at least %d argument, %d provided",
6314 " candidate expects at least %d arguments, %d provided",
6317 inform_n (input_location
, wanted
,
6318 " candidate expects %d argument, %d provided",
6319 " candidate expects %d arguments, %d provided",
6322 return unify_invalid (explain_p
);
6326 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6328 return unify_arity (explain_p
, have
, wanted
);
6332 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6333 bool least_p
= false)
6335 return unify_arity (explain_p
, have
, wanted
, least_p
);
6339 unify_arg_conversion (bool explain_p
, tree to_type
,
6340 tree from_type
, tree arg
)
6343 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6344 " cannot convert %qE (type %qT) to type %qT",
6345 arg
, from_type
, to_type
);
6346 return unify_invalid (explain_p
);
6350 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6351 tree parm
, tree arg
)
6356 case tbr_ambiguous_baseclass
:
6357 inform (input_location
, " %qT is an ambiguous base class of %qT",
6361 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6364 return unify_invalid (explain_p
);
6368 unify_inconsistent_template_template_parameters (bool explain_p
)
6371 inform (input_location
,
6372 " template parameters of a template template argument are "
6373 "inconsistent with other deduced template arguments");
6374 return unify_invalid (explain_p
);
6378 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6381 inform (input_location
,
6382 " can't deduce a template for %qT from non-template type %qT",
6384 return unify_invalid (explain_p
);
6388 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6391 inform (input_location
,
6392 " template argument %qE does not match %qE", arg
, parm
);
6393 return unify_invalid (explain_p
);
6396 /* Attempt to convert the non-type template parameter EXPR to the
6397 indicated TYPE. If the conversion is successful, return the
6398 converted value. If the conversion is unsuccessful, return
6399 NULL_TREE if we issued an error message, or error_mark_node if we
6400 did not. We issue error messages for out-and-out bad template
6401 parameters, but not simply because the conversion failed, since we
6402 might be just trying to do argument deduction. Both TYPE and EXPR
6403 must be non-dependent.
6405 The conversion follows the special rules described in
6406 [temp.arg.nontype], and it is much more strict than an implicit
6409 This function is called twice for each template argument (see
6410 lookup_template_class for a more accurate description of this
6411 problem). This means that we need to handle expressions which
6412 are not valid in a C++ source, but can be created from the
6413 first call (for instance, casts to perform conversions). These
6414 hacks can go away after we fix the double coercion problem. */
6417 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6420 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6421 tree orig_expr
= expr
;
6423 /* Detect immediately string literals as invalid non-type argument.
6424 This special-case is not needed for correctness (we would easily
6425 catch this later), but only to provide better diagnostic for this
6426 common user mistake. As suggested by DR 100, we do not mention
6427 linkage issues in the diagnostic as this is not the point. */
6428 /* FIXME we're making this OK. */
6429 if (TREE_CODE (expr
) == STRING_CST
)
6431 if (complain
& tf_error
)
6432 error ("%qE is not a valid template argument for type %qT "
6433 "because string literals can never be used in this context",
6438 /* Add the ADDR_EXPR now for the benefit of
6439 value_dependent_expression_p. */
6440 if (TYPE_PTROBV_P (type
)
6441 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6443 expr
= decay_conversion (expr
, complain
);
6444 if (expr
== error_mark_node
)
6445 return error_mark_node
;
6448 /* If we are in a template, EXPR may be non-dependent, but still
6449 have a syntactic, rather than semantic, form. For example, EXPR
6450 might be a SCOPE_REF, rather than the VAR_DECL to which the
6451 SCOPE_REF refers. Preserving the qualifying scope is necessary
6452 so that access checking can be performed when the template is
6453 instantiated -- but here we need the resolved form so that we can
6454 convert the argument. */
6455 bool non_dep
= false;
6456 if (TYPE_REF_OBJ_P (type
)
6457 && has_value_dependent_address (expr
))
6458 /* If we want the address and it's value-dependent, don't fold. */;
6459 else if (processing_template_decl
6460 && is_nondependent_constant_expression (expr
))
6462 if (error_operand_p (expr
))
6463 return error_mark_node
;
6464 expr_type
= TREE_TYPE (expr
);
6466 /* If the argument is non-dependent, perform any conversions in
6467 non-dependent context as well. */
6468 processing_template_decl_sentinel
s (non_dep
);
6470 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6472 if (value_dependent_expression_p (expr
))
6473 expr
= canonicalize_expr_argument (expr
, complain
);
6475 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6476 to a non-type argument of "nullptr". */
6477 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6478 expr
= fold_simple (convert (type
, expr
));
6480 /* In C++11, integral or enumeration non-type template arguments can be
6481 arbitrary constant expressions. Pointer and pointer to
6482 member arguments can be general constant expressions that evaluate
6483 to a null value, but otherwise still need to be of a specific form. */
6484 if (cxx_dialect
>= cxx11
)
6486 if (TREE_CODE (expr
) == PTRMEM_CST
)
6487 /* A PTRMEM_CST is already constant, and a valid template
6488 argument for a parameter of pointer to member type, we just want
6489 to leave it in that form rather than lower it to a
6491 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6492 || cxx_dialect
>= cxx17
)
6494 /* C++17: A template-argument for a non-type template-parameter shall
6495 be a converted constant expression (8.20) of the type of the
6496 template-parameter. */
6497 expr
= build_converted_constant_expr (type
, expr
, complain
);
6498 if (expr
== error_mark_node
)
6499 return error_mark_node
;
6500 expr
= maybe_constant_value (expr
);
6501 expr
= convert_from_reference (expr
);
6503 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6505 tree folded
= maybe_constant_value (expr
);
6506 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6507 : null_member_pointer_value_p (folded
))
6512 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6513 expr
= mark_lvalue_use (expr
);
6515 expr
= mark_rvalue_use (expr
);
6517 /* HACK: Due to double coercion, we can get a
6518 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6519 which is the tree that we built on the first call (see
6520 below when coercing to reference to object or to reference to
6521 function). We just strip everything and get to the arg.
6522 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6524 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6526 tree probe_type
, probe
= expr
;
6527 if (REFERENCE_REF_P (probe
))
6528 probe
= TREE_OPERAND (probe
, 0);
6529 probe_type
= TREE_TYPE (probe
);
6530 if (TREE_CODE (probe
) == NOP_EXPR
)
6532 /* ??? Maybe we could use convert_from_reference here, but we
6533 would need to relax its constraints because the NOP_EXPR
6534 could actually change the type to something more cv-qualified,
6535 and this is not folded by convert_from_reference. */
6536 tree addr
= TREE_OPERAND (probe
, 0);
6537 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6538 && TREE_CODE (addr
) == ADDR_EXPR
6539 && TYPE_PTR_P (TREE_TYPE (addr
))
6540 && (same_type_ignoring_top_level_qualifiers_p
6541 (TREE_TYPE (probe_type
),
6542 TREE_TYPE (TREE_TYPE (addr
)))))
6544 expr
= TREE_OPERAND (addr
, 0);
6545 expr_type
= TREE_TYPE (probe_type
);
6550 /* [temp.arg.nontype]/5, bullet 1
6552 For a non-type template-parameter of integral or enumeration type,
6553 integral promotions (_conv.prom_) and integral conversions
6554 (_conv.integral_) are applied. */
6555 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6557 if (cxx_dialect
< cxx11
)
6559 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6560 t
= maybe_constant_value (t
);
6561 if (t
!= error_mark_node
)
6565 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6566 return error_mark_node
;
6568 /* Notice that there are constant expressions like '4 % 0' which
6569 do not fold into integer constants. */
6570 if (TREE_CODE (expr
) != INTEGER_CST
6571 && !value_dependent_expression_p (expr
))
6573 if (complain
& tf_error
)
6575 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6576 if (!require_potential_constant_expression (expr
))
6577 expr
= error_mark_node
;
6579 expr
= cxx_constant_value (expr
);
6580 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6581 inform (loc
, "in template argument for type %qT ", type
);
6582 if (expr
== error_mark_node
)
6584 /* else cxx_constant_value complained but gave us
6585 a real constant, so go ahead. */
6586 if (TREE_CODE (expr
) != INTEGER_CST
)
6588 /* Some assemble time constant expressions like
6589 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6590 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6591 as we can emit them into .rodata initializers of
6592 variables, yet they can't fold into an INTEGER_CST at
6593 compile time. Refuse them here. */
6594 gcc_checking_assert (reduced_constant_expression_p (expr
));
6595 error_at (loc
, "template argument %qE for type %qT not "
6596 "a constant integer", expr
, type
);
6604 /* Avoid typedef problems. */
6605 if (TREE_TYPE (expr
) != type
)
6606 expr
= fold_convert (type
, expr
);
6608 /* [temp.arg.nontype]/5, bullet 2
6610 For a non-type template-parameter of type pointer to object,
6611 qualification conversions (_conv.qual_) and the array-to-pointer
6612 conversion (_conv.array_) are applied. */
6613 else if (TYPE_PTROBV_P (type
))
6615 tree decayed
= expr
;
6617 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6618 decay_conversion or an explicit cast. If it's a problematic cast,
6619 we'll complain about it below. */
6620 if (TREE_CODE (expr
) == NOP_EXPR
)
6624 if (TREE_CODE (probe
) == ADDR_EXPR
6625 && TYPE_PTR_P (TREE_TYPE (probe
)))
6628 expr_type
= TREE_TYPE (expr
);
6632 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6634 A template-argument for a non-type, non-template template-parameter
6635 shall be one of: [...]
6637 -- the name of a non-type template-parameter;
6638 -- the address of an object or function with external linkage, [...]
6639 expressed as "& id-expression" where the & is optional if the name
6640 refers to a function or array, or if the corresponding
6641 template-parameter is a reference.
6643 Here, we do not care about functions, as they are invalid anyway
6644 for a parameter of type pointer-to-object. */
6646 if (value_dependent_expression_p (expr
))
6647 /* Non-type template parameters are OK. */
6649 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6650 /* Null pointer values are OK in C++11. */;
6651 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6655 if (complain
& tf_error
)
6656 error ("%qD is not a valid template argument "
6657 "because %qD is a variable, not the address of "
6658 "a variable", orig_expr
, expr
);
6661 if (POINTER_TYPE_P (expr_type
))
6663 if (complain
& tf_error
)
6664 error ("%qE is not a valid template argument for %qT "
6665 "because it is not the address of a variable",
6669 /* Other values, like integer constants, might be valid
6670 non-type arguments of some other type. */
6671 return error_mark_node
;
6675 tree decl
= TREE_OPERAND (expr
, 0);
6679 if (complain
& tf_error
)
6680 error ("%qE is not a valid template argument of type %qT "
6681 "because %qE is not a variable", orig_expr
, type
, decl
);
6684 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6686 if (complain
& tf_error
)
6687 error ("%qE is not a valid template argument of type %qT "
6688 "because %qD does not have external linkage",
6689 orig_expr
, type
, decl
);
6692 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6693 && decl_linkage (decl
) == lk_none
)
6695 if (complain
& tf_error
)
6696 error ("%qE is not a valid template argument of type %qT "
6697 "because %qD has no linkage", orig_expr
, type
, decl
);
6700 /* C++17: For a non-type template-parameter of reference or pointer
6701 type, the value of the constant expression shall not refer to (or
6702 for a pointer type, shall not be the address of):
6703 * a subobject (4.5),
6704 * a temporary object (15.2),
6705 * a string literal (5.13.5),
6706 * the result of a typeid expression (8.2.8), or
6707 * a predefined __func__ variable (11.4.1). */
6708 else if (DECL_ARTIFICIAL (decl
))
6710 if (complain
& tf_error
)
6711 error ("the address of %qD is not a valid template argument",
6715 else if (!same_type_ignoring_top_level_qualifiers_p
6716 (strip_array_types (TREE_TYPE (type
)),
6717 strip_array_types (TREE_TYPE (decl
))))
6719 if (complain
& tf_error
)
6720 error ("the address of the %qT subobject of %qD is not a "
6721 "valid template argument", TREE_TYPE (type
), decl
);
6724 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6726 if (complain
& tf_error
)
6727 error ("the address of %qD is not a valid template argument "
6728 "because it does not have static storage duration",
6736 expr
= perform_qualification_conversions (type
, expr
);
6737 if (expr
== error_mark_node
)
6738 return error_mark_node
;
6740 /* [temp.arg.nontype]/5, bullet 3
6742 For a non-type template-parameter of type reference to object, no
6743 conversions apply. The type referred to by the reference may be more
6744 cv-qualified than the (otherwise identical) type of the
6745 template-argument. The template-parameter is bound directly to the
6746 template-argument, which must be an lvalue. */
6747 else if (TYPE_REF_OBJ_P (type
))
6749 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6751 return error_mark_node
;
6753 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6755 if (complain
& tf_error
)
6756 error ("%qE is not a valid template argument for type %qT "
6757 "because of conflicts in cv-qualification", expr
, type
);
6761 if (!lvalue_p (expr
))
6763 if (complain
& tf_error
)
6764 error ("%qE is not a valid template argument for type %qT "
6765 "because it is not an lvalue", expr
, type
);
6769 /* [temp.arg.nontype]/1
6771 A template-argument for a non-type, non-template template-parameter
6772 shall be one of: [...]
6774 -- the address of an object or function with external linkage. */
6775 if (INDIRECT_REF_P (expr
)
6776 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6778 expr
= TREE_OPERAND (expr
, 0);
6781 if (complain
& tf_error
)
6782 error ("%q#D is not a valid template argument for type %qT "
6783 "because a reference variable does not have a constant "
6784 "address", expr
, type
);
6789 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6790 && value_dependent_expression_p (expr
))
6791 /* OK, dependent reference. We don't want to ask whether a DECL is
6792 itself value-dependent, since what we want here is its address. */;
6797 if (complain
& tf_error
)
6798 error ("%qE is not a valid template argument for type %qT "
6799 "because it is not an object with linkage",
6804 /* DR 1155 allows internal linkage in C++11 and up. */
6805 linkage_kind linkage
= decl_linkage (expr
);
6806 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6808 if (complain
& tf_error
)
6809 error ("%qE is not a valid template argument for type %qT "
6810 "because object %qD does not have linkage",
6815 expr
= build_address (expr
);
6818 if (!same_type_p (type
, TREE_TYPE (expr
)))
6819 expr
= build_nop (type
, expr
);
6821 /* [temp.arg.nontype]/5, bullet 4
6823 For a non-type template-parameter of type pointer to function, only
6824 the function-to-pointer conversion (_conv.func_) is applied. If the
6825 template-argument represents a set of overloaded functions (or a
6826 pointer to such), the matching function is selected from the set
6828 else if (TYPE_PTRFN_P (type
))
6830 /* If the argument is a template-id, we might not have enough
6831 context information to decay the pointer. */
6832 if (!type_unknown_p (expr_type
))
6834 expr
= decay_conversion (expr
, complain
);
6835 if (expr
== error_mark_node
)
6836 return error_mark_node
;
6839 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6840 /* Null pointer values are OK in C++11. */
6841 return perform_qualification_conversions (type
, expr
);
6843 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6844 if (!expr
|| expr
== error_mark_node
)
6847 /* [temp.arg.nontype]/5, bullet 5
6849 For a non-type template-parameter of type reference to function, no
6850 conversions apply. If the template-argument represents a set of
6851 overloaded functions, the matching function is selected from the set
6853 else if (TYPE_REFFN_P (type
))
6855 if (TREE_CODE (expr
) == ADDR_EXPR
)
6857 if (complain
& tf_error
)
6859 error ("%qE is not a valid template argument for type %qT "
6860 "because it is a pointer", expr
, type
);
6861 inform (input_location
, "try using %qE instead",
6862 TREE_OPERAND (expr
, 0));
6867 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6868 if (!expr
|| expr
== error_mark_node
)
6871 /* [temp.arg.nontype]/5, bullet 6
6873 For a non-type template-parameter of type pointer to member function,
6874 no conversions apply. If the template-argument represents a set of
6875 overloaded member functions, the matching member function is selected
6876 from the set (_over.over_). */
6877 else if (TYPE_PTRMEMFUNC_P (type
))
6879 expr
= instantiate_type (type
, expr
, tf_none
);
6880 if (expr
== error_mark_node
)
6881 return error_mark_node
;
6883 /* [temp.arg.nontype] bullet 1 says the pointer to member
6884 expression must be a pointer-to-member constant. */
6885 if (!value_dependent_expression_p (expr
)
6886 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6889 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6890 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6891 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6892 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6894 /* [temp.arg.nontype]/5, bullet 7
6896 For a non-type template-parameter of type pointer to data member,
6897 qualification conversions (_conv.qual_) are applied. */
6898 else if (TYPE_PTRDATAMEM_P (type
))
6900 /* [temp.arg.nontype] bullet 1 says the pointer to member
6901 expression must be a pointer-to-member constant. */
6902 if (!value_dependent_expression_p (expr
)
6903 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6906 expr
= perform_qualification_conversions (type
, expr
);
6907 if (expr
== error_mark_node
)
6910 else if (NULLPTR_TYPE_P (type
))
6912 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
6914 if (complain
& tf_error
)
6915 error ("%qE is not a valid template argument for type %qT "
6916 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6921 /* A template non-type parameter must be one of the above. */
6925 /* Sanity check: did we actually convert the argument to the
6927 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6928 (type
, TREE_TYPE (expr
)));
6929 return convert_from_reference (expr
);
6932 /* Subroutine of coerce_template_template_parms, which returns 1 if
6933 PARM_PARM and ARG_PARM match using the rule for the template
6934 parameters of template template parameters. Both PARM and ARG are
6935 template parameters; the rest of the arguments are the same as for
6936 coerce_template_template_parms.
6939 coerce_template_template_parm (tree parm
,
6941 tsubst_flags_t complain
,
6945 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6946 || parm
== NULL_TREE
|| error_operand_p (parm
))
6949 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6952 switch (TREE_CODE (parm
))
6955 /* We encounter instantiations of templates like
6956 template <template <template <class> class> class TT>
6959 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6960 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6962 if (!coerce_template_template_parms
6963 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6969 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6970 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6971 /* Argument is a parameter pack but parameter is not. */
6976 /* The tsubst call is used to handle cases such as
6978 template <int> class C {};
6979 template <class T, template <T> class TT> class D {};
6982 i.e. the parameter list of TT depends on earlier parameters. */
6983 if (!uses_template_parms (TREE_TYPE (arg
)))
6985 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6986 if (!uses_template_parms (t
)
6987 && !same_type_p (t
, TREE_TYPE (arg
)))
6991 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6992 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6993 /* Argument is a parameter pack but parameter is not. */
7005 /* Coerce template argument list ARGLIST for use with template
7006 template-parameter TEMPL. */
7009 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7010 tsubst_flags_t complain
)
7012 /* Consider an example where a template template parameter declared as
7014 template <class T, class U = std::allocator<T> > class TT
7016 The template parameter level of T and U are one level larger than
7017 of TT. To proper process the default argument of U, say when an
7018 instantiation `TT<int>' is seen, we need to build the full
7019 arguments containing {int} as the innermost level. Outer levels,
7020 available when not appearing as default template argument, can be
7021 obtained from the arguments of the enclosing template.
7023 Suppose that TT is later substituted with std::vector. The above
7024 instantiation is `TT<int, std::allocator<T> >' with TT at
7025 level 1, and T at level 2, while the template arguments at level 1
7026 becomes {std::vector} and the inner level 2 is {int}. */
7028 tree outer
= DECL_CONTEXT (templ
);
7031 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7032 /* We want arguments for the partial specialization, not arguments for
7033 the primary template. */
7034 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7036 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7038 else if (current_template_parms
)
7040 /* This is an argument of the current template, so we haven't set
7041 DECL_CONTEXT yet. */
7042 tree relevant_template_parms
;
7044 /* Parameter levels that are greater than the level of the given
7045 template template parm are irrelevant. */
7046 relevant_template_parms
= current_template_parms
;
7047 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7048 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7049 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7051 outer
= template_parms_to_args (relevant_template_parms
);
7055 arglist
= add_to_template_args (outer
, arglist
);
7057 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7058 return coerce_template_parms (parmlist
, arglist
, templ
,
7060 /*require_all_args=*/true,
7061 /*use_default_args=*/true);
7064 /* A cache of template template parameters with match-all default
7066 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7068 store_defaulted_ttp (tree v
, tree t
)
7070 if (!defaulted_ttp_cache
)
7071 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7072 defaulted_ttp_cache
->put (v
, t
);
7075 lookup_defaulted_ttp (tree v
)
7077 if (defaulted_ttp_cache
)
7078 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7083 /* T is a bound template template-parameter. Copy its arguments into default
7084 arguments of the template template-parameter's template parameters. */
7087 add_defaults_to_ttp (tree otmpl
)
7089 if (tree c
= lookup_defaulted_ttp (otmpl
))
7092 tree ntmpl
= copy_node (otmpl
);
7094 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7095 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7096 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7097 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7098 TYPE_NAME (ntype
) = ntmpl
;
7099 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7101 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7102 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7103 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7104 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7106 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7107 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7108 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7109 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7110 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7112 tree o
= TREE_VEC_ELT (vec
, i
);
7113 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7115 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7116 TREE_PURPOSE (n
) = any_targ_node
;
7120 store_defaulted_ttp (otmpl
, ntmpl
);
7124 /* ARG is a bound potential template template-argument, and PARGS is a list
7125 of arguments for the corresponding template template-parameter. Adjust
7126 PARGS as appropriate for application to ARG's template, and if ARG is a
7127 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7128 arguments to the template template parameter. */
7131 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7133 ++processing_template_decl
;
7134 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7135 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7137 /* When comparing two template template-parameters in partial ordering,
7138 rewrite the one currently being used as an argument to have default
7139 arguments for all parameters. */
7140 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7141 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7142 if (pargs
!= error_mark_node
)
7143 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7144 TYPE_TI_ARGS (arg
));
7149 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7150 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7151 /*require_all*/true,
7152 /*use_default*/true);
7154 --processing_template_decl
;
7158 /* Subroutine of unify for the case when PARM is a
7159 BOUND_TEMPLATE_TEMPLATE_PARM. */
7162 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7165 tree parmvec
= TYPE_TI_ARGS (parm
);
7166 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7168 /* The template template parm might be variadic and the argument
7169 not, so flatten both argument lists. */
7170 parmvec
= expand_template_argument_pack (parmvec
);
7171 argvec
= expand_template_argument_pack (argvec
);
7175 /* In keeping with P0522R0, adjust P's template arguments
7176 to apply to A's template; then flatten it again. */
7177 tree nparmvec
= parmvec
;
7178 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7179 nparmvec
= expand_template_argument_pack (nparmvec
);
7181 if (unify (tparms
, targs
, nparmvec
, argvec
,
7182 UNIFY_ALLOW_NONE
, explain_p
))
7185 /* If the P0522 adjustment eliminated a pack expansion, deduce
7188 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7189 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7190 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7195 /* Deduce arguments T, i from TT<T> or TT<i>.
7196 We check each element of PARMVEC and ARGVEC individually
7197 rather than the whole TREE_VEC since they can have
7198 different number of elements, which is allowed under N2555. */
7200 int len
= TREE_VEC_LENGTH (parmvec
);
7202 /* Check if the parameters end in a pack, making them
7204 int parm_variadic_p
= 0;
7206 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7207 parm_variadic_p
= 1;
7209 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7210 /* If the template argument list of P contains a pack
7211 expansion that is not the last template argument, the
7212 entire template argument list is a non-deduced
7214 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7215 return unify_success (explain_p
);
7217 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7218 return unify_too_few_arguments (explain_p
,
7219 TREE_VEC_LENGTH (argvec
), len
);
7221 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7222 if (unify (tparms
, targs
,
7223 TREE_VEC_ELT (parmvec
, i
),
7224 TREE_VEC_ELT (argvec
, i
),
7225 UNIFY_ALLOW_NONE
, explain_p
))
7229 && unify_pack_expansion (tparms
, targs
,
7232 /*subr=*/true, explain_p
))
7239 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7240 template template parameters. Both PARM_PARMS and ARG_PARMS are
7241 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7244 Consider the example:
7245 template <class T> class A;
7246 template<template <class U> class TT> class B;
7248 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7249 the parameters to A, and OUTER_ARGS contains A. */
7252 coerce_template_template_parms (tree parm_parms
,
7254 tsubst_flags_t complain
,
7258 int nparms
, nargs
, i
;
7262 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7263 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7265 nparms
= TREE_VEC_LENGTH (parm_parms
);
7266 nargs
= TREE_VEC_LENGTH (arg_parms
);
7270 /* P0522R0: A template template-parameter P is at least as specialized as
7271 a template template-argument A if, given the following rewrite to two
7272 function templates, the function template corresponding to P is at
7273 least as specialized as the function template corresponding to A
7274 according to the partial ordering rules for function templates
7275 ([temp.func.order]). Given an invented class template X with the
7276 template parameter list of A (including default arguments):
7278 * Each of the two function templates has the same template parameters,
7279 respectively, as P or A.
7281 * Each function template has a single function parameter whose type is
7282 a specialization of X with template arguments corresponding to the
7283 template parameters from the respective function template where, for
7284 each template parameter PP in the template parameter list of the
7285 function template, a corresponding template argument AA is formed. If
7286 PP declares a parameter pack, then AA is the pack expansion
7287 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7289 If the rewrite produces an invalid type, then P is not at least as
7290 specialized as A. */
7292 /* So coerce P's args to apply to A's parms, and then deduce between A's
7293 args and the converted args. If that succeeds, A is at least as
7294 specialized as P, so they match.*/
7295 tree pargs
= template_parms_level_to_args (parm_parms
);
7296 ++processing_template_decl
;
7297 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7298 /*require_all*/true, /*use_default*/true);
7299 --processing_template_decl
;
7300 if (pargs
!= error_mark_node
)
7302 tree targs
= make_tree_vec (nargs
);
7303 tree aargs
= template_parms_level_to_args (arg_parms
);
7304 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7310 /* Determine whether we have a parameter pack at the end of the
7311 template template parameter's template parameter list. */
7312 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7314 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7316 if (error_operand_p (parm
))
7319 switch (TREE_CODE (parm
))
7323 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7328 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7338 && !(variadic_p
&& nargs
>= nparms
- 1))
7341 /* Check all of the template parameters except the parameter pack at
7342 the end (if any). */
7343 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7345 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7346 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7349 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7350 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7352 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7360 /* Check each of the template parameters in the template
7361 argument against the template parameter pack at the end of
7362 the template template parameter. */
7363 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7366 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7368 for (; i
< nargs
; ++i
)
7370 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7373 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7375 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7384 /* Verifies that the deduced template arguments (in TARGS) for the
7385 template template parameters (in TPARMS) represent valid bindings,
7386 by comparing the template parameter list of each template argument
7387 to the template parameter list of its corresponding template
7388 template parameter, in accordance with DR150. This
7389 routine can only be called after all template arguments have been
7390 deduced. It will return TRUE if all of the template template
7391 parameter bindings are okay, FALSE otherwise. */
7393 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7395 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7398 /* We're dealing with template parms in this process. */
7399 ++processing_template_decl
;
7401 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7403 for (i
= 0; i
< ntparms
; ++i
)
7405 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7406 tree targ
= TREE_VEC_ELT (targs
, i
);
7408 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7410 tree packed_args
= NULL_TREE
;
7413 if (ARGUMENT_PACK_P (targ
))
7415 /* Look inside the argument pack. */
7416 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7417 len
= TREE_VEC_LENGTH (packed_args
);
7420 for (idx
= 0; idx
< len
; ++idx
)
7422 tree targ_parms
= NULL_TREE
;
7425 /* Extract the next argument from the argument
7427 targ
= TREE_VEC_ELT (packed_args
, idx
);
7429 if (PACK_EXPANSION_P (targ
))
7430 /* Look at the pattern of the pack expansion. */
7431 targ
= PACK_EXPANSION_PATTERN (targ
);
7433 /* Extract the template parameters from the template
7435 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7436 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7437 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7438 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7440 /* Verify that we can coerce the template template
7441 parameters from the template argument to the template
7442 parameter. This requires an exact match. */
7444 && !coerce_template_template_parms
7445 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7460 --processing_template_decl
;
7464 /* Since type attributes aren't mangled, we need to strip them from
7465 template type arguments. */
7468 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7470 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7472 bool removed_attributes
= false;
7473 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7474 if (removed_attributes
7475 && (complain
& tf_warning
))
7476 warning (OPT_Wignored_attributes
,
7477 "ignoring attributes on template argument %qT", arg
);
7481 /* And from inside dependent non-type arguments like sizeof(Type). */
7484 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7486 if (!arg
|| arg
== error_mark_node
)
7488 bool removed_attributes
= false;
7489 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7490 if (removed_attributes
7491 && (complain
& tf_warning
))
7492 warning (OPT_Wignored_attributes
,
7493 "ignoring attributes in template argument %qE", arg
);
7497 // A template declaration can be substituted for a constrained
7498 // template template parameter only when the argument is more
7499 // constrained than the parameter.
7501 is_compatible_template_arg (tree parm
, tree arg
)
7503 tree parm_cons
= get_constraints (parm
);
7505 /* For now, allow constrained template template arguments
7506 and unconstrained template template parameters. */
7507 if (parm_cons
== NULL_TREE
)
7510 tree arg_cons
= get_constraints (arg
);
7512 // If the template parameter is constrained, we need to rewrite its
7513 // constraints in terms of the ARG's template parameters. This ensures
7514 // that all of the template parameter types will have the same depth.
7516 // Note that this is only valid when coerce_template_template_parm is
7517 // true for the innermost template parameters of PARM and ARG. In other
7518 // words, because coercion is successful, this conversion will be valid.
7521 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7522 parm_cons
= tsubst_constraint_info (parm_cons
,
7523 INNERMOST_TEMPLATE_ARGS (args
),
7524 tf_none
, NULL_TREE
);
7525 if (parm_cons
== error_mark_node
)
7529 return subsumes (parm_cons
, arg_cons
);
7532 // Convert a placeholder argument into a binding to the original
7533 // parameter. The original parameter is saved as the TREE_TYPE of
7536 convert_wildcard_argument (tree parm
, tree arg
)
7538 TREE_TYPE (arg
) = parm
;
7542 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7543 because one of them is dependent. But we need to represent the
7544 conversion for the benefit of cp_tree_equal. */
7547 maybe_convert_nontype_argument (tree type
, tree arg
)
7549 /* Auto parms get no conversion. */
7550 if (type_uses_auto (type
))
7552 /* We don't need or want to add this conversion now if we're going to use the
7553 argument for deduction. */
7554 if (value_dependent_expression_p (arg
))
7557 type
= cv_unqualified (type
);
7558 tree argtype
= TREE_TYPE (arg
);
7559 if (same_type_p (type
, argtype
))
7562 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7563 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7567 /* Convert the indicated template ARG as necessary to match the
7568 indicated template PARM. Returns the converted ARG, or
7569 error_mark_node if the conversion was unsuccessful. Error and
7570 warning messages are issued under control of COMPLAIN. This
7571 conversion is for the Ith parameter in the parameter list. ARGS is
7572 the full set of template arguments deduced so far. */
7575 convert_template_argument (tree parm
,
7578 tsubst_flags_t complain
,
7584 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7586 if (parm
== error_mark_node
)
7587 return error_mark_node
;
7589 /* Trivially convert placeholders. */
7590 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7591 return convert_wildcard_argument (parm
, arg
);
7593 if (arg
== any_targ_node
)
7596 if (TREE_CODE (arg
) == TREE_LIST
7597 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7599 /* The template argument was the name of some
7600 member function. That's usually
7601 invalid, but static members are OK. In any
7602 case, grab the underlying fields/functions
7603 and issue an error later if required. */
7604 orig_arg
= TREE_VALUE (arg
);
7605 TREE_TYPE (arg
) = unknown_type_node
;
7610 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7611 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7612 || requires_tmpl_type
);
7614 /* When determining whether an argument pack expansion is a template,
7615 look at the pattern. */
7616 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7617 arg
= PACK_EXPANSION_PATTERN (arg
);
7619 /* Deal with an injected-class-name used as a template template arg. */
7620 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7622 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7623 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7625 if (cxx_dialect
>= cxx11
)
7626 /* OK under DR 1004. */;
7627 else if (complain
& tf_warning_or_error
)
7628 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7629 " used as template template argument", TYPE_NAME (arg
));
7630 else if (flag_pedantic_errors
)
7638 ((TREE_CODE (arg
) == TEMPLATE_DECL
7639 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7640 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7641 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7642 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7645 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7646 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7647 arg
= TYPE_STUB_DECL (arg
);
7649 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7651 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7652 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7654 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7656 if (complain
& tf_error
)
7657 error ("invalid use of destructor %qE as a type", orig_arg
);
7658 return error_mark_node
;
7661 permerror (input_location
,
7662 "to refer to a type member of a template parameter, "
7663 "use %<typename %E%>", orig_arg
);
7665 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7666 TREE_OPERAND (arg
, 1),
7672 if (is_type
!= requires_type
)
7676 if (complain
& tf_error
)
7678 error ("type/value mismatch at argument %d in template "
7679 "parameter list for %qD",
7682 inform (input_location
,
7683 " expected a constant of type %qT, got %qT",
7685 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7686 else if (requires_tmpl_type
)
7687 inform (input_location
,
7688 " expected a class template, got %qE", orig_arg
);
7690 inform (input_location
,
7691 " expected a type, got %qE", orig_arg
);
7694 return error_mark_node
;
7696 if (is_tmpl_type
^ requires_tmpl_type
)
7698 if (in_decl
&& (complain
& tf_error
))
7700 error ("type/value mismatch at argument %d in template "
7701 "parameter list for %qD",
7704 inform (input_location
,
7705 " expected a type, got %qT", DECL_NAME (arg
));
7707 inform (input_location
,
7708 " expected a class template, got %qT", orig_arg
);
7710 return error_mark_node
;
7713 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7714 /* We already did the appropriate conversion when packing args. */
7718 if (requires_tmpl_type
)
7720 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7721 /* The number of argument required is not known yet.
7722 Just accept it for now. */
7726 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7729 /* Strip alias templates that are equivalent to another
7731 arg
= get_underlying_template (arg
);
7732 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7734 if (coerce_template_template_parms (parmparm
, argparm
,
7740 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7742 if (val
!= error_mark_node
)
7744 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7745 val
= TREE_TYPE (val
);
7746 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7747 val
= make_pack_expansion (val
, complain
);
7752 if (in_decl
&& (complain
& tf_error
))
7754 error ("type/value mismatch at argument %d in "
7755 "template parameter list for %qD",
7757 inform (input_location
,
7758 " expected a template of type %qD, got %qT",
7762 val
= error_mark_node
;
7765 // Check that the constraints are compatible before allowing the
7767 if (val
!= error_mark_node
)
7768 if (!is_compatible_template_arg (parm
, arg
))
7770 if (in_decl
&& (complain
& tf_error
))
7772 error ("constraint mismatch at argument %d in "
7773 "template parameter list for %qD",
7775 inform (input_location
, " expected %qD but got %qD",
7778 val
= error_mark_node
;
7784 /* We only form one instance of each template specialization.
7785 Therefore, if we use a non-canonical variant (i.e., a
7786 typedef), any future messages referring to the type will use
7787 the typedef, which is confusing if those future uses do not
7788 themselves also use the typedef. */
7790 val
= canonicalize_type_argument (val
, complain
);
7794 tree t
= TREE_TYPE (parm
);
7796 if (tree a
= type_uses_auto (t
))
7798 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7799 if (t
== error_mark_node
)
7800 return error_mark_node
;
7803 t
= tsubst (t
, args
, complain
, in_decl
);
7805 if (invalid_nontype_parm_type_p (t
, complain
))
7806 return error_mark_node
;
7808 if (!type_dependent_expression_p (orig_arg
)
7809 && !uses_template_parms (t
))
7810 /* We used to call digest_init here. However, digest_init
7811 will report errors, which we don't want when complain
7812 is zero. More importantly, digest_init will try too
7813 hard to convert things: for example, `0' should not be
7814 converted to pointer type at this point according to
7815 the standard. Accepting this is not merely an
7816 extension, since deciding whether or not these
7817 conversions can occur is part of determining which
7818 function template to call, or whether a given explicit
7819 argument specification is valid. */
7820 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7823 val
= canonicalize_expr_argument (orig_arg
, complain
);
7824 val
= maybe_convert_nontype_argument (t
, val
);
7828 if (val
== NULL_TREE
)
7829 val
= error_mark_node
;
7830 else if (val
== error_mark_node
&& (complain
& tf_error
))
7831 error ("could not convert template argument %qE from %qT to %qT",
7832 orig_arg
, TREE_TYPE (orig_arg
), t
);
7834 if (INDIRECT_REF_P (val
))
7836 /* Reject template arguments that are references to built-in
7837 functions with no library fallbacks. */
7838 const_tree inner
= TREE_OPERAND (val
, 0);
7839 const_tree innertype
= TREE_TYPE (inner
);
7841 && TREE_CODE (innertype
) == REFERENCE_TYPE
7842 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7843 && TREE_OPERAND_LENGTH (inner
) > 0
7844 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7845 return error_mark_node
;
7848 if (TREE_CODE (val
) == SCOPE_REF
)
7850 /* Strip typedefs from the SCOPE_REF. */
7851 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7852 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7854 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7855 QUALIFIED_NAME_IS_TEMPLATE (val
));
7862 /* Coerces the remaining template arguments in INNER_ARGS (from
7863 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7864 Returns the coerced argument pack. PARM_IDX is the position of this
7865 parameter in the template parameter list. ARGS is the original
7866 template argument list. */
7868 coerce_template_parameter_pack (tree parms
,
7876 tsubst_flags_t complain
)
7878 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7879 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7882 tree packed_parms
= NULL_TREE
;
7884 if (arg_idx
> nargs
)
7887 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7889 /* When the template parameter is a non-type template parameter pack
7890 or template template parameter pack whose type or template
7891 parameters use parameter packs, we know exactly how many arguments
7892 we are looking for. Build a vector of the instantiated decls for
7893 these template parameters in PACKED_PARMS. */
7894 /* We can't use make_pack_expansion here because it would interpret a
7895 _DECL as a use rather than a declaration. */
7896 tree decl
= TREE_VALUE (parm
);
7897 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7898 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7899 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7900 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7902 TREE_VEC_LENGTH (args
)--;
7903 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7904 TREE_VEC_LENGTH (args
)++;
7906 if (packed_parms
== error_mark_node
)
7907 return error_mark_node
;
7909 /* If we're doing a partial instantiation of a member template,
7910 verify that all of the types used for the non-type
7911 template parameter pack are, in fact, valid for non-type
7912 template parameters. */
7914 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7916 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7917 for (j
= 0; j
< len
; ++j
)
7919 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7920 if (invalid_nontype_parm_type_p (t
, complain
))
7921 return error_mark_node
;
7923 /* We don't know how many args we have yet, just
7924 use the unconverted ones for now. */
7928 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7930 /* Check if we have a placeholder pack, which indicates we're
7931 in the context of a introduction list. In that case we want
7932 to match this pack to the single placeholder. */
7933 else if (arg_idx
< nargs
7934 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7935 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7937 nargs
= arg_idx
+ 1;
7938 packed_args
= make_tree_vec (1);
7941 packed_args
= make_tree_vec (nargs
- arg_idx
);
7943 /* Convert the remaining arguments, which will be a part of the
7944 parameter pack "parm". */
7945 int first_pack_arg
= arg_idx
;
7946 for (; arg_idx
< nargs
; ++arg_idx
)
7948 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7949 tree actual_parm
= TREE_VALUE (parm
);
7950 int pack_idx
= arg_idx
- first_pack_arg
;
7954 /* Once we've packed as many args as we have types, stop. */
7955 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7957 else if (PACK_EXPANSION_P (arg
))
7958 /* We don't know how many args we have yet, just
7959 use the unconverted ones for now. */
7962 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7965 if (arg
== error_mark_node
)
7967 if (complain
& tf_error
)
7968 error ("template argument %d is invalid", arg_idx
+ 1);
7971 arg
= convert_template_argument (actual_parm
,
7972 arg
, new_args
, complain
, parm_idx
,
7974 if (arg
== error_mark_node
)
7976 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7979 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7980 && TREE_VEC_LENGTH (packed_args
) > 0)
7982 if (complain
& tf_error
)
7983 error ("wrong number of template arguments (%d, should be %d)",
7984 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7985 return error_mark_node
;
7988 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7989 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7990 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7993 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7994 TREE_CONSTANT (argument_pack
) = 1;
7997 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7999 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8000 TREE_VEC_LENGTH (packed_args
));
8001 return argument_pack
;
8004 /* Returns the number of pack expansions in the template argument vector
8008 pack_expansion_args_count (tree args
)
8013 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8015 tree elt
= TREE_VEC_ELT (args
, i
);
8016 if (elt
&& PACK_EXPANSION_P (elt
))
8022 /* Convert all template arguments to their appropriate types, and
8023 return a vector containing the innermost resulting template
8024 arguments. If any error occurs, return error_mark_node. Error and
8025 warning messages are issued under control of COMPLAIN.
8027 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8028 for arguments not specified in ARGS. Otherwise, if
8029 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8030 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8031 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8035 coerce_template_parms (tree parms
,
8038 tsubst_flags_t complain
,
8039 bool require_all_args
,
8040 bool use_default_args
)
8042 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8043 tree orig_inner_args
;
8046 tree new_inner_args
;
8047 int saved_unevaluated_operand
;
8048 int saved_inhibit_evaluation_warnings
;
8050 /* When used as a boolean value, indicates whether this is a
8051 variadic template parameter list. Since it's an int, we can also
8052 subtract it from nparms to get the number of non-variadic
8055 int variadic_args_p
= 0;
8056 int post_variadic_parms
= 0;
8058 /* Likewise for parameters with default arguments. */
8061 if (args
== error_mark_node
)
8062 return error_mark_node
;
8064 nparms
= TREE_VEC_LENGTH (parms
);
8066 /* Determine if there are any parameter packs or default arguments. */
8067 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8069 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8071 ++post_variadic_parms
;
8072 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8074 if (TREE_PURPOSE (parm
))
8078 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8079 /* If there are no parameters that follow a parameter pack, we need to
8080 expand any argument packs so that we can deduce a parameter pack from
8081 some non-packed args followed by an argument pack, as in variadic85.C.
8082 If there are such parameters, we need to leave argument packs intact
8083 so the arguments are assigned properly. This can happen when dealing
8084 with a nested class inside a partial specialization of a class
8085 template, as in variadic92.C, or when deducing a template parameter pack
8086 from a sub-declarator, as in variadic114.C. */
8087 if (!post_variadic_parms
)
8088 inner_args
= expand_template_argument_pack (inner_args
);
8090 /* Count any pack expansion args. */
8091 variadic_args_p
= pack_expansion_args_count (inner_args
);
8093 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8094 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8095 || (nargs
< nparms
- variadic_p
8098 && (!use_default_args
8099 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8100 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8102 if (complain
& tf_error
)
8104 if (variadic_p
|| default_p
)
8106 nparms
-= variadic_p
+ default_p
;
8107 error ("wrong number of template arguments "
8108 "(%d, should be at least %d)", nargs
, nparms
);
8111 error ("wrong number of template arguments "
8112 "(%d, should be %d)", nargs
, nparms
);
8115 inform (DECL_SOURCE_LOCATION (in_decl
),
8116 "provided for %qD", in_decl
);
8119 return error_mark_node
;
8121 /* We can't pass a pack expansion to a non-pack parameter of an alias
8122 template (DR 1430). */
8124 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8125 || concept_template_p (in_decl
))
8127 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8129 if (complain
& tf_error
)
8131 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8133 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8134 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8136 if (PACK_EXPANSION_P (arg
)
8137 && !template_parameter_pack_p (parm
))
8139 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8140 error_at (location_of (arg
),
8141 "pack expansion argument for non-pack parameter "
8142 "%qD of alias template %qD", parm
, in_decl
);
8144 error_at (location_of (arg
),
8145 "pack expansion argument for non-pack parameter "
8146 "%qD of concept %qD", parm
, in_decl
);
8147 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8154 return error_mark_node
;
8157 /* We need to evaluate the template arguments, even though this
8158 template-id may be nested within a "sizeof". */
8159 saved_unevaluated_operand
= cp_unevaluated_operand
;
8160 cp_unevaluated_operand
= 0;
8161 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8162 c_inhibit_evaluation_warnings
= 0;
8163 new_inner_args
= make_tree_vec (nparms
);
8164 new_args
= add_outermost_template_args (args
, new_inner_args
);
8165 int pack_adjust
= 0;
8166 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8171 /* Get the Ith template parameter. */
8172 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8174 if (parm
== error_mark_node
)
8176 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8180 /* Calculate the next argument. */
8181 if (arg_idx
< nargs
)
8182 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8186 if (template_parameter_pack_p (TREE_VALUE (parm
))
8187 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8189 /* Some arguments will be placed in the
8190 template parameter pack PARM. */
8191 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8192 inner_args
, arg_idx
,
8196 if (arg
== NULL_TREE
)
8198 /* We don't know how many args we have yet, just use the
8199 unconverted (and still packed) ones for now. */
8200 new_inner_args
= orig_inner_args
;
8205 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8207 /* Store this argument. */
8208 if (arg
== error_mark_node
)
8211 /* We are done with all of the arguments. */
8216 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8217 arg_idx
+= pack_adjust
;
8224 if (PACK_EXPANSION_P (arg
))
8226 /* "If every valid specialization of a variadic template
8227 requires an empty template parameter pack, the template is
8228 ill-formed, no diagnostic required." So check that the
8229 pattern works with this parameter. */
8230 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8231 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8235 if (conv
== error_mark_node
)
8237 if (complain
& tf_error
)
8238 inform (input_location
, "so any instantiation with a "
8239 "non-empty parameter pack would be ill-formed");
8242 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8243 /* Recover from missing typename. */
8244 TREE_VEC_ELT (inner_args
, arg_idx
)
8245 = make_pack_expansion (conv
, complain
);
8247 /* We don't know how many args we have yet, just
8248 use the unconverted ones for now. */
8249 new_inner_args
= inner_args
;
8254 else if (require_all_args
)
8256 /* There must be a default arg in this case. */
8257 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8259 /* The position of the first default template argument,
8260 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8262 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8264 arg_idx
- pack_adjust
);
8269 if (arg
== error_mark_node
)
8271 if (complain
& tf_error
)
8272 error ("template argument %d is invalid", arg_idx
+ 1);
8275 /* This only occurs if there was an error in the template
8276 parameter list itself (which we would already have
8277 reported) that we are trying to recover from, e.g., a class
8278 template with a parameter list such as
8279 template<typename..., typename>. */
8282 arg
= convert_template_argument (TREE_VALUE (parm
),
8283 arg
, new_args
, complain
,
8286 if (arg
== error_mark_node
)
8288 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8290 cp_unevaluated_operand
= saved_unevaluated_operand
;
8291 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8293 if (variadic_p
&& arg_idx
< nargs
)
8295 if (complain
& tf_error
)
8297 error ("wrong number of template arguments "
8298 "(%d, should be %d)", nargs
, arg_idx
);
8300 error ("provided for %q+D", in_decl
);
8302 return error_mark_node
;
8306 return error_mark_node
;
8308 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8310 TREE_VEC_LENGTH (new_inner_args
));
8312 return new_inner_args
;
8315 /* Convert all template arguments to their appropriate types, and
8316 return a vector containing the innermost resulting template
8317 arguments. If any error occurs, return error_mark_node. Error and
8318 warning messages are not issued.
8320 Note that no function argument deduction is performed, and default
8321 arguments are used to fill in unspecified arguments. */
8323 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8325 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8328 /* Convert all template arguments to their appropriate type, and
8329 instantiate default arguments as needed. This returns a vector
8330 containing the innermost resulting template arguments, or
8331 error_mark_node if unsuccessful. */
8333 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8334 tsubst_flags_t complain
)
8336 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8339 /* Like coerce_template_parms. If PARMS represents all template
8340 parameters levels, this function returns a vector of vectors
8341 representing all the resulting argument levels. Note that in this
8342 case, only the innermost arguments are coerced because the
8343 outermost ones are supposed to have been coerced already.
8345 Otherwise, if PARMS represents only (the innermost) vector of
8346 parameters, this function returns a vector containing just the
8347 innermost resulting arguments. */
8350 coerce_innermost_template_parms (tree parms
,
8353 tsubst_flags_t complain
,
8354 bool require_all_args
,
8355 bool use_default_args
)
8357 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8358 int args_depth
= TMPL_ARGS_DEPTH (args
);
8361 if (parms_depth
> 1)
8363 coerced_args
= make_tree_vec (parms_depth
);
8367 for (level
= parms
, cur_depth
= parms_depth
;
8368 parms_depth
> 0 && level
!= NULL_TREE
;
8369 level
= TREE_CHAIN (level
), --cur_depth
)
8372 if (cur_depth
== args_depth
)
8373 l
= coerce_template_parms (TREE_VALUE (level
),
8374 args
, in_decl
, complain
,
8378 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8380 if (l
== error_mark_node
)
8381 return error_mark_node
;
8383 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8387 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8388 args
, in_decl
, complain
,
8391 return coerced_args
;
8394 /* Returns 1 if template args OT and NT are equivalent. */
8397 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8401 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8403 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8406 if (TREE_CODE (nt
) == TREE_VEC
)
8407 /* For member templates */
8408 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8409 else if (PACK_EXPANSION_P (ot
))
8410 return (PACK_EXPANSION_P (nt
)
8411 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8412 PACK_EXPANSION_PATTERN (nt
))
8413 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8414 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8415 else if (ARGUMENT_PACK_P (ot
))
8420 if (!ARGUMENT_PACK_P (nt
))
8423 opack
= ARGUMENT_PACK_ARGS (ot
);
8424 npack
= ARGUMENT_PACK_ARGS (nt
);
8425 len
= TREE_VEC_LENGTH (opack
);
8426 if (TREE_VEC_LENGTH (npack
) != len
)
8428 for (i
= 0; i
< len
; ++i
)
8429 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8430 TREE_VEC_ELT (npack
, i
)))
8434 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8436 else if (TYPE_P (nt
))
8440 /* Don't treat an alias template specialization with dependent
8441 arguments as equivalent to its underlying type when used as a
8442 template argument; we need them to be distinct so that we
8443 substitute into the specialization arguments at instantiation
8444 time. And aliases can't be equivalent without being ==, so
8445 we don't need to look any deeper.
8447 During partial ordering, however, we need to treat them normally so
8448 that we can order uses of the same alias with different
8449 cv-qualification (79960). */
8451 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8454 return same_type_p (ot
, nt
);
8456 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8460 /* Try to treat a template non-type argument that has been converted
8461 to the parameter type as equivalent to one that hasn't yet. */
8462 for (enum tree_code code1
= TREE_CODE (ot
);
8463 CONVERT_EXPR_CODE_P (code1
)
8464 || code1
== NON_LVALUE_EXPR
;
8465 code1
= TREE_CODE (ot
))
8466 ot
= TREE_OPERAND (ot
, 0);
8467 for (enum tree_code code2
= TREE_CODE (nt
);
8468 CONVERT_EXPR_CODE_P (code2
)
8469 || code2
== NON_LVALUE_EXPR
;
8470 code2
= TREE_CODE (nt
))
8471 nt
= TREE_OPERAND (nt
, 0);
8473 return cp_tree_equal (ot
, nt
);
8477 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8478 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8479 NEWARG_PTR with the offending arguments if they are non-NULL. */
8482 comp_template_args (tree oldargs
, tree newargs
,
8483 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8488 if (oldargs
== newargs
)
8491 if (!oldargs
|| !newargs
)
8494 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8497 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8499 tree nt
= TREE_VEC_ELT (newargs
, i
);
8500 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8502 if (! template_args_equal (ot
, nt
, partial_order
))
8504 if (oldarg_ptr
!= NULL
)
8506 if (newarg_ptr
!= NULL
)
8515 comp_template_args_porder (tree oargs
, tree nargs
)
8517 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8521 add_pending_template (tree d
)
8523 tree ti
= (TYPE_P (d
)
8524 ? CLASSTYPE_TEMPLATE_INFO (d
)
8525 : DECL_TEMPLATE_INFO (d
));
8526 struct pending_template
*pt
;
8529 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8532 /* We are called both from instantiate_decl, where we've already had a
8533 tinst_level pushed, and instantiate_template, where we haven't.
8535 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8538 push_tinst_level (d
);
8540 pt
= ggc_alloc
<pending_template
> ();
8542 pt
->tinst
= current_tinst_level
;
8543 if (last_pending_template
)
8544 last_pending_template
->next
= pt
;
8546 pending_templates
= pt
;
8548 last_pending_template
= pt
;
8550 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8557 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8558 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8559 documentation for TEMPLATE_ID_EXPR. */
8562 lookup_template_function (tree fns
, tree arglist
)
8566 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8567 return error_mark_node
;
8569 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8571 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8573 error ("%q#D is not a function template", fns
);
8574 return error_mark_node
;
8577 if (BASELINK_P (fns
))
8579 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8581 BASELINK_FUNCTIONS (fns
),
8586 type
= TREE_TYPE (fns
);
8587 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8588 type
= unknown_type_node
;
8590 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8593 /* Within the scope of a template class S<T>, the name S gets bound
8594 (in build_self_reference) to a TYPE_DECL for the class, not a
8595 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8596 or one of its enclosing classes, and that type is a template,
8597 return the associated TEMPLATE_DECL. Otherwise, the original
8600 Also handle the case when DECL is a TREE_LIST of ambiguous
8601 injected-class-names from different bases. */
8604 maybe_get_template_decl_from_type_decl (tree decl
)
8606 if (decl
== NULL_TREE
)
8609 /* DR 176: A lookup that finds an injected-class-name (10.2
8610 [class.member.lookup]) can result in an ambiguity in certain cases
8611 (for example, if it is found in more than one base class). If all of
8612 the injected-class-names that are found refer to specializations of
8613 the same class template, and if the name is followed by a
8614 template-argument-list, the reference refers to the class template
8615 itself and not a specialization thereof, and is not ambiguous. */
8616 if (TREE_CODE (decl
) == TREE_LIST
)
8618 tree t
, tmpl
= NULL_TREE
;
8619 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8621 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8624 else if (tmpl
!= elt
)
8627 if (tmpl
&& t
== NULL_TREE
)
8633 return (decl
!= NULL_TREE
8634 && DECL_SELF_REFERENCE_P (decl
)
8635 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8636 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8639 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8640 parameters, find the desired type.
8642 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8644 IN_DECL, if non-NULL, is the template declaration we are trying to
8647 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8648 the class we are looking up.
8650 Issue error and warning messages under control of COMPLAIN.
8652 If the template class is really a local class in a template
8653 function, then the FUNCTION_CONTEXT is the function in which it is
8656 ??? Note that this function is currently called *twice* for each
8657 template-id: the first time from the parser, while creating the
8658 incomplete type (finish_template_type), and the second type during the
8659 real instantiation (instantiate_template_class). This is surely something
8660 that we want to avoid. It also causes some problems with argument
8661 coercion (see convert_nontype_argument for more information on this). */
8664 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8665 int entering_scope
, tsubst_flags_t complain
)
8667 tree templ
= NULL_TREE
, parmlist
;
8674 if (identifier_p (d1
))
8676 tree value
= innermost_non_namespace_value (d1
);
8677 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8682 push_decl_namespace (context
);
8683 templ
= lookup_name (d1
);
8684 templ
= maybe_get_template_decl_from_type_decl (templ
);
8686 pop_decl_namespace ();
8689 context
= DECL_CONTEXT (templ
);
8691 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8693 tree type
= TREE_TYPE (d1
);
8695 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8696 an implicit typename for the second A. Deal with it. */
8697 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8698 type
= TREE_TYPE (type
);
8700 if (CLASSTYPE_TEMPLATE_INFO (type
))
8702 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8703 d1
= DECL_NAME (templ
);
8706 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8707 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8709 templ
= TYPE_TI_TEMPLATE (d1
);
8710 d1
= DECL_NAME (templ
);
8712 else if (DECL_TYPE_TEMPLATE_P (d1
))
8715 d1
= DECL_NAME (templ
);
8716 context
= DECL_CONTEXT (templ
);
8718 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8721 d1
= DECL_NAME (templ
);
8724 /* Issue an error message if we didn't find a template. */
8727 if (complain
& tf_error
)
8728 error ("%qT is not a template", d1
);
8729 return error_mark_node
;
8732 if (TREE_CODE (templ
) != TEMPLATE_DECL
8733 /* Make sure it's a user visible template, if it was named by
8735 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8736 && !PRIMARY_TEMPLATE_P (templ
)))
8738 if (complain
& tf_error
)
8740 error ("non-template type %qT used as a template", d1
);
8742 error ("for template declaration %q+D", in_decl
);
8744 return error_mark_node
;
8747 complain
&= ~tf_user
;
8749 /* An alias that just changes the name of a template is equivalent to the
8750 other template, so if any of the arguments are pack expansions, strip
8751 the alias to avoid problems with a pack expansion passed to a non-pack
8752 alias template parameter (DR 1430). */
8753 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8754 templ
= get_underlying_template (templ
);
8756 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8759 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8760 if (arglist2
== error_mark_node
8761 || (!uses_template_parms (arglist2
)
8762 && check_instantiated_args (templ
, arglist2
, complain
)))
8763 return error_mark_node
;
8765 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8770 tree template_type
= TREE_TYPE (templ
);
8773 tree found
= NULL_TREE
;
8776 int is_dependent_type
;
8777 int use_partial_inst_tmpl
= false;
8779 if (template_type
== error_mark_node
)
8780 /* An error occurred while building the template TEMPL, and a
8781 diagnostic has most certainly been emitted for that
8782 already. Let's propagate that error. */
8783 return error_mark_node
;
8785 gen_tmpl
= most_general_template (templ
);
8786 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8787 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8788 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8790 if (arg_depth
== 1 && parm_depth
> 1)
8792 /* We've been given an incomplete set of template arguments.
8795 template <class T> struct S1 {
8796 template <class U> struct S2 {};
8797 template <class U> struct S2<U*> {};
8800 we will be called with an ARGLIST of `U*', but the
8801 TEMPLATE will be `template <class T> template
8802 <class U> struct S1<T>::S2'. We must fill in the missing
8804 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8805 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8806 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8809 /* Now we should have enough arguments. */
8810 gcc_assert (parm_depth
== arg_depth
);
8812 /* From here on, we're only interested in the most general
8815 /* Calculate the BOUND_ARGS. These will be the args that are
8816 actually tsubst'd into the definition to create the
8818 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8820 /*require_all_args=*/true,
8821 /*use_default_args=*/true);
8823 if (arglist
== error_mark_node
)
8824 /* We were unable to bind the arguments. */
8825 return error_mark_node
;
8827 /* In the scope of a template class, explicit references to the
8828 template class refer to the type of the template, not any
8829 instantiation of it. For example, in:
8831 template <class T> class C { void f(C<T>); }
8833 the `C<T>' is just the same as `C'. Outside of the
8834 class, however, such a reference is an instantiation. */
8836 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8837 || currently_open_class (template_type
))
8839 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8841 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8842 return template_type
;
8845 /* If we already have this specialization, return it. */
8846 elt
.tmpl
= gen_tmpl
;
8848 elt
.spec
= NULL_TREE
;
8849 hash
= spec_hasher::hash (&elt
);
8850 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8855 /* If the the template's constraints are not satisfied,
8856 then we cannot form a valid type.
8858 Note that the check is deferred until after the hash
8859 lookup. This prevents redundant checks on previously
8860 instantiated specializations. */
8861 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8863 if (complain
& tf_error
)
8865 error ("template constraint failure");
8866 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8868 return error_mark_node
;
8871 is_dependent_type
= uses_template_parms (arglist
);
8873 /* If the deduced arguments are invalid, then the binding
8875 if (!is_dependent_type
8876 && check_instantiated_args (gen_tmpl
,
8877 INNERMOST_TEMPLATE_ARGS (arglist
),
8879 return error_mark_node
;
8881 if (!is_dependent_type
8882 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8883 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8884 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8886 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8887 DECL_NAME (gen_tmpl
),
8888 /*tag_scope=*/ts_global
);
8892 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8894 if (context
== error_mark_node
)
8895 return error_mark_node
;
8898 context
= global_namespace
;
8900 /* Create the type. */
8901 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8903 /* The user referred to a specialization of an alias
8904 template represented by GEN_TMPL.
8906 [temp.alias]/2 says:
8908 When a template-id refers to the specialization of an
8909 alias template, it is equivalent to the associated
8910 type obtained by substitution of its
8911 template-arguments for the template-parameters in the
8912 type-id of the alias template. */
8914 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8915 /* Note that the call above (by indirectly calling
8916 register_specialization in tsubst_decl) registers the
8917 TYPE_DECL representing the specialization of the alias
8918 template. So next time someone substitutes ARGLIST for
8919 the template parms into the alias template (GEN_TMPL),
8920 she'll get that TYPE_DECL back. */
8922 if (t
== error_mark_node
)
8925 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8927 if (!is_dependent_type
)
8929 set_current_access_from_decl (TYPE_NAME (template_type
));
8930 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8931 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8932 arglist
, complain
, in_decl
),
8933 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8934 arglist
, complain
, in_decl
),
8935 SCOPED_ENUM_P (template_type
), NULL
);
8937 if (t
== error_mark_node
)
8942 /* We don't want to call start_enum for this type, since
8943 the values for the enumeration constants may involve
8944 template parameters. And, no one should be interested
8945 in the enumeration constants for such a type. */
8946 t
= cxx_make_type (ENUMERAL_TYPE
);
8947 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8949 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8950 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8951 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8953 else if (CLASS_TYPE_P (template_type
))
8955 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8956 instantiated here. */
8957 gcc_assert (!LAMBDA_TYPE_P (template_type
));
8959 t
= make_class_type (TREE_CODE (template_type
));
8960 CLASSTYPE_DECLARED_CLASS (t
)
8961 = CLASSTYPE_DECLARED_CLASS (template_type
);
8962 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8964 /* A local class. Make sure the decl gets registered properly. */
8965 if (context
== current_function_decl
)
8966 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8968 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8969 /* This instantiation is another name for the primary
8970 template type. Set the TYPE_CANONICAL field
8972 TYPE_CANONICAL (t
) = template_type
;
8973 else if (any_template_arguments_need_structural_equality_p (arglist
))
8974 /* Some of the template arguments require structural
8975 equality testing, so this template class requires
8976 structural equality testing. */
8977 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8982 /* If we called start_enum or pushtag above, this information
8983 will already be set up. */
8986 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8988 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8989 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8990 DECL_SOURCE_LOCATION (type_decl
)
8991 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8994 type_decl
= TYPE_NAME (t
);
8996 if (CLASS_TYPE_P (template_type
))
8998 TREE_PRIVATE (type_decl
)
8999 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9000 TREE_PROTECTED (type_decl
)
9001 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9002 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9004 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9005 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9009 if (OVERLOAD_TYPE_P (t
)
9010 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9012 static const char *tags
[] = {"abi_tag", "may_alias"};
9014 for (unsigned ix
= 0; ix
!= 2; ix
++)
9017 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9021 = tree_cons (TREE_PURPOSE (attributes
),
9022 TREE_VALUE (attributes
),
9023 TYPE_ATTRIBUTES (t
));
9027 /* Let's consider the explicit specialization of a member
9028 of a class template specialization that is implicitly instantiated,
9033 template<class U> struct M {}; //#0
9038 struct S<int>::M<char> //#1
9042 [temp.expl.spec]/4 says this is valid.
9044 In this case, when we write:
9047 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9050 When we encounter #1, we want to store the partial instantiation
9051 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9053 For all cases other than this "explicit specialization of member of a
9054 class template", we just want to store the most general template into
9055 the CLASSTYPE_TI_TEMPLATE of M.
9057 This case of "explicit specialization of member of a class template"
9059 1/ the enclosing class is an instantiation of, and therefore not
9060 the same as, the context of the most general template, and
9061 2/ we aren't looking at the partial instantiation itself, i.e.
9062 the innermost arguments are not the same as the innermost parms of
9063 the most general template.
9065 So it's only when 1/ and 2/ happens that we want to use the partial
9066 instantiation of the member template in lieu of its most general
9069 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9070 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9071 /* the enclosing class must be an instantiation... */
9072 && CLASS_TYPE_P (context
)
9073 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9075 TREE_VEC_LENGTH (arglist
)--;
9076 ++processing_template_decl
;
9077 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9078 tree partial_inst_args
=
9079 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9080 arglist
, complain
, NULL_TREE
);
9081 --processing_template_decl
;
9082 TREE_VEC_LENGTH (arglist
)++;
9083 if (partial_inst_args
== error_mark_node
)
9084 return error_mark_node
;
9085 use_partial_inst_tmpl
=
9086 /*...and we must not be looking at the partial instantiation
9088 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9092 if (!use_partial_inst_tmpl
)
9093 /* This case is easy; there are no member templates involved. */
9097 /* This is a full instantiation of a member template. Find
9098 the partial instantiation of which this is an instance. */
9100 /* Temporarily reduce by one the number of levels in the ARGLIST
9101 so as to avoid comparing the last set of arguments. */
9102 TREE_VEC_LENGTH (arglist
)--;
9103 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9104 TREE_VEC_LENGTH (arglist
)++;
9105 /* FOUND is either a proper class type, or an alias
9106 template specialization. In the later case, it's a
9107 TYPE_DECL, resulting from the substituting of arguments
9108 for parameters in the TYPE_DECL of the alias template
9109 done earlier. So be careful while getting the template
9111 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9113 : (TREE_CODE (found
) == TYPE_DECL
9114 ? DECL_TI_TEMPLATE (found
)
9115 : CLASSTYPE_TI_TEMPLATE (found
)));
9118 // Build template info for the new specialization.
9119 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9122 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9123 entry
= ggc_alloc
<spec_entry
> ();
9127 /* Note this use of the partial instantiation so we can check it
9128 later in maybe_process_partial_specialization. */
9129 DECL_TEMPLATE_INSTANTIATIONS (found
)
9130 = tree_cons (arglist
, t
,
9131 DECL_TEMPLATE_INSTANTIATIONS (found
));
9133 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9134 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9135 /* Now that the type has been registered on the instantiations
9136 list, we set up the enumerators. Because the enumeration
9137 constants may involve the enumeration type itself, we make
9138 sure to register the type first, and then create the
9139 constants. That way, doing tsubst_expr for the enumeration
9140 constants won't result in recursive calls here; we'll find
9141 the instantiation and exit above. */
9142 tsubst_enum (template_type
, t
, arglist
);
9144 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9145 /* If the type makes use of template parameters, the
9146 code that generates debugging information will crash. */
9147 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9149 /* Possibly limit visibility based on template args. */
9150 TREE_PUBLIC (type_decl
) = 1;
9151 determine_visibility (type_decl
);
9153 inherit_targ_abi_tags (t
);
9159 /* Wrapper for lookup_template_class_1. */
9162 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9163 int entering_scope
, tsubst_flags_t complain
)
9166 timevar_push (TV_TEMPLATE_INST
);
9167 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9168 entering_scope
, complain
);
9169 timevar_pop (TV_TEMPLATE_INST
);
9173 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9176 lookup_template_variable (tree templ
, tree arglist
)
9178 /* The type of the expression is NULL_TREE since the template-id could refer
9179 to an explicit or partial specialization. */
9180 tree type
= NULL_TREE
;
9181 if (flag_concepts
&& variable_concept_p (templ
))
9182 /* Except that concepts are always bool. */
9183 type
= boolean_type_node
;
9184 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9187 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9190 finish_template_variable (tree var
, tsubst_flags_t complain
)
9192 tree templ
= TREE_OPERAND (var
, 0);
9193 tree arglist
= TREE_OPERAND (var
, 1);
9195 /* We never want to return a VAR_DECL for a variable concept, since they
9196 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9197 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9198 if (concept_p
&& processing_template_decl
)
9201 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9202 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9204 templ
= most_general_template (templ
);
9205 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9206 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9208 /*use_default*/true);
9210 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9212 if (complain
& tf_error
)
9214 error ("use of invalid variable template %qE", var
);
9215 diagnose_constraints (location_of (var
), templ
, arglist
);
9217 return error_mark_node
;
9220 /* If a template-id refers to a specialization of a variable
9221 concept, then the expression is true if and only if the
9222 concept's constraints are satisfied by the given template
9225 NOTE: This is an extension of Concepts Lite TS that
9226 allows constraints to be used in expressions. */
9229 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9230 return evaluate_variable_concept (decl
, arglist
);
9233 return instantiate_template (templ
, arglist
, complain
);
9236 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9237 TARGS template args, and instantiate it if it's not dependent. */
9240 lookup_and_finish_template_variable (tree templ
, tree targs
,
9241 tsubst_flags_t complain
)
9243 templ
= lookup_template_variable (templ
, targs
);
9244 if (!any_dependent_template_arguments_p (targs
))
9246 templ
= finish_template_variable (templ
, complain
);
9250 return convert_from_reference (templ
);
9259 /* True when we should also visit template parameters that occur in
9260 non-deduced contexts. */
9261 bool include_nondeduced_p
;
9262 hash_set
<tree
> *visited
;
9265 /* Called from for_each_template_parm via walk_tree. */
9268 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9271 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9272 tree_fn_t fn
= pfd
->fn
;
9273 void *data
= pfd
->data
;
9274 tree result
= NULL_TREE
;
9276 #define WALK_SUBTREE(NODE) \
9279 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9280 pfd->include_nondeduced_p, \
9282 if (result) goto out; \
9286 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9290 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9291 WALK_SUBTREE (TYPE_CONTEXT (t
));
9293 switch (TREE_CODE (t
))
9296 if (TYPE_PTRMEMFUNC_P (t
))
9302 if (!TYPE_TEMPLATE_INFO (t
))
9305 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9309 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9310 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9314 /* Since we're not going to walk subtrees, we have to do this
9316 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9320 /* Check the return type. */
9321 WALK_SUBTREE (TREE_TYPE (t
));
9323 /* Check the parameter types. Since default arguments are not
9324 instantiated until they are needed, the TYPE_ARG_TYPES may
9325 contain expressions that involve template parameters. But,
9326 no-one should be looking at them yet. And, once they're
9327 instantiated, they don't contain template parameters, so
9328 there's no point in looking at them then, either. */
9332 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9333 WALK_SUBTREE (TREE_VALUE (parm
));
9335 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9336 want walk_tree walking into them itself. */
9340 if (flag_noexcept_type
)
9342 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9344 WALK_SUBTREE (TREE_PURPOSE (spec
));
9349 case UNDERLYING_TYPE
:
9350 if (pfd
->include_nondeduced_p
9351 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9353 pfd
->include_nondeduced_p
,
9355 return error_mark_node
;
9360 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9361 WALK_SUBTREE (DECL_TI_ARGS (t
));
9366 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9367 WALK_SUBTREE (DECL_INITIAL (t
));
9368 if (DECL_CONTEXT (t
)
9369 && pfd
->include_nondeduced_p
)
9370 WALK_SUBTREE (DECL_CONTEXT (t
));
9373 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9374 /* Record template parameters such as `T' inside `TT<T>'. */
9375 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9378 case TEMPLATE_TEMPLATE_PARM
:
9379 case TEMPLATE_TYPE_PARM
:
9380 case TEMPLATE_PARM_INDEX
:
9381 if (fn
&& (*fn
)(t
, data
))
9388 /* A template template parameter is encountered. */
9389 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9390 WALK_SUBTREE (TREE_TYPE (t
));
9392 /* Already substituted template template parameter */
9397 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9398 partial instantiation. */
9399 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9403 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9404 && pfd
->include_nondeduced_p
)
9405 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9410 /* If there's no type, then this thing must be some expression
9411 involving template parameters. */
9412 if (!fn
&& !TREE_TYPE (t
))
9413 return error_mark_node
;
9418 case IMPLICIT_CONV_EXPR
:
9419 case REINTERPRET_CAST_EXPR
:
9420 case CONST_CAST_EXPR
:
9421 case STATIC_CAST_EXPR
:
9422 case DYNAMIC_CAST_EXPR
:
9426 case PSEUDO_DTOR_EXPR
:
9428 return error_mark_node
;
9437 /* We didn't find any template parameters we liked. */
9442 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9443 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9444 call FN with the parameter and the DATA.
9445 If FN returns nonzero, the iteration is terminated, and
9446 for_each_template_parm returns 1. Otherwise, the iteration
9447 continues. If FN never returns a nonzero value, the value
9448 returned by for_each_template_parm is 0. If FN is NULL, it is
9449 considered to be the function which always returns 1.
9451 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9452 parameters that occur in non-deduced contexts. When false, only
9453 visits those template parameters that can be deduced. */
9456 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9457 hash_set
<tree
> *visited
,
9458 bool include_nondeduced_p
,
9461 struct pair_fn_data pfd
;
9466 pfd
.any_fn
= any_fn
;
9468 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9470 /* Walk the tree. (Conceptually, we would like to walk without
9471 duplicates, but for_each_template_parm_r recursively calls
9472 for_each_template_parm, so we would need to reorganize a fair
9473 bit to use walk_tree_without_duplicates, so we keep our own
9476 pfd
.visited
= visited
;
9478 pfd
.visited
= new hash_set
<tree
>;
9479 result
= cp_walk_tree (&t
,
9480 for_each_template_parm_r
,
9494 /* Returns true if T depends on any template parameter. */
9497 uses_template_parms (tree t
)
9503 int saved_processing_template_decl
;
9505 saved_processing_template_decl
= processing_template_decl
;
9506 if (!saved_processing_template_decl
)
9507 processing_template_decl
= 1;
9509 dependent_p
= dependent_type_p (t
);
9510 else if (TREE_CODE (t
) == TREE_VEC
)
9511 dependent_p
= any_dependent_template_arguments_p (t
);
9512 else if (TREE_CODE (t
) == TREE_LIST
)
9513 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9514 || uses_template_parms (TREE_CHAIN (t
)));
9515 else if (TREE_CODE (t
) == TYPE_DECL
)
9516 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9519 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9520 || TREE_CODE (t
) == OVERLOAD
9523 || TREE_CODE (t
) == TRAIT_EXPR
9524 || TREE_CODE (t
) == CONSTRUCTOR
9525 || CONSTANT_CLASS_P (t
))
9526 dependent_p
= (type_dependent_expression_p (t
)
9527 || value_dependent_expression_p (t
));
9530 gcc_assert (t
== error_mark_node
);
9531 dependent_p
= false;
9534 processing_template_decl
= saved_processing_template_decl
;
9539 /* Returns true iff current_function_decl is an incompletely instantiated
9540 template. Useful instead of processing_template_decl because the latter
9541 is set to 0 during instantiate_non_dependent_expr. */
9544 in_template_function (void)
9546 tree fn
= current_function_decl
;
9548 ++processing_template_decl
;
9549 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9550 && DECL_TEMPLATE_INFO (fn
)
9551 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9552 --processing_template_decl
;
9556 /* Returns true if T depends on any template parameter with level LEVEL. */
9559 uses_template_parms_level (tree t
, int level
)
9561 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9562 /*include_nondeduced_p=*/true);
9565 /* Returns true if the signature of DECL depends on any template parameter from
9566 its enclosing class. */
9569 uses_outer_template_parms (tree decl
)
9571 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9574 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9575 &depth
, NULL
, /*include_nondeduced_p=*/true))
9577 if (PRIMARY_TEMPLATE_P (decl
)
9578 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9579 (DECL_TEMPLATE_PARMS (decl
)),
9580 template_parm_outer_level
,
9581 &depth
, NULL
, /*include_nondeduced_p=*/true))
9583 tree ci
= get_constraints (decl
);
9585 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9586 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9587 &depth
, NULL
, /*nondeduced*/true))
9592 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9593 ill-formed translation unit, i.e. a variable or function that isn't
9594 usable in a constant expression. */
9597 neglectable_inst_p (tree d
)
9600 && !undeduced_auto_decl (d
)
9601 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9602 : decl_maybe_constant_var_p (d
)));
9605 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9606 neglectable and instantiated from within an erroneous instantiation. */
9609 limit_bad_template_recursion (tree decl
)
9611 struct tinst_level
*lev
= current_tinst_level
;
9612 int errs
= errorcount
+ sorrycount
;
9613 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9616 for (; lev
; lev
= lev
->next
)
9617 if (neglectable_inst_p (lev
->decl
))
9620 return (lev
&& errs
> lev
->errors
);
9623 static int tinst_depth
;
9624 extern int max_tinst_depth
;
9627 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9629 /* We're starting to instantiate D; record the template instantiation context
9630 for diagnostics and to restore it later. */
9633 push_tinst_level (tree d
)
9635 return push_tinst_level_loc (d
, input_location
);
9638 /* We're starting to instantiate D; record the template instantiation context
9639 at LOC for diagnostics and to restore it later. */
9642 push_tinst_level_loc (tree d
, location_t loc
)
9644 struct tinst_level
*new_level
;
9646 if (tinst_depth
>= max_tinst_depth
)
9648 /* Tell error.c not to try to instantiate any templates. */
9650 fatal_error (input_location
,
9651 "template instantiation depth exceeds maximum of %d"
9652 " (use -ftemplate-depth= to increase the maximum)",
9657 /* If the current instantiation caused problems, don't let it instantiate
9658 anything else. Do allow deduction substitution and decls usable in
9659 constant expressions. */
9660 if (limit_bad_template_recursion (d
))
9663 /* When not -quiet, dump template instantiations other than functions, since
9664 announce_function will take care of those. */
9666 && TREE_CODE (d
) != TREE_LIST
9667 && TREE_CODE (d
) != FUNCTION_DECL
)
9668 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9670 new_level
= ggc_alloc
<tinst_level
> ();
9671 new_level
->decl
= d
;
9672 new_level
->locus
= loc
;
9673 new_level
->errors
= errorcount
+sorrycount
;
9674 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9675 new_level
->next
= current_tinst_level
;
9676 current_tinst_level
= new_level
;
9679 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9680 depth_reached
= tinst_depth
;
9685 /* We're done instantiating this template; return to the instantiation
9689 pop_tinst_level (void)
9691 /* Restore the filename and line number stashed away when we started
9692 this instantiation. */
9693 input_location
= current_tinst_level
->locus
;
9694 current_tinst_level
= current_tinst_level
->next
;
9698 /* We're instantiating a deferred template; restore the template
9699 instantiation context in which the instantiation was requested, which
9700 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9703 reopen_tinst_level (struct tinst_level
*level
)
9705 struct tinst_level
*t
;
9708 for (t
= level
; t
; t
= t
->next
)
9711 current_tinst_level
= level
;
9713 if (current_tinst_level
)
9714 current_tinst_level
->errors
= errorcount
+sorrycount
;
9718 /* Returns the TINST_LEVEL which gives the original instantiation
9721 struct tinst_level
*
9722 outermost_tinst_level (void)
9724 struct tinst_level
*level
= current_tinst_level
;
9727 level
= level
->next
;
9731 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9732 vector of template arguments, as for tsubst.
9734 Returns an appropriate tsubst'd friend declaration. */
9737 tsubst_friend_function (tree decl
, tree args
)
9741 if (TREE_CODE (decl
) == FUNCTION_DECL
9742 && DECL_TEMPLATE_INSTANTIATION (decl
)
9743 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9744 /* This was a friend declared with an explicit template
9745 argument list, e.g.:
9749 to indicate that f was a template instantiation, not a new
9750 function declaration. Now, we have to figure out what
9751 instantiation of what template. */
9753 tree template_id
, arglist
, fns
;
9756 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9758 /* Friend functions are looked up in the containing namespace scope.
9759 We must enter that scope, to avoid finding member functions of the
9760 current class with same name. */
9761 push_nested_namespace (ns
);
9762 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9763 tf_warning_or_error
, NULL_TREE
,
9764 /*integral_constant_expression_p=*/false);
9765 pop_nested_namespace (ns
);
9766 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9767 tf_warning_or_error
, NULL_TREE
);
9768 template_id
= lookup_template_function (fns
, arglist
);
9770 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9771 tmpl
= determine_specialization (template_id
, new_friend
,
9773 /*need_member_template=*/0,
9774 TREE_VEC_LENGTH (args
),
9776 return instantiate_template (tmpl
, new_args
, tf_error
);
9779 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9781 /* The NEW_FRIEND will look like an instantiation, to the
9782 compiler, but is not an instantiation from the point of view of
9783 the language. For example, we might have had:
9785 template <class T> struct S {
9786 template <class U> friend void f(T, U);
9789 Then, in S<int>, template <class U> void f(int, U) is not an
9790 instantiation of anything. */
9791 if (new_friend
== error_mark_node
)
9792 return error_mark_node
;
9794 DECL_USE_TEMPLATE (new_friend
) = 0;
9795 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9797 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9798 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9799 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9802 /* The mangled name for the NEW_FRIEND is incorrect. The function
9803 is not a template instantiation and should not be mangled like
9804 one. Therefore, we forget the mangling here; we'll recompute it
9805 later if we need it. */
9806 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9808 SET_DECL_RTL (new_friend
, NULL
);
9809 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9812 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9815 tree new_friend_template_info
;
9816 tree new_friend_result_template_info
;
9818 int new_friend_is_defn
;
9820 /* We must save some information from NEW_FRIEND before calling
9821 duplicate decls since that function will free NEW_FRIEND if
9823 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9824 new_friend_is_defn
=
9825 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9826 (template_for_substitution (new_friend
)))
9828 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9830 /* This declaration is a `primary' template. */
9831 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9833 new_friend_result_template_info
9834 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9837 new_friend_result_template_info
= NULL_TREE
;
9839 /* Inside pushdecl_namespace_level, we will push into the
9840 current namespace. However, the friend function should go
9841 into the namespace of the template. */
9842 ns
= decl_namespace_context (new_friend
);
9843 push_nested_namespace (ns
);
9844 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9845 pop_nested_namespace (ns
);
9847 if (old_decl
== error_mark_node
)
9848 return error_mark_node
;
9850 if (old_decl
!= new_friend
)
9852 /* This new friend declaration matched an existing
9853 declaration. For example, given:
9855 template <class T> void f(T);
9856 template <class U> class C {
9857 template <class T> friend void f(T) {}
9860 the friend declaration actually provides the definition
9861 of `f', once C has been instantiated for some type. So,
9862 old_decl will be the out-of-class template declaration,
9863 while new_friend is the in-class definition.
9865 But, if `f' was called before this point, the
9866 instantiation of `f' will have DECL_TI_ARGS corresponding
9867 to `T' but not to `U', references to which might appear
9868 in the definition of `f'. Previously, the most general
9869 template for an instantiation of `f' was the out-of-class
9870 version; now it is the in-class version. Therefore, we
9871 run through all specialization of `f', adding to their
9872 DECL_TI_ARGS appropriately. In particular, they need a
9873 new set of outer arguments, corresponding to the
9874 arguments for this class instantiation.
9876 The same situation can arise with something like this:
9879 template <class T> class C {
9883 when `C<int>' is instantiated. Now, `f(int)' is defined
9886 if (!new_friend_is_defn
)
9887 /* On the other hand, if the in-class declaration does
9888 *not* provide a definition, then we don't want to alter
9889 existing definitions. We can just leave everything
9894 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9895 tree new_args
= TI_ARGS (new_friend_template_info
);
9897 /* Overwrite whatever template info was there before, if
9898 any, with the new template information pertaining to
9900 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9902 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9904 /* We should have called reregister_specialization in
9906 gcc_assert (retrieve_specialization (new_template
,
9910 /* Instantiate it if the global has already been used. */
9911 if (DECL_ODR_USED (old_decl
))
9912 instantiate_decl (old_decl
, /*defer_ok=*/true,
9913 /*expl_inst_class_mem_p=*/false);
9919 /* Indicate that the old function template is a partial
9921 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9922 = new_friend_result_template_info
;
9924 gcc_assert (new_template
9925 == most_general_template (new_template
));
9926 gcc_assert (new_template
!= old_decl
);
9928 /* Reassign any specializations already in the hash table
9929 to the new more general template, and add the
9930 additional template args. */
9931 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9935 tree spec
= TREE_VALUE (t
);
9938 elt
.tmpl
= old_decl
;
9939 elt
.args
= DECL_TI_ARGS (spec
);
9940 elt
.spec
= NULL_TREE
;
9942 decl_specializations
->remove_elt (&elt
);
9945 = add_outermost_template_args (new_args
,
9946 DECL_TI_ARGS (spec
));
9948 register_specialization
9949 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9952 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9956 /* The information from NEW_FRIEND has been merged into OLD_DECL
9957 by duplicate_decls. */
9958 new_friend
= old_decl
;
9963 tree context
= DECL_CONTEXT (new_friend
);
9967 template <class T> class C {
9968 template <class U> friend void C1<U>::f (); // case 1
9969 friend void C2<T>::f (); // case 2
9971 we only need to make sure CONTEXT is a complete type for
9972 case 2. To distinguish between the two cases, we note that
9973 CONTEXT of case 1 remains dependent type after tsubst while
9974 this isn't true for case 2. */
9975 ++processing_template_decl
;
9976 dependent_p
= dependent_type_p (context
);
9977 --processing_template_decl
;
9980 && !complete_type_or_else (context
, NULL_TREE
))
9981 return error_mark_node
;
9983 if (COMPLETE_TYPE_P (context
))
9985 tree fn
= new_friend
;
9986 /* do_friend adds the TEMPLATE_DECL for any member friend
9987 template even if it isn't a member template, i.e.
9988 template <class T> friend A<T>::f();
9989 Look through it in that case. */
9990 if (TREE_CODE (fn
) == TEMPLATE_DECL
9991 && !PRIMARY_TEMPLATE_P (fn
))
9992 fn
= DECL_TEMPLATE_RESULT (fn
);
9993 /* Check to see that the declaration is really present, and,
9994 possibly obtain an improved declaration. */
9995 fn
= check_classfn (context
, fn
, NULL_TREE
);
10005 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10006 template arguments, as for tsubst.
10008 Returns an appropriate tsubst'd friend type or error_mark_node on
10012 tsubst_friend_class (tree friend_tmpl
, tree args
)
10016 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10018 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10019 return TREE_TYPE (tmpl
);
10022 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10023 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10024 push_nested_namespace (context
);
10026 push_nested_class (context
);
10028 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10029 /*non_class=*/false, /*block_p=*/false,
10030 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10032 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10034 /* The friend template has already been declared. Just
10035 check to see that the declarations match, and install any new
10036 default parameters. We must tsubst the default parameters,
10037 of course. We only need the innermost template parameters
10038 because that is all that redeclare_class_template will look
10040 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10041 > TMPL_ARGS_DEPTH (args
))
10043 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10044 args
, tf_warning_or_error
);
10045 location_t saved_input_location
= input_location
;
10046 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10047 tree cons
= get_constraints (tmpl
);
10048 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10049 input_location
= saved_input_location
;
10054 /* The friend template has not already been declared. In this
10055 case, the instantiation of the template class will cause the
10056 injection of this template into the namespace scope. */
10057 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10059 if (tmpl
!= error_mark_node
)
10061 /* The new TMPL is not an instantiation of anything, so we
10062 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10063 for the new type because that is supposed to be the
10064 corresponding template decl, i.e., TMPL. */
10065 DECL_USE_TEMPLATE (tmpl
) = 0;
10066 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10067 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10068 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10069 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10071 /* It is hidden. */
10072 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10073 DECL_ANTICIPATED (tmpl
)
10074 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10076 /* Inject this template into the enclosing namspace scope. */
10077 tmpl
= pushdecl_namespace_level (tmpl
, true);
10081 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10082 pop_nested_namespace (context
);
10084 pop_nested_class ();
10086 return TREE_TYPE (tmpl
);
10089 /* Returns zero if TYPE cannot be completed later due to circularity.
10090 Otherwise returns one. */
10093 can_complete_type_without_circularity (tree type
)
10095 if (type
== NULL_TREE
|| type
== error_mark_node
)
10097 else if (COMPLETE_TYPE_P (type
))
10099 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10100 return can_complete_type_without_circularity (TREE_TYPE (type
));
10101 else if (CLASS_TYPE_P (type
)
10102 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10108 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10109 tsubst_flags_t
, tree
);
10111 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10112 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10115 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10116 tsubst_flags_t complain
, tree in_decl
)
10118 gcc_assert (ATTR_IS_DEPENDENT (t
));
10120 tree val
= TREE_VALUE (t
);
10121 if (val
== NULL_TREE
)
10122 /* Nothing to do. */;
10123 else if ((flag_openmp
|| flag_openmp_simd
)
10124 && is_attribute_p ("omp declare simd",
10125 get_attribute_name (t
)))
10127 tree clauses
= TREE_VALUE (val
);
10128 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10129 complain
, in_decl
);
10130 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10131 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10132 tree parms
= DECL_ARGUMENTS (*decl_p
);
10134 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10136 val
= build_tree_list (NULL_TREE
, clauses
);
10140 /* If the first attribute argument is an identifier, don't
10141 pass it through tsubst. Attributes like mode, format,
10142 cleanup and several target specific attributes expect it
10144 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10147 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10148 /*integral_constant_expression_p=*/false);
10149 if (chain
!= TREE_CHAIN (val
))
10150 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10152 else if (PACK_EXPANSION_P (val
))
10154 /* An attribute pack expansion. */
10155 tree purp
= TREE_PURPOSE (t
);
10156 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10157 if (pack
== error_mark_node
)
10158 return error_mark_node
;
10159 int len
= TREE_VEC_LENGTH (pack
);
10160 tree list
= NULL_TREE
;
10162 for (int i
= 0; i
< len
; ++i
)
10164 tree elt
= TREE_VEC_ELT (pack
, i
);
10165 *q
= build_tree_list (purp
, elt
);
10166 q
= &TREE_CHAIN (*q
);
10171 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10172 /*integral_constant_expression_p=*/false);
10174 if (val
!= TREE_VALUE (t
))
10175 return build_tree_list (TREE_PURPOSE (t
), val
);
10179 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10180 unchanged or a new TREE_LIST chain. */
10183 tsubst_attributes (tree attributes
, tree args
,
10184 tsubst_flags_t complain
, tree in_decl
)
10186 tree last_dep
= NULL_TREE
;
10188 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10189 if (ATTR_IS_DEPENDENT (t
))
10192 attributes
= copy_list (attributes
);
10197 for (tree
*p
= &attributes
; *p
; )
10200 if (ATTR_IS_DEPENDENT (t
))
10202 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10207 p
= &TREE_CHAIN (*p
);
10209 *p
= TREE_CHAIN (t
);
10213 p
= &TREE_CHAIN (*p
);
10219 /* Apply any attributes which had to be deferred until instantiation
10220 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10221 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10224 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10225 tree args
, tsubst_flags_t complain
, tree in_decl
)
10227 tree last_dep
= NULL_TREE
;
10231 if (attributes
== NULL_TREE
)
10234 if (DECL_P (*decl_p
))
10236 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10238 p
= &DECL_ATTRIBUTES (*decl_p
);
10239 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10240 to our attributes parameter. */
10241 gcc_assert (*p
== attributes
);
10245 p
= &TYPE_ATTRIBUTES (*decl_p
);
10246 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10247 lookup_template_class_1, and should be preserved. */
10248 gcc_assert (*p
!= attributes
);
10250 p
= &TREE_CHAIN (*p
);
10253 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10254 if (ATTR_IS_DEPENDENT (t
))
10257 attributes
= copy_list (attributes
);
10264 tree late_attrs
= NULL_TREE
;
10265 tree
*q
= &late_attrs
;
10270 if (ATTR_IS_DEPENDENT (t
))
10272 *p
= TREE_CHAIN (t
);
10273 TREE_CHAIN (t
) = NULL_TREE
;
10274 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10276 q
= &TREE_CHAIN (*q
);
10280 p
= &TREE_CHAIN (t
);
10283 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10287 /* Perform (or defer) access check for typedefs that were referenced
10288 from within the template TMPL code.
10289 This is a subroutine of instantiate_decl and instantiate_class_template.
10290 TMPL is the template to consider and TARGS is the list of arguments of
10294 perform_typedefs_access_check (tree tmpl
, tree targs
)
10296 location_t saved_location
;
10298 qualified_typedef_usage_t
*iter
;
10301 || (!CLASS_TYPE_P (tmpl
)
10302 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10305 saved_location
= input_location
;
10306 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10308 tree type_decl
= iter
->typedef_decl
;
10309 tree type_scope
= iter
->context
;
10311 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10314 if (uses_template_parms (type_decl
))
10315 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10316 if (uses_template_parms (type_scope
))
10317 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10319 /* Make access check error messages point to the location
10320 of the use of the typedef. */
10321 input_location
= iter
->locus
;
10322 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10323 type_decl
, type_decl
,
10324 tf_warning_or_error
);
10326 input_location
= saved_location
;
10330 instantiate_class_template_1 (tree type
)
10332 tree templ
, args
, pattern
, t
, member
;
10336 unsigned int saved_maximum_field_alignment
;
10339 if (type
== error_mark_node
)
10340 return error_mark_node
;
10342 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10343 || uses_template_parms (type
))
10346 /* Figure out which template is being instantiated. */
10347 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10348 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10350 /* Determine what specialization of the original template to
10352 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10353 if (t
== error_mark_node
)
10355 TYPE_BEING_DEFINED (type
) = 1;
10356 return error_mark_node
;
10360 /* This TYPE is actually an instantiation of a partial
10361 specialization. We replace the innermost set of ARGS with
10362 the arguments appropriate for substitution. For example,
10365 template <class T> struct S {};
10366 template <class T> struct S<T*> {};
10368 and supposing that we are instantiating S<int*>, ARGS will
10369 presently be {int*} -- but we need {int}. */
10370 pattern
= TREE_TYPE (t
);
10371 args
= TREE_PURPOSE (t
);
10375 pattern
= TREE_TYPE (templ
);
10376 args
= CLASSTYPE_TI_ARGS (type
);
10379 /* If the template we're instantiating is incomplete, then clearly
10380 there's nothing we can do. */
10381 if (!COMPLETE_TYPE_P (pattern
))
10384 /* If we've recursively instantiated too many templates, stop. */
10385 if (! push_tinst_level (type
))
10388 /* Now we're really doing the instantiation. Mark the type as in
10389 the process of being defined. */
10390 TYPE_BEING_DEFINED (type
) = 1;
10392 /* We may be in the middle of deferred access check. Disable
10394 push_deferring_access_checks (dk_no_deferred
);
10396 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10397 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10399 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10400 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10401 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10402 fn_context
= error_mark_node
;
10404 push_to_top_level ();
10407 cp_unevaluated_operand
= 0;
10408 c_inhibit_evaluation_warnings
= 0;
10410 /* Use #pragma pack from the template context. */
10411 saved_maximum_field_alignment
= maximum_field_alignment
;
10412 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10414 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10416 /* Set the input location to the most specialized template definition.
10417 This is needed if tsubsting causes an error. */
10418 typedecl
= TYPE_MAIN_DECL (pattern
);
10419 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10420 DECL_SOURCE_LOCATION (typedecl
);
10422 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10423 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10424 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10425 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10426 if (ANON_AGGR_TYPE_P (pattern
))
10427 SET_ANON_AGGR_TYPE_P (type
);
10428 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10430 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10431 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10432 /* Adjust visibility for template arguments. */
10433 determine_visibility (TYPE_MAIN_DECL (type
));
10435 if (CLASS_TYPE_P (type
))
10436 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10438 pbinfo
= TYPE_BINFO (pattern
);
10440 /* We should never instantiate a nested class before its enclosing
10441 class; we need to look up the nested class by name before we can
10442 instantiate it, and that lookup should instantiate the enclosing
10444 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10445 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10447 base_list
= NULL_TREE
;
10448 if (BINFO_N_BASE_BINFOS (pbinfo
))
10454 /* We must enter the scope containing the type, as that is where
10455 the accessibility of types named in dependent bases are
10457 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10459 /* Substitute into each of the bases to determine the actual
10461 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10464 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10465 tree expanded_bases
= NULL_TREE
;
10468 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10471 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10472 args
, tf_error
, NULL_TREE
);
10473 if (expanded_bases
== error_mark_node
)
10476 len
= TREE_VEC_LENGTH (expanded_bases
);
10479 for (idx
= 0; idx
< len
; idx
++)
10481 if (expanded_bases
)
10482 /* Extract the already-expanded base class. */
10483 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10485 /* Substitute to figure out the base class. */
10486 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10489 if (base
== error_mark_node
)
10492 base_list
= tree_cons (access
, base
, base_list
);
10493 if (BINFO_VIRTUAL_P (pbase_binfo
))
10494 TREE_TYPE (base_list
) = integer_type_node
;
10498 /* The list is now in reverse order; correct that. */
10499 base_list
= nreverse (base_list
);
10502 pop_scope (pushed_scope
);
10504 /* Now call xref_basetypes to set up all the base-class
10506 xref_basetypes (type
, base_list
);
10508 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10509 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10510 args
, tf_error
, NULL_TREE
);
10511 fixup_attribute_variants (type
);
10513 /* Now that our base classes are set up, enter the scope of the
10514 class, so that name lookups into base classes, etc. will work
10515 correctly. This is precisely analogous to what we do in
10516 begin_class_definition when defining an ordinary non-template
10517 class, except we also need to push the enclosing classes. */
10518 push_nested_class (type
);
10520 /* Now members are processed in the order of declaration. */
10521 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10522 member
; member
= TREE_CHAIN (member
))
10524 tree t
= TREE_VALUE (member
);
10526 if (TREE_PURPOSE (member
))
10530 /* Build new CLASSTYPE_NESTED_UTDS. */
10533 bool class_template_p
;
10535 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10536 && TYPE_LANG_SPECIFIC (t
)
10537 && CLASSTYPE_IS_TEMPLATE (t
));
10538 /* If the member is a class template, then -- even after
10539 substitution -- there may be dependent types in the
10540 template argument list for the class. We increment
10541 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10542 that function will assume that no types are dependent
10543 when outside of a template. */
10544 if (class_template_p
)
10545 ++processing_template_decl
;
10546 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10547 if (class_template_p
)
10548 --processing_template_decl
;
10549 if (newtag
== error_mark_node
)
10552 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10554 tree name
= TYPE_IDENTIFIER (t
);
10556 if (class_template_p
)
10557 /* Unfortunately, lookup_template_class sets
10558 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10559 instantiation (i.e., for the type of a member
10560 template class nested within a template class.)
10561 This behavior is required for
10562 maybe_process_partial_specialization to work
10563 correctly, but is not accurate in this case;
10564 the TAG is not an instantiation of anything.
10565 (The corresponding TEMPLATE_DECL is an
10566 instantiation, but the TYPE is not.) */
10567 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10569 /* Now, we call pushtag to put this NEWTAG into the scope of
10570 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10571 pushtag calling push_template_decl. We don't have to do
10572 this for enums because it will already have been done in
10575 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10576 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10579 else if (DECL_DECLARES_FUNCTION_P (t
))
10583 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10584 ++processing_template_decl
;
10585 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10586 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10587 --processing_template_decl
;
10588 set_current_access_from_decl (r
);
10589 finish_member_declaration (r
);
10590 /* Instantiate members marked with attribute used. */
10591 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10593 if (TREE_CODE (r
) == FUNCTION_DECL
10594 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10595 cp_check_omp_declare_reduction (r
);
10597 else if (DECL_CLASS_TEMPLATE_P (t
)
10598 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10599 /* A closure type for a lambda in a default argument for a
10600 member template. Ignore it; it will be instantiated with
10601 the default argument. */;
10604 /* Build new TYPE_FIELDS. */
10605 if (TREE_CODE (t
) == STATIC_ASSERT
)
10609 ++c_inhibit_evaluation_warnings
;
10611 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10612 tf_warning_or_error
, NULL_TREE
,
10613 /*integral_constant_expression_p=*/true);
10614 --c_inhibit_evaluation_warnings
;
10616 finish_static_assert (condition
,
10617 STATIC_ASSERT_MESSAGE (t
),
10618 STATIC_ASSERT_SOURCE_LOCATION (t
),
10619 /*member_p=*/true);
10621 else if (TREE_CODE (t
) != CONST_DECL
)
10624 tree vec
= NULL_TREE
;
10627 /* The file and line for this declaration, to
10628 assist in error message reporting. Since we
10629 called push_tinst_level above, we don't need to
10631 input_location
= DECL_SOURCE_LOCATION (t
);
10633 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10634 ++processing_template_decl
;
10635 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10636 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10637 --processing_template_decl
;
10639 if (TREE_CODE (r
) == TREE_VEC
)
10641 /* A capture pack became multiple fields. */
10643 len
= TREE_VEC_LENGTH (vec
);
10646 for (int i
= 0; i
< len
; ++i
)
10649 r
= TREE_VEC_ELT (vec
, i
);
10654 [t]he initialization (and any associated
10655 side-effects) of a static data member does
10656 not occur unless the static data member is
10657 itself used in a way that requires the
10658 definition of the static data member to
10661 Therefore, we do not substitute into the
10662 initialized for the static data member here. */
10663 finish_static_data_member_decl
10665 /*init=*/NULL_TREE
,
10666 /*init_const_expr_p=*/false,
10667 /*asmspec_tree=*/NULL_TREE
,
10669 /* Instantiate members marked with attribute used. */
10670 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10673 else if (TREE_CODE (r
) == FIELD_DECL
)
10675 /* Determine whether R has a valid type and can be
10676 completed later. If R is invalid, then its type
10677 is replaced by error_mark_node. */
10678 tree rtype
= TREE_TYPE (r
);
10679 if (can_complete_type_without_circularity (rtype
))
10680 complete_type (rtype
);
10682 if (!complete_or_array_type_p (rtype
))
10684 /* If R's type couldn't be completed and
10685 it isn't a flexible array member (whose
10686 type is incomplete by definition) give
10688 cxx_incomplete_type_error (r
, rtype
);
10689 TREE_TYPE (r
) = error_mark_node
;
10693 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10694 such a thing will already have been added to the field
10695 list by tsubst_enum in finish_member_declaration in the
10696 CLASSTYPE_NESTED_UTDS case above. */
10697 if (!(TREE_CODE (r
) == TYPE_DECL
10698 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10699 && DECL_ARTIFICIAL (r
)))
10701 set_current_access_from_decl (r
);
10702 finish_member_declaration (r
);
10710 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10711 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10713 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10715 tree friend_type
= t
;
10716 bool adjust_processing_template_decl
= false;
10718 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10720 /* template <class T> friend class C; */
10721 friend_type
= tsubst_friend_class (friend_type
, args
);
10722 adjust_processing_template_decl
= true;
10724 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10726 /* template <class T> friend class C::D; */
10727 friend_type
= tsubst (friend_type
, args
,
10728 tf_warning_or_error
, NULL_TREE
);
10729 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10730 friend_type
= TREE_TYPE (friend_type
);
10731 adjust_processing_template_decl
= true;
10733 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10734 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10736 /* This could be either
10740 when dependent_type_p is false or
10742 template <class U> friend class T::C;
10745 /* Bump processing_template_decl in case this is something like
10746 template <class T> friend struct A<T>::B. */
10747 ++processing_template_decl
;
10748 friend_type
= tsubst (friend_type
, args
,
10749 tf_warning_or_error
, NULL_TREE
);
10750 if (dependent_type_p (friend_type
))
10751 adjust_processing_template_decl
= true;
10752 --processing_template_decl
;
10754 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10755 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10756 && TYPE_HIDDEN_P (friend_type
))
10760 where C hasn't been declared yet. Let's lookup name
10761 from namespace scope directly, bypassing any name that
10762 come from dependent base class. */
10763 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10765 /* The call to xref_tag_from_type does injection for friend
10767 push_nested_namespace (ns
);
10769 xref_tag_from_type (friend_type
, NULL_TREE
,
10770 /*tag_scope=*/ts_current
);
10771 pop_nested_namespace (ns
);
10773 else if (uses_template_parms (friend_type
))
10774 /* friend class C<T>; */
10775 friend_type
= tsubst (friend_type
, args
,
10776 tf_warning_or_error
, NULL_TREE
);
10781 where C is already declared or
10783 friend class C<int>;
10785 We don't have to do anything in these cases. */
10787 if (adjust_processing_template_decl
)
10788 /* Trick make_friend_class into realizing that the friend
10789 we're adding is a template, not an ordinary class. It's
10790 important that we use make_friend_class since it will
10791 perform some error-checking and output cross-reference
10793 ++processing_template_decl
;
10795 if (friend_type
!= error_mark_node
)
10796 make_friend_class (type
, friend_type
, /*complain=*/false);
10798 if (adjust_processing_template_decl
)
10799 --processing_template_decl
;
10803 /* Build new DECL_FRIENDLIST. */
10806 /* The file and line for this declaration, to
10807 assist in error message reporting. Since we
10808 called push_tinst_level above, we don't need to
10810 input_location
= DECL_SOURCE_LOCATION (t
);
10812 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10814 ++processing_template_decl
;
10815 push_deferring_access_checks (dk_no_check
);
10818 r
= tsubst_friend_function (t
, args
);
10819 add_friend (type
, r
, /*complain=*/false);
10820 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10822 pop_deferring_access_checks ();
10823 --processing_template_decl
;
10831 /* Restore these before substituting into the lambda capture
10833 cp_unevaluated_operand
= saved_unevaluated_operand
;
10834 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10837 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10839 tree decl
= lambda_function (type
);
10842 if (cxx_dialect
>= cxx17
)
10843 CLASSTYPE_LITERAL_P (type
) = true;
10845 if (!DECL_TEMPLATE_INFO (decl
)
10846 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10848 /* Set function_depth to avoid garbage collection. */
10850 instantiate_decl (decl
, /*defer_ok=*/false, false);
10854 /* We need to instantiate the capture list from the template
10855 after we've instantiated the closure members, but before we
10856 consider adding the conversion op. Also keep any captures
10857 that may have been added during instantiation of the op(). */
10858 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10860 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10861 args
, tf_warning_or_error
, NULL_TREE
,
10864 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10865 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10867 maybe_add_lambda_conv_op (type
);
10870 gcc_assert (errorcount
);
10873 /* Set the file and line number information to whatever is given for
10874 the class itself. This puts error messages involving generated
10875 implicit functions at a predictable point, and the same point
10876 that would be used for non-template classes. */
10877 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10879 unreverse_member_declarations (type
);
10880 finish_struct_1 (type
);
10881 TYPE_BEING_DEFINED (type
) = 0;
10883 /* We don't instantiate default arguments for member functions. 14.7.1:
10885 The implicit instantiation of a class template specialization causes
10886 the implicit instantiation of the declarations, but not of the
10887 definitions or default arguments, of the class member functions,
10888 member classes, static data members and member templates.... */
10890 /* Some typedefs referenced from within the template code need to be access
10891 checked at template instantiation time, i.e now. These types were
10892 added to the template at parsing time. Let's get those and perform
10893 the access checks then. */
10894 perform_typedefs_access_check (pattern
, args
);
10895 perform_deferred_access_checks (tf_warning_or_error
);
10896 pop_nested_class ();
10897 maximum_field_alignment
= saved_maximum_field_alignment
;
10899 pop_from_top_level ();
10900 pop_deferring_access_checks ();
10901 pop_tinst_level ();
10903 /* The vtable for a template class can be emitted in any translation
10904 unit in which the class is instantiated. When there is no key
10905 method, however, finish_struct_1 will already have added TYPE to
10906 the keyed_classes. */
10907 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10908 vec_safe_push (keyed_classes
, type
);
10913 /* Wrapper for instantiate_class_template_1. */
10916 instantiate_class_template (tree type
)
10919 timevar_push (TV_TEMPLATE_INST
);
10920 ret
= instantiate_class_template_1 (type
);
10921 timevar_pop (TV_TEMPLATE_INST
);
10926 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10932 else if (TYPE_P (t
))
10933 r
= tsubst (t
, args
, complain
, in_decl
);
10936 if (!(complain
& tf_warning
))
10937 ++c_inhibit_evaluation_warnings
;
10938 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10939 /*integral_constant_expression_p=*/true);
10940 if (!(complain
& tf_warning
))
10941 --c_inhibit_evaluation_warnings
;
10946 /* Given a function parameter pack TMPL_PARM and some function parameters
10947 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10948 and set *SPEC_P to point at the next point in the list. */
10951 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10953 /* Collect all of the extra "packed" parameters into an
10956 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10957 tree spec_parm
= *spec_p
;
10960 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10962 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10965 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10966 parmvec
= make_tree_vec (len
);
10967 spec_parm
= *spec_p
;
10968 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10970 tree elt
= spec_parm
;
10971 if (DECL_PACK_P (elt
))
10972 elt
= make_pack_expansion (elt
);
10973 TREE_VEC_ELT (parmvec
, i
) = elt
;
10976 /* Build the argument packs. */
10977 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10978 *spec_p
= spec_parm
;
10983 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10984 NONTYPE_ARGUMENT_PACK. */
10987 make_fnparm_pack (tree spec_parm
)
10989 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10992 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10993 pack expansion with no extra args, 2 if it has extra args, or 0
10994 if it is not a pack expansion. */
10997 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10999 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11000 if (i
>= TREE_VEC_LENGTH (vec
))
11002 tree elt
= TREE_VEC_ELT (vec
, i
);
11004 /* A decl pack is itself an expansion. */
11005 elt
= TREE_TYPE (elt
);
11006 if (!PACK_EXPANSION_P (elt
))
11008 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11014 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11017 make_argument_pack_select (tree arg_pack
, unsigned index
)
11019 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11021 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11022 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11027 /* This is a subroutine of tsubst_pack_expansion.
11029 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11030 mechanism to store the (non complete list of) arguments of the
11031 substitution and return a non substituted pack expansion, in order
11032 to wait for when we have enough arguments to really perform the
11036 use_pack_expansion_extra_args_p (tree parm_packs
,
11038 bool has_empty_arg
)
11040 /* If one pack has an expansion and another pack has a normal
11041 argument or if one pack has an empty argument and an another
11042 one hasn't then tsubst_pack_expansion cannot perform the
11043 substitution and need to fall back on the
11044 PACK_EXPANSION_EXTRA mechanism. */
11045 if (parm_packs
== NULL_TREE
)
11047 else if (has_empty_arg
)
11050 bool has_expansion_arg
= false;
11051 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11053 bool has_non_expansion_arg
= false;
11054 for (tree parm_pack
= parm_packs
;
11056 parm_pack
= TREE_CHAIN (parm_pack
))
11058 tree arg
= TREE_VALUE (parm_pack
);
11060 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11062 /* We can't substitute a pack expansion with extra args into
11066 has_expansion_arg
= true;
11068 has_non_expansion_arg
= true;
11071 if (has_expansion_arg
&& has_non_expansion_arg
)
11077 /* [temp.variadic]/6 says that:
11079 The instantiation of a pack expansion [...]
11080 produces a list E1,E2, ..., En, where N is the number of elements
11081 in the pack expansion parameters.
11083 This subroutine of tsubst_pack_expansion produces one of these Ei.
11085 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11086 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11087 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11088 INDEX is the index 'i' of the element Ei to produce. ARGS,
11089 COMPLAIN, and IN_DECL are the same parameters as for the
11090 tsubst_pack_expansion function.
11092 The function returns the resulting Ei upon successful completion,
11093 or error_mark_node.
11095 Note that this function possibly modifies the ARGS parameter, so
11096 it's the responsibility of the caller to restore it. */
11099 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11102 tree args
/* This parm gets
11104 tsubst_flags_t complain
,
11108 bool ith_elem_is_expansion
= false;
11110 /* For each parameter pack, change the substitution of the parameter
11111 pack to the ith argument in its argument pack, then expand the
11113 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11115 tree parm
= TREE_PURPOSE (pack
);
11116 tree arg_pack
= TREE_VALUE (pack
);
11117 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11119 ith_elem_is_expansion
|=
11120 argument_pack_element_is_expansion_p (arg_pack
, index
);
11122 /* Select the Ith argument from the pack. */
11123 if (TREE_CODE (parm
) == PARM_DECL
11124 || TREE_CODE (parm
) == FIELD_DECL
)
11128 aps
= make_argument_pack_select (arg_pack
, index
);
11129 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11130 return error_mark_node
;
11131 register_local_specialization (aps
, parm
);
11134 aps
= retrieve_local_specialization (parm
);
11139 template_parm_level_and_index (parm
, &level
, &idx
);
11143 aps
= make_argument_pack_select (arg_pack
, index
);
11144 /* Update the corresponding argument. */
11145 TMPL_ARG (args
, level
, idx
) = aps
;
11148 /* Re-use the ARGUMENT_PACK_SELECT. */
11149 aps
= TMPL_ARG (args
, level
, idx
);
11151 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11154 /* Substitute into the PATTERN with the (possibly altered)
11156 if (pattern
== in_decl
)
11157 /* Expanding a fixed parameter pack from
11158 coerce_template_parameter_pack. */
11159 t
= tsubst_decl (pattern
, args
, complain
);
11160 else if (pattern
== error_mark_node
)
11161 t
= error_mark_node
;
11162 else if (constraint_p (pattern
))
11164 if (processing_template_decl
)
11165 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11167 t
= (constraints_satisfied_p (pattern
, args
)
11168 ? boolean_true_node
: boolean_false_node
);
11170 else if (!TYPE_P (pattern
))
11171 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11172 /*integral_constant_expression_p=*/false);
11174 t
= tsubst (pattern
, args
, complain
, in_decl
);
11176 /* If the Ith argument pack element is a pack expansion, then
11177 the Ith element resulting from the substituting is going to
11178 be a pack expansion as well. */
11179 if (ith_elem_is_expansion
)
11180 t
= make_pack_expansion (t
, complain
);
11185 /* When the unexpanded parameter pack in a fold expression expands to an empty
11186 sequence, the value of the expression is as follows; the program is
11187 ill-formed if the operator is not listed in this table.
11194 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11196 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11197 if (!FOLD_EXPR_MODIFY_P (t
))
11200 case TRUTH_ANDIF_EXPR
:
11201 return boolean_true_node
;
11202 case TRUTH_ORIF_EXPR
:
11203 return boolean_false_node
;
11204 case COMPOUND_EXPR
:
11210 if (complain
& tf_error
)
11211 error_at (location_of (t
),
11212 "fold of empty expansion over %O", code
);
11213 return error_mark_node
;
11216 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11217 form an expression that combines the two terms using the
11221 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11223 tree op
= FOLD_EXPR_OP (t
);
11224 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11226 // Handle compound assignment operators.
11227 if (FOLD_EXPR_MODIFY_P (t
))
11228 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11232 case COMPOUND_EXPR
:
11233 return build_x_compound_expr (input_location
, left
, right
, complain
);
11235 return build_m_component_ref (left
, right
, complain
);
11237 return build_x_binary_op (input_location
, code
,
11238 left
, TREE_CODE (left
),
11239 right
, TREE_CODE (right
),
11245 /* Substitute ARGS into the pack of a fold expression T. */
11248 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11250 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11253 /* Substitute ARGS into the pack of a fold expression T. */
11256 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11258 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11261 /* Expand a PACK of arguments into a grouped as left fold.
11262 Given a pack containing elements A0, A1, ..., An and an
11263 operator @, this builds the expression:
11265 ((A0 @ A1) @ A2) ... @ An
11267 Note that PACK must not be empty.
11269 The operator is defined by the original fold expression T. */
11272 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11274 tree left
= TREE_VEC_ELT (pack
, 0);
11275 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11277 tree right
= TREE_VEC_ELT (pack
, i
);
11278 left
= fold_expression (t
, left
, right
, complain
);
11283 /* Substitute into a unary left fold expression. */
11286 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11289 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11290 if (pack
== error_mark_node
)
11291 return error_mark_node
;
11292 if (PACK_EXPANSION_P (pack
))
11294 tree r
= copy_node (t
);
11295 FOLD_EXPR_PACK (r
) = pack
;
11298 if (TREE_VEC_LENGTH (pack
) == 0)
11299 return expand_empty_fold (t
, complain
);
11301 return expand_left_fold (t
, pack
, complain
);
11304 /* Substitute into a binary left fold expression.
11306 Do ths by building a single (non-empty) vector of argumnts and
11307 building the expression from those elements. */
11310 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11313 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11314 if (pack
== error_mark_node
)
11315 return error_mark_node
;
11316 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11317 if (init
== error_mark_node
)
11318 return error_mark_node
;
11320 if (PACK_EXPANSION_P (pack
))
11322 tree r
= copy_node (t
);
11323 FOLD_EXPR_PACK (r
) = pack
;
11324 FOLD_EXPR_INIT (r
) = init
;
11328 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11329 TREE_VEC_ELT (vec
, 0) = init
;
11330 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11331 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11333 return expand_left_fold (t
, vec
, complain
);
11336 /* Expand a PACK of arguments into a grouped as right fold.
11337 Given a pack containing elementns A0, A1, ..., and an
11338 operator @, this builds the expression:
11340 A0@ ... (An-2 @ (An-1 @ An))
11342 Note that PACK must not be empty.
11344 The operator is defined by the original fold expression T. */
11347 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11349 // Build the expression.
11350 int n
= TREE_VEC_LENGTH (pack
);
11351 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11352 for (--n
; n
!= 0; --n
)
11354 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11355 right
= fold_expression (t
, left
, right
, complain
);
11360 /* Substitute into a unary right fold expression. */
11363 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11366 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11367 if (pack
== error_mark_node
)
11368 return error_mark_node
;
11369 if (PACK_EXPANSION_P (pack
))
11371 tree r
= copy_node (t
);
11372 FOLD_EXPR_PACK (r
) = pack
;
11375 if (TREE_VEC_LENGTH (pack
) == 0)
11376 return expand_empty_fold (t
, complain
);
11378 return expand_right_fold (t
, pack
, complain
);
11381 /* Substitute into a binary right fold expression.
11383 Do ths by building a single (non-empty) vector of arguments and
11384 building the expression from those elements. */
11387 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11390 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11391 if (pack
== error_mark_node
)
11392 return error_mark_node
;
11393 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11394 if (init
== error_mark_node
)
11395 return error_mark_node
;
11397 if (PACK_EXPANSION_P (pack
))
11399 tree r
= copy_node (t
);
11400 FOLD_EXPR_PACK (r
) = pack
;
11401 FOLD_EXPR_INIT (r
) = init
;
11405 int n
= TREE_VEC_LENGTH (pack
);
11406 tree vec
= make_tree_vec (n
+ 1);
11407 for (int i
= 0; i
< n
; ++i
)
11408 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11409 TREE_VEC_ELT (vec
, n
) = init
;
11411 return expand_right_fold (t
, vec
, complain
);
11415 /* Substitute ARGS into T, which is an pack expansion
11416 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11417 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11418 (if only a partial substitution could be performed) or
11419 ERROR_MARK_NODE if there was an error. */
11421 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11425 tree pack
, packs
= NULL_TREE
;
11426 bool unsubstituted_packs
= false;
11427 bool unsubstituted_fn_pack
= false;
11430 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11431 bool need_local_specializations
= false;
11434 gcc_assert (PACK_EXPANSION_P (t
));
11435 pattern
= PACK_EXPANSION_PATTERN (t
);
11437 /* Add in any args remembered from an earlier partial instantiation. */
11438 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11440 levels
= TMPL_ARGS_DEPTH (args
);
11442 /* Determine the argument packs that will instantiate the parameter
11443 packs used in the expansion expression. While we're at it,
11444 compute the number of arguments to be expanded and make sure it
11446 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11447 pack
= TREE_CHAIN (pack
))
11449 tree parm_pack
= TREE_VALUE (pack
);
11450 tree arg_pack
= NULL_TREE
;
11451 tree orig_arg
= NULL_TREE
;
11454 if (TREE_CODE (parm_pack
) == BASES
)
11456 gcc_assert (parm_pack
== pattern
);
11457 if (BASES_DIRECT (parm_pack
))
11458 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11459 args
, complain
, in_decl
, false));
11461 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11462 args
, complain
, in_decl
, false));
11464 else if (builtin_pack_call_p (parm_pack
))
11466 /* ??? Support use in other patterns. */
11467 gcc_assert (parm_pack
== pattern
);
11468 return expand_builtin_pack_call (parm_pack
, args
,
11469 complain
, in_decl
);
11471 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11473 /* We know we have correct local_specializations if this
11474 expansion is at function scope, or if we're dealing with a
11475 local parameter in a requires expression; for the latter,
11476 tsubst_requires_expr set it up appropriately. */
11477 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11478 arg_pack
= retrieve_local_specialization (parm_pack
);
11480 /* We can't rely on local_specializations for a parameter
11481 name used later in a function declaration (such as in a
11482 late-specified return type). Even if it exists, it might
11483 have the wrong value for a recursive call. */
11484 need_local_specializations
= true;
11488 /* This parameter pack was used in an unevaluated context. Just
11489 make a dummy decl, since it's only used for its type. */
11490 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11491 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11492 /* Partial instantiation of the parm_pack, we can't build
11493 up an argument pack yet. */
11494 arg_pack
= NULL_TREE
;
11496 arg_pack
= make_fnparm_pack (arg_pack
);
11498 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11499 /* This argument pack isn't fully instantiated yet. We set this
11500 flag rather than clear arg_pack because we do want to do the
11501 optimization below, and we don't want to substitute directly
11502 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11503 where it isn't expected). */
11504 unsubstituted_fn_pack
= true;
11506 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11507 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11511 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11513 if (level
<= levels
)
11514 arg_pack
= TMPL_ARG (args
, level
, idx
);
11517 orig_arg
= arg_pack
;
11518 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11519 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11521 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11522 /* This can only happen if we forget to expand an argument
11523 pack somewhere else. Just return an error, silently. */
11525 result
= make_tree_vec (1);
11526 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11533 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11535 /* Don't bother trying to do a partial substitution with
11536 incomplete packs; we'll try again after deduction. */
11537 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11542 else if (len
!= my_len
11543 && !unsubstituted_fn_pack
)
11545 if (!(complain
& tf_error
))
11546 /* Fail quietly. */;
11547 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11548 error ("mismatched argument pack lengths while expanding %qT",
11551 error ("mismatched argument pack lengths while expanding %qE",
11553 return error_mark_node
;
11556 /* Keep track of the parameter packs and their corresponding
11558 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11559 TREE_TYPE (packs
) = orig_arg
;
11563 /* We can't substitute for this parameter pack. We use a flag as
11564 well as the missing_level counter because function parameter
11565 packs don't have a level. */
11566 gcc_assert (processing_template_decl
);
11567 unsubstituted_packs
= true;
11571 /* If the expansion is just T..., return the matching argument pack, unless
11572 we need to call convert_from_reference on all the elements. This is an
11573 important optimization; see c++/68422. */
11574 if (!unsubstituted_packs
11575 && TREE_PURPOSE (packs
) == pattern
)
11577 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11579 /* If the argument pack is a single pack expansion, pull it out. */
11580 if (TREE_VEC_LENGTH (args
) == 1
11581 && pack_expansion_args_count (args
))
11582 return TREE_VEC_ELT (args
, 0);
11584 /* Types need no adjustment, nor does sizeof..., and if we still have
11585 some pack expansion args we won't do anything yet. */
11586 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11587 || PACK_EXPANSION_SIZEOF_P (t
)
11588 || pack_expansion_args_count (args
))
11590 /* Also optimize expression pack expansions if we can tell that the
11591 elements won't have reference type. */
11592 tree type
= TREE_TYPE (pattern
);
11593 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11594 && !PACK_EXPANSION_P (type
)
11595 && !WILDCARD_TYPE_P (type
))
11597 /* Otherwise use the normal path so we get convert_from_reference. */
11600 /* We cannot expand this expansion expression, because we don't have
11601 all of the argument packs we need. */
11602 if (use_pack_expansion_extra_args_p (packs
, len
, (unsubstituted_packs
11603 || unsubstituted_fn_pack
)))
11605 /* We got some full packs, but we can't substitute them in until we
11606 have values for all the packs. So remember these until then. */
11608 t
= make_pack_expansion (pattern
, complain
);
11609 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11612 else if (unsubstituted_packs
)
11614 /* There were no real arguments, we're just replacing a parameter
11615 pack with another version of itself. Substitute into the
11616 pattern and return a PACK_EXPANSION_*. The caller will need to
11618 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11619 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11620 /*integral_constant_expression_p=*/false);
11622 t
= tsubst (pattern
, args
, complain
, in_decl
);
11623 t
= make_pack_expansion (t
, complain
);
11627 gcc_assert (len
>= 0);
11629 if (need_local_specializations
)
11631 /* We're in a late-specified return type, so create our own local
11632 specializations map; the current map is either NULL or (in the
11633 case of recursive unification) might have bindings that we don't
11634 want to use or alter. */
11635 saved_local_specializations
= local_specializations
;
11636 local_specializations
= new hash_map
<tree
, tree
>;
11639 /* For each argument in each argument pack, substitute into the
11641 result
= make_tree_vec (len
);
11642 tree elem_args
= copy_template_args (args
);
11643 for (i
= 0; i
< len
; ++i
)
11645 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11647 elem_args
, complain
,
11649 TREE_VEC_ELT (result
, i
) = t
;
11650 if (t
== error_mark_node
)
11652 result
= error_mark_node
;
11657 /* Update ARGS to restore the substitution from parameter packs to
11658 their argument packs. */
11659 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11661 tree parm
= TREE_PURPOSE (pack
);
11663 if (TREE_CODE (parm
) == PARM_DECL
11664 || TREE_CODE (parm
) == FIELD_DECL
)
11665 register_local_specialization (TREE_TYPE (pack
), parm
);
11670 if (TREE_VALUE (pack
) == NULL_TREE
)
11673 template_parm_level_and_index (parm
, &level
, &idx
);
11675 /* Update the corresponding argument. */
11676 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11677 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11680 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11684 if (need_local_specializations
)
11686 delete local_specializations
;
11687 local_specializations
= saved_local_specializations
;
11690 /* If the dependent pack arguments were such that we end up with only a
11691 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11692 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11693 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11694 return TREE_VEC_ELT (result
, 0);
11699 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11700 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11701 parameter packs; all parms generated from a function parameter pack will
11702 have the same DECL_PARM_INDEX. */
11705 get_pattern_parm (tree parm
, tree tmpl
)
11707 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11710 if (DECL_ARTIFICIAL (parm
))
11712 for (patparm
= DECL_ARGUMENTS (pattern
);
11713 patparm
; patparm
= DECL_CHAIN (patparm
))
11714 if (DECL_ARTIFICIAL (patparm
)
11715 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11720 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11721 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11722 gcc_assert (DECL_PARM_INDEX (patparm
)
11723 == DECL_PARM_INDEX (parm
));
11729 /* Make an argument pack out of the TREE_VEC VEC. */
11732 make_argument_pack (tree vec
)
11735 tree elt
= TREE_VEC_ELT (vec
, 0);
11737 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11740 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11741 TREE_CONSTANT (pack
) = 1;
11743 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11747 /* Return an exact copy of template args T that can be modified
11751 copy_template_args (tree t
)
11753 if (t
== error_mark_node
)
11756 int len
= TREE_VEC_LENGTH (t
);
11757 tree new_vec
= make_tree_vec (len
);
11759 for (int i
= 0; i
< len
; ++i
)
11761 tree elt
= TREE_VEC_ELT (t
, i
);
11762 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11763 elt
= copy_template_args (elt
);
11764 TREE_VEC_ELT (new_vec
, i
) = elt
;
11767 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11768 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11773 /* Substitute ARGS into the vector or list of template arguments T. */
11776 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11779 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11782 if (t
== error_mark_node
)
11783 return error_mark_node
;
11785 len
= TREE_VEC_LENGTH (t
);
11786 elts
= XALLOCAVEC (tree
, len
);
11788 for (i
= 0; i
< len
; i
++)
11790 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11793 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11794 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11795 else if (PACK_EXPANSION_P (orig_arg
))
11797 /* Substitute into an expansion expression. */
11798 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11800 if (TREE_CODE (new_arg
) == TREE_VEC
)
11801 /* Add to the expanded length adjustment the number of
11802 expanded arguments. We subtract one from this
11803 measurement, because the argument pack expression
11804 itself is already counted as 1 in
11805 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11806 the argument pack is empty. */
11807 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11809 else if (ARGUMENT_PACK_P (orig_arg
))
11811 /* Substitute into each of the arguments. */
11812 new_arg
= TYPE_P (orig_arg
)
11813 ? cxx_make_type (TREE_CODE (orig_arg
))
11814 : make_node (TREE_CODE (orig_arg
));
11816 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11817 args
, complain
, in_decl
);
11818 if (pack_args
== error_mark_node
)
11819 new_arg
= error_mark_node
;
11821 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11823 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11824 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11827 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11829 if (new_arg
== error_mark_node
)
11830 return error_mark_node
;
11833 if (new_arg
!= orig_arg
)
11840 /* Make space for the expanded arguments coming from template
11842 t
= make_tree_vec (len
+ expanded_len_adjust
);
11843 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11844 arguments for a member template.
11845 In that case each TREE_VEC in ORIG_T represents a level of template
11846 arguments, and ORIG_T won't carry any non defaulted argument count.
11847 It will rather be the nested TREE_VECs that will carry one.
11848 In other words, ORIG_T carries a non defaulted argument count only
11849 if it doesn't contain any nested TREE_VEC. */
11850 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11852 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11853 count
+= expanded_len_adjust
;
11854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11856 for (i
= 0, out
= 0; i
< len
; i
++)
11858 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11859 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11860 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11864 /* Now expand the template argument pack "in place". */
11865 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11866 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11870 TREE_VEC_ELT (t
, out
) = elts
[i
];
11878 /* Substitute ARGS into one level PARMS of template parameters. */
11881 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11883 if (parms
== error_mark_node
)
11884 return error_mark_node
;
11886 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11888 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11890 tree tuple
= TREE_VEC_ELT (parms
, i
);
11892 if (tuple
== error_mark_node
)
11895 TREE_VEC_ELT (new_vec
, i
) =
11896 tsubst_template_parm (tuple
, args
, complain
);
11902 /* Return the result of substituting ARGS into the template parameters
11903 given by PARMS. If there are m levels of ARGS and m + n levels of
11904 PARMS, then the result will contain n levels of PARMS. For
11905 example, if PARMS is `template <class T> template <class U>
11906 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11907 result will be `template <int*, double, class V>'. */
11910 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11912 tree r
= NULL_TREE
;
11915 /* When substituting into a template, we must set
11916 PROCESSING_TEMPLATE_DECL as the template parameters may be
11917 dependent if they are based on one-another, and the dependency
11918 predicates are short-circuit outside of templates. */
11919 ++processing_template_decl
;
11921 for (new_parms
= &r
;
11922 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11923 new_parms
= &(TREE_CHAIN (*new_parms
)),
11924 parms
= TREE_CHAIN (parms
))
11926 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11929 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11930 - TMPL_ARGS_DEPTH (args
)),
11931 new_vec
, NULL_TREE
);
11934 --processing_template_decl
;
11939 /* Return the result of substituting ARGS into one template parameter
11940 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11941 parameter and which TREE_PURPOSE is the default argument of the
11942 template parameter. */
11945 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11947 tree default_value
, parm_decl
;
11949 if (args
== NULL_TREE
11951 || t
== error_mark_node
)
11954 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11956 default_value
= TREE_PURPOSE (t
);
11957 parm_decl
= TREE_VALUE (t
);
11959 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11960 if (TREE_CODE (parm_decl
) == PARM_DECL
11961 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11962 parm_decl
= error_mark_node
;
11963 default_value
= tsubst_template_arg (default_value
, args
,
11964 complain
, NULL_TREE
);
11966 return build_tree_list (default_value
, parm_decl
);
11969 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11970 type T. If T is not an aggregate or enumeration type, it is
11971 handled as if by tsubst. IN_DECL is as for tsubst. If
11972 ENTERING_SCOPE is nonzero, T is the context for a template which
11973 we are presently tsubst'ing. Return the substituted value. */
11976 tsubst_aggr_type (tree t
,
11978 tsubst_flags_t complain
,
11980 int entering_scope
)
11982 if (t
== NULL_TREE
)
11985 switch (TREE_CODE (t
))
11988 if (TYPE_PTRMEMFUNC_P (t
))
11989 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11991 /* Fall through. */
11992 case ENUMERAL_TYPE
:
11994 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11999 int saved_unevaluated_operand
;
12000 int saved_inhibit_evaluation_warnings
;
12002 /* In "sizeof(X<I>)" we need to evaluate "I". */
12003 saved_unevaluated_operand
= cp_unevaluated_operand
;
12004 cp_unevaluated_operand
= 0;
12005 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12006 c_inhibit_evaluation_warnings
= 0;
12008 /* First, determine the context for the type we are looking
12010 context
= TYPE_CONTEXT (t
);
12011 if (context
&& TYPE_P (context
))
12013 context
= tsubst_aggr_type (context
, args
, complain
,
12014 in_decl
, /*entering_scope=*/1);
12015 /* If context is a nested class inside a class template,
12016 it may still need to be instantiated (c++/33959). */
12017 context
= complete_type (context
);
12020 /* Then, figure out what arguments are appropriate for the
12021 type we are trying to find. For example, given:
12023 template <class T> struct S;
12024 template <class T, class U> void f(T, U) { S<U> su; }
12026 and supposing that we are instantiating f<int, double>,
12027 then our ARGS will be {int, double}, but, when looking up
12028 S we only want {double}. */
12029 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12030 complain
, in_decl
);
12031 if (argvec
== error_mark_node
)
12032 r
= error_mark_node
;
12035 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12036 entering_scope
, complain
);
12037 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12040 cp_unevaluated_operand
= saved_unevaluated_operand
;
12041 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12046 /* This is not a template type, so there's nothing to do. */
12050 return tsubst (t
, args
, complain
, in_decl
);
12054 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12056 /* Substitute into the default argument ARG (a default argument for
12057 FN), which has the indicated TYPE. */
12060 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12061 tsubst_flags_t complain
)
12063 tree saved_class_ptr
= NULL_TREE
;
12064 tree saved_class_ref
= NULL_TREE
;
12065 int errs
= errorcount
+ sorrycount
;
12067 /* This can happen in invalid code. */
12068 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12071 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12072 parm
= chain_index (parmnum
, parm
);
12073 tree parmtype
= TREE_TYPE (parm
);
12074 if (DECL_BY_REFERENCE (parm
))
12075 parmtype
= TREE_TYPE (parmtype
);
12076 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12079 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12082 /* This default argument came from a template. Instantiate the
12083 default argument here, not in tsubst. In the case of
12092 we must be careful to do name lookup in the scope of S<T>,
12093 rather than in the current class. */
12094 push_access_scope (fn
);
12095 /* The "this" pointer is not valid in a default argument. */
12098 saved_class_ptr
= current_class_ptr
;
12099 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12100 saved_class_ref
= current_class_ref
;
12101 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12104 start_lambda_scope (parm
);
12106 push_deferring_access_checks(dk_no_deferred
);
12107 /* The default argument expression may cause implicitly defined
12108 member functions to be synthesized, which will result in garbage
12109 collection. We must treat this situation as if we were within
12110 the body of function so as to avoid collecting live data on the
12113 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12114 complain
, NULL_TREE
,
12115 /*integral_constant_expression_p=*/false);
12117 pop_deferring_access_checks();
12119 finish_lambda_scope ();
12121 /* Restore the "this" pointer. */
12124 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12125 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12128 if (errorcount
+sorrycount
> errs
12129 && (complain
& tf_warning_or_error
))
12130 inform (input_location
,
12131 " when instantiating default argument for call to %qD", fn
);
12133 /* Make sure the default argument is reasonable. */
12134 arg
= check_default_argument (type
, arg
, complain
);
12136 pop_access_scope (fn
);
12138 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12141 defarg_inst
= tree_cache_map::create_ggc (37);
12142 defarg_inst
->put (parm
, arg
);
12148 /* Substitute into all the default arguments for FN. */
12151 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12156 tmpl_args
= DECL_TI_ARGS (fn
);
12158 /* If this function is not yet instantiated, we certainly don't need
12159 its default arguments. */
12160 if (uses_template_parms (tmpl_args
))
12162 /* Don't do this again for clones. */
12163 if (DECL_CLONED_FUNCTION_P (fn
))
12167 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12169 arg
= TREE_CHAIN (arg
), ++i
)
12170 if (TREE_PURPOSE (arg
))
12171 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12173 TREE_PURPOSE (arg
),
12177 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12180 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12181 tree lambda_fntype
)
12183 tree gen_tmpl
, argvec
;
12184 hashval_t hash
= 0;
12187 /* Nobody should be tsubst'ing into non-template functions. */
12188 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12190 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12192 /* If T is not dependent, just return it. */
12193 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12196 /* Calculate the most general template of which R is a
12198 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12200 /* We're substituting a lambda function under tsubst_lambda_expr but not
12201 directly from it; find the matching function we're already inside.
12202 But don't do this if T is a generic lambda with a single level of
12203 template parms, as in that case we're doing a normal instantiation. */
12204 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12205 && (!generic_lambda_fn_p (t
)
12206 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12207 return enclosing_instantiation_of (t
);
12209 /* Calculate the complete set of arguments used to
12211 argvec
= tsubst_template_args (DECL_TI_ARGS
12212 (DECL_TEMPLATE_RESULT
12213 (DECL_TI_TEMPLATE (t
))),
12214 args
, complain
, in_decl
);
12215 if (argvec
== error_mark_node
)
12216 return error_mark_node
;
12218 /* Check to see if we already have this specialization. */
12219 if (!lambda_fntype
)
12221 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12222 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12226 /* We can see more levels of arguments than parameters if
12227 there was a specialization of a member template, like
12230 template <class T> struct S { template <class U> void f(); }
12231 template <> template <class U> void S<int>::f(U);
12233 Here, we'll be substituting into the specialization,
12234 because that's where we can find the code we actually
12235 want to generate, but we'll have enough arguments for
12236 the most general template.
12238 We also deal with the peculiar case:
12240 template <class T> struct S {
12241 template <class U> friend void f();
12243 template <class U> void f() {}
12245 template void f<double>();
12247 Here, the ARGS for the instantiation of will be {int,
12248 double}. But, we only need as many ARGS as there are
12249 levels of template parameters in CODE_PATTERN. We are
12250 careful not to get fooled into reducing the ARGS in
12253 template <class T> struct S { template <class U> void f(U); }
12254 template <class T> template <> void S<T>::f(int) {}
12256 which we can spot because the pattern will be a
12257 specialization in this case. */
12258 int args_depth
= TMPL_ARGS_DEPTH (args
);
12260 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12262 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12263 args
= get_innermost_template_args (args
, parms_depth
);
12267 /* This special case arises when we have something like this:
12269 template <class T> struct S {
12270 friend void f<int>(int, double);
12273 Here, the DECL_TI_TEMPLATE for the friend declaration
12274 will be an IDENTIFIER_NODE. We are being called from
12275 tsubst_friend_function, and we want only to create a
12276 new decl (R) with appropriate types so that we can call
12277 determine_specialization. */
12278 gen_tmpl
= NULL_TREE
;
12279 argvec
= NULL_TREE
;
12282 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12284 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12285 bool member
= ctx
&& TYPE_P (ctx
);
12287 if (member
&& !closure
)
12288 ctx
= tsubst_aggr_type (ctx
, args
,
12289 complain
, t
, /*entering_scope=*/1);
12291 tree type
= (lambda_fntype
? lambda_fntype
12292 : tsubst (TREE_TYPE (t
), args
,
12293 complain
| tf_fndecl_type
, in_decl
));
12294 if (type
== error_mark_node
)
12295 return error_mark_node
;
12297 /* If we hit excessive deduction depth, the type is bogus even if
12298 it isn't error_mark_node, so don't build a decl. */
12299 if (excessive_deduction_depth
)
12300 return error_mark_node
;
12302 /* We do NOT check for matching decls pushed separately at this
12303 point, as they may not represent instantiations of this
12304 template, and in any case are considered separate under the
12306 tree r
= copy_decl (t
);
12307 DECL_USE_TEMPLATE (r
) = 0;
12308 TREE_TYPE (r
) = type
;
12309 /* Clear out the mangled name and RTL for the instantiation. */
12310 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12311 SET_DECL_RTL (r
, NULL
);
12312 /* Leave DECL_INITIAL set on deleted instantiations. */
12313 if (!DECL_DELETED_FN (r
))
12314 DECL_INITIAL (r
) = NULL_TREE
;
12315 DECL_CONTEXT (r
) = ctx
;
12317 /* OpenMP UDRs have the only argument a reference to the declared
12318 type. We want to diagnose if the declared type is a reference,
12319 which is invalid, but as references to references are usually
12320 quietly merged, diagnose it here. */
12321 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12324 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12325 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12326 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12327 error_at (DECL_SOURCE_LOCATION (t
),
12328 "reference type %qT in "
12329 "%<#pragma omp declare reduction%>", argtype
);
12330 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12331 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12335 if (member
&& DECL_CONV_FN_P (r
))
12336 /* Type-conversion operator. Reconstruct the name, in
12337 case it's the name of one of the template's parameters. */
12338 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12340 tree parms
= DECL_ARGUMENTS (t
);
12342 parms
= DECL_CHAIN (parms
);
12343 parms
= tsubst (parms
, args
, complain
, t
);
12344 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12345 DECL_CONTEXT (parm
) = r
;
12348 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12349 DECL_CHAIN (tparm
) = parms
;
12352 DECL_ARGUMENTS (r
) = parms
;
12353 DECL_RESULT (r
) = NULL_TREE
;
12355 TREE_STATIC (r
) = 0;
12356 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12357 DECL_EXTERNAL (r
) = 1;
12358 /* If this is an instantiation of a function with internal
12359 linkage, we already know what object file linkage will be
12360 assigned to the instantiation. */
12361 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12362 DECL_DEFER_OUTPUT (r
) = 0;
12363 DECL_CHAIN (r
) = NULL_TREE
;
12364 DECL_PENDING_INLINE_INFO (r
) = 0;
12365 DECL_PENDING_INLINE_P (r
) = 0;
12366 DECL_SAVED_TREE (r
) = NULL_TREE
;
12367 DECL_STRUCT_FUNCTION (r
) = NULL
;
12369 /* We'll re-clone as appropriate in instantiate_template. */
12370 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12372 /* If we aren't complaining now, return on error before we register
12373 the specialization so that we'll complain eventually. */
12374 if ((complain
& tf_error
) == 0
12375 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12376 && !grok_op_properties (r
, /*complain=*/false))
12377 return error_mark_node
;
12379 /* When instantiating a constrained member, substitute
12380 into the constraints to create a new constraint. */
12381 if (tree ci
= get_constraints (t
))
12384 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12385 set_constraints (r
, ci
);
12388 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12389 this in the special friend case mentioned above where
12390 GEN_TMPL is NULL. */
12391 if (gen_tmpl
&& !closure
)
12393 DECL_TEMPLATE_INFO (r
)
12394 = build_template_info (gen_tmpl
, argvec
);
12395 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12398 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12400 /* We instantiated this while substituting into
12401 the type earlier (template/friend54.C). */
12404 /* We're not supposed to instantiate default arguments
12405 until they are called, for a template. But, for a
12408 template <class T> void f ()
12409 { extern void g(int i = T()); }
12411 we should do the substitution when the template is
12412 instantiated. We handle the member function case in
12413 instantiate_class_template since the default arguments
12414 might refer to other members of the class. */
12416 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12417 && !uses_template_parms (argvec
))
12418 tsubst_default_arguments (r
, complain
);
12421 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12423 /* Copy the list of befriending classes. */
12424 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12426 friends
= &TREE_CHAIN (*friends
))
12428 *friends
= copy_node (*friends
);
12429 TREE_VALUE (*friends
)
12430 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12433 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12435 maybe_retrofit_in_chrg (r
);
12436 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12437 return error_mark_node
;
12438 /* If this is an instantiation of a member template, clone it.
12439 If it isn't, that'll be handled by
12440 clone_constructors_and_destructors. */
12441 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12442 clone_function_decl (r
, /*update_methods=*/false);
12444 else if ((complain
& tf_error
) != 0
12445 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12446 && !grok_op_properties (r
, /*complain=*/true))
12447 return error_mark_node
;
12449 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12450 SET_DECL_FRIEND_CONTEXT (r
,
12451 tsubst (DECL_FRIEND_CONTEXT (t
),
12452 args
, complain
, in_decl
));
12454 /* Possibly limit visibility based on template args. */
12455 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12456 if (DECL_VISIBILITY_SPECIFIED (t
))
12458 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12459 DECL_ATTRIBUTES (r
)
12460 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12462 determine_visibility (r
);
12463 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12464 && !processing_template_decl
)
12465 defaulted_late_check (r
);
12467 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12468 args
, complain
, in_decl
);
12472 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12475 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12476 tree lambda_fntype
)
12478 /* We can get here when processing a member function template,
12479 member class template, or template template parameter. */
12480 tree decl
= DECL_TEMPLATE_RESULT (t
);
12486 hashval_t hash
= 0;
12488 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12490 /* Template template parameter is treated here. */
12491 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12492 if (new_type
== error_mark_node
)
12493 r
= error_mark_node
;
12494 /* If we get a real template back, return it. This can happen in
12495 the context of most_specialized_partial_spec. */
12496 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12499 /* The new TEMPLATE_DECL was built in
12500 reduce_template_parm_level. */
12501 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12505 if (!lambda_fntype
)
12507 /* We might already have an instance of this template.
12508 The ARGS are for the surrounding class type, so the
12509 full args contain the tsubst'd args for the context,
12510 plus the innermost args from the template decl. */
12511 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12512 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12513 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12514 /* Because this is a template, the arguments will still be
12515 dependent, even after substitution. If
12516 PROCESSING_TEMPLATE_DECL is not set, the dependency
12517 predicates will short-circuit. */
12518 ++processing_template_decl
;
12519 full_args
= tsubst_template_args (tmpl_args
, args
,
12520 complain
, in_decl
);
12521 --processing_template_decl
;
12522 if (full_args
== error_mark_node
)
12523 return error_mark_node
;
12525 /* If this is a default template template argument,
12526 tsubst might not have changed anything. */
12527 if (full_args
== tmpl_args
)
12530 hash
= hash_tmpl_and_args (t
, full_args
);
12531 spec
= retrieve_specialization (t
, full_args
, hash
);
12532 if (spec
!= NULL_TREE
)
12536 /* Make a new template decl. It will be similar to the
12537 original, but will record the current template arguments.
12538 We also create a new function declaration, which is just
12539 like the old one, but points to this new template, rather
12540 than the old one. */
12542 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12543 DECL_CHAIN (r
) = NULL_TREE
;
12545 // Build new template info linking to the original template decl.
12546 if (!lambda_fntype
)
12548 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12549 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12552 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12554 /* The template parameters for this new template are all the
12555 template parameters for the old template, except the
12556 outermost level of parameters. */
12557 DECL_TEMPLATE_PARMS (r
)
12558 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12561 if (TREE_CODE (decl
) == TYPE_DECL
12562 && !TYPE_DECL_ALIAS_P (decl
))
12565 ++processing_template_decl
;
12566 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12567 --processing_template_decl
;
12568 if (new_type
== error_mark_node
)
12569 return error_mark_node
;
12571 TREE_TYPE (r
) = new_type
;
12572 /* For a partial specialization, we need to keep pointing to
12573 the primary template. */
12574 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12575 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12576 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12577 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12578 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12583 ++processing_template_decl
;
12584 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12585 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12587 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12588 --processing_template_decl
;
12589 if (new_decl
== error_mark_node
)
12590 return error_mark_node
;
12592 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12593 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12594 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12597 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12598 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12602 DECL_TI_TEMPLATE (new_decl
) = r
;
12603 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12607 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12608 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12610 if (PRIMARY_TEMPLATE_P (t
))
12611 DECL_PRIMARY_TEMPLATE (r
) = r
;
12613 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12615 /* Record this non-type partial instantiation. */
12616 register_specialization (r
, t
,
12617 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12623 /* True if FN is the op() for a lambda in an uninstantiated template. */
12626 lambda_fn_in_template_p (tree fn
)
12628 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12630 tree closure
= DECL_CONTEXT (fn
);
12631 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12634 /* We're instantiating a variable from template function TCTX. Return the
12635 corresponding current enclosing scope. This gets complicated because lambda
12636 functions in templates are regenerated rather than instantiated, but generic
12637 lambda functions are subsequently instantiated. */
12640 enclosing_instantiation_of (tree otctx
)
12643 tree fn
= current_function_decl
;
12644 int lambda_count
= 0;
12646 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12647 tctx
= decl_function_context (tctx
))
12649 for (; fn
; fn
= decl_function_context (fn
))
12652 int flambda_count
= 0;
12653 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12654 fn
= decl_function_context (fn
))
12656 if (DECL_TEMPLATE_INFO (fn
)
12657 ? most_general_template (fn
) != most_general_template (tctx
)
12660 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12661 || DECL_CONV_FN_P (ofn
));
12664 gcc_unreachable ();
12667 /* Substitute the ARGS into the T, which is a _DECL. Return the
12668 result of the substitution. Issue error and warning messages under
12669 control of COMPLAIN. */
12672 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12674 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12675 location_t saved_loc
;
12676 tree r
= NULL_TREE
;
12678 hashval_t hash
= 0;
12680 /* Set the filename and linenumber to improve error-reporting. */
12681 saved_loc
= input_location
;
12682 input_location
= DECL_SOURCE_LOCATION (t
);
12684 switch (TREE_CODE (t
))
12686 case TEMPLATE_DECL
:
12687 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12690 case FUNCTION_DECL
:
12691 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12696 tree type
= NULL_TREE
;
12698 tree expanded_types
= NULL_TREE
;
12699 tree prev_r
= NULL_TREE
;
12700 tree first_r
= NULL_TREE
;
12702 if (DECL_PACK_P (t
))
12704 /* If there is a local specialization that isn't a
12705 parameter pack, it means that we're doing a "simple"
12706 substitution from inside tsubst_pack_expansion. Just
12707 return the local specialization (which will be a single
12709 tree spec
= retrieve_local_specialization (t
);
12711 && TREE_CODE (spec
) == PARM_DECL
12712 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12715 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12716 the parameters in this function parameter pack. */
12717 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12718 complain
, in_decl
);
12719 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12721 len
= TREE_VEC_LENGTH (expanded_types
);
12723 /* Zero-length parameter packs are boring. Just substitute
12726 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12731 /* All we did was update the type. Make a note of that. */
12732 type
= expanded_types
;
12733 expanded_types
= NULL_TREE
;
12737 /* Loop through all of the parameters we'll build. When T is
12738 a function parameter pack, LEN is the number of expanded
12739 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12741 for (i
= 0; i
< len
; ++i
)
12745 if (DECL_TEMPLATE_PARM_P (t
))
12746 SET_DECL_TEMPLATE_PARM_P (r
);
12748 if (expanded_types
)
12749 /* We're on the Ith parameter of the function parameter
12752 /* Get the Ith type. */
12753 type
= TREE_VEC_ELT (expanded_types
, i
);
12755 /* Rename the parameter to include the index. */
12757 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12760 /* We're dealing with a normal parameter. */
12761 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12763 type
= type_decays_to (type
);
12764 TREE_TYPE (r
) = type
;
12765 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12767 if (DECL_INITIAL (r
))
12769 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12770 DECL_INITIAL (r
) = TREE_TYPE (r
);
12772 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12773 complain
, in_decl
);
12776 DECL_CONTEXT (r
) = NULL_TREE
;
12778 if (!DECL_TEMPLATE_PARM_P (r
))
12779 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12781 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12782 args
, complain
, in_decl
);
12784 /* Keep track of the first new parameter we
12785 generate. That's what will be returned to the
12790 /* Build a proper chain of parameters when substituting
12791 into a function parameter pack. */
12793 DECL_CHAIN (prev_r
) = r
;
12796 /* If cp_unevaluated_operand is set, we're just looking for a
12797 single dummy parameter, so don't keep going. */
12798 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12799 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12800 complain
, DECL_CHAIN (t
));
12802 /* FIRST_R contains the start of the chain we've built. */
12809 tree type
= NULL_TREE
;
12810 tree vec
= NULL_TREE
;
12811 tree expanded_types
= NULL_TREE
;
12814 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12816 /* This field is a lambda capture pack. Return a TREE_VEC of
12817 the expanded fields to instantiate_class_template_1 and
12818 store them in the specializations hash table as a
12819 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12820 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12821 complain
, in_decl
);
12822 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12824 len
= TREE_VEC_LENGTH (expanded_types
);
12825 vec
= make_tree_vec (len
);
12829 /* All we did was update the type. Make a note of that. */
12830 type
= expanded_types
;
12831 expanded_types
= NULL_TREE
;
12835 for (int i
= 0; i
< len
; ++i
)
12838 if (expanded_types
)
12840 type
= TREE_VEC_ELT (expanded_types
, i
);
12842 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12845 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12847 if (type
== error_mark_node
)
12848 RETURN (error_mark_node
);
12849 TREE_TYPE (r
) = type
;
12850 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12852 if (DECL_C_BIT_FIELD (r
))
12853 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12855 DECL_BIT_FIELD_REPRESENTATIVE (r
)
12856 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
12858 /*integral_constant_expression_p=*/true);
12859 if (DECL_INITIAL (t
))
12861 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12862 NSDMI in perform_member_init. Still set DECL_INITIAL
12863 so that we know there is one. */
12864 DECL_INITIAL (r
) = void_node
;
12865 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12866 retrofit_lang_decl (r
);
12867 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12869 /* We don't have to set DECL_CONTEXT here; it is set by
12870 finish_member_declaration. */
12871 DECL_CHAIN (r
) = NULL_TREE
;
12873 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12874 args
, complain
, in_decl
);
12877 TREE_VEC_ELT (vec
, i
) = r
;
12883 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12884 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12885 register_specialization (pack
, t
, args
, false, 0);
12891 /* We reach here only for member using decls. We also need to check
12892 uses_template_parms because DECL_DEPENDENT_P is not set for a
12893 using-declaration that designates a member of the current
12894 instantiation (c++/53549). */
12895 if (DECL_DEPENDENT_P (t
)
12896 || uses_template_parms (USING_DECL_SCOPE (t
)))
12898 tree scope
= USING_DECL_SCOPE (t
);
12899 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12900 if (PACK_EXPANSION_P (scope
))
12902 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12903 int len
= TREE_VEC_LENGTH (vec
);
12904 r
= make_tree_vec (len
);
12905 for (int i
= 0; i
< len
; ++i
)
12907 tree escope
= TREE_VEC_ELT (vec
, i
);
12908 tree elt
= do_class_using_decl (escope
, name
);
12911 r
= error_mark_node
;
12916 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12917 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12919 TREE_VEC_ELT (r
, i
) = elt
;
12924 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12925 complain
, in_decl
);
12926 r
= do_class_using_decl (inst_scope
, name
);
12928 r
= error_mark_node
;
12931 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12932 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12939 DECL_CHAIN (r
) = NULL_TREE
;
12946 tree argvec
= NULL_TREE
;
12947 tree gen_tmpl
= NULL_TREE
;
12949 tree tmpl
= NULL_TREE
;
12951 tree type
= NULL_TREE
;
12954 if (TREE_TYPE (t
) == error_mark_node
)
12955 RETURN (error_mark_node
);
12957 if (TREE_CODE (t
) == TYPE_DECL
12958 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12960 /* If this is the canonical decl, we don't have to
12961 mess with instantiations, and often we can't (for
12962 typename, template type parms and such). Note that
12963 TYPE_NAME is not correct for the above test if
12964 we've copied the type for a typedef. */
12965 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12966 if (type
== error_mark_node
)
12967 RETURN (error_mark_node
);
12968 r
= TYPE_NAME (type
);
12972 /* Check to see if we already have the specialization we
12975 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12977 /* T is a static data member or namespace-scope entity.
12978 We have to substitute into namespace-scope variables
12979 (not just variable templates) because of cases like:
12981 template <class T> void f() { extern T t; }
12983 where the entity referenced is not known until
12984 instantiation time. */
12986 ctx
= DECL_CONTEXT (t
);
12987 if (DECL_CLASS_SCOPE_P (t
))
12989 ctx
= tsubst_aggr_type (ctx
, args
,
12991 in_decl
, /*entering_scope=*/1);
12992 /* If CTX is unchanged, then T is in fact the
12993 specialization we want. That situation occurs when
12994 referencing a static data member within in its own
12995 class. We can use pointer equality, rather than
12996 same_type_p, because DECL_CONTEXT is always
12998 if (ctx
== DECL_CONTEXT (t
)
12999 /* ... unless T is a member template; in which
13000 case our caller can be willing to create a
13001 specialization of that template represented
13003 && !(DECL_TI_TEMPLATE (t
)
13004 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13010 tmpl
= DECL_TI_TEMPLATE (t
);
13011 gen_tmpl
= most_general_template (tmpl
);
13012 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13013 if (argvec
!= error_mark_node
)
13014 argvec
= (coerce_innermost_template_parms
13015 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13016 argvec
, t
, complain
,
13017 /*all*/true, /*defarg*/true));
13018 if (argvec
== error_mark_node
)
13019 RETURN (error_mark_node
);
13020 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13021 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13026 /* A local variable. */
13028 /* Subsequent calls to pushdecl will fill this in. */
13030 /* Unless this is a reference to a static variable from an
13031 enclosing function, in which case we need to fill it in now. */
13032 if (TREE_STATIC (t
))
13034 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13035 if (fn
!= current_function_decl
)
13038 spec
= retrieve_local_specialization (t
);
13040 /* If we already have the specialization we need, there is
13041 nothing more to do. */
13048 /* Create a new node for the specialization we need. */
13050 if (type
== NULL_TREE
)
13052 if (is_typedef_decl (t
))
13053 type
= DECL_ORIGINAL_TYPE (t
);
13055 type
= TREE_TYPE (t
);
13057 && VAR_HAD_UNKNOWN_BOUND (t
)
13058 && type
!= error_mark_node
)
13059 type
= strip_array_domain (type
);
13060 tree sub_args
= args
;
13061 if (tree auto_node
= type_uses_auto (type
))
13063 /* Mask off any template args past the variable's context so we
13064 don't replace the auto with an unrelated argument. */
13065 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13066 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13068 /* This should never happen with the new lambda instantiation
13069 model, but keep the handling just in case. */
13070 gcc_assert (!CHECKING_P
),
13071 sub_args
= strip_innermost_template_args (args
, extra
);
13073 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13077 /* Even if the original location is out of scope, the
13078 newly substituted one is not. */
13079 DECL_DEAD_FOR_LOCAL (r
) = 0;
13080 DECL_INITIALIZED_P (r
) = 0;
13081 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13082 if (type
== error_mark_node
)
13083 RETURN (error_mark_node
);
13084 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13086 /* It may seem that this case cannot occur, since:
13091 declares a function, not a variable. However:
13094 template <typename T> void g() { T t; }
13095 template void g<f>();
13097 is an attempt to declare a variable with function
13099 error ("variable %qD has function type",
13100 /* R is not yet sufficiently initialized, so we
13101 just use its name. */
13103 RETURN (error_mark_node
);
13105 type
= complete_type (type
);
13106 /* Wait until cp_finish_decl to set this again, to handle
13107 circular dependency (template/instantiate6.C). */
13108 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13109 type
= check_var_type (DECL_NAME (r
), type
);
13111 if (DECL_HAS_VALUE_EXPR_P (t
))
13113 tree ve
= DECL_VALUE_EXPR (t
);
13114 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13115 /*constant_expression_p=*/false);
13116 if (REFERENCE_REF_P (ve
))
13118 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13119 ve
= TREE_OPERAND (ve
, 0);
13121 SET_DECL_VALUE_EXPR (r
, ve
);
13123 if (CP_DECL_THREAD_LOCAL_P (r
)
13124 && !processing_template_decl
)
13125 set_decl_tls_model (r
, decl_default_tls_model (r
));
13127 else if (DECL_SELF_REFERENCE_P (t
))
13128 SET_DECL_SELF_REFERENCE_P (r
);
13129 TREE_TYPE (r
) = type
;
13130 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13131 DECL_CONTEXT (r
) = ctx
;
13132 /* Clear out the mangled name and RTL for the instantiation. */
13133 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13134 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13135 SET_DECL_RTL (r
, NULL
);
13136 /* The initializer must not be expanded until it is required;
13137 see [temp.inst]. */
13138 DECL_INITIAL (r
) = NULL_TREE
;
13139 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13142 if (DECL_LANG_SPECIFIC (r
))
13143 SET_DECL_DEPENDENT_INIT_P (r
, false);
13145 SET_DECL_MODE (r
, VOIDmode
);
13147 /* Possibly limit visibility based on template args. */
13148 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13149 if (DECL_VISIBILITY_SPECIFIED (t
))
13151 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13152 DECL_ATTRIBUTES (r
)
13153 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13155 determine_visibility (r
);
13160 /* A static data member declaration is always marked
13161 external when it is declared in-class, even if an
13162 initializer is present. We mimic the non-template
13163 processing here. */
13164 DECL_EXTERNAL (r
) = 1;
13165 if (DECL_NAMESPACE_SCOPE_P (t
))
13166 DECL_NOT_REALLY_EXTERN (r
) = 1;
13168 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13169 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13170 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13174 if (DECL_LANG_SPECIFIC (r
))
13175 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13176 if (!cp_unevaluated_operand
)
13177 register_local_specialization (r
, t
);
13180 DECL_CHAIN (r
) = NULL_TREE
;
13182 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13184 args
, complain
, in_decl
);
13186 /* Preserve a typedef that names a type. */
13187 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13189 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13190 set_underlying_type (r
);
13191 if (TYPE_DECL_ALIAS_P (r
))
13192 /* An alias template specialization can be dependent
13193 even if its underlying type is not. */
13194 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13197 layout_decl (r
, 0);
13202 gcc_unreachable ();
13207 /* Restore the file and line information. */
13208 input_location
= saved_loc
;
13213 /* Substitute into the ARG_TYPES of a function type.
13214 If END is a TREE_CHAIN, leave it and any following types
13218 tsubst_arg_types (tree arg_types
,
13221 tsubst_flags_t complain
,
13224 tree remaining_arg_types
;
13225 tree type
= NULL_TREE
;
13227 tree expanded_args
= NULL_TREE
;
13230 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13233 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13234 args
, end
, complain
, in_decl
);
13235 if (remaining_arg_types
== error_mark_node
)
13236 return error_mark_node
;
13238 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13240 /* For a pack expansion, perform substitution on the
13241 entire expression. Later on, we'll handle the arguments
13243 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13244 args
, complain
, in_decl
);
13246 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13247 /* So that we'll spin through the parameters, one by one. */
13248 i
= TREE_VEC_LENGTH (expanded_args
);
13251 /* We only partially substituted into the parameter
13252 pack. Our type is TYPE_PACK_EXPANSION. */
13253 type
= expanded_args
;
13254 expanded_args
= NULL_TREE
;
13262 type
= TREE_VEC_ELT (expanded_args
, i
);
13264 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13266 if (type
== error_mark_node
)
13267 return error_mark_node
;
13268 if (VOID_TYPE_P (type
))
13270 if (complain
& tf_error
)
13272 error ("invalid parameter type %qT", type
);
13274 error ("in declaration %q+D", in_decl
);
13276 return error_mark_node
;
13279 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13280 return error_mark_node
;
13282 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13283 top-level qualifiers as required. */
13284 type
= cv_unqualified (type_decays_to (type
));
13286 /* We do not substitute into default arguments here. The standard
13287 mandates that they be instantiated only when needed, which is
13288 done in build_over_call. */
13289 default_arg
= TREE_PURPOSE (arg_types
);
13291 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13292 since the new op() won't have any associated template arguments for us
13293 to refer to later. */
13294 if (lambda_fn_in_template_p (in_decl
))
13295 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13296 false/*fn*/, false/*constexpr*/);
13298 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13300 /* We've instantiated a template before its default arguments
13301 have been parsed. This can happen for a nested template
13302 class, and is not an error unless we require the default
13303 argument in a call of this function. */
13304 remaining_arg_types
=
13305 tree_cons (default_arg
, type
, remaining_arg_types
);
13306 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13309 remaining_arg_types
=
13310 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13313 return remaining_arg_types
;
13316 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13317 *not* handle the exception-specification for FNTYPE, because the
13318 initial substitution of explicitly provided template parameters
13319 during argument deduction forbids substitution into the
13320 exception-specification:
13324 All references in the function type of the function template to the
13325 corresponding template parameters are replaced by the specified tem-
13326 plate argument values. If a substitution in a template parameter or
13327 in the function type of the function template results in an invalid
13328 type, type deduction fails. [Note: The equivalent substitution in
13329 exception specifications is done only when the function is instanti-
13330 ated, at which point a program is ill-formed if the substitution
13331 results in an invalid type.] */
13334 tsubst_function_type (tree t
,
13336 tsubst_flags_t complain
,
13340 tree arg_types
= NULL_TREE
;
13343 /* The TYPE_CONTEXT is not used for function/method types. */
13344 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13346 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13348 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13350 if (late_return_type_p
)
13352 /* Substitute the argument types. */
13353 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13354 complain
, in_decl
);
13355 if (arg_types
== error_mark_node
)
13356 return error_mark_node
;
13358 tree save_ccp
= current_class_ptr
;
13359 tree save_ccr
= current_class_ref
;
13360 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13361 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13362 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13365 /* DR 1207: 'this' is in scope in the trailing return type. */
13366 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13369 /* Substitute the return type. */
13370 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13374 current_class_ptr
= save_ccp
;
13375 current_class_ref
= save_ccr
;
13379 /* Substitute the return type. */
13380 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13382 if (return_type
== error_mark_node
)
13383 return error_mark_node
;
13384 /* DR 486 clarifies that creation of a function type with an
13385 invalid return type is a deduction failure. */
13386 if (TREE_CODE (return_type
) == ARRAY_TYPE
13387 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13389 if (complain
& tf_error
)
13391 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13392 error ("function returning an array");
13394 error ("function returning a function");
13396 return error_mark_node
;
13399 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13400 return error_mark_node
;
13402 if (!late_return_type_p
)
13404 /* Substitute the argument types. */
13405 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13406 complain
, in_decl
);
13407 if (arg_types
== error_mark_node
)
13408 return error_mark_node
;
13411 /* Construct a new type node and return it. */
13412 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13414 fntype
= build_function_type (return_type
, arg_types
);
13415 fntype
= apply_memfn_quals (fntype
,
13416 type_memfn_quals (t
),
13417 type_memfn_rqual (t
));
13421 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13422 /* Don't pick up extra function qualifiers from the basetype. */
13423 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13424 if (! MAYBE_CLASS_TYPE_P (r
))
13428 Type deduction may fail for any of the following
13431 -- Attempting to create "pointer to member of T" when T
13432 is not a class type. */
13433 if (complain
& tf_error
)
13434 error ("creating pointer to member function of non-class type %qT",
13436 return error_mark_node
;
13439 fntype
= build_method_type_directly (r
, return_type
,
13440 TREE_CHAIN (arg_types
));
13441 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13443 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13445 if (late_return_type_p
)
13446 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13451 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13452 ARGS into that specification, and return the substituted
13453 specification. If there is no specification, return NULL_TREE. */
13456 tsubst_exception_specification (tree fntype
,
13458 tsubst_flags_t complain
,
13465 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13466 new_specs
= NULL_TREE
;
13467 if (specs
&& TREE_PURPOSE (specs
))
13469 /* A noexcept-specifier. */
13470 tree expr
= TREE_PURPOSE (specs
);
13471 if (TREE_CODE (expr
) == INTEGER_CST
)
13475 /* Defer instantiation of noexcept-specifiers to avoid
13476 excessive instantiations (c++/49107). */
13477 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13478 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13480 /* We already partially instantiated this member template,
13481 so combine the new args with the old. */
13482 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13483 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13484 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13485 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13489 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13490 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13494 new_specs
= tsubst_copy_and_build
13495 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13496 /*integral_constant_expression_p=*/true);
13497 new_specs
= build_noexcept_spec (new_specs
, complain
);
13501 if (! TREE_VALUE (specs
))
13508 tree expanded_specs
= NULL_TREE
;
13510 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13512 /* Expand the pack expansion type. */
13513 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13517 if (expanded_specs
== error_mark_node
)
13518 return error_mark_node
;
13519 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13520 len
= TREE_VEC_LENGTH (expanded_specs
);
13523 /* We're substituting into a member template, so
13524 we got a TYPE_PACK_EXPANSION back. Add that
13525 expansion and move on. */
13526 gcc_assert (TREE_CODE (expanded_specs
)
13527 == TYPE_PACK_EXPANSION
);
13528 new_specs
= add_exception_specifier (new_specs
,
13531 specs
= TREE_CHAIN (specs
);
13536 for (i
= 0; i
< len
; ++i
)
13538 if (expanded_specs
)
13539 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13541 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13542 if (spec
== error_mark_node
)
13544 new_specs
= add_exception_specifier (new_specs
, spec
,
13548 specs
= TREE_CHAIN (specs
);
13554 /* Take the tree structure T and replace template parameters used
13555 therein with the argument vector ARGS. IN_DECL is an associated
13556 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13557 Issue error and warning messages under control of COMPLAIN. Note
13558 that we must be relatively non-tolerant of extensions here, in
13559 order to preserve conformance; if we allow substitutions that
13560 should not be allowed, we may allow argument deductions that should
13561 not succeed, and therefore report ambiguous overload situations
13562 where there are none. In theory, we could allow the substitution,
13563 but indicate that it should have failed, and allow our caller to
13564 make sure that the right thing happens, but we don't try to do this
13567 This function is used for dealing with types, decls and the like;
13568 for expressions, use tsubst_expr or tsubst_copy. */
13571 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13573 enum tree_code code
;
13574 tree type
, r
= NULL_TREE
;
13576 if (t
== NULL_TREE
|| t
== error_mark_node
13577 || t
== integer_type_node
13578 || t
== void_type_node
13579 || t
== char_type_node
13580 || t
== unknown_type_node
13581 || TREE_CODE (t
) == NAMESPACE_DECL
13582 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13586 return tsubst_decl (t
, args
, complain
);
13588 if (args
== NULL_TREE
)
13591 code
= TREE_CODE (t
);
13593 if (code
== IDENTIFIER_NODE
)
13594 type
= IDENTIFIER_TYPE_VALUE (t
);
13596 type
= TREE_TYPE (t
);
13598 gcc_assert (type
!= unknown_type_node
);
13600 /* Reuse typedefs. We need to do this to handle dependent attributes,
13601 such as attribute aligned. */
13603 && typedef_variant_p (t
))
13605 tree decl
= TYPE_NAME (t
);
13607 if (alias_template_specialization_p (t
))
13609 /* DECL represents an alias template and we want to
13611 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13612 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13613 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13615 else if (DECL_CLASS_SCOPE_P (decl
)
13616 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13617 && uses_template_parms (DECL_CONTEXT (decl
)))
13619 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13620 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13621 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13623 else if (DECL_FUNCTION_SCOPE_P (decl
)
13624 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13625 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13626 r
= retrieve_local_specialization (decl
);
13628 /* The typedef is from a non-template context. */
13634 r
= cp_build_qualified_type_real
13635 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13636 complain
| tf_ignore_bad_quals
);
13641 /* We don't have an instantiation yet, so drop the typedef. */
13642 int quals
= cp_type_quals (t
);
13643 t
= DECL_ORIGINAL_TYPE (decl
);
13644 t
= cp_build_qualified_type_real (t
, quals
,
13645 complain
| tf_ignore_bad_quals
);
13649 bool fndecl_type
= (complain
& tf_fndecl_type
);
13650 complain
&= ~tf_fndecl_type
;
13653 && code
!= TYPENAME_TYPE
13654 && code
!= TEMPLATE_TYPE_PARM
13655 && code
!= TEMPLATE_PARM_INDEX
13656 && code
!= IDENTIFIER_NODE
13657 && code
!= FUNCTION_TYPE
13658 && code
!= METHOD_TYPE
)
13659 type
= tsubst (type
, args
, complain
, in_decl
);
13660 if (type
== error_mark_node
)
13661 return error_mark_node
;
13667 case ENUMERAL_TYPE
:
13668 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13669 /*entering_scope=*/0);
13672 case IDENTIFIER_NODE
:
13683 if (t
== integer_type_node
)
13686 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13687 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13691 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13693 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13694 /*integral_constant_expression_p=*/false);
13696 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13698 if (TREE_CODE (max
) == NOP_EXPR
13699 && TREE_SIDE_EFFECTS (omax
)
13700 && !TREE_TYPE (max
))
13701 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13703 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13704 with TREE_SIDE_EFFECTS that indicates this is not an integral
13705 constant expression. */
13706 if (processing_template_decl
13707 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13709 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13710 TREE_SIDE_EFFECTS (max
) = 1;
13713 return compute_array_index_type (NULL_TREE
, max
, complain
);
13716 case TEMPLATE_TYPE_PARM
:
13717 case TEMPLATE_TEMPLATE_PARM
:
13718 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13719 case TEMPLATE_PARM_INDEX
:
13724 tree arg
= NULL_TREE
;
13726 /* Early in template argument deduction substitution, we don't
13727 want to reduce the level of 'auto', or it will be confused
13728 with a normal template parm in subsequent deduction. */
13729 if (is_auto (t
) && (complain
& tf_partial
))
13734 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13735 template_parm_level_and_index (t
, &level
, &idx
);
13737 levels
= TMPL_ARGS_DEPTH (args
);
13738 if (level
<= levels
13739 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13741 arg
= TMPL_ARG (args
, level
, idx
);
13743 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13745 /* See through ARGUMENT_PACK_SELECT arguments. */
13746 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13747 /* If the selected argument is an expansion E, that most
13748 likely means we were called from
13749 gen_elem_of_pack_expansion_instantiation during the
13750 substituting of pack an argument pack (which Ith
13751 element is a pack expansion, where I is
13752 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13753 In this case, the Ith element resulting from this
13754 substituting is going to be a pack expansion, which
13755 pattern is the pattern of E. Let's return the
13757 gen_elem_of_pack_expansion_instantiation will
13758 build the resulting pack expansion from it. */
13759 if (PACK_EXPANSION_P (arg
))
13761 /* Make sure we aren't throwing away arg info. */
13762 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13763 arg
= PACK_EXPANSION_PATTERN (arg
);
13768 if (arg
== error_mark_node
)
13769 return error_mark_node
;
13770 else if (arg
!= NULL_TREE
)
13772 if (ARGUMENT_PACK_P (arg
))
13773 /* If ARG is an argument pack, we don't actually want to
13774 perform a substitution here, because substitutions
13775 for argument packs are only done
13776 element-by-element. We can get to this point when
13777 substituting the type of a non-type template
13778 parameter pack, when that type actually contains
13779 template parameter packs from an outer template, e.g.,
13781 template<typename... Types> struct A {
13782 template<Types... Values> struct B { };
13786 if (code
== TEMPLATE_TYPE_PARM
)
13789 gcc_assert (TYPE_P (arg
));
13791 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13793 return cp_build_qualified_type_real
13794 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13796 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13798 /* We are processing a type constructed from a
13799 template template parameter. */
13800 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13801 args
, complain
, in_decl
);
13802 if (argvec
== error_mark_node
)
13803 return error_mark_node
;
13805 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13806 || TREE_CODE (arg
) == TEMPLATE_DECL
13807 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13809 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13810 /* Consider this code:
13812 template <template <class> class Template>
13814 template <class Arg> using Bind = Template<Arg>;
13817 template <template <class> class Template, class Arg>
13818 using Instantiate = Template<Arg>; //#0
13820 template <template <class> class Template,
13823 Instantiate<Internal<Template>::template Bind,
13826 When #1 is parsed, the
13827 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13828 parameter `Template' in #0 matches the
13829 UNBOUND_CLASS_TEMPLATE representing the argument
13830 `Internal<Template>::template Bind'; We then want
13831 to assemble the type `Bind<Argument>' that can't
13832 be fully created right now, because
13833 `Internal<Template>' not being complete, the Bind
13834 template cannot be looked up in that context. So
13835 we need to "store" `Bind<Argument>' for later
13836 when the context of Bind becomes complete. Let's
13837 store that in a TYPENAME_TYPE. */
13838 return make_typename_type (TYPE_CONTEXT (arg
),
13839 build_nt (TEMPLATE_ID_EXPR
,
13840 TYPE_IDENTIFIER (arg
),
13845 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13846 are resolving nested-types in the signature of a
13847 member function templates. Otherwise ARG is a
13848 TEMPLATE_DECL and is the real template to be
13850 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13851 arg
= TYPE_NAME (arg
);
13853 r
= lookup_template_class (arg
,
13855 DECL_CONTEXT (arg
),
13856 /*entering_scope=*/0,
13858 return cp_build_qualified_type_real
13859 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13861 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13864 /* TEMPLATE_PARM_INDEX. */
13865 return convert_from_reference (unshare_expr (arg
));
13869 /* This can happen during the attempted tsubst'ing in
13870 unify. This means that we don't yet have any information
13871 about the template parameter in question. */
13874 /* If we get here, we must have been looking at a parm for a
13875 more deeply nested template. Make a new version of this
13876 template parameter, but with a lower level. */
13879 case TEMPLATE_TYPE_PARM
:
13880 case TEMPLATE_TEMPLATE_PARM
:
13881 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13882 if (cp_type_quals (t
))
13884 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13885 r
= cp_build_qualified_type_real
13886 (r
, cp_type_quals (t
),
13887 complain
| (code
== TEMPLATE_TYPE_PARM
13888 ? tf_ignore_bad_quals
: 0));
13890 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13891 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13892 && (r
= (TEMPLATE_PARM_DESCENDANTS
13893 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13894 && (r
= TREE_TYPE (r
))
13895 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13896 /* Break infinite recursion when substituting the constraints
13897 of a constrained placeholder. */;
13901 TEMPLATE_TYPE_PARM_INDEX (r
)
13902 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13903 r
, levels
, args
, complain
);
13904 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13905 TYPE_MAIN_VARIANT (r
) = r
;
13906 TYPE_POINTER_TO (r
) = NULL_TREE
;
13907 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13909 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13911 /* Propagate constraints on placeholders. */
13912 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13913 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13914 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13915 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13917 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13918 pl
= tsubst (pl
, args
, complain
, in_decl
);
13919 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13923 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13924 /* We have reduced the level of the template
13925 template parameter, but not the levels of its
13926 template parameters, so canonical_type_parameter
13927 will not be able to find the canonical template
13928 template parameter for this level. Thus, we
13929 require structural equality checking to compare
13930 TEMPLATE_TEMPLATE_PARMs. */
13931 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13932 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13933 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13935 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13937 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13939 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13940 /* We might need to substitute into the types of non-type
13941 template parameters. */
13942 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13943 complain
, in_decl
);
13944 if (tmpl
== error_mark_node
)
13945 return error_mark_node
;
13946 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13947 complain
, in_decl
);
13948 if (argvec
== error_mark_node
)
13949 return error_mark_node
;
13951 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13952 = build_template_info (tmpl
, argvec
);
13957 case TEMPLATE_PARM_INDEX
:
13958 /* OK, now substitute the type of the non-type parameter. We
13959 couldn't do it earlier because it might be an auto parameter,
13960 and we wouldn't need to if we had an argument. */
13961 type
= tsubst (type
, args
, complain
, in_decl
);
13962 if (type
== error_mark_node
)
13963 return error_mark_node
;
13964 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13968 gcc_unreachable ();
13976 tree purpose
, value
, chain
;
13978 if (t
== void_list_node
)
13981 purpose
= TREE_PURPOSE (t
);
13984 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13985 if (purpose
== error_mark_node
)
13986 return error_mark_node
;
13988 value
= TREE_VALUE (t
);
13991 value
= tsubst (value
, args
, complain
, in_decl
);
13992 if (value
== error_mark_node
)
13993 return error_mark_node
;
13995 chain
= TREE_CHAIN (t
);
13996 if (chain
&& chain
!= void_type_node
)
13998 chain
= tsubst (chain
, args
, complain
, in_decl
);
13999 if (chain
== error_mark_node
)
14000 return error_mark_node
;
14002 if (purpose
== TREE_PURPOSE (t
)
14003 && value
== TREE_VALUE (t
)
14004 && chain
== TREE_CHAIN (t
))
14006 return hash_tree_cons (purpose
, value
, chain
);
14010 /* We should never be tsubsting a binfo. */
14011 gcc_unreachable ();
14014 /* A vector of template arguments. */
14015 gcc_assert (!type
);
14016 return tsubst_template_args (t
, args
, complain
, in_decl
);
14019 case REFERENCE_TYPE
:
14021 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14026 Type deduction may fail for any of the following
14029 -- Attempting to create a pointer to reference type.
14030 -- Attempting to create a reference to a reference type or
14031 a reference to void.
14033 Core issue 106 says that creating a reference to a reference
14034 during instantiation is no longer a cause for failure. We
14035 only enforce this check in strict C++98 mode. */
14036 if ((TREE_CODE (type
) == REFERENCE_TYPE
14037 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14038 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14040 static location_t last_loc
;
14042 /* We keep track of the last time we issued this error
14043 message to avoid spewing a ton of messages during a
14044 single bad template instantiation. */
14045 if (complain
& tf_error
14046 && last_loc
!= input_location
)
14048 if (VOID_TYPE_P (type
))
14049 error ("forming reference to void");
14050 else if (code
== POINTER_TYPE
)
14051 error ("forming pointer to reference type %qT", type
);
14053 error ("forming reference to reference type %qT", type
);
14054 last_loc
= input_location
;
14057 return error_mark_node
;
14059 else if (TREE_CODE (type
) == FUNCTION_TYPE
14060 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14061 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14063 if (complain
& tf_error
)
14065 if (code
== POINTER_TYPE
)
14066 error ("forming pointer to qualified function type %qT",
14069 error ("forming reference to qualified function type %qT",
14072 return error_mark_node
;
14074 else if (code
== POINTER_TYPE
)
14076 r
= build_pointer_type (type
);
14077 if (TREE_CODE (type
) == METHOD_TYPE
)
14078 r
= build_ptrmemfunc_type (r
);
14080 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14081 /* In C++0x, during template argument substitution, when there is an
14082 attempt to create a reference to a reference type, reference
14083 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14085 "If a template-argument for a template-parameter T names a type
14086 that is a reference to a type A, an attempt to create the type
14087 'lvalue reference to cv T' creates the type 'lvalue reference to
14088 A,' while an attempt to create the type type rvalue reference to
14089 cv T' creates the type T"
14091 r
= cp_build_reference_type
14093 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14095 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14096 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14098 if (r
!= error_mark_node
)
14099 /* Will this ever be needed for TYPE_..._TO values? */
14106 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14107 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14111 Type deduction may fail for any of the following
14114 -- Attempting to create "pointer to member of T" when T
14115 is not a class type. */
14116 if (complain
& tf_error
)
14117 error ("creating pointer to member of non-class type %qT", r
);
14118 return error_mark_node
;
14120 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14122 if (complain
& tf_error
)
14123 error ("creating pointer to member reference type %qT", type
);
14124 return error_mark_node
;
14126 if (VOID_TYPE_P (type
))
14128 if (complain
& tf_error
)
14129 error ("creating pointer to member of type void");
14130 return error_mark_node
;
14132 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14133 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14135 /* The type of the implicit object parameter gets its
14136 cv-qualifiers from the FUNCTION_TYPE. */
14139 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14140 type_memfn_rqual (type
));
14141 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14142 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14146 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14150 case FUNCTION_TYPE
:
14155 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14156 if (fntype
== error_mark_node
)
14157 return error_mark_node
;
14159 /* Substitute the exception specification. */
14160 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14161 /*defer_ok*/fndecl_type
);
14162 if (specs
== error_mark_node
)
14163 return error_mark_node
;
14165 fntype
= build_exception_variant (fntype
, specs
);
14170 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14171 if (domain
== error_mark_node
)
14172 return error_mark_node
;
14174 /* As an optimization, we avoid regenerating the array type if
14175 it will obviously be the same as T. */
14176 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14179 /* These checks should match the ones in create_array_type_for_decl.
14183 The deduction may fail for any of the following reasons:
14185 -- Attempting to create an array with an element type that
14186 is void, a function type, or a reference type, or [DR337]
14187 an abstract class type. */
14188 if (VOID_TYPE_P (type
)
14189 || TREE_CODE (type
) == FUNCTION_TYPE
14190 || (TREE_CODE (type
) == ARRAY_TYPE
14191 && TYPE_DOMAIN (type
) == NULL_TREE
)
14192 || TREE_CODE (type
) == REFERENCE_TYPE
)
14194 if (complain
& tf_error
)
14195 error ("creating array of %qT", type
);
14196 return error_mark_node
;
14199 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14200 return error_mark_node
;
14202 r
= build_cplus_array_type (type
, domain
);
14204 if (TYPE_USER_ALIGN (t
))
14206 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14207 TYPE_USER_ALIGN (r
) = 1;
14213 case TYPENAME_TYPE
:
14215 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14216 in_decl
, /*entering_scope=*/1);
14217 if (ctx
== error_mark_node
)
14218 return error_mark_node
;
14220 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14221 complain
, in_decl
);
14222 if (f
== error_mark_node
)
14223 return error_mark_node
;
14225 if (!MAYBE_CLASS_TYPE_P (ctx
))
14227 if (complain
& tf_error
)
14228 error ("%qT is not a class, struct, or union type", ctx
);
14229 return error_mark_node
;
14231 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14233 /* Normally, make_typename_type does not require that the CTX
14234 have complete type in order to allow things like:
14236 template <class T> struct S { typename S<T>::X Y; };
14238 But, such constructs have already been resolved by this
14239 point, so here CTX really should have complete type, unless
14240 it's a partial instantiation. */
14241 ctx
= complete_type (ctx
);
14242 if (!COMPLETE_TYPE_P (ctx
))
14244 if (complain
& tf_error
)
14245 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14246 return error_mark_node
;
14250 f
= make_typename_type (ctx
, f
, typename_type
,
14251 complain
| tf_keep_type_decl
);
14252 if (f
== error_mark_node
)
14254 if (TREE_CODE (f
) == TYPE_DECL
)
14256 complain
|= tf_ignore_bad_quals
;
14260 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14262 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14264 if (complain
& tf_error
)
14265 error ("%qT resolves to %qT, which is not an enumeration type",
14268 return error_mark_node
;
14270 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14272 if (complain
& tf_error
)
14273 error ("%qT resolves to %qT, which is is not a class type",
14276 return error_mark_node
;
14280 return cp_build_qualified_type_real
14281 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14284 case UNBOUND_CLASS_TEMPLATE
:
14286 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14287 in_decl
, /*entering_scope=*/1);
14288 tree name
= TYPE_IDENTIFIER (t
);
14289 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14291 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14292 return error_mark_node
;
14295 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14296 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14303 ++cp_unevaluated_operand
;
14304 ++c_inhibit_evaluation_warnings
;
14306 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14308 /*integral_constant_expression_p=*/false);
14310 --cp_unevaluated_operand
;
14311 --c_inhibit_evaluation_warnings
;
14313 type
= finish_typeof (type
);
14314 return cp_build_qualified_type_real (type
,
14316 | cp_type_quals (type
),
14320 case DECLTYPE_TYPE
:
14324 ++cp_unevaluated_operand
;
14325 ++c_inhibit_evaluation_warnings
;
14327 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14328 complain
|tf_decltype
, in_decl
,
14329 /*function_p*/false,
14330 /*integral_constant_expression*/false);
14332 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14334 if (type
== NULL_TREE
)
14336 if (complain
& tf_error
)
14337 error ("empty initializer in lambda init-capture");
14338 type
= error_mark_node
;
14340 else if (TREE_CODE (type
) == TREE_LIST
)
14341 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14344 --cp_unevaluated_operand
;
14345 --c_inhibit_evaluation_warnings
;
14347 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14348 type
= lambda_capture_field_type (type
,
14349 DECLTYPE_FOR_INIT_CAPTURE (t
),
14350 DECLTYPE_FOR_REF_CAPTURE (t
));
14351 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14352 type
= lambda_proxy_type (type
);
14355 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14356 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14358 /* In a template ~id could be either a complement expression
14359 or an unqualified-id naming a destructor; if instantiating
14360 it produces an expression, it's not an id-expression or
14363 type
= finish_decltype_type (type
, id
, complain
);
14365 return cp_build_qualified_type_real (type
,
14367 | cp_type_quals (type
),
14368 complain
| tf_ignore_bad_quals
);
14371 case UNDERLYING_TYPE
:
14373 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14374 complain
, in_decl
);
14375 return finish_underlying_type (type
);
14378 case TYPE_ARGUMENT_PACK
:
14379 case NONTYPE_ARGUMENT_PACK
:
14383 if (code
== NONTYPE_ARGUMENT_PACK
)
14384 r
= make_node (code
);
14386 r
= cxx_make_type (code
);
14388 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14389 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14390 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14408 /* We should use one of the expression tsubsts for these codes. */
14409 gcc_unreachable ();
14412 sorry ("use of %qs in template", get_tree_code_name (code
));
14413 return error_mark_node
;
14417 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14418 expression on the left-hand side of the "." or "->" operator. We
14419 only do the lookup if we had a dependent BASELINK. Otherwise we
14420 adjust it onto the instantiated heirarchy. */
14423 tsubst_baselink (tree baselink
, tree object_type
,
14424 tree args
, tsubst_flags_t complain
, tree in_decl
)
14426 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14427 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14428 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14430 tree optype
= BASELINK_OPTYPE (baselink
);
14431 optype
= tsubst (optype
, args
, complain
, in_decl
);
14433 tree template_args
= NULL_TREE
;
14434 bool template_id_p
= false;
14435 tree fns
= BASELINK_FUNCTIONS (baselink
);
14436 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14438 template_id_p
= true;
14439 template_args
= TREE_OPERAND (fns
, 1);
14440 fns
= TREE_OPERAND (fns
, 0);
14442 template_args
= tsubst_template_args (template_args
, args
,
14443 complain
, in_decl
);
14446 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14447 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14448 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14452 tree name
= OVL_NAME (fns
);
14453 if (IDENTIFIER_CONV_OP_P (name
))
14454 name
= make_conv_op_name (optype
);
14456 if (name
== complete_dtor_identifier
)
14457 /* Treat as-if non-dependent below. */
14458 dependent_p
= false;
14460 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14463 if ((complain
& tf_error
)
14464 && constructor_name_p (name
, qualifying_scope
))
14465 error ("cannot call constructor %<%T::%D%> directly",
14466 qualifying_scope
, name
);
14467 return error_mark_node
;
14470 if (BASELINK_P (baselink
))
14471 fns
= BASELINK_FUNCTIONS (baselink
);
14474 /* We're going to overwrite pieces below, make a duplicate. */
14475 baselink
= copy_node (baselink
);
14477 /* If lookup found a single function, mark it as used at this point.
14478 (If lookup found multiple functions the one selected later by
14479 overload resolution will be marked as used at that point.) */
14480 if (!template_id_p
&& !really_overloaded_fn (fns
)
14481 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14482 return error_mark_node
;
14484 if (BASELINK_P (baselink
))
14486 /* Add back the template arguments, if present. */
14488 BASELINK_FUNCTIONS (baselink
)
14489 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14491 /* Update the conversion operator type. */
14492 BASELINK_OPTYPE (baselink
) = optype
;
14496 object_type
= current_class_type
;
14498 if (qualified_p
|| !dependent_p
)
14500 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14504 /* We need to call adjust_result_of_qualified_name_lookup in case the
14505 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14506 so that we still get virtual function binding. */
14507 BASELINK_QUALIFIED_P (baselink
) = false;
14513 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14514 true if the qualified-id will be a postfix-expression in-and-of
14515 itself; false if more of the postfix-expression follows the
14516 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14520 tsubst_qualified_id (tree qualified_id
, tree args
,
14521 tsubst_flags_t complain
, tree in_decl
,
14522 bool done
, bool address_p
)
14528 tree template_args
;
14529 location_t loc
= UNKNOWN_LOCATION
;
14531 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14533 /* Figure out what name to look up. */
14534 name
= TREE_OPERAND (qualified_id
, 1);
14535 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14537 is_template
= true;
14538 loc
= EXPR_LOCATION (name
);
14539 template_args
= TREE_OPERAND (name
, 1);
14541 template_args
= tsubst_template_args (template_args
, args
,
14542 complain
, in_decl
);
14543 if (template_args
== error_mark_node
)
14544 return error_mark_node
;
14545 name
= TREE_OPERAND (name
, 0);
14549 is_template
= false;
14550 template_args
= NULL_TREE
;
14553 /* Substitute into the qualifying scope. When there are no ARGS, we
14554 are just trying to simplify a non-dependent expression. In that
14555 case the qualifying scope may be dependent, and, in any case,
14556 substituting will not help. */
14557 scope
= TREE_OPERAND (qualified_id
, 0);
14560 scope
= tsubst (scope
, args
, complain
, in_decl
);
14561 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14566 if (dependent_scope_p (scope
))
14569 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14570 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14571 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14572 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14576 if (!BASELINK_P (name
) && !DECL_P (expr
))
14578 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14580 /* A BIT_NOT_EXPR is used to represent a destructor. */
14581 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14583 error ("qualifying type %qT does not match destructor name ~%qT",
14584 scope
, TREE_OPERAND (expr
, 0));
14585 expr
= error_mark_node
;
14588 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14589 /*is_type_p=*/0, false);
14592 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14593 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14594 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14596 if (complain
& tf_error
)
14598 error ("dependent-name %qE is parsed as a non-type, but "
14599 "instantiation yields a type", qualified_id
);
14600 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14602 return error_mark_node
;
14608 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14610 /* Remember that there was a reference to this entity. */
14611 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14612 return error_mark_node
;
14615 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14617 if (complain
& tf_error
)
14618 qualified_name_lookup_error (scope
,
14619 TREE_OPERAND (qualified_id
, 1),
14620 expr
, input_location
);
14621 return error_mark_node
;
14626 if (variable_template_p (expr
))
14627 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14630 expr
= lookup_template_function (expr
, template_args
);
14633 if (expr
== error_mark_node
&& complain
& tf_error
)
14634 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14635 expr
, input_location
);
14636 else if (TYPE_P (scope
))
14638 expr
= (adjust_result_of_qualified_name_lookup
14639 (expr
, scope
, current_nonlambda_class_type ()));
14640 expr
= (finish_qualified_id_expr
14641 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14642 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14643 /*template_arg_p=*/false, complain
));
14646 /* Expressions do not generally have reference type. */
14647 if (TREE_CODE (expr
) != SCOPE_REF
14648 /* However, if we're about to form a pointer-to-member, we just
14649 want the referenced member referenced. */
14650 && TREE_CODE (expr
) != OFFSET_REF
)
14651 expr
= convert_from_reference (expr
);
14653 if (REF_PARENTHESIZED_P (qualified_id
))
14654 expr
= force_paren_expr (expr
);
14659 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14660 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14664 tsubst_init (tree init
, tree decl
, tree args
,
14665 tsubst_flags_t complain
, tree in_decl
)
14670 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14672 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14674 /* If we had an initializer but it
14675 instantiated to nothing,
14676 value-initialize the object. This will
14677 only occur when the initializer was a
14678 pack expansion where the parameter packs
14679 used in that expansion were of length
14681 init
= build_value_init (TREE_TYPE (decl
),
14683 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14684 init
= get_target_expr_sfinae (init
, complain
);
14685 if (TREE_CODE (init
) == TARGET_EXPR
)
14686 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14692 /* Like tsubst, but deals with expressions. This function just replaces
14693 template parms; to finish processing the resultant expression, use
14694 tsubst_copy_and_build or tsubst_expr. */
14697 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14699 enum tree_code code
;
14702 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14705 code
= TREE_CODE (t
);
14710 r
= retrieve_local_specialization (t
);
14712 if (r
== NULL_TREE
)
14714 /* We get here for a use of 'this' in an NSDMI. */
14715 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14716 return current_class_ptr
;
14718 /* This can happen for a parameter name used later in a function
14719 declaration (such as in a late-specified return type). Just
14720 make a dummy decl, since it's only used for its type. */
14721 gcc_assert (cp_unevaluated_operand
!= 0);
14722 r
= tsubst_decl (t
, args
, complain
);
14723 /* Give it the template pattern as its context; its true context
14724 hasn't been instantiated yet and this is good enough for
14726 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14729 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14730 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14731 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14732 return error_mark_node
;
14740 if (DECL_TEMPLATE_PARM_P (t
))
14741 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14742 /* There is no need to substitute into namespace-scope
14744 if (DECL_NAMESPACE_SCOPE_P (t
))
14746 /* If ARGS is NULL, then T is known to be non-dependent. */
14747 if (args
== NULL_TREE
)
14748 return scalar_constant_value (t
);
14750 /* Unfortunately, we cannot just call lookup_name here.
14753 template <int I> int f() {
14755 struct S { void g() { E e = a; } };
14758 When we instantiate f<7>::S::g(), say, lookup_name is not
14759 clever enough to find f<7>::a. */
14761 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14762 /*entering_scope=*/0);
14764 for (v
= TYPE_VALUES (enum_type
);
14766 v
= TREE_CHAIN (v
))
14767 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14768 return TREE_VALUE (v
);
14770 /* We didn't find the name. That should never happen; if
14771 name-lookup found it during preliminary parsing, we
14772 should find it again here during instantiation. */
14773 gcc_unreachable ();
14778 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14780 /* Check for a local specialization set up by
14781 tsubst_pack_expansion. */
14782 if (tree r
= retrieve_local_specialization (t
))
14784 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14785 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14789 /* When retrieving a capture pack from a generic lambda, remove the
14790 lambda call op's own template argument list from ARGS. Only the
14791 template arguments active for the closure type should be used to
14792 retrieve the pack specialization. */
14793 if (LAMBDA_FUNCTION_P (current_function_decl
)
14794 && (template_class_depth (DECL_CONTEXT (t
))
14795 != TMPL_ARGS_DEPTH (args
)))
14796 args
= strip_innermost_template_args (args
, 1);
14798 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14799 tsubst_decl put in the hash table. */
14800 return retrieve_specialization (t
, args
, 0);
14803 if (DECL_CONTEXT (t
))
14807 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14808 /*entering_scope=*/1);
14809 if (ctx
!= DECL_CONTEXT (t
))
14811 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14814 if (complain
& tf_error
)
14815 error ("using invalid field %qD", t
);
14816 return error_mark_node
;
14825 case FUNCTION_DECL
:
14826 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14827 r
= tsubst (t
, args
, complain
, in_decl
);
14828 else if (local_variable_p (t
)
14829 && uses_template_parms (DECL_CONTEXT (t
)))
14831 r
= retrieve_local_specialization (t
);
14832 if (r
== NULL_TREE
)
14834 /* First try name lookup to find the instantiation. */
14835 r
= lookup_name (DECL_NAME (t
));
14836 if (r
&& !is_capture_proxy (r
))
14838 /* Make sure that the one we found is the one we want. */
14839 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
14840 if (ctx
!= DECL_CONTEXT (r
))
14848 /* This can happen for a variable used in a
14849 late-specified return type of a local lambda, or for a
14850 local static or constant. Building a new VAR_DECL
14851 should be OK in all those cases. */
14852 r
= tsubst_decl (t
, args
, complain
);
14853 if (local_specializations
)
14854 /* Avoid infinite recursion (79640). */
14855 register_local_specialization (r
, t
);
14856 if (decl_maybe_constant_var_p (r
))
14858 /* We can't call cp_finish_decl, so handle the
14859 initializer by hand. */
14860 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14861 complain
, in_decl
);
14862 if (!processing_template_decl
)
14863 init
= maybe_constant_init (init
);
14864 if (processing_template_decl
14865 ? potential_constant_expression (init
)
14866 : reduced_constant_expression_p (init
))
14867 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14868 = TREE_CONSTANT (r
) = true;
14869 DECL_INITIAL (r
) = init
;
14870 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
14872 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
14873 complain
, adc_variable_type
);
14875 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14876 || decl_constant_var_p (r
)
14877 || errorcount
|| sorrycount
);
14878 if (!processing_template_decl
14879 && !TREE_STATIC (r
))
14880 r
= process_outer_var_ref (r
, complain
);
14882 /* Remember this for subsequent uses. */
14883 if (local_specializations
)
14884 register_local_specialization (r
, t
);
14889 if (!mark_used (r
, complain
))
14890 return error_mark_node
;
14893 case NAMESPACE_DECL
:
14897 /* An OVERLOAD will always be a non-dependent overload set; an
14898 overload set from function scope will just be represented with an
14899 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14900 gcc_assert (!uses_template_parms (t
));
14901 /* We must have marked any lookups as persistent. */
14902 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14906 return tsubst_baselink (t
, current_nonlambda_class_type (),
14907 args
, complain
, in_decl
);
14909 case TEMPLATE_DECL
:
14910 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14911 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14912 args
, complain
, in_decl
);
14913 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14914 return tsubst (t
, args
, complain
, in_decl
);
14915 else if (DECL_CLASS_SCOPE_P (t
)
14916 && uses_template_parms (DECL_CONTEXT (t
)))
14918 /* Template template argument like the following example need
14921 template <template <class> class TT> struct C {};
14922 template <class T> struct D {
14923 template <class U> struct E {};
14928 We are processing the template argument `E' in #1 for
14929 the template instantiation #2. Originally, `E' is a
14930 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14931 have to substitute this with one having context `D<int>'. */
14933 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14934 if (dependent_scope_p (context
))
14936 /* When rewriting a constructor into a deduction guide, a
14937 non-dependent name can become dependent, so memtmpl<args>
14938 becomes context::template memtmpl<args>. */
14939 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14940 return build_qualified_name (type
, context
, DECL_NAME (t
),
14943 return lookup_field (context
, DECL_NAME(t
), 0, false);
14946 /* Ordinary template template argument. */
14949 case NON_LVALUE_EXPR
:
14950 case VIEW_CONVERT_EXPR
:
14952 /* Handle location wrappers by substituting the wrapped node
14953 first, *then* reusing the resulting type. Doing the type
14954 first ensures that we handle template parameters and
14955 parameter pack expansions. */
14956 gcc_assert (location_wrapper_p (t
));
14957 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14958 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
14962 case REINTERPRET_CAST_EXPR
:
14963 case CONST_CAST_EXPR
:
14964 case STATIC_CAST_EXPR
:
14965 case DYNAMIC_CAST_EXPR
:
14966 case IMPLICIT_CONV_EXPR
:
14970 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14971 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14972 return build1 (code
, type
, op0
);
14976 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14977 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14979 tree expanded
, op
= TREE_OPERAND (t
, 0);
14982 if (SIZEOF_EXPR_TYPE_P (t
))
14983 op
= TREE_TYPE (op
);
14985 ++cp_unevaluated_operand
;
14986 ++c_inhibit_evaluation_warnings
;
14987 /* We only want to compute the number of arguments. */
14988 if (PACK_EXPANSION_P (op
))
14989 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14991 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14992 args
, complain
, in_decl
);
14993 --cp_unevaluated_operand
;
14994 --c_inhibit_evaluation_warnings
;
14996 if (TREE_CODE (expanded
) == TREE_VEC
)
14998 len
= TREE_VEC_LENGTH (expanded
);
14999 /* Set TREE_USED for the benefit of -Wunused. */
15000 for (int i
= 0; i
< len
; i
++)
15001 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15002 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15005 if (expanded
== error_mark_node
)
15006 return error_mark_node
;
15007 else if (PACK_EXPANSION_P (expanded
)
15008 || (TREE_CODE (expanded
) == TREE_VEC
15009 && pack_expansion_args_count (expanded
)))
15012 if (PACK_EXPANSION_P (expanded
))
15014 else if (TREE_VEC_LENGTH (expanded
) == 1)
15015 expanded
= TREE_VEC_ELT (expanded
, 0);
15017 expanded
= make_argument_pack (expanded
);
15019 if (TYPE_P (expanded
))
15020 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15021 complain
& tf_error
);
15023 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15024 complain
& tf_error
);
15027 return build_int_cst (size_type_node
, len
);
15029 if (SIZEOF_EXPR_TYPE_P (t
))
15031 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15032 args
, complain
, in_decl
);
15033 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15034 r
= build1 (SIZEOF_EXPR
,
15035 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15036 SIZEOF_EXPR_TYPE_P (r
) = 1;
15043 case TRUTH_NOT_EXPR
:
15046 case UNARY_PLUS_EXPR
: /* Unary + */
15048 case AT_ENCODE_EXPR
:
15052 case REALPART_EXPR
:
15053 case IMAGPART_EXPR
:
15056 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15057 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15058 return build1 (code
, type
, op0
);
15061 case COMPONENT_REF
:
15066 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15067 name
= TREE_OPERAND (t
, 1);
15068 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15070 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15071 complain
, in_decl
);
15072 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15074 else if (TREE_CODE (name
) == SCOPE_REF
15075 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15077 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15078 complain
, in_decl
);
15079 name
= TREE_OPERAND (name
, 1);
15080 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15081 complain
, in_decl
);
15082 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15083 name
= build_qualified_name (/*type=*/NULL_TREE
,
15085 /*template_p=*/false);
15087 else if (BASELINK_P (name
))
15088 name
= tsubst_baselink (name
,
15089 non_reference (TREE_TYPE (object
)),
15093 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15094 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15100 case TRUNC_DIV_EXPR
:
15101 case CEIL_DIV_EXPR
:
15102 case FLOOR_DIV_EXPR
:
15103 case ROUND_DIV_EXPR
:
15104 case EXACT_DIV_EXPR
:
15108 case TRUNC_MOD_EXPR
:
15109 case FLOOR_MOD_EXPR
:
15110 case TRUTH_ANDIF_EXPR
:
15111 case TRUTH_ORIF_EXPR
:
15112 case TRUTH_AND_EXPR
:
15113 case TRUTH_OR_EXPR
:
15126 case COMPOUND_EXPR
:
15129 case PREDECREMENT_EXPR
:
15130 case PREINCREMENT_EXPR
:
15131 case POSTDECREMENT_EXPR
:
15132 case POSTINCREMENT_EXPR
:
15134 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15135 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15136 return build_nt (code
, op0
, op1
);
15141 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15142 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15143 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15144 QUALIFIED_NAME_IS_TEMPLATE (t
));
15149 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15150 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15151 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15156 int n
= VL_EXP_OPERAND_LENGTH (t
);
15157 tree result
= build_vl_exp (CALL_EXPR
, n
);
15159 for (i
= 0; i
< n
; i
++)
15160 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15161 complain
, in_decl
);
15167 case PSEUDO_DTOR_EXPR
:
15168 case VEC_PERM_EXPR
:
15170 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15171 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15172 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15173 r
= build_nt (code
, op0
, op1
, op2
);
15174 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15180 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15181 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15182 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15183 r
= build_nt (code
, op0
, op1
, op2
);
15184 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15190 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15191 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15192 r
= build_nt (code
, op0
, op1
);
15193 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15194 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15198 case TEMPLATE_ID_EXPR
:
15200 /* Substituted template arguments */
15201 tree fn
= TREE_OPERAND (t
, 0);
15202 tree targs
= TREE_OPERAND (t
, 1);
15204 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15206 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15208 return lookup_template_function (fn
, targs
);
15213 tree purpose
, value
, chain
;
15215 if (t
== void_list_node
)
15218 purpose
= TREE_PURPOSE (t
);
15220 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15221 value
= TREE_VALUE (t
);
15223 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15224 chain
= TREE_CHAIN (t
);
15225 if (chain
&& chain
!= void_type_node
)
15226 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15227 if (purpose
== TREE_PURPOSE (t
)
15228 && value
== TREE_VALUE (t
)
15229 && chain
== TREE_CHAIN (t
))
15231 return tree_cons (purpose
, value
, chain
);
15236 case ENUMERAL_TYPE
:
15238 case TEMPLATE_TYPE_PARM
:
15239 case TEMPLATE_TEMPLATE_PARM
:
15240 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15241 case TEMPLATE_PARM_INDEX
:
15243 case REFERENCE_TYPE
:
15245 case FUNCTION_TYPE
:
15248 case TYPENAME_TYPE
:
15249 case UNBOUND_CLASS_TEMPLATE
:
15251 case DECLTYPE_TYPE
:
15253 return tsubst (t
, args
, complain
, in_decl
);
15257 /* Fall through. */
15258 case IDENTIFIER_NODE
:
15259 if (IDENTIFIER_CONV_OP_P (t
))
15261 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15262 return make_conv_op_name (new_type
);
15268 /* This is handled by tsubst_copy_and_build. */
15269 gcc_unreachable ();
15273 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15274 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15275 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15278 case CLEANUP_POINT_EXPR
:
15279 /* We shouldn't have built any of these during initial template
15280 generation. Instead, they should be built during instantiation
15281 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15282 gcc_unreachable ();
15286 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15287 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15288 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15289 r
= build2 (code
, type
, op0
, op1
);
15290 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15291 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15292 && !(complain
& tf_error
))
15293 return error_mark_node
;
15297 case EXPR_PACK_EXPANSION
:
15298 error ("invalid use of pack expansion expression");
15299 return error_mark_node
;
15301 case NONTYPE_ARGUMENT_PACK
:
15302 error ("use %<...%> to expand argument pack");
15303 return error_mark_node
;
15306 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15314 /* Instantiate any typedefs in the type. */
15315 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15316 r
= fold_convert (type
, t
);
15317 gcc_assert (TREE_CODE (r
) == code
);
15322 /* These can sometimes show up in a partial instantiation, but never
15323 involve template parms. */
15324 gcc_assert (!uses_template_parms (t
));
15327 case UNARY_LEFT_FOLD_EXPR
:
15328 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15329 case UNARY_RIGHT_FOLD_EXPR
:
15330 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15331 case BINARY_LEFT_FOLD_EXPR
:
15332 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15333 case BINARY_RIGHT_FOLD_EXPR
:
15334 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15338 case DEBUG_BEGIN_STMT
:
15339 /* ??? There's no point in copying it for now, but maybe some
15340 day it will contain more information, such as a pointer back
15341 to the containing function, inlined copy or so. */
15345 /* We shouldn't get here, but keep going if !flag_checking. */
15347 gcc_unreachable ();
15352 /* Helper function for tsubst_omp_clauses, used for instantiation of
15353 OMP_CLAUSE_DECL of clauses. */
15356 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15359 if (decl
== NULL_TREE
)
15362 /* Handle an OpenMP array section represented as a TREE_LIST (or
15363 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15364 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15365 TREE_LIST. We can handle it exactly the same as an array section
15366 (purpose, value, and a chain), even though the nomenclature
15367 (low_bound, length, etc) is different. */
15368 if (TREE_CODE (decl
) == TREE_LIST
)
15371 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15372 /*integral_constant_expression_p=*/false);
15373 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15374 /*integral_constant_expression_p=*/false);
15375 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15377 if (TREE_PURPOSE (decl
) == low_bound
15378 && TREE_VALUE (decl
) == length
15379 && TREE_CHAIN (decl
) == chain
)
15381 tree ret
= tree_cons (low_bound
, length
, chain
);
15382 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15383 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15386 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15387 /*integral_constant_expression_p=*/false);
15388 /* Undo convert_from_reference tsubst_expr could have called. */
15390 && REFERENCE_REF_P (ret
)
15391 && !REFERENCE_REF_P (decl
))
15392 ret
= TREE_OPERAND (ret
, 0);
15396 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15399 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15400 tree args
, tsubst_flags_t complain
, tree in_decl
)
15402 tree new_clauses
= NULL_TREE
, nc
, oc
;
15403 tree linear_no_step
= NULL_TREE
;
15405 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15407 nc
= copy_node (oc
);
15408 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15411 switch (OMP_CLAUSE_CODE (nc
))
15413 case OMP_CLAUSE_LASTPRIVATE
:
15414 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15416 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15417 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15418 in_decl
, /*integral_constant_expression_p=*/false);
15419 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15420 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15423 case OMP_CLAUSE_PRIVATE
:
15424 case OMP_CLAUSE_SHARED
:
15425 case OMP_CLAUSE_FIRSTPRIVATE
:
15426 case OMP_CLAUSE_COPYIN
:
15427 case OMP_CLAUSE_COPYPRIVATE
:
15428 case OMP_CLAUSE_UNIFORM
:
15429 case OMP_CLAUSE_DEPEND
:
15430 case OMP_CLAUSE_FROM
:
15431 case OMP_CLAUSE_TO
:
15432 case OMP_CLAUSE_MAP
:
15433 case OMP_CLAUSE_USE_DEVICE_PTR
:
15434 case OMP_CLAUSE_IS_DEVICE_PTR
:
15435 OMP_CLAUSE_DECL (nc
)
15436 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15439 case OMP_CLAUSE_TILE
:
15440 case OMP_CLAUSE_IF
:
15441 case OMP_CLAUSE_NUM_THREADS
:
15442 case OMP_CLAUSE_SCHEDULE
:
15443 case OMP_CLAUSE_COLLAPSE
:
15444 case OMP_CLAUSE_FINAL
:
15445 case OMP_CLAUSE_DEVICE
:
15446 case OMP_CLAUSE_DIST_SCHEDULE
:
15447 case OMP_CLAUSE_NUM_TEAMS
:
15448 case OMP_CLAUSE_THREAD_LIMIT
:
15449 case OMP_CLAUSE_SAFELEN
:
15450 case OMP_CLAUSE_SIMDLEN
:
15451 case OMP_CLAUSE_NUM_TASKS
:
15452 case OMP_CLAUSE_GRAINSIZE
:
15453 case OMP_CLAUSE_PRIORITY
:
15454 case OMP_CLAUSE_ORDERED
:
15455 case OMP_CLAUSE_HINT
:
15456 case OMP_CLAUSE_NUM_GANGS
:
15457 case OMP_CLAUSE_NUM_WORKERS
:
15458 case OMP_CLAUSE_VECTOR_LENGTH
:
15459 case OMP_CLAUSE_WORKER
:
15460 case OMP_CLAUSE_VECTOR
:
15461 case OMP_CLAUSE_ASYNC
:
15462 case OMP_CLAUSE_WAIT
:
15463 OMP_CLAUSE_OPERAND (nc
, 0)
15464 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15465 in_decl
, /*integral_constant_expression_p=*/false);
15467 case OMP_CLAUSE_REDUCTION
:
15468 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15470 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15471 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15473 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15474 complain
, in_decl
);
15475 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15476 = build_qualified_name (NULL_TREE
, scope
,
15477 TREE_OPERAND (placeholder
, 1),
15481 gcc_assert (identifier_p (placeholder
));
15483 OMP_CLAUSE_DECL (nc
)
15484 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15487 case OMP_CLAUSE_GANG
:
15488 case OMP_CLAUSE_ALIGNED
:
15489 OMP_CLAUSE_DECL (nc
)
15490 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15492 OMP_CLAUSE_OPERAND (nc
, 1)
15493 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15494 in_decl
, /*integral_constant_expression_p=*/false);
15496 case OMP_CLAUSE_LINEAR
:
15497 OMP_CLAUSE_DECL (nc
)
15498 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15500 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15502 gcc_assert (!linear_no_step
);
15503 linear_no_step
= nc
;
15505 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15506 OMP_CLAUSE_LINEAR_STEP (nc
)
15507 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15508 complain
, in_decl
);
15510 OMP_CLAUSE_LINEAR_STEP (nc
)
15511 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15513 /*integral_constant_expression_p=*/false);
15515 case OMP_CLAUSE_NOWAIT
:
15516 case OMP_CLAUSE_DEFAULT
:
15517 case OMP_CLAUSE_UNTIED
:
15518 case OMP_CLAUSE_MERGEABLE
:
15519 case OMP_CLAUSE_INBRANCH
:
15520 case OMP_CLAUSE_NOTINBRANCH
:
15521 case OMP_CLAUSE_PROC_BIND
:
15522 case OMP_CLAUSE_FOR
:
15523 case OMP_CLAUSE_PARALLEL
:
15524 case OMP_CLAUSE_SECTIONS
:
15525 case OMP_CLAUSE_TASKGROUP
:
15526 case OMP_CLAUSE_NOGROUP
:
15527 case OMP_CLAUSE_THREADS
:
15528 case OMP_CLAUSE_SIMD
:
15529 case OMP_CLAUSE_DEFAULTMAP
:
15530 case OMP_CLAUSE_INDEPENDENT
:
15531 case OMP_CLAUSE_AUTO
:
15532 case OMP_CLAUSE_SEQ
:
15535 gcc_unreachable ();
15537 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15538 switch (OMP_CLAUSE_CODE (nc
))
15540 case OMP_CLAUSE_SHARED
:
15541 case OMP_CLAUSE_PRIVATE
:
15542 case OMP_CLAUSE_FIRSTPRIVATE
:
15543 case OMP_CLAUSE_LASTPRIVATE
:
15544 case OMP_CLAUSE_COPYPRIVATE
:
15545 case OMP_CLAUSE_LINEAR
:
15546 case OMP_CLAUSE_REDUCTION
:
15547 case OMP_CLAUSE_USE_DEVICE_PTR
:
15548 case OMP_CLAUSE_IS_DEVICE_PTR
:
15549 /* tsubst_expr on SCOPE_REF results in returning
15550 finish_non_static_data_member result. Undo that here. */
15551 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15552 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15553 == IDENTIFIER_NODE
))
15555 tree t
= OMP_CLAUSE_DECL (nc
);
15558 switch (TREE_CODE (v
))
15560 case COMPONENT_REF
:
15564 case POINTER_PLUS_EXPR
:
15565 v
= TREE_OPERAND (v
, 0);
15568 if (DECL_CONTEXT (v
) == current_function_decl
15569 && DECL_ARTIFICIAL (v
)
15570 && DECL_NAME (v
) == this_identifier
)
15571 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15578 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15579 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15580 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15581 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15582 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15584 tree decl
= OMP_CLAUSE_DECL (nc
);
15587 retrofit_lang_decl (decl
);
15588 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15597 new_clauses
= nreverse (new_clauses
);
15598 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15600 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15601 if (linear_no_step
)
15602 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15603 if (nc
== linear_no_step
)
15605 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15609 return new_clauses
;
15612 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15615 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15618 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15620 tree purpose
, value
, chain
;
15625 if (TREE_CODE (t
) != TREE_LIST
)
15626 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15627 /*function_p=*/false,
15628 /*integral_constant_expression_p=*/false);
15630 if (t
== void_list_node
)
15633 purpose
= TREE_PURPOSE (t
);
15635 purpose
= RECUR (purpose
);
15636 value
= TREE_VALUE (t
);
15639 if (TREE_CODE (value
) != LABEL_DECL
)
15640 value
= RECUR (value
);
15643 value
= lookup_label (DECL_NAME (value
));
15644 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15645 TREE_USED (value
) = 1;
15648 chain
= TREE_CHAIN (t
);
15649 if (chain
&& chain
!= void_type_node
)
15650 chain
= RECUR (chain
);
15651 return tree_cons (purpose
, value
, chain
);
15655 /* Used to temporarily communicate the list of #pragma omp parallel
15656 clauses to #pragma omp for instantiation if they are combined
15659 static tree
*omp_parallel_combined_clauses
;
15661 /* Substitute one OMP_FOR iterator. */
15664 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15665 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15666 tree args
, tsubst_flags_t complain
, tree in_decl
,
15667 bool integral_constant_expression_p
)
15669 #define RECUR(NODE) \
15670 tsubst_expr ((NODE), args, complain, in_decl, \
15671 integral_constant_expression_p)
15672 tree decl
, init
, cond
, incr
;
15674 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15675 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15677 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15679 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15680 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15683 decl
= TREE_OPERAND (init
, 0);
15684 init
= TREE_OPERAND (init
, 1);
15685 tree decl_expr
= NULL_TREE
;
15686 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15688 /* We need to jump through some hoops to handle declarations in the
15689 init-statement, since we might need to handle auto deduction,
15690 but we need to keep control of initialization. */
15692 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15693 decl
= tsubst_decl (decl
, args
, complain
);
15697 if (TREE_CODE (decl
) == SCOPE_REF
)
15699 decl
= RECUR (decl
);
15700 if (TREE_CODE (decl
) == COMPONENT_REF
)
15704 switch (TREE_CODE (v
))
15706 case COMPONENT_REF
:
15710 case POINTER_PLUS_EXPR
:
15711 v
= TREE_OPERAND (v
, 0);
15714 if (DECL_CONTEXT (v
) == current_function_decl
15715 && DECL_ARTIFICIAL (v
)
15716 && DECL_NAME (v
) == this_identifier
)
15718 decl
= TREE_OPERAND (decl
, 1);
15719 decl
= omp_privatize_field (decl
, false);
15729 decl
= RECUR (decl
);
15731 init
= RECUR (init
);
15733 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15734 if (auto_node
&& init
)
15736 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15738 gcc_assert (!type_dependent_expression_p (decl
));
15740 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15744 /* Declare the variable, but don't let that initialize it. */
15745 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15746 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15748 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15751 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15752 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15753 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15755 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15756 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15757 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15758 NOP_EXPR
, rhs
, complain
);
15761 incr
= RECUR (incr
);
15762 TREE_VEC_ELT (declv
, i
) = decl
;
15763 TREE_VEC_ELT (initv
, i
) = init
;
15764 TREE_VEC_ELT (condv
, i
) = cond
;
15765 TREE_VEC_ELT (incrv
, i
) = incr
;
15771 /* Declare and initialize the variable. */
15779 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15781 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15783 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15784 && OMP_CLAUSE_DECL (*pc
) == decl
)
15786 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15787 && OMP_CLAUSE_DECL (*pc
) == decl
)
15791 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15793 *pc
= OMP_CLAUSE_CHAIN (c
);
15794 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15797 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15798 && OMP_CLAUSE_DECL (*pc
) == decl
)
15800 error ("iteration variable %qD should not be firstprivate",
15802 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15804 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15805 && OMP_CLAUSE_DECL (*pc
) == decl
)
15807 error ("iteration variable %qD should not be reduction",
15809 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15812 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15817 if (*pc
== NULL_TREE
)
15819 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15820 OMP_CLAUSE_DECL (c
) = decl
;
15821 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15824 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15829 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15830 if (COMPARISON_CLASS_P (cond
))
15832 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15833 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15834 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15837 cond
= RECUR (cond
);
15838 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15839 switch (TREE_CODE (incr
))
15841 case PREINCREMENT_EXPR
:
15842 case PREDECREMENT_EXPR
:
15843 case POSTINCREMENT_EXPR
:
15844 case POSTDECREMENT_EXPR
:
15845 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15846 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15849 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15850 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15852 tree rhs
= TREE_OPERAND (incr
, 1);
15853 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15854 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15855 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15856 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15857 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15861 incr
= RECUR (incr
);
15864 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15865 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15867 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15868 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15869 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15870 TREE_TYPE (decl
), lhs
,
15871 RECUR (TREE_OPERAND (incr
, 2))));
15873 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15874 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15875 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15877 tree rhs
= TREE_OPERAND (incr
, 2);
15878 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15879 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15880 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15881 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15882 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15886 incr
= RECUR (incr
);
15889 incr
= RECUR (incr
);
15893 TREE_VEC_ELT (declv
, i
) = decl
;
15894 TREE_VEC_ELT (initv
, i
) = init
;
15895 TREE_VEC_ELT (condv
, i
) = cond
;
15896 TREE_VEC_ELT (incrv
, i
) = incr
;
15900 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15901 of OMP_TARGET's body. */
15904 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15906 *walk_subtrees
= 0;
15907 switch (TREE_CODE (*tp
))
15912 case STATEMENT_LIST
:
15913 *walk_subtrees
= 1;
15921 /* Helper function for tsubst_expr. For decomposition declaration
15922 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15923 also the corresponding decls representing the identifiers
15924 of the decomposition declaration. Return DECL if successful
15925 or error_mark_node otherwise, set *FIRST to the first decl
15926 in the list chained through DECL_CHAIN and *CNT to the number
15930 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15931 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15934 tree decl2
, decl3
, prev
= decl
;
15936 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15937 for (decl2
= DECL_CHAIN (pattern_decl
);
15940 && DECL_DECOMPOSITION_P (decl2
)
15941 && DECL_NAME (decl2
);
15942 decl2
= DECL_CHAIN (decl2
))
15944 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15946 gcc_assert (errorcount
);
15947 return error_mark_node
;
15950 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
15951 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15952 tree v
= DECL_VALUE_EXPR (decl2
);
15953 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15954 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15955 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15956 SET_DECL_VALUE_EXPR (decl2
, v
);
15957 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15959 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15960 maybe_push_decl (decl3
);
15961 if (error_operand_p (decl3
))
15962 decl
= error_mark_node
;
15963 else if (decl
!= error_mark_node
15964 && DECL_CHAIN (decl3
) != prev
)
15966 gcc_assert (errorcount
);
15967 decl
= error_mark_node
;
15976 /* Like tsubst_copy for expressions, etc. but also does semantic
15980 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15981 bool integral_constant_expression_p
)
15983 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15984 #define RECUR(NODE) \
15985 tsubst_expr ((NODE), args, complain, in_decl, \
15986 integral_constant_expression_p)
15992 if (t
== NULL_TREE
|| t
== error_mark_node
)
15995 loc
= input_location
;
15996 if (EXPR_HAS_LOCATION (t
))
15997 input_location
= EXPR_LOCATION (t
);
15998 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15999 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16001 switch (TREE_CODE (t
))
16003 case STATEMENT_LIST
:
16005 tree_stmt_iterator i
;
16006 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16007 RECUR (tsi_stmt (i
));
16011 case CTOR_INITIALIZER
:
16012 finish_mem_initializers (tsubst_initializer_list
16013 (TREE_OPERAND (t
, 0), args
));
16017 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16021 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16022 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16023 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16025 finish_expr_stmt (tmp
);
16029 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16030 /*attribs=*/NULL_TREE
);
16035 tree decl
, pattern_decl
;
16038 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16039 if (TREE_CODE (decl
) == LABEL_DECL
)
16040 finish_label_decl (DECL_NAME (decl
));
16041 else if (TREE_CODE (decl
) == USING_DECL
)
16043 tree scope
= USING_DECL_SCOPE (decl
);
16044 tree name
= DECL_NAME (decl
);
16046 scope
= tsubst (scope
, args
, complain
, in_decl
);
16047 decl
= lookup_qualified_name (scope
, name
,
16048 /*is_type_p=*/false,
16049 /*complain=*/false);
16050 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16051 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16053 finish_local_using_decl (decl
, scope
, name
);
16055 else if (DECL_PACK_P (decl
))
16057 /* Don't build up decls for a variadic capture proxy, we'll
16058 instantiate the elements directly as needed. */
16061 else if (is_capture_proxy (decl
)
16062 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16064 /* We're in tsubst_lambda_expr, we've already inserted a new
16065 capture proxy, so look it up and register it. */
16066 tree inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16067 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16068 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16069 register_local_specialization (inst
, decl
);
16072 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16073 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16074 /* Don't copy the old closure; we'll create a new one in
16075 tsubst_lambda_expr. */
16079 init
= DECL_INITIAL (decl
);
16080 decl
= tsubst (decl
, args
, complain
, in_decl
);
16081 if (decl
!= error_mark_node
)
16083 /* By marking the declaration as instantiated, we avoid
16084 trying to instantiate it. Since instantiate_decl can't
16085 handle local variables, and since we've already done
16086 all that needs to be done, that's the right thing to
16089 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16091 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16092 /* Anonymous aggregates are a special case. */
16093 finish_anon_union (decl
);
16094 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16096 DECL_CONTEXT (decl
) = current_function_decl
;
16097 if (DECL_NAME (decl
) == this_identifier
)
16099 tree lam
= DECL_CONTEXT (current_function_decl
);
16100 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16101 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16103 insert_capture_proxy (decl
);
16105 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16106 /* We already did a pushtag. */;
16107 else if (TREE_CODE (decl
) == FUNCTION_DECL
16108 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16109 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16111 DECL_CONTEXT (decl
) = NULL_TREE
;
16113 DECL_CONTEXT (decl
) = current_function_decl
;
16114 cp_check_omp_declare_reduction (decl
);
16118 int const_init
= false;
16119 maybe_push_decl (decl
);
16121 && DECL_PRETTY_FUNCTION_P (decl
))
16123 /* For __PRETTY_FUNCTION__ we have to adjust the
16125 const char *const name
16126 = cxx_printable_name (current_function_decl
, 2);
16127 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16130 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16133 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16136 && DECL_DECOMPOSITION_P (decl
)
16137 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16142 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16143 complain
, in_decl
, &first
, &cnt
);
16144 if (ndecl
!= error_mark_node
)
16145 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16146 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16147 if (ndecl
!= error_mark_node
)
16148 cp_finish_decomp (ndecl
, first
, cnt
);
16151 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16160 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16161 RECUR (FOR_INIT_STMT (t
));
16162 finish_init_stmt (stmt
);
16163 tmp
= RECUR (FOR_COND (t
));
16164 finish_for_cond (tmp
, stmt
, false, 0);
16165 tmp
= RECUR (FOR_EXPR (t
));
16166 finish_for_expr (tmp
, stmt
);
16168 bool prev
= note_iteration_stmt_body_start ();
16169 RECUR (FOR_BODY (t
));
16170 note_iteration_stmt_body_end (prev
);
16172 finish_for_stmt (stmt
);
16175 case RANGE_FOR_STMT
:
16177 /* Construct another range_for, if this is not a final
16178 substitution (for inside inside a generic lambda of a
16179 template). Otherwise convert to a regular for. */
16181 stmt
= (processing_template_decl
16182 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16183 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16184 decl
= RANGE_FOR_DECL (t
);
16185 decl
= tsubst (decl
, args
, complain
, in_decl
);
16186 maybe_push_decl (decl
);
16187 expr
= RECUR (RANGE_FOR_EXPR (t
));
16189 tree decomp_first
= NULL_TREE
;
16190 unsigned decomp_cnt
= 0;
16191 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16192 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16194 &decomp_first
, &decomp_cnt
);
16196 if (processing_template_decl
)
16198 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16199 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16200 finish_range_for_decl (stmt
, decl
, expr
);
16204 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16205 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16206 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16207 decomp_first
, decomp_cnt
,
16208 RANGE_FOR_IVDEP (t
), unroll
);
16211 bool prev
= note_iteration_stmt_body_start ();
16212 RECUR (RANGE_FOR_BODY (t
));
16213 note_iteration_stmt_body_end (prev
);
16214 finish_for_stmt (stmt
);
16219 stmt
= begin_while_stmt ();
16220 tmp
= RECUR (WHILE_COND (t
));
16221 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16223 bool prev
= note_iteration_stmt_body_start ();
16224 RECUR (WHILE_BODY (t
));
16225 note_iteration_stmt_body_end (prev
);
16227 finish_while_stmt (stmt
);
16231 stmt
= begin_do_stmt ();
16233 bool prev
= note_iteration_stmt_body_start ();
16234 RECUR (DO_BODY (t
));
16235 note_iteration_stmt_body_end (prev
);
16237 finish_do_body (stmt
);
16238 tmp
= RECUR (DO_COND (t
));
16239 finish_do_stmt (tmp
, stmt
, false, 0);
16243 stmt
= begin_if_stmt ();
16244 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16245 tmp
= RECUR (IF_COND (t
));
16246 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16247 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16248 /* Don't instantiate the THEN_CLAUSE. */;
16251 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16253 ++c_inhibit_evaluation_warnings
;
16254 RECUR (THEN_CLAUSE (t
));
16256 --c_inhibit_evaluation_warnings
;
16258 finish_then_clause (stmt
);
16260 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16261 /* Don't instantiate the ELSE_CLAUSE. */;
16262 else if (ELSE_CLAUSE (t
))
16264 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16265 begin_else_clause (stmt
);
16267 ++c_inhibit_evaluation_warnings
;
16268 RECUR (ELSE_CLAUSE (t
));
16270 --c_inhibit_evaluation_warnings
;
16271 finish_else_clause (stmt
);
16274 finish_if_stmt (stmt
);
16278 if (BIND_EXPR_BODY_BLOCK (t
))
16279 stmt
= begin_function_body ();
16281 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16282 ? BCS_TRY_BLOCK
: 0);
16284 RECUR (BIND_EXPR_BODY (t
));
16286 if (BIND_EXPR_BODY_BLOCK (t
))
16287 finish_function_body (stmt
);
16289 finish_compound_stmt (stmt
);
16293 finish_break_stmt ();
16296 case CONTINUE_STMT
:
16297 finish_continue_stmt ();
16301 stmt
= begin_switch_stmt ();
16302 tmp
= RECUR (SWITCH_STMT_COND (t
));
16303 finish_switch_cond (tmp
, stmt
);
16304 RECUR (SWITCH_STMT_BODY (t
));
16305 finish_switch_stmt (stmt
);
16308 case CASE_LABEL_EXPR
:
16310 tree low
= RECUR (CASE_LOW (t
));
16311 tree high
= RECUR (CASE_HIGH (t
));
16312 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16313 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16314 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16315 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16321 tree decl
= LABEL_EXPR_LABEL (t
);
16324 label
= finish_label_stmt (DECL_NAME (decl
));
16325 if (TREE_CODE (label
) == LABEL_DECL
)
16326 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16327 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16328 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16333 tmp
= GOTO_DESTINATION (t
);
16334 if (TREE_CODE (tmp
) != LABEL_DECL
)
16335 /* Computed goto's must be tsubst'd into. On the other hand,
16336 non-computed gotos must not be; the identifier in question
16337 will have no binding. */
16340 tmp
= DECL_NAME (tmp
);
16341 finish_goto_stmt (tmp
);
16346 tree string
= RECUR (ASM_STRING (t
));
16347 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16348 complain
, in_decl
);
16349 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16350 complain
, in_decl
);
16351 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16352 complain
, in_decl
);
16353 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16354 complain
, in_decl
);
16355 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16357 tree asm_expr
= tmp
;
16358 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16359 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16360 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16367 stmt
= begin_try_block ();
16368 RECUR (TRY_STMTS (t
));
16369 finish_cleanup_try_block (stmt
);
16370 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16374 tree compound_stmt
= NULL_TREE
;
16376 if (FN_TRY_BLOCK_P (t
))
16377 stmt
= begin_function_try_block (&compound_stmt
);
16379 stmt
= begin_try_block ();
16381 RECUR (TRY_STMTS (t
));
16383 if (FN_TRY_BLOCK_P (t
))
16384 finish_function_try_block (stmt
);
16386 finish_try_block (stmt
);
16388 RECUR (TRY_HANDLERS (t
));
16389 if (FN_TRY_BLOCK_P (t
))
16390 finish_function_handler_sequence (stmt
, compound_stmt
);
16392 finish_handler_sequence (stmt
);
16398 tree decl
= HANDLER_PARMS (t
);
16402 decl
= tsubst (decl
, args
, complain
, in_decl
);
16403 /* Prevent instantiate_decl from trying to instantiate
16404 this variable. We've already done all that needs to be
16406 if (decl
!= error_mark_node
)
16407 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16409 stmt
= begin_handler ();
16410 finish_handler_parms (decl
, stmt
);
16411 RECUR (HANDLER_BODY (t
));
16412 finish_handler (stmt
);
16417 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16418 if (CLASS_TYPE_P (tmp
))
16420 /* Local classes are not independent templates; they are
16421 instantiated along with their containing function. And this
16422 way we don't have to deal with pushing out of one local class
16423 to instantiate a member of another local class. */
16424 /* Closures are handled by the LAMBDA_EXPR. */
16425 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16426 complete_type (tmp
);
16427 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16429 || (TREE_CODE (fld
) == FUNCTION_DECL
16430 && !DECL_ARTIFICIAL (fld
)))
16431 && DECL_TEMPLATE_INSTANTIATION (fld
))
16432 instantiate_decl (fld
, /*defer_ok=*/false,
16433 /*expl_inst_class=*/false);
16437 case STATIC_ASSERT
:
16441 ++c_inhibit_evaluation_warnings
;
16443 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16446 /*integral_constant_expression_p=*/true);
16447 --c_inhibit_evaluation_warnings
;
16449 finish_static_assert (condition
,
16450 STATIC_ASSERT_MESSAGE (t
),
16451 STATIC_ASSERT_SOURCE_LOCATION (t
),
16452 /*member_p=*/false);
16457 case OACC_PARALLEL
:
16458 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16460 stmt
= begin_omp_parallel ();
16461 RECUR (OMP_BODY (t
));
16462 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16466 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16467 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16468 complain
, in_decl
);
16469 if (OMP_PARALLEL_COMBINED (t
))
16470 omp_parallel_combined_clauses
= &tmp
;
16471 stmt
= begin_omp_parallel ();
16472 RECUR (OMP_PARALLEL_BODY (t
));
16473 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16474 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16475 = OMP_PARALLEL_COMBINED (t
);
16476 pop_omp_privatization_clauses (r
);
16480 r
= push_omp_privatization_clauses (false);
16481 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16482 complain
, in_decl
);
16483 stmt
= begin_omp_task ();
16484 RECUR (OMP_TASK_BODY (t
));
16485 finish_omp_task (tmp
, stmt
);
16486 pop_omp_privatization_clauses (r
);
16491 case OMP_DISTRIBUTE
:
16495 tree clauses
, body
, pre_body
;
16496 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16497 tree orig_declv
= NULL_TREE
;
16498 tree incrv
= NULL_TREE
;
16499 enum c_omp_region_type ort
= C_ORT_OMP
;
16502 if (TREE_CODE (t
) == OACC_LOOP
)
16505 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16506 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16508 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16510 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16511 if (OMP_FOR_ORIG_DECLS (t
))
16512 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16513 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16514 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16515 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16518 stmt
= begin_omp_structured_block ();
16520 pre_body
= push_stmt_list ();
16521 RECUR (OMP_FOR_PRE_BODY (t
));
16522 pre_body
= pop_stmt_list (pre_body
);
16524 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16525 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16526 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16527 incrv
, &clauses
, args
, complain
, in_decl
,
16528 integral_constant_expression_p
);
16529 omp_parallel_combined_clauses
= NULL
;
16531 body
= push_stmt_list ();
16532 RECUR (OMP_FOR_BODY (t
));
16533 body
= pop_stmt_list (body
);
16535 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16536 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16537 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16541 t
= make_node (TREE_CODE (t
));
16542 TREE_TYPE (t
) = void_type_node
;
16543 OMP_FOR_BODY (t
) = body
;
16544 OMP_FOR_PRE_BODY (t
) = pre_body
;
16545 OMP_FOR_CLAUSES (t
) = clauses
;
16546 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16550 add_stmt (finish_omp_structured_block (stmt
));
16551 pop_omp_privatization_clauses (r
);
16556 omp_parallel_combined_clauses
= NULL
;
16561 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16562 && OMP_TEAMS_COMBINED (t
));
16563 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16565 stmt
= push_stmt_list ();
16566 RECUR (OMP_BODY (t
));
16567 stmt
= pop_stmt_list (stmt
);
16570 OMP_BODY (t
) = stmt
;
16571 OMP_CLAUSES (t
) = tmp
;
16573 pop_omp_privatization_clauses (r
);
16577 case OMP_TARGET_DATA
:
16579 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16580 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16582 keep_next_level (true);
16583 stmt
= begin_omp_structured_block ();
16585 RECUR (OMP_BODY (t
));
16586 stmt
= finish_omp_structured_block (stmt
);
16589 OMP_BODY (t
) = stmt
;
16590 OMP_CLAUSES (t
) = tmp
;
16591 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16593 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16596 /* For combined target teams, ensure the num_teams and
16597 thread_limit clause expressions are evaluated on the host,
16598 before entering the target construct. */
16600 for (c
= OMP_TEAMS_CLAUSES (teams
);
16601 c
; c
= OMP_CLAUSE_CHAIN (c
))
16602 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16603 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16604 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16606 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16607 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16608 if (expr
== error_mark_node
)
16610 tmp
= TARGET_EXPR_SLOT (expr
);
16612 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16613 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16614 OMP_CLAUSE_FIRSTPRIVATE
);
16615 OMP_CLAUSE_DECL (tc
) = tmp
;
16616 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16617 OMP_TARGET_CLAUSES (t
) = tc
;
16626 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16627 complain
, in_decl
);
16628 OACC_DECLARE_CLAUSES (t
) = tmp
;
16632 case OMP_TARGET_UPDATE
:
16633 case OMP_TARGET_ENTER_DATA
:
16634 case OMP_TARGET_EXIT_DATA
:
16635 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16636 complain
, in_decl
);
16638 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16642 case OACC_ENTER_DATA
:
16643 case OACC_EXIT_DATA
:
16645 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16646 complain
, in_decl
);
16648 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16653 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16654 complain
, in_decl
);
16655 stmt
= push_stmt_list ();
16656 RECUR (OMP_BODY (t
));
16657 stmt
= pop_stmt_list (stmt
);
16660 OMP_BODY (t
) = stmt
;
16661 OMP_ORDERED_CLAUSES (t
) = tmp
;
16667 case OMP_TASKGROUP
:
16668 stmt
= push_stmt_list ();
16669 RECUR (OMP_BODY (t
));
16670 stmt
= pop_stmt_list (stmt
);
16673 OMP_BODY (t
) = stmt
;
16678 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16679 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16681 tree op1
= TREE_OPERAND (t
, 1);
16682 tree rhs1
= NULL_TREE
;
16684 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16686 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16687 op1
= TREE_OPERAND (op1
, 1);
16689 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16690 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16691 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16692 NULL_TREE
, NULL_TREE
, rhs1
,
16693 OMP_ATOMIC_SEQ_CST (t
));
16697 tree op1
= TREE_OPERAND (t
, 1);
16698 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16699 tree rhs1
= NULL_TREE
;
16700 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16701 enum tree_code opcode
= NOP_EXPR
;
16702 if (code
== OMP_ATOMIC_READ
)
16704 v
= RECUR (TREE_OPERAND (op1
, 0));
16705 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16707 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16708 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16710 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16711 v
= RECUR (TREE_OPERAND (op1
, 0));
16712 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16713 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16715 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16716 op11
= TREE_OPERAND (op11
, 1);
16718 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16719 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16720 opcode
= TREE_CODE (op11
);
16721 if (opcode
== MODIFY_EXPR
)
16727 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16728 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16730 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16731 OMP_ATOMIC_SEQ_CST (t
));
16735 case TRANSACTION_EXPR
:
16738 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16739 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16741 if (TRANSACTION_EXPR_IS_STMT (t
))
16743 tree body
= TRANSACTION_EXPR_BODY (t
);
16744 tree noex
= NULL_TREE
;
16745 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16747 noex
= MUST_NOT_THROW_COND (body
);
16748 if (noex
== NULL_TREE
)
16749 noex
= boolean_true_node
;
16750 body
= TREE_OPERAND (body
, 0);
16752 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16754 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16758 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16759 RECUR (TRANSACTION_EXPR_BODY (t
)),
16766 case MUST_NOT_THROW_EXPR
:
16768 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16769 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16770 RETURN (build_must_not_throw_expr (op0
, cond
));
16773 case EXPR_PACK_EXPANSION
:
16774 error ("invalid use of pack expansion expression");
16775 RETURN (error_mark_node
);
16777 case NONTYPE_ARGUMENT_PACK
:
16778 error ("use %<...%> to expand argument pack");
16779 RETURN (error_mark_node
);
16781 case COMPOUND_EXPR
:
16782 tmp
= RECUR (TREE_OPERAND (t
, 0));
16783 if (tmp
== NULL_TREE
)
16784 /* If the first operand was a statement, we're done with it. */
16785 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16786 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16787 RECUR (TREE_OPERAND (t
, 1)),
16790 case ANNOTATE_EXPR
:
16791 tmp
= RECUR (TREE_OPERAND (t
, 0));
16792 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16793 TREE_TYPE (tmp
), tmp
,
16794 RECUR (TREE_OPERAND (t
, 1)),
16795 RECUR (TREE_OPERAND (t
, 2))));
16798 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16800 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16801 /*function_p=*/false,
16802 integral_constant_expression_p
));
16805 RETURN (NULL_TREE
);
16807 input_location
= loc
;
16813 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16814 function. For description of the body see comment above
16815 cp_parser_omp_declare_reduction_exprs. */
16818 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16820 if (t
== NULL_TREE
|| t
== error_mark_node
)
16823 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16825 tree_stmt_iterator tsi
;
16828 memset (stmts
, 0, sizeof stmts
);
16829 for (i
= 0, tsi
= tsi_start (t
);
16830 i
< 7 && !tsi_end_p (tsi
);
16831 i
++, tsi_next (&tsi
))
16832 stmts
[i
] = tsi_stmt (tsi
);
16833 gcc_assert (tsi_end_p (tsi
));
16837 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16838 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16839 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16840 args
, complain
, in_decl
);
16841 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16842 args
, complain
, in_decl
);
16843 DECL_CONTEXT (omp_out
) = current_function_decl
;
16844 DECL_CONTEXT (omp_in
) = current_function_decl
;
16845 keep_next_level (true);
16846 tree block
= begin_omp_structured_block ();
16847 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16848 block
= finish_omp_structured_block (block
);
16849 block
= maybe_cleanup_point_expr_void (block
);
16850 add_decl_expr (omp_out
);
16851 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16852 TREE_NO_WARNING (omp_out
) = 1;
16853 add_decl_expr (omp_in
);
16854 finish_expr_stmt (block
);
16858 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16859 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16860 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16861 args
, complain
, in_decl
);
16862 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16863 args
, complain
, in_decl
);
16864 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16865 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16866 keep_next_level (true);
16867 tree block
= begin_omp_structured_block ();
16868 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16869 block
= finish_omp_structured_block (block
);
16870 block
= maybe_cleanup_point_expr_void (block
);
16871 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16872 add_decl_expr (omp_priv
);
16873 add_decl_expr (omp_orig
);
16874 finish_expr_stmt (block
);
16876 add_decl_expr (omp_orig
);
16880 /* T is a postfix-expression that is not being used in a function
16881 call. Return the substituted version of T. */
16884 tsubst_non_call_postfix_expression (tree t
, tree args
,
16885 tsubst_flags_t complain
,
16888 if (TREE_CODE (t
) == SCOPE_REF
)
16889 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16890 /*done=*/false, /*address_p=*/false);
16892 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16893 /*function_p=*/false,
16894 /*integral_constant_expression_p=*/false);
16899 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16900 instantiation context. Instantiating a pack expansion containing a lambda
16901 might result in multiple lambdas all based on the same lambda in the
16905 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16907 tree oldfn
= lambda_function (t
);
16910 tree r
= build_lambda_expr ();
16912 LAMBDA_EXPR_LOCATION (r
)
16913 = LAMBDA_EXPR_LOCATION (t
);
16914 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
16915 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
16916 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
16918 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
16919 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
16921 record_lambda_scope (r
);
16923 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
16924 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
16926 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
16927 cap
= TREE_CHAIN (cap
))
16929 tree field
= TREE_PURPOSE (cap
);
16930 if (PACK_EXPANSION_P (field
))
16931 field
= PACK_EXPANSION_PATTERN (field
);
16932 field
= tsubst_decl (field
, args
, complain
);
16934 if (field
== error_mark_node
)
16935 return error_mark_node
;
16937 tree init
= TREE_VALUE (cap
);
16938 if (PACK_EXPANSION_P (init
))
16939 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
16941 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
16942 /*fn*/false, /*constexpr*/false);
16944 if (TREE_CODE (field
) == TREE_VEC
)
16946 int len
= TREE_VEC_LENGTH (field
);
16947 gcc_assert (TREE_CODE (init
) == TREE_VEC
16948 && TREE_VEC_LENGTH (init
) == len
);
16949 for (int i
= 0; i
< len
; ++i
)
16950 LAMBDA_EXPR_CAPTURE_LIST (r
)
16951 = tree_cons (TREE_VEC_ELT (field
, i
),
16952 TREE_VEC_ELT (init
, i
),
16953 LAMBDA_EXPR_CAPTURE_LIST (r
));
16957 LAMBDA_EXPR_CAPTURE_LIST (r
)
16958 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
16960 if (id_equal (DECL_NAME (field
), "__this"))
16961 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
16965 tree type
= begin_lambda_type (r
);
16967 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16968 determine_visibility (TYPE_NAME (type
));
16970 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
16972 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
16973 ? DECL_TI_TEMPLATE (oldfn
)
16976 tree fntype
= static_fn_type (oldfn
);
16978 ++processing_template_decl
;
16979 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
16981 --processing_template_decl
;
16983 if (fntype
== error_mark_node
)
16984 r
= error_mark_node
;
16987 /* Fix the type of 'this'. */
16988 fntype
= build_memfn_type (fntype
, type
,
16989 type_memfn_quals (fntype
),
16990 type_memfn_rqual (fntype
));
16994 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
16995 fn
= DECL_TEMPLATE_RESULT (tmpl
);
16996 finish_member_declaration (tmpl
);
17001 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17002 finish_member_declaration (fn
);
17005 /* Let finish_function set this. */
17006 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17008 bool nested
= cfun
;
17010 push_function_context ();
17012 local_specialization_stack
s (lss_copy
);
17014 tree body
= start_lambda_function (fn
, r
);
17016 register_parameter_specializations (oldfn
, fn
);
17018 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17019 /*constexpr*/false);
17021 finish_lambda_function (body
);
17024 pop_function_context ();
17026 /* The capture list was built up in reverse order; fix that now. */
17027 LAMBDA_EXPR_CAPTURE_LIST (r
)
17028 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17030 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17032 maybe_add_lambda_conv_op (type
);
17035 finish_struct (type
, /*attr*/NULL_TREE
);
17037 insert_pending_capture_proxies ();
17042 /* Like tsubst but deals with expressions and performs semantic
17043 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17046 tsubst_copy_and_build (tree t
,
17048 tsubst_flags_t complain
,
17051 bool integral_constant_expression_p
)
17053 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17054 #define RECUR(NODE) \
17055 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17056 /*function_p=*/false, \
17057 integral_constant_expression_p)
17062 if (t
== NULL_TREE
|| t
== error_mark_node
)
17065 loc
= input_location
;
17066 if (EXPR_HAS_LOCATION (t
))
17067 input_location
= EXPR_LOCATION (t
);
17069 /* N3276 decltype magic only applies to calls at the top level or on the
17070 right side of a comma. */
17071 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17072 complain
&= ~tf_decltype
;
17074 switch (TREE_CODE (t
))
17078 /* Fall through. */
17079 case IDENTIFIER_NODE
:
17083 bool non_integral_constant_expression_p
;
17084 const char *error_msg
;
17086 if (IDENTIFIER_CONV_OP_P (t
))
17088 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17089 t
= make_conv_op_name (new_type
);
17092 /* Look up the name. */
17093 decl
= lookup_name (t
);
17095 /* By convention, expressions use ERROR_MARK_NODE to indicate
17096 failure, not NULL_TREE. */
17097 if (decl
== NULL_TREE
)
17098 decl
= error_mark_node
;
17100 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17102 integral_constant_expression_p
,
17103 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17104 &non_integral_constant_expression_p
,
17105 /*template_p=*/false,
17107 /*address_p=*/false,
17108 /*template_arg_p=*/false,
17113 if (!function_p
&& identifier_p (decl
))
17115 if (complain
& tf_error
)
17116 unqualified_name_lookup_error (decl
);
17117 decl
= error_mark_node
;
17122 case TEMPLATE_ID_EXPR
:
17125 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17126 tree targs
= TREE_OPERAND (t
, 1);
17129 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17130 if (targs
== error_mark_node
)
17131 return error_mark_node
;
17133 if (TREE_CODE (templ
) == SCOPE_REF
)
17135 tree name
= TREE_OPERAND (templ
, 1);
17136 tree tid
= lookup_template_function (name
, targs
);
17137 TREE_OPERAND (templ
, 1) = tid
;
17141 if (variable_template_p (templ
))
17142 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17144 if (TREE_CODE (templ
) == COMPONENT_REF
)
17146 object
= TREE_OPERAND (templ
, 0);
17147 templ
= TREE_OPERAND (templ
, 1);
17150 object
= NULL_TREE
;
17151 templ
= lookup_template_function (templ
, targs
);
17154 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17155 object
, templ
, NULL_TREE
));
17157 RETURN (baselink_for_fns (templ
));
17162 tree r
= RECUR (TREE_OPERAND (t
, 0));
17164 if (REFERENCE_REF_P (t
))
17166 /* A type conversion to reference type will be enclosed in
17167 such an indirect ref, but the substitution of the cast
17168 will have also added such an indirect ref. */
17169 r
= convert_from_reference (r
);
17172 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17173 complain
|decltype_flag
);
17175 if (TREE_CODE (r
) == INDIRECT_REF
)
17176 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17183 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17184 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17185 RETURN (build_nop (type
, op0
));
17188 case IMPLICIT_CONV_EXPR
:
17190 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17191 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17192 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17194 retval
= copy_node (t
);
17195 TREE_TYPE (retval
) = type
;
17196 TREE_OPERAND (retval
, 0) = expr
;
17199 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17200 /* We'll pass this to convert_nontype_argument again, we don't need
17201 to actually perform any conversion here. */
17203 int flags
= LOOKUP_IMPLICIT
;
17204 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17205 flags
= LOOKUP_NORMAL
;
17206 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17212 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17213 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17214 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17218 case REINTERPRET_CAST_EXPR
:
17219 case CONST_CAST_EXPR
:
17220 case DYNAMIC_CAST_EXPR
:
17221 case STATIC_CAST_EXPR
:
17224 tree op
, r
= NULL_TREE
;
17226 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17227 if (integral_constant_expression_p
17228 && !cast_valid_in_integral_constant_expression_p (type
))
17230 if (complain
& tf_error
)
17231 error ("a cast to a type other than an integral or "
17232 "enumeration type cannot appear in a constant-expression");
17233 RETURN (error_mark_node
);
17236 op
= RECUR (TREE_OPERAND (t
, 0));
17238 warning_sentinel
s(warn_useless_cast
);
17239 warning_sentinel
s2(warn_ignored_qualifiers
);
17240 switch (TREE_CODE (t
))
17243 r
= build_functional_cast (type
, op
, complain
);
17245 case REINTERPRET_CAST_EXPR
:
17246 r
= build_reinterpret_cast (type
, op
, complain
);
17248 case CONST_CAST_EXPR
:
17249 r
= build_const_cast (type
, op
, complain
);
17251 case DYNAMIC_CAST_EXPR
:
17252 r
= build_dynamic_cast (type
, op
, complain
);
17254 case STATIC_CAST_EXPR
:
17255 r
= build_static_cast (type
, op
, complain
);
17258 gcc_unreachable ();
17264 case POSTDECREMENT_EXPR
:
17265 case POSTINCREMENT_EXPR
:
17266 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17267 args
, complain
, in_decl
);
17268 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17269 complain
|decltype_flag
));
17271 case PREDECREMENT_EXPR
:
17272 case PREINCREMENT_EXPR
:
17276 case TRUTH_NOT_EXPR
:
17277 case UNARY_PLUS_EXPR
: /* Unary + */
17278 case REALPART_EXPR
:
17279 case IMAGPART_EXPR
:
17280 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17281 RECUR (TREE_OPERAND (t
, 0)),
17282 complain
|decltype_flag
));
17284 case FIX_TRUNC_EXPR
:
17285 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17289 op1
= TREE_OPERAND (t
, 0);
17290 if (TREE_CODE (op1
) == LABEL_DECL
)
17291 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17292 EXPR_LOCATION (op1
)));
17293 if (TREE_CODE (op1
) == SCOPE_REF
)
17294 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17295 /*done=*/true, /*address_p=*/true);
17297 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17299 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17300 complain
|decltype_flag
));
17305 case TRUNC_DIV_EXPR
:
17306 case CEIL_DIV_EXPR
:
17307 case FLOOR_DIV_EXPR
:
17308 case ROUND_DIV_EXPR
:
17309 case EXACT_DIV_EXPR
:
17313 case TRUNC_MOD_EXPR
:
17314 case FLOOR_MOD_EXPR
:
17315 case TRUTH_ANDIF_EXPR
:
17316 case TRUTH_ORIF_EXPR
:
17317 case TRUTH_AND_EXPR
:
17318 case TRUTH_OR_EXPR
:
17334 warning_sentinel
s1(warn_type_limits
);
17335 warning_sentinel
s2(warn_div_by_zero
);
17336 warning_sentinel
s3(warn_logical_op
);
17337 warning_sentinel
s4(warn_tautological_compare
);
17338 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17339 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17340 tree r
= build_x_binary_op
17341 (input_location
, TREE_CODE (t
),
17343 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17345 : TREE_CODE (TREE_OPERAND (t
, 0))),
17347 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17349 : TREE_CODE (TREE_OPERAND (t
, 1))),
17351 complain
|decltype_flag
);
17352 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17353 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17358 case POINTER_PLUS_EXPR
:
17360 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17361 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17362 return fold_build_pointer_plus (op0
, op1
);
17366 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17367 /*address_p=*/false));
17369 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17370 args
, complain
, in_decl
);
17371 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17372 RECUR (TREE_OPERAND (t
, 1)),
17373 complain
|decltype_flag
));
17376 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17377 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17378 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17385 op1
= TREE_OPERAND (t
, 0);
17386 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17387 op1
= TREE_TYPE (op1
);
17390 /* When there are no ARGS, we are trying to evaluate a
17391 non-dependent expression from the parser. Trying to do
17392 the substitutions may not work. */
17394 op1
= TREE_TYPE (op1
);
17398 ++cp_unevaluated_operand
;
17399 ++c_inhibit_evaluation_warnings
;
17401 op1
= tsubst (op1
, args
, complain
, in_decl
);
17403 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17404 /*function_p=*/false,
17405 /*integral_constant_expression_p=*/
17407 --cp_unevaluated_operand
;
17408 --c_inhibit_evaluation_warnings
;
17411 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17412 complain
& tf_error
);
17414 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17415 complain
& tf_error
);
17416 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17418 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17420 if (!processing_template_decl
&& TYPE_P (op1
))
17422 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17423 build1 (NOP_EXPR
, op1
, error_mark_node
));
17424 SIZEOF_EXPR_TYPE_P (r
) = 1;
17427 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17428 TREE_SIDE_EFFECTS (r
) = 0;
17429 TREE_READONLY (r
) = 1;
17431 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17436 case AT_ENCODE_EXPR
:
17438 op1
= TREE_OPERAND (t
, 0);
17439 ++cp_unevaluated_operand
;
17440 ++c_inhibit_evaluation_warnings
;
17441 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17442 /*function_p=*/false,
17443 /*integral_constant_expression_p=*/false);
17444 --cp_unevaluated_operand
;
17445 --c_inhibit_evaluation_warnings
;
17446 RETURN (objc_build_encode_expr (op1
));
17449 case NOEXCEPT_EXPR
:
17450 op1
= TREE_OPERAND (t
, 0);
17451 ++cp_unevaluated_operand
;
17452 ++c_inhibit_evaluation_warnings
;
17453 ++cp_noexcept_operand
;
17454 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17455 /*function_p=*/false,
17456 /*integral_constant_expression_p=*/false);
17457 --cp_unevaluated_operand
;
17458 --c_inhibit_evaluation_warnings
;
17459 --cp_noexcept_operand
;
17460 RETURN (finish_noexcept_expr (op1
, complain
));
17464 warning_sentinel
s(warn_div_by_zero
);
17465 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17466 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17467 tree r
= build_x_modify_expr
17468 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17469 complain
|decltype_flag
);
17470 /* TREE_NO_WARNING must be set if either the expression was
17471 parenthesized or it uses an operator such as >>= rather
17472 than plain assignment. In the former case, it was already
17473 set and must be copied. In the latter case,
17474 build_x_modify_expr sets it and it must not be reset
17476 if (TREE_NO_WARNING (t
))
17477 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17483 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17484 args
, complain
, in_decl
);
17485 /* Remember that there was a reference to this entity. */
17487 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17488 RETURN (error_mark_node
);
17489 RETURN (build_x_arrow (input_location
, op1
, complain
));
17493 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17494 tree init
= RECUR (TREE_OPERAND (t
, 3));
17495 vec
<tree
, va_gc
> *placement_vec
;
17496 vec
<tree
, va_gc
> *init_vec
;
17499 if (placement
== NULL_TREE
)
17500 placement_vec
= NULL
;
17503 placement_vec
= make_tree_vector ();
17504 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17505 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17508 /* If there was an initializer in the original tree, but it
17509 instantiated to an empty list, then we should pass a
17510 non-NULL empty vector to tell build_new that it was an
17511 empty initializer() rather than no initializer. This can
17512 only happen when the initializer is a pack expansion whose
17513 parameter packs are of length zero. */
17514 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17518 init_vec
= make_tree_vector ();
17519 if (init
== void_node
)
17520 gcc_assert (init_vec
!= NULL
);
17523 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17524 vec_safe_push (init_vec
, TREE_VALUE (init
));
17528 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17529 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17530 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17531 NEW_EXPR_USE_GLOBAL (t
),
17534 if (placement_vec
!= NULL
)
17535 release_tree_vector (placement_vec
);
17536 if (init_vec
!= NULL
)
17537 release_tree_vector (init_vec
);
17544 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17545 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17546 RETURN (delete_sanity (op0
, op1
,
17547 DELETE_EXPR_USE_VEC (t
),
17548 DELETE_EXPR_USE_GLOBAL (t
),
17552 case COMPOUND_EXPR
:
17554 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17555 complain
& ~tf_decltype
, in_decl
,
17556 /*function_p=*/false,
17557 integral_constant_expression_p
);
17558 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17560 RECUR (TREE_OPERAND (t
, 1)),
17561 complain
|decltype_flag
));
17567 vec
<tree
, va_gc
> *call_args
;
17568 unsigned int nargs
, i
;
17573 function
= CALL_EXPR_FN (t
);
17574 /* Internal function with no arguments. */
17575 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17578 /* When we parsed the expression, we determined whether or
17579 not Koenig lookup should be performed. */
17580 koenig_p
= KOENIG_LOOKUP_P (t
);
17581 if (function
== NULL_TREE
)
17584 qualified_p
= false;
17586 else if (TREE_CODE (function
) == SCOPE_REF
)
17588 qualified_p
= true;
17589 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17591 /*address_p=*/false);
17593 else if (koenig_p
&& identifier_p (function
))
17595 /* Do nothing; calling tsubst_copy_and_build on an identifier
17596 would incorrectly perform unqualified lookup again.
17598 Note that we can also have an IDENTIFIER_NODE if the earlier
17599 unqualified lookup found a member function; in that case
17600 koenig_p will be false and we do want to do the lookup
17601 again to find the instantiated member function.
17603 FIXME but doing that causes c++/15272, so we need to stop
17604 using IDENTIFIER_NODE in that situation. */
17605 qualified_p
= false;
17609 if (TREE_CODE (function
) == COMPONENT_REF
)
17611 tree op
= TREE_OPERAND (function
, 1);
17613 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17614 || (BASELINK_P (op
)
17615 && BASELINK_QUALIFIED_P (op
)));
17618 qualified_p
= false;
17620 if (TREE_CODE (function
) == ADDR_EXPR
17621 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17622 /* Avoid error about taking the address of a constructor. */
17623 function
= TREE_OPERAND (function
, 0);
17625 function
= tsubst_copy_and_build (function
, args
, complain
,
17628 integral_constant_expression_p
);
17630 if (BASELINK_P (function
))
17631 qualified_p
= true;
17634 nargs
= call_expr_nargs (t
);
17635 call_args
= make_tree_vector ();
17636 for (i
= 0; i
< nargs
; ++i
)
17638 tree arg
= CALL_EXPR_ARG (t
, i
);
17640 if (!PACK_EXPANSION_P (arg
))
17641 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17644 /* Expand the pack expansion and push each entry onto
17646 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17647 if (TREE_CODE (arg
) == TREE_VEC
)
17649 unsigned int len
, j
;
17651 len
= TREE_VEC_LENGTH (arg
);
17652 for (j
= 0; j
< len
; ++j
)
17654 tree value
= TREE_VEC_ELT (arg
, j
);
17655 if (value
!= NULL_TREE
)
17656 value
= convert_from_reference (value
);
17657 vec_safe_push (call_args
, value
);
17662 /* A partial substitution. Add one entry. */
17663 vec_safe_push (call_args
, arg
);
17668 /* We do not perform argument-dependent lookup if normal
17669 lookup finds a non-function, in accordance with the
17670 expected resolution of DR 218. */
17672 && ((is_overloaded_fn (function
)
17673 /* If lookup found a member function, the Koenig lookup is
17674 not appropriate, even if an unqualified-name was used
17675 to denote the function. */
17676 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17677 || identifier_p (function
))
17678 /* Only do this when substitution turns a dependent call
17679 into a non-dependent call. */
17680 && type_dependent_expression_p_push (t
)
17681 && !any_type_dependent_arguments_p (call_args
))
17682 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17684 if (function
!= NULL_TREE
17685 && identifier_p (function
)
17686 && !any_type_dependent_arguments_p (call_args
))
17688 if (koenig_p
&& (complain
& tf_warning_or_error
))
17690 /* For backwards compatibility and good diagnostics, try
17691 the unqualified lookup again if we aren't in SFINAE
17693 tree unq
= (tsubst_copy_and_build
17694 (function
, args
, complain
, in_decl
, true,
17695 integral_constant_expression_p
));
17696 if (unq
== error_mark_node
)
17698 release_tree_vector (call_args
);
17699 RETURN (error_mark_node
);
17702 if (unq
!= function
)
17704 /* In a lambda fn, we have to be careful to not
17705 introduce new this captures. Legacy code can't
17706 be using lambdas anyway, so it's ok to be
17708 bool in_lambda
= (current_class_type
17709 && LAMBDA_TYPE_P (current_class_type
));
17710 char const *const msg
17711 = G_("%qD was not declared in this scope, "
17712 "and no declarations were found by "
17713 "argument-dependent lookup at the point "
17714 "of instantiation");
17718 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17721 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17727 if (INDIRECT_REF_P (fn
))
17728 fn
= TREE_OPERAND (fn
, 0);
17729 if (is_overloaded_fn (fn
))
17730 fn
= get_first_fn (fn
);
17733 /* Can't say anything more. */;
17734 else if (DECL_CLASS_SCOPE_P (fn
))
17736 location_t loc
= EXPR_LOC_OR_LOC (t
,
17739 "declarations in dependent base %qT are "
17740 "not found by unqualified lookup",
17741 DECL_CLASS_CONTEXT (fn
));
17742 if (current_class_ptr
)
17744 "use %<this->%D%> instead", function
);
17747 "use %<%T::%D%> instead",
17748 current_class_name
, function
);
17751 inform (DECL_SOURCE_LOCATION (fn
),
17752 "%qD declared here, later in the "
17753 "translation unit", fn
);
17756 release_tree_vector (call_args
);
17757 RETURN (error_mark_node
);
17764 if (identifier_p (function
))
17766 if (complain
& tf_error
)
17767 unqualified_name_lookup_error (function
);
17768 release_tree_vector (call_args
);
17769 RETURN (error_mark_node
);
17773 /* Remember that there was a reference to this entity. */
17774 if (function
!= NULL_TREE
17775 && DECL_P (function
)
17776 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17778 release_tree_vector (call_args
);
17779 RETURN (error_mark_node
);
17782 /* Put back tf_decltype for the actual call. */
17783 complain
|= decltype_flag
;
17785 if (function
== NULL_TREE
)
17786 switch (CALL_EXPR_IFN (t
))
17789 gcc_assert (nargs
== 1);
17790 if (vec_safe_length (call_args
) != 1)
17792 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17793 "wrong number of arguments to "
17794 "%<__builtin_launder%>");
17795 ret
= error_mark_node
;
17798 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17800 (*call_args
)[0], complain
);
17804 /* Unsupported internal function with arguments. */
17805 gcc_unreachable ();
17807 else if (TREE_CODE (function
) == OFFSET_REF
)
17808 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17810 else if (TREE_CODE (function
) == COMPONENT_REF
)
17812 tree instance
= TREE_OPERAND (function
, 0);
17813 tree fn
= TREE_OPERAND (function
, 1);
17815 if (processing_template_decl
17816 && (type_dependent_expression_p (instance
)
17817 || (!BASELINK_P (fn
)
17818 && TREE_CODE (fn
) != FIELD_DECL
)
17819 || type_dependent_expression_p (fn
)
17820 || any_type_dependent_arguments_p (call_args
)))
17821 ret
= build_min_nt_call_vec (function
, call_args
);
17822 else if (!BASELINK_P (fn
))
17823 ret
= finish_call_expr (function
, &call_args
,
17824 /*disallow_virtual=*/false,
17825 /*koenig_p=*/false,
17828 ret
= (build_new_method_call
17830 &call_args
, NULL_TREE
,
17831 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17836 ret
= finish_call_expr (function
, &call_args
,
17837 /*disallow_virtual=*/qualified_p
,
17841 release_tree_vector (call_args
);
17843 if (ret
!= error_mark_node
)
17845 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17846 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17847 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17848 bool thk
= CALL_FROM_THUNK_P (t
);
17849 if (op
|| ord
|| rev
|| thk
)
17851 function
= extract_call_expr (ret
);
17852 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17853 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17854 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17857 if (TREE_CODE (function
) == CALL_EXPR
)
17858 CALL_FROM_THUNK_P (function
) = true;
17860 AGGR_INIT_FROM_THUNK_P (function
) = true;
17861 /* The thunk location is not interesting. */
17862 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17872 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17873 tree folded_cond
= fold_non_dependent_expr (cond
);
17876 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17878 if (integer_zerop (folded_cond
))
17880 ++c_inhibit_evaluation_warnings
;
17881 exp1
= RECUR (TREE_OPERAND (t
, 1));
17882 --c_inhibit_evaluation_warnings
;
17883 exp2
= RECUR (TREE_OPERAND (t
, 2));
17887 exp1
= RECUR (TREE_OPERAND (t
, 1));
17888 ++c_inhibit_evaluation_warnings
;
17889 exp2
= RECUR (TREE_OPERAND (t
, 2));
17890 --c_inhibit_evaluation_warnings
;
17892 cond
= folded_cond
;
17896 exp1
= RECUR (TREE_OPERAND (t
, 1));
17897 exp2
= RECUR (TREE_OPERAND (t
, 2));
17900 warning_sentinel
s(warn_duplicated_branches
);
17901 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17902 cond
, exp1
, exp2
, complain
));
17905 case PSEUDO_DTOR_EXPR
:
17907 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17908 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17909 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17910 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17916 tree purpose
, value
, chain
;
17918 if (t
== void_list_node
)
17921 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17922 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17924 /* We have pack expansions, so expand those and
17925 create a new list out of it. */
17926 tree purposevec
= NULL_TREE
;
17927 tree valuevec
= NULL_TREE
;
17931 /* Expand the argument expressions. */
17932 if (TREE_PURPOSE (t
))
17933 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17934 complain
, in_decl
);
17935 if (TREE_VALUE (t
))
17936 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17937 complain
, in_decl
);
17939 /* Build the rest of the list. */
17940 chain
= TREE_CHAIN (t
);
17941 if (chain
&& chain
!= void_type_node
)
17942 chain
= RECUR (chain
);
17944 /* Determine the number of arguments. */
17945 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17947 len
= TREE_VEC_LENGTH (purposevec
);
17948 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17950 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17951 len
= TREE_VEC_LENGTH (valuevec
);
17954 /* Since we only performed a partial substitution into
17955 the argument pack, we only RETURN (a single list
17957 if (purposevec
== TREE_PURPOSE (t
)
17958 && valuevec
== TREE_VALUE (t
)
17959 && chain
== TREE_CHAIN (t
))
17962 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17965 /* Convert the argument vectors into a TREE_LIST */
17969 /* Grab the Ith values. */
17971 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17974 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17977 /* Build the list (backwards). */
17978 chain
= tree_cons (purpose
, value
, chain
);
17984 purpose
= TREE_PURPOSE (t
);
17986 purpose
= RECUR (purpose
);
17987 value
= TREE_VALUE (t
);
17989 value
= RECUR (value
);
17990 chain
= TREE_CHAIN (t
);
17991 if (chain
&& chain
!= void_type_node
)
17992 chain
= RECUR (chain
);
17993 if (purpose
== TREE_PURPOSE (t
)
17994 && value
== TREE_VALUE (t
)
17995 && chain
== TREE_CHAIN (t
))
17997 RETURN (tree_cons (purpose
, value
, chain
));
18000 case COMPONENT_REF
:
18007 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18008 args
, complain
, in_decl
);
18009 /* Remember that there was a reference to this entity. */
18010 if (DECL_P (object
)
18011 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18012 RETURN (error_mark_node
);
18013 object_type
= TREE_TYPE (object
);
18015 member
= TREE_OPERAND (t
, 1);
18016 if (BASELINK_P (member
))
18017 member
= tsubst_baselink (member
,
18018 non_reference (TREE_TYPE (object
)),
18019 args
, complain
, in_decl
);
18021 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18022 if (member
== error_mark_node
)
18023 RETURN (error_mark_node
);
18025 if (TREE_CODE (member
) == FIELD_DECL
)
18027 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18028 if (TREE_CODE (r
) == COMPONENT_REF
)
18029 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18032 else if (type_dependent_expression_p (object
))
18033 /* We can't do much here. */;
18034 else if (!CLASS_TYPE_P (object_type
))
18036 if (scalarish_type_p (object_type
))
18038 tree s
= NULL_TREE
;
18039 tree dtor
= member
;
18041 if (TREE_CODE (dtor
) == SCOPE_REF
)
18043 s
= TREE_OPERAND (dtor
, 0);
18044 dtor
= TREE_OPERAND (dtor
, 1);
18046 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18048 dtor
= TREE_OPERAND (dtor
, 0);
18050 RETURN (finish_pseudo_destructor_expr
18051 (object
, s
, dtor
, input_location
));
18055 else if (TREE_CODE (member
) == SCOPE_REF
18056 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18058 /* Lookup the template functions now that we know what the
18060 tree scope
= TREE_OPERAND (member
, 0);
18061 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18062 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18063 member
= lookup_qualified_name (scope
, tmpl
,
18064 /*is_type_p=*/false,
18065 /*complain=*/false);
18066 if (BASELINK_P (member
))
18068 BASELINK_FUNCTIONS (member
)
18069 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18071 member
= (adjust_result_of_qualified_name_lookup
18072 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18077 qualified_name_lookup_error (scope
, tmpl
, member
,
18079 RETURN (error_mark_node
);
18082 else if (TREE_CODE (member
) == SCOPE_REF
18083 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18084 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18086 if (complain
& tf_error
)
18088 if (TYPE_P (TREE_OPERAND (member
, 0)))
18089 error ("%qT is not a class or namespace",
18090 TREE_OPERAND (member
, 0));
18092 error ("%qD is not a class or namespace",
18093 TREE_OPERAND (member
, 0));
18095 RETURN (error_mark_node
);
18098 r
= finish_class_member_access_expr (object
, member
,
18099 /*template_p=*/false,
18101 if (TREE_CODE (r
) == COMPONENT_REF
)
18102 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18107 RETURN (build_throw
18108 (RECUR (TREE_OPERAND (t
, 0))));
18112 vec
<constructor_elt
, va_gc
> *n
;
18113 constructor_elt
*ce
;
18114 unsigned HOST_WIDE_INT idx
;
18115 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18116 bool process_index_p
;
18118 bool need_copy_p
= false;
18121 if (type
== error_mark_node
)
18122 RETURN (error_mark_node
);
18124 /* We do not want to process the index of aggregate
18125 initializers as they are identifier nodes which will be
18126 looked up by digest_init. */
18127 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18129 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18130 newlen
= vec_safe_length (n
);
18131 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18133 if (ce
->index
&& process_index_p
18134 /* An identifier index is looked up in the type
18135 being initialized, not the current scope. */
18136 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18137 ce
->index
= RECUR (ce
->index
);
18139 if (PACK_EXPANSION_P (ce
->value
))
18141 /* Substitute into the pack expansion. */
18142 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18145 if (ce
->value
== error_mark_node
18146 || PACK_EXPANSION_P (ce
->value
))
18148 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18149 /* Just move the argument into place. */
18150 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18153 /* Update the length of the final CONSTRUCTOR
18154 arguments vector, and note that we will need to
18156 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18157 need_copy_p
= true;
18161 ce
->value
= RECUR (ce
->value
);
18166 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18168 vec_alloc (n
, newlen
);
18169 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18171 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18173 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18174 for (i
= 0; i
< len
; ++i
)
18175 CONSTRUCTOR_APPEND_ELT (n
, 0,
18176 TREE_VEC_ELT (ce
->value
, i
));
18179 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18183 r
= build_constructor (init_list_type_node
, n
);
18184 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18186 if (TREE_HAS_CONSTRUCTOR (t
))
18188 fcl_t cl
= fcl_functional
;
18189 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18191 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18194 TREE_TYPE (r
) = type
;
18200 tree operand_0
= TREE_OPERAND (t
, 0);
18201 if (TYPE_P (operand_0
))
18203 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18204 RETURN (get_typeid (operand_0
, complain
));
18208 operand_0
= RECUR (operand_0
);
18209 RETURN (build_typeid (operand_0
, complain
));
18216 else if (DECL_PACK_P (t
))
18218 /* We don't build decls for an instantiation of a
18219 variadic capture proxy, we instantiate the elements
18221 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
18222 return RECUR (DECL_VALUE_EXPR (t
));
18228 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18229 /* ??? We're doing a subset of finish_id_expression here. */
18231 && !processing_template_decl
18232 && !cp_unevaluated_operand
18233 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18234 && CP_DECL_THREAD_LOCAL_P (r
))
18236 if (tree wrap
= get_tls_wrapper_fn (r
))
18237 /* Replace an evaluated use of the thread_local variable with
18238 a call to its wrapper. */
18239 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18241 else if (outer_automatic_var_p (r
))
18242 r
= process_outer_var_ref (r
, complain
);
18244 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18245 /* If the original type was a reference, we'll be wrapped in
18246 the appropriate INDIRECT_REF. */
18247 r
= convert_from_reference (r
);
18253 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18254 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18255 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18258 case OFFSETOF_EXPR
:
18261 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18262 in_decl
, /*function_p=*/false,
18263 /*integral_constant_expression_p=*/false);
18264 RETURN (finish_offsetof (object_ptr
,
18265 RECUR (TREE_OPERAND (t
, 0)),
18266 EXPR_LOCATION (t
)));
18269 case ADDRESSOF_EXPR
:
18270 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18271 RECUR (TREE_OPERAND (t
, 0)), complain
));
18275 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18276 complain
, in_decl
);
18278 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18279 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18280 type2
= RECUR (type2
);
18282 type2
= tsubst (type2
, args
, complain
, in_decl
);
18284 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18289 tree old_stmt_expr
= cur_stmt_expr
;
18290 tree stmt_expr
= begin_stmt_expr ();
18292 cur_stmt_expr
= stmt_expr
;
18293 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18294 integral_constant_expression_p
);
18295 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18296 cur_stmt_expr
= old_stmt_expr
;
18298 /* If the resulting list of expression statement is empty,
18299 fold it further into void_node. */
18300 if (empty_expr_stmt_p (stmt_expr
))
18301 stmt_expr
= void_node
;
18303 RETURN (stmt_expr
);
18308 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18310 RETURN (build_lambda_object (r
));
18314 /* We can get here for a constant initializer of non-dependent type.
18315 FIXME stop folding in cp_parser_initializer_clause. */
18317 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18322 case TRANSACTION_EXPR
:
18323 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18324 integral_constant_expression_p
));
18327 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18329 case VEC_PERM_EXPR
:
18331 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18332 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18333 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18334 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18338 case REQUIRES_EXPR
:
18339 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18341 case NON_LVALUE_EXPR
:
18342 case VIEW_CONVERT_EXPR
:
18343 /* We should only see these for location wrapper nodes, or within
18344 instantiate_non_dependent_expr (when args is NULL_TREE). */
18345 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18346 if (location_wrapper_p (t
))
18347 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18348 EXPR_LOCATION (t
)));
18352 /* Handle Objective-C++ constructs, if appropriate. */
18355 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18356 in_decl
, /*function_p=*/false);
18360 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18366 input_location
= loc
;
18370 /* Verify that the instantiated ARGS are valid. For type arguments,
18371 make sure that the type's linkage is ok. For non-type arguments,
18372 make sure they are constants if they are integral or enumerations.
18373 Emit an error under control of COMPLAIN, and return TRUE on error. */
18376 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18378 if (dependent_template_arg_p (t
))
18380 if (ARGUMENT_PACK_P (t
))
18382 tree vec
= ARGUMENT_PACK_ARGS (t
);
18383 int len
= TREE_VEC_LENGTH (vec
);
18384 bool result
= false;
18387 for (i
= 0; i
< len
; ++i
)
18388 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18392 else if (TYPE_P (t
))
18394 /* [basic.link]: A name with no linkage (notably, the name
18395 of a class or enumeration declared in a local scope)
18396 shall not be used to declare an entity with linkage.
18397 This implies that names with no linkage cannot be used as
18400 DR 757 relaxes this restriction for C++0x. */
18401 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18402 : no_linkage_check (t
, /*relaxed_p=*/false));
18406 /* DR 488 makes use of a type with no linkage cause
18407 type deduction to fail. */
18408 if (complain
& tf_error
)
18410 if (TYPE_UNNAMED_P (nt
))
18411 error ("%qT is/uses unnamed type", t
);
18413 error ("template argument for %qD uses local type %qT",
18418 /* In order to avoid all sorts of complications, we do not
18419 allow variably-modified types as template arguments. */
18420 else if (variably_modified_type_p (t
, NULL_TREE
))
18422 if (complain
& tf_error
)
18423 error ("%qT is a variably modified type", t
);
18427 /* Class template and alias template arguments should be OK. */
18428 else if (DECL_TYPE_TEMPLATE_P (t
))
18430 /* A non-type argument of integral or enumerated type must be a
18432 else if (TREE_TYPE (t
)
18433 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18434 && !REFERENCE_REF_P (t
)
18435 && !TREE_CONSTANT (t
))
18437 if (complain
& tf_error
)
18438 error ("integral expression %qE is not constant", t
);
18445 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18447 int ix
, len
= DECL_NTPARMS (tmpl
);
18448 bool result
= false;
18450 for (ix
= 0; ix
!= len
; ix
++)
18452 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18455 if (result
&& (complain
& tf_error
))
18456 error (" trying to instantiate %qD", tmpl
);
18460 /* We're out of SFINAE context now, so generate diagnostics for the access
18461 errors we saw earlier when instantiating D from TMPL and ARGS. */
18464 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18466 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18467 tree type
= TREE_TYPE (pattern
);
18468 location_t loc
= input_location
;
18470 push_access_scope (d
);
18471 push_deferring_access_checks (dk_no_deferred
);
18472 input_location
= DECL_SOURCE_LOCATION (pattern
);
18473 tsubst (type
, args
, tf_warning_or_error
, d
);
18474 input_location
= loc
;
18475 pop_deferring_access_checks ();
18476 pop_access_scope (d
);
18479 /* Instantiate the indicated variable, function, or alias template TMPL with
18480 the template arguments in TARG_PTR. */
18483 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18485 tree targ_ptr
= orig_args
;
18489 bool access_ok
= true;
18491 if (tmpl
== error_mark_node
)
18492 return error_mark_node
;
18494 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18496 /* If this function is a clone, handle it specially. */
18497 if (DECL_CLONED_FUNCTION_P (tmpl
))
18502 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18503 DECL_CLONED_FUNCTION. */
18504 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18505 targ_ptr
, complain
);
18506 if (spec
== error_mark_node
)
18507 return error_mark_node
;
18509 /* Look for the clone. */
18510 FOR_EACH_CLONE (clone
, spec
)
18511 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18513 /* We should always have found the clone by now. */
18514 gcc_unreachable ();
18518 if (targ_ptr
== error_mark_node
)
18519 return error_mark_node
;
18521 /* Check to see if we already have this specialization. */
18522 gen_tmpl
= most_general_template (tmpl
);
18523 if (TMPL_ARGS_DEPTH (targ_ptr
)
18524 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18525 /* targ_ptr only has the innermost template args, so add the outer ones
18526 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18527 the case of a non-dependent call within a template definition). */
18528 targ_ptr
= (add_outermost_template_args
18529 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18532 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18533 but it doesn't seem to be on the hot path. */
18534 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18536 gcc_assert (tmpl
== gen_tmpl
18537 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18539 || fndecl
== NULL_TREE
);
18541 if (spec
!= NULL_TREE
)
18543 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18545 if (complain
& tf_error
)
18546 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18547 return error_mark_node
;
18552 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18554 return error_mark_node
;
18556 /* We are building a FUNCTION_DECL, during which the access of its
18557 parameters and return types have to be checked. However this
18558 FUNCTION_DECL which is the desired context for access checking
18559 is not built yet. We solve this chicken-and-egg problem by
18560 deferring all checks until we have the FUNCTION_DECL. */
18561 push_deferring_access_checks (dk_deferred
);
18563 /* Instantiation of the function happens in the context of the function
18564 template, not the context of the overload resolution we're doing. */
18565 push_to_top_level ();
18566 /* If there are dependent arguments, e.g. because we're doing partial
18567 ordering, make sure processing_template_decl stays set. */
18568 if (uses_template_parms (targ_ptr
))
18569 ++processing_template_decl
;
18570 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18572 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18573 complain
, gen_tmpl
, true);
18574 push_nested_class (ctx
);
18577 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18579 fndecl
= NULL_TREE
;
18580 if (VAR_P (pattern
))
18582 /* We need to determine if we're using a partial or explicit
18583 specialization now, because the type of the variable could be
18585 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18586 tree elt
= most_specialized_partial_spec (tid
, complain
);
18587 if (elt
== error_mark_node
)
18588 pattern
= error_mark_node
;
18591 tree partial_tmpl
= TREE_VALUE (elt
);
18592 tree partial_args
= TREE_PURPOSE (elt
);
18593 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18594 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18598 /* Substitute template parameters to obtain the specialization. */
18599 if (fndecl
== NULL_TREE
)
18600 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18601 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18602 pop_nested_class ();
18603 pop_from_top_level ();
18605 if (fndecl
== error_mark_node
)
18607 pop_deferring_access_checks ();
18608 return error_mark_node
;
18611 /* The DECL_TI_TEMPLATE should always be the immediate parent
18612 template, not the most general template. */
18613 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18614 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18616 /* Now we know the specialization, compute access previously
18617 deferred. Do no access control for inheriting constructors,
18618 as we already checked access for the inherited constructor. */
18619 if (!(flag_new_inheriting_ctors
18620 && DECL_INHERITED_CTOR (fndecl
)))
18622 push_access_scope (fndecl
);
18623 if (!perform_deferred_access_checks (complain
))
18625 pop_access_scope (fndecl
);
18627 pop_deferring_access_checks ();
18629 /* If we've just instantiated the main entry point for a function,
18630 instantiate all the alternate entry points as well. We do this
18631 by cloning the instantiation of the main entry point, not by
18632 instantiating the template clones. */
18633 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18634 clone_function_decl (fndecl
, /*update_methods=*/false);
18638 if (!(complain
& tf_error
))
18640 /* Remember to reinstantiate when we're out of SFINAE so the user
18641 can see the errors. */
18642 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18644 return error_mark_node
;
18649 /* Wrapper for instantiate_template_1. */
18652 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18655 timevar_push (TV_TEMPLATE_INST
);
18656 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18657 timevar_pop (TV_TEMPLATE_INST
);
18661 /* Instantiate the alias template TMPL with ARGS. Also push a template
18662 instantiation level, which instantiate_template doesn't do because
18663 functions and variables have sufficient context established by the
18667 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18669 struct pending_template
*old_last_pend
= last_pending_template
;
18670 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18671 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18672 return error_mark_node
;
18673 tree tinst
= build_tree_list (tmpl
, args
);
18674 if (!push_tinst_level (tinst
))
18677 return error_mark_node
;
18681 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18682 args
, tmpl
, complain
,
18683 /*require_all_args=*/true,
18684 /*use_default_args=*/true);
18686 tree r
= instantiate_template (tmpl
, args
, complain
);
18687 pop_tinst_level ();
18688 /* We can't free this if a pending_template entry or last_error_tinst_level
18689 is pointing at it. */
18690 if (last_pending_template
== old_last_pend
18691 && last_error_tinst_level
== old_error_tinst
)
18697 /* PARM is a template parameter pack for FN. Returns true iff
18698 PARM is used in a deducible way in the argument list of FN. */
18701 pack_deducible_p (tree parm
, tree fn
)
18703 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18704 for (; t
; t
= TREE_CHAIN (t
))
18706 tree type
= TREE_VALUE (t
);
18708 if (!PACK_EXPANSION_P (type
))
18710 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18711 packs
; packs
= TREE_CHAIN (packs
))
18712 if (template_args_equal (TREE_VALUE (packs
), parm
))
18714 /* The template parameter pack is used in a function parameter
18715 pack. If this is the end of the parameter list, the
18716 template parameter pack is deducible. */
18717 if (TREE_CHAIN (t
) == void_list_node
)
18720 /* Otherwise, not. Well, it could be deduced from
18721 a non-pack parameter, but doing so would end up with
18722 a deduction mismatch, so don't bother. */
18726 /* The template parameter pack isn't used in any function parameter
18727 packs, but it might be used deeper, e.g. tuple<Args...>. */
18731 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18732 NARGS elements of the arguments that are being used when calling
18733 it. TARGS is a vector into which the deduced template arguments
18736 Returns either a FUNCTION_DECL for the matching specialization of FN or
18737 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18738 true, diagnostics will be printed to explain why it failed.
18740 If FN is a conversion operator, or we are trying to produce a specific
18741 specialization, RETURN_TYPE is the return type desired.
18743 The EXPLICIT_TARGS are explicit template arguments provided via a
18746 The parameter STRICT is one of:
18749 We are deducing arguments for a function call, as in
18750 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18751 deducing arguments for a call to the result of a conversion
18752 function template, as in [over.call.object].
18755 We are deducing arguments for a conversion function, as in
18756 [temp.deduct.conv].
18759 We are deducing arguments when doing an explicit instantiation
18760 as in [temp.explicit], when determining an explicit specialization
18761 as in [temp.expl.spec], or when taking the address of a function
18762 template, as in [temp.deduct.funcaddr]. */
18765 fn_type_unification (tree fn
,
18766 tree explicit_targs
,
18769 unsigned int nargs
,
18771 unification_kind_t strict
,
18778 tree decl
= NULL_TREE
;
18779 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18781 static int deduction_depth
;
18782 struct pending_template
*old_last_pend
= last_pending_template
;
18783 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18786 if (flag_new_inheriting_ctors
)
18787 fn
= strip_inheriting_ctors (fn
);
18789 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18791 tree r
= error_mark_node
;
18793 tree full_targs
= targs
;
18794 if (TMPL_ARGS_DEPTH (targs
)
18795 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18796 full_targs
= (add_outermost_template_args
18797 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18801 complain
|= tf_decltype
;
18803 /* In C++0x, it's possible to have a function template whose type depends
18804 on itself recursively. This is most obvious with decltype, but can also
18805 occur with enumeration scope (c++/48969). So we need to catch infinite
18806 recursion and reject the substitution at deduction time; this function
18807 will return error_mark_node for any repeated substitution.
18809 This also catches excessive recursion such as when f<N> depends on
18810 f<N-1> across all integers, and returns error_mark_node for all the
18811 substitutions back up to the initial one.
18813 This is, of course, not reentrant. */
18814 if (excessive_deduction_depth
)
18815 return error_mark_node
;
18816 tinst
= build_tree_list (fn
, NULL_TREE
);
18819 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18821 fntype
= TREE_TYPE (fn
);
18822 if (explicit_targs
)
18826 The specified template arguments must match the template
18827 parameters in kind (i.e., type, nontype, template), and there
18828 must not be more arguments than there are parameters;
18829 otherwise type deduction fails.
18831 Nontype arguments must match the types of the corresponding
18832 nontype template parameters, or must be convertible to the
18833 types of the corresponding nontype parameters as specified in
18834 _temp.arg.nontype_, otherwise type deduction fails.
18836 All references in the function type of the function template
18837 to the corresponding template parameters are replaced by the
18838 specified template argument values. If a substitution in a
18839 template parameter or in the function type of the function
18840 template results in an invalid type, type deduction fails. */
18841 int i
, len
= TREE_VEC_LENGTH (tparms
);
18842 location_t loc
= input_location
;
18843 bool incomplete
= false;
18845 if (explicit_targs
== error_mark_node
)
18848 if (TMPL_ARGS_DEPTH (explicit_targs
)
18849 < TMPL_ARGS_DEPTH (full_targs
))
18850 explicit_targs
= add_outermost_template_args (full_targs
,
18853 /* Adjust any explicit template arguments before entering the
18854 substitution context. */
18856 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18858 /*require_all_args=*/false,
18859 /*use_default_args=*/false));
18860 if (explicit_targs
== error_mark_node
)
18863 /* Substitute the explicit args into the function type. This is
18864 necessary so that, for instance, explicitly declared function
18865 arguments can match null pointed constants. If we were given
18866 an incomplete set of explicit args, we must not do semantic
18867 processing during substitution as we could create partial
18869 for (i
= 0; i
< len
; i
++)
18871 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18872 bool parameter_pack
= false;
18873 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18875 /* Dig out the actual parm. */
18876 if (TREE_CODE (parm
) == TYPE_DECL
18877 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18879 parm
= TREE_TYPE (parm
);
18880 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18882 else if (TREE_CODE (parm
) == PARM_DECL
)
18884 parm
= DECL_INITIAL (parm
);
18885 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18888 if (!parameter_pack
&& targ
== NULL_TREE
)
18889 /* No explicit argument for this template parameter. */
18892 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18894 /* Mark the argument pack as "incomplete". We could
18895 still deduce more arguments during unification.
18896 We remove this mark in type_unification_real. */
18899 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18900 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18901 = ARGUMENT_PACK_ARGS (targ
);
18904 /* We have some incomplete argument packs. */
18909 TREE_VALUE (tinst
) = explicit_targs
;
18910 if (!push_tinst_level (tinst
))
18912 excessive_deduction_depth
= true;
18915 processing_template_decl
+= incomplete
;
18916 input_location
= DECL_SOURCE_LOCATION (fn
);
18917 /* Ignore any access checks; we'll see them again in
18918 instantiate_template and they might have the wrong
18919 access path at this point. */
18920 push_deferring_access_checks (dk_deferred
);
18921 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18922 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18923 pop_deferring_access_checks ();
18924 input_location
= loc
;
18925 processing_template_decl
-= incomplete
;
18926 pop_tinst_level ();
18928 if (fntype
== error_mark_node
)
18931 /* Place the explicitly specified arguments in TARGS. */
18932 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18933 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18934 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18937 /* Never do unification on the 'this' parameter. */
18938 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18940 if (return_type
&& strict
== DEDUCE_CALL
)
18942 /* We're deducing for a call to the result of a template conversion
18943 function. The parms we really want are in return_type. */
18944 if (POINTER_TYPE_P (return_type
))
18945 return_type
= TREE_TYPE (return_type
);
18946 parms
= TYPE_ARG_TYPES (return_type
);
18948 else if (return_type
)
18952 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18953 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18954 new_args
[0] = return_type
;
18955 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18960 /* We allow incomplete unification without an error message here
18961 because the standard doesn't seem to explicitly prohibit it. Our
18962 callers must be ready to deal with unification failures in any
18965 TREE_VALUE (tinst
) = targs
;
18966 /* If we aren't explaining yet, push tinst context so we can see where
18967 any errors (e.g. from class instantiations triggered by instantiation
18968 of default template arguments) come from. If we are explaining, this
18969 context is redundant. */
18970 if (!explain_p
&& !push_tinst_level (tinst
))
18972 excessive_deduction_depth
= true;
18976 /* type_unification_real will pass back any access checks from default
18977 template argument substitution. */
18978 vec
<deferred_access_check
, va_gc
> *checks
;
18981 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18982 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18983 strict
, flags
, &checks
, explain_p
);
18985 pop_tinst_level ();
18989 /* Now that we have bindings for all of the template arguments,
18990 ensure that the arguments deduced for the template template
18991 parameters have compatible template parameter lists. We cannot
18992 check this property before we have deduced all template
18993 arguments, because the template parameter types of a template
18994 template parameter might depend on prior template parameters
18995 deduced after the template template parameter. The following
18996 ill-formed example illustrates this issue:
18998 template<typename T, template<T> class C> void f(C<5>, T);
19000 template<int N> struct X {};
19003 f(X<5>(), 5l); // error: template argument deduction fails
19006 The template parameter list of 'C' depends on the template type
19007 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19008 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19009 time that we deduce 'C'. */
19010 if (!template_template_parm_bindings_ok_p
19011 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19013 unify_inconsistent_template_template_parameters (explain_p
);
19017 /* All is well so far. Now, check:
19021 When all template arguments have been deduced, all uses of
19022 template parameters in nondeduced contexts are replaced with
19023 the corresponding deduced argument values. If the
19024 substitution results in an invalid type, as described above,
19025 type deduction fails. */
19026 TREE_VALUE (tinst
) = targs
;
19027 if (!push_tinst_level (tinst
))
19029 excessive_deduction_depth
= true;
19033 /* Also collect access checks from the instantiation. */
19034 reopen_deferring_access_checks (checks
);
19036 decl
= instantiate_template (fn
, targs
, complain
);
19038 checks
= get_deferred_access_checks ();
19039 pop_deferring_access_checks ();
19041 pop_tinst_level ();
19043 if (decl
== error_mark_node
)
19046 /* Now perform any access checks encountered during substitution. */
19047 push_access_scope (decl
);
19048 ok
= perform_access_checks (checks
, complain
);
19049 pop_access_scope (decl
);
19053 /* If we're looking for an exact match, check that what we got
19054 is indeed an exact match. It might not be if some template
19055 parameters are used in non-deduced contexts. But don't check
19056 for an exact match if we have dependent template arguments;
19057 in that case we're doing partial ordering, and we already know
19058 that we have two candidates that will provide the actual type. */
19059 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19061 tree substed
= TREE_TYPE (decl
);
19065 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19067 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19068 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19069 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19071 unify_type_mismatch (explain_p
, args
[i
],
19072 TREE_VALUE (sarg
));
19077 /* After doing deduction with the inherited constructor, actually return an
19078 instantiation of the inheriting constructor. */
19080 decl
= instantiate_template (orig_fn
, targs
, complain
);
19086 if (excessive_deduction_depth
)
19088 if (deduction_depth
== 0)
19089 /* Reset once we're all the way out. */
19090 excessive_deduction_depth
= false;
19093 /* We can't free this if a pending_template entry or last_error_tinst_level
19094 is pointing at it. */
19095 if (last_pending_template
== old_last_pend
19096 && last_error_tinst_level
== old_error_tinst
)
19102 /* Adjust types before performing type deduction, as described in
19103 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19104 sections are symmetric. PARM is the type of a function parameter
19105 or the return type of the conversion function. ARG is the type of
19106 the argument passed to the call, or the type of the value
19107 initialized with the result of the conversion function.
19108 ARG_EXPR is the original argument expression, which may be null. */
19111 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19124 /* Swap PARM and ARG throughout the remainder of this
19125 function; the handling is precisely symmetric since PARM
19126 will initialize ARG rather than vice versa. */
19127 std::swap (parm
, arg
);
19131 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19132 too, but here handle it by stripping the reference from PARM
19133 rather than by adding it to ARG. */
19134 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19135 && TYPE_REF_IS_RVALUE (*parm
)
19136 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19137 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19138 && TREE_CODE (*arg
) == REFERENCE_TYPE
19139 && !TYPE_REF_IS_RVALUE (*arg
))
19140 *parm
= TREE_TYPE (*parm
);
19141 /* Nothing else to do in this case. */
19145 gcc_unreachable ();
19148 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19150 /* [temp.deduct.call]
19152 If P is not a reference type:
19154 --If A is an array type, the pointer type produced by the
19155 array-to-pointer standard conversion (_conv.array_) is
19156 used in place of A for type deduction; otherwise,
19158 --If A is a function type, the pointer type produced by
19159 the function-to-pointer standard conversion
19160 (_conv.func_) is used in place of A for type deduction;
19163 --If A is a cv-qualified type, the top level
19164 cv-qualifiers of A's type are ignored for type
19166 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19167 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19168 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19169 *arg
= build_pointer_type (*arg
);
19171 *arg
= TYPE_MAIN_VARIANT (*arg
);
19174 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19175 reference to a cv-unqualified template parameter that does not represent a
19176 template parameter of a class template (during class template argument
19177 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19178 an lvalue, the type "lvalue reference to A" is used in place of A for type
19180 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19181 && TYPE_REF_IS_RVALUE (*parm
)
19182 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19183 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19184 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19185 && (arg_expr
? lvalue_p (arg_expr
)
19186 /* try_one_overload doesn't provide an arg_expr, but
19187 functions are always lvalues. */
19188 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19189 *arg
= build_reference_type (*arg
);
19191 /* [temp.deduct.call]
19193 If P is a cv-qualified type, the top level cv-qualifiers
19194 of P's type are ignored for type deduction. If P is a
19195 reference type, the type referred to by P is used for
19197 *parm
= TYPE_MAIN_VARIANT (*parm
);
19198 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19200 *parm
= TREE_TYPE (*parm
);
19201 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19204 /* DR 322. For conversion deduction, remove a reference type on parm
19205 too (which has been swapped into ARG). */
19206 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19207 *arg
= TREE_TYPE (*arg
);
19212 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19213 template which does contain any deducible template parameters; check if
19214 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19215 unify_one_argument. */
19218 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19219 int flags
, bool explain_p
)
19224 type
= TREE_TYPE (arg
);
19228 if (same_type_p (parm
, type
))
19229 return unify_success (explain_p
);
19231 if (strict
== DEDUCE_CONV
)
19233 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19234 explain_p
? tf_warning_or_error
: tf_none
))
19235 return unify_success (explain_p
);
19237 else if (strict
!= DEDUCE_EXACT
)
19239 if (can_convert_arg (parm
, type
,
19240 TYPE_P (arg
) ? NULL_TREE
: arg
,
19241 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19242 return unify_success (explain_p
);
19245 if (strict
== DEDUCE_EXACT
)
19246 return unify_type_mismatch (explain_p
, parm
, arg
);
19248 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19251 static bool uses_deducible_template_parms (tree type
);
19253 /* Returns true iff the expression EXPR is one from which a template
19254 argument can be deduced. In other words, if it's an undecorated
19255 use of a template non-type parameter. */
19258 deducible_expression (tree expr
)
19260 /* Strip implicit conversions. */
19261 while (CONVERT_EXPR_P (expr
))
19262 expr
= TREE_OPERAND (expr
, 0);
19263 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19266 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19267 deducible way; that is, if it has a max value of <PARM> - 1. */
19270 deducible_array_bound (tree domain
)
19272 if (domain
== NULL_TREE
)
19275 tree max
= TYPE_MAX_VALUE (domain
);
19276 if (TREE_CODE (max
) != MINUS_EXPR
)
19279 return deducible_expression (TREE_OPERAND (max
, 0));
19282 /* Returns true iff the template arguments ARGS use a template parameter
19283 in a deducible way. */
19286 deducible_template_args (tree args
)
19288 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19291 tree elt
= TREE_VEC_ELT (args
, i
);
19292 if (ARGUMENT_PACK_P (elt
))
19293 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19296 if (PACK_EXPANSION_P (elt
))
19297 elt
= PACK_EXPANSION_PATTERN (elt
);
19298 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19300 else if (TYPE_P (elt
))
19301 deducible
= uses_deducible_template_parms (elt
);
19303 deducible
= deducible_expression (elt
);
19311 /* Returns true iff TYPE contains any deducible references to template
19312 parameters, as per 14.8.2.5. */
19315 uses_deducible_template_parms (tree type
)
19317 if (PACK_EXPANSION_P (type
))
19318 type
= PACK_EXPANSION_PATTERN (type
);
19325 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19326 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19332 if (POINTER_TYPE_P (type
))
19333 return uses_deducible_template_parms (TREE_TYPE (type
));
19335 /* T[integer-constant ]
19337 if (TREE_CODE (type
) == ARRAY_TYPE
)
19338 return (uses_deducible_template_parms (TREE_TYPE (type
))
19339 || deducible_array_bound (TYPE_DOMAIN (type
)));
19351 if (TYPE_PTRMEM_P (type
))
19352 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19353 || (uses_deducible_template_parms
19354 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19356 /* template-name <T> (where template-name refers to a class template)
19357 template-name <i> (where template-name refers to a class template) */
19358 if (CLASS_TYPE_P (type
)
19359 && CLASSTYPE_TEMPLATE_INFO (type
)
19360 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19361 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19362 (CLASSTYPE_TI_ARGS (type
)));
19367 if (TREE_CODE (type
) == FUNCTION_TYPE
19368 || TREE_CODE (type
) == METHOD_TYPE
)
19370 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19372 tree parm
= TYPE_ARG_TYPES (type
);
19373 if (TREE_CODE (type
) == METHOD_TYPE
)
19374 parm
= TREE_CHAIN (parm
);
19375 for (; parm
; parm
= TREE_CHAIN (parm
))
19376 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19383 /* Subroutine of type_unification_real and unify_pack_expansion to
19384 handle unification of a single P/A pair. Parameters are as
19385 for those functions. */
19388 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19389 int subr
, unification_kind_t strict
,
19392 tree arg_expr
= NULL_TREE
;
19395 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19396 return unify_invalid (explain_p
);
19397 if (arg
== unknown_type_node
)
19398 /* We can't deduce anything from this, but we might get all the
19399 template args from other function args. */
19400 return unify_success (explain_p
);
19402 /* Implicit conversions (Clause 4) will be performed on a function
19403 argument to convert it to the type of the corresponding function
19404 parameter if the parameter type contains no template-parameters that
19405 participate in template argument deduction. */
19406 if (strict
!= DEDUCE_EXACT
19407 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19408 /* For function parameters with no deducible template parameters,
19409 just return. We'll check non-dependent conversions later. */
19410 return unify_success (explain_p
);
19415 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19416 | UNIFY_ALLOW_MORE_CV_QUAL
19417 | UNIFY_ALLOW_DERIVED
);
19421 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19425 arg_strict
= UNIFY_ALLOW_NONE
;
19429 gcc_unreachable ();
19432 /* We only do these transformations if this is the top-level
19433 parameter_type_list in a call or declaration matching; in other
19434 situations (nested function declarators, template argument lists) we
19435 won't be comparing a type to an expression, and we don't do any type
19441 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19442 if (type_unknown_p (arg
))
19444 /* [temp.deduct.type] A template-argument can be
19445 deduced from a pointer to function or pointer
19446 to member function argument if the set of
19447 overloaded functions does not contain function
19448 templates and at most one of a set of
19449 overloaded functions provides a unique
19451 resolve_overloaded_unification (tparms
, targs
, parm
,
19453 arg_strict
, explain_p
);
19454 /* If a unique match was not found, this is a
19455 non-deduced context, so we still succeed. */
19456 return unify_success (explain_p
);
19460 arg
= unlowered_expr_type (arg
);
19461 if (arg
== error_mark_node
)
19462 return unify_invalid (explain_p
);
19466 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19469 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19470 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19471 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19473 /* For deduction from an init-list we need the actual list. */
19474 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19476 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19479 /* for_each_template_parm callback that always returns 0. */
19482 zero_r (tree
, void *)
19487 /* for_each_template_parm any_fn callback to handle deduction of a template
19488 type argument from the type of an array bound. */
19491 array_deduction_r (tree t
, void *data
)
19493 tree_pair_p d
= (tree_pair_p
)data
;
19494 tree
&tparms
= d
->purpose
;
19495 tree
&targs
= d
->value
;
19497 if (TREE_CODE (t
) == ARRAY_TYPE
)
19498 if (tree dom
= TYPE_DOMAIN (t
))
19499 if (tree max
= TYPE_MAX_VALUE (dom
))
19501 if (TREE_CODE (max
) == MINUS_EXPR
)
19502 max
= TREE_OPERAND (max
, 0);
19503 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19504 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19505 UNIFY_ALLOW_NONE
, /*explain*/false);
19508 /* Keep walking. */
19512 /* Try to deduce any not-yet-deduced template type arguments from the type of
19513 an array bound. This is handled separately from unify because 14.8.2.5 says
19514 "The type of a type parameter is only deduced from an array bound if it is
19515 not otherwise deduced." */
19518 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19520 tree_pair_s data
= { tparms
, targs
};
19521 hash_set
<tree
> visited
;
19522 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19523 /*nondeduced*/false, array_deduction_r
);
19526 /* Most parms like fn_type_unification.
19528 If SUBR is 1, we're being called recursively (to unify the
19529 arguments of a function or method parameter of a function
19532 CHECKS is a pointer to a vector of access checks encountered while
19533 substituting default template arguments. */
19536 type_unification_real (tree tparms
,
19540 unsigned int xnargs
,
19542 unification_kind_t strict
,
19544 vec
<deferred_access_check
, va_gc
> **checks
,
19549 int ntparms
= TREE_VEC_LENGTH (tparms
);
19550 int saw_undeduced
= 0;
19553 unsigned int nargs
;
19556 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19557 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19558 gcc_assert (ntparms
> 0);
19560 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19562 /* Reset the number of non-defaulted template arguments contained
19564 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19572 while (parms
&& parms
!= void_list_node
19575 parm
= TREE_VALUE (parms
);
19577 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19578 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19579 /* For a function parameter pack that occurs at the end of the
19580 parameter-declaration-list, the type A of each remaining
19581 argument of the call is compared with the type P of the
19582 declarator-id of the function parameter pack. */
19585 parms
= TREE_CHAIN (parms
);
19587 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19588 /* For a function parameter pack that does not occur at the
19589 end of the parameter-declaration-list, the type of the
19590 parameter pack is a non-deduced context. */
19596 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19602 && parms
!= void_list_node
19603 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19605 /* Unify the remaining arguments with the pack expansion type. */
19607 tree parmvec
= make_tree_vec (1);
19609 /* Allocate a TREE_VEC and copy in all of the arguments */
19610 argvec
= make_tree_vec (nargs
- ia
);
19611 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19612 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19614 /* Copy the parameter into parmvec. */
19615 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19616 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19617 /*subr=*/subr
, explain_p
))
19620 /* Advance to the end of the list of parameters. */
19621 parms
= TREE_CHAIN (parms
);
19624 /* Fail if we've reached the end of the parm list, and more args
19625 are present, and the parm list isn't variadic. */
19626 if (ia
< nargs
&& parms
== void_list_node
)
19627 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19628 /* Fail if parms are left and they don't have default values and
19629 they aren't all deduced as empty packs (c++/57397). This is
19630 consistent with sufficient_parms_p. */
19631 if (parms
&& parms
!= void_list_node
19632 && TREE_PURPOSE (parms
) == NULL_TREE
)
19634 unsigned int count
= nargs
;
19639 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19642 p
= TREE_CHAIN (p
);
19644 while (p
&& p
!= void_list_node
);
19645 if (count
!= nargs
)
19646 return unify_too_few_arguments (explain_p
, ia
, count
,
19652 tsubst_flags_t complain
= (explain_p
19653 ? tf_warning_or_error
19655 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19657 for (i
= 0; i
< ntparms
; i
++)
19659 tree targ
= TREE_VEC_ELT (targs
, i
);
19660 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19662 /* Clear the "incomplete" flags on all argument packs now so that
19663 substituting them into later default arguments works. */
19664 if (targ
&& ARGUMENT_PACK_P (targ
))
19666 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19667 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19670 if (targ
|| tparm
== error_mark_node
)
19672 tparm
= TREE_VALUE (tparm
);
19674 if (TREE_CODE (tparm
) == TYPE_DECL
19675 && !tried_array_deduction
)
19677 try_array_deduction (tparms
, targs
, xparms
);
19678 tried_array_deduction
= true;
19679 if (TREE_VEC_ELT (targs
, i
))
19683 /* If this is an undeduced nontype parameter that depends on
19684 a type parameter, try another pass; its type may have been
19685 deduced from a later argument than the one from which
19686 this parameter can be deduced. */
19687 if (TREE_CODE (tparm
) == PARM_DECL
19688 && uses_template_parms (TREE_TYPE (tparm
))
19689 && saw_undeduced
< 2)
19695 /* Core issue #226 (C++0x) [temp.deduct]:
19697 If a template argument has not been deduced, its
19698 default template argument, if any, is used.
19700 When we are in C++98 mode, TREE_PURPOSE will either
19701 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19702 to explicitly check cxx_dialect here. */
19703 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19704 /* OK, there is a default argument. Wait until after the
19705 conversion check to do substitution. */
19708 /* If the type parameter is a parameter pack, then it will
19709 be deduced to an empty parameter pack. */
19710 if (template_parameter_pack_p (tparm
))
19714 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19716 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19717 TREE_CONSTANT (arg
) = 1;
19720 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19722 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19724 TREE_VEC_ELT (targs
, i
) = arg
;
19728 return unify_parameter_deduction_failure (explain_p
, tparm
);
19731 /* DR 1391: All parameters have args, now check non-dependent parms for
19733 if (saw_undeduced
< 2)
19734 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19735 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19737 parm
= TREE_VALUE (parms
);
19739 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19740 && (!TREE_CHAIN (parms
)
19741 || TREE_CHAIN (parms
) == void_list_node
))
19742 /* For a function parameter pack that occurs at the end of the
19743 parameter-declaration-list, the type A of each remaining
19744 argument of the call is compared with the type P of the
19745 declarator-id of the function parameter pack. */
19748 parms
= TREE_CHAIN (parms
);
19750 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19751 /* For a function parameter pack that does not occur at the
19752 end of the parameter-declaration-list, the type of the
19753 parameter pack is a non-deduced context. */
19759 if (uses_template_parms (parm
))
19761 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19766 /* Now substitute into the default template arguments. */
19767 for (i
= 0; i
< ntparms
; i
++)
19769 tree targ
= TREE_VEC_ELT (targs
, i
);
19770 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19772 if (targ
|| tparm
== error_mark_node
)
19774 tree parm
= TREE_VALUE (tparm
);
19776 if (TREE_CODE (parm
) == PARM_DECL
19777 && uses_template_parms (TREE_TYPE (parm
))
19778 && saw_undeduced
< 2)
19781 tree arg
= TREE_PURPOSE (tparm
);
19782 reopen_deferring_access_checks (*checks
);
19783 location_t save_loc
= input_location
;
19785 input_location
= DECL_SOURCE_LOCATION (parm
);
19786 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19787 if (!uses_template_parms (arg
))
19788 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19790 else if (saw_undeduced
< 2)
19793 arg
= error_mark_node
;
19794 input_location
= save_loc
;
19795 *checks
= get_deferred_access_checks ();
19796 pop_deferring_access_checks ();
19797 if (arg
== error_mark_node
)
19801 TREE_VEC_ELT (targs
, i
) = arg
;
19802 /* The position of the first default template argument,
19803 is also the number of non-defaulted arguments in TARGS.
19805 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19806 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19810 if (saw_undeduced
++ == 1)
19814 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19815 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19817 return unify_success (explain_p
);
19820 /* Subroutine of type_unification_real. Args are like the variables
19821 at the call site. ARG is an overloaded function (or template-id);
19822 we try deducing template args from each of the overloads, and if
19823 only one succeeds, we go with that. Modifies TARGS and returns
19824 true on success. */
19827 resolve_overloaded_unification (tree tparms
,
19831 unification_kind_t strict
,
19835 tree tempargs
= copy_node (targs
);
19837 tree goodfn
= NULL_TREE
;
19840 if (TREE_CODE (arg
) == ADDR_EXPR
)
19842 arg
= TREE_OPERAND (arg
, 0);
19848 if (TREE_CODE (arg
) == COMPONENT_REF
)
19849 /* Handle `&x' where `x' is some static or non-static member
19851 arg
= TREE_OPERAND (arg
, 1);
19853 if (TREE_CODE (arg
) == OFFSET_REF
)
19854 arg
= TREE_OPERAND (arg
, 1);
19856 /* Strip baselink information. */
19857 if (BASELINK_P (arg
))
19858 arg
= BASELINK_FUNCTIONS (arg
);
19860 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19862 /* If we got some explicit template args, we need to plug them into
19863 the affected templates before we try to unify, in case the
19864 explicit args will completely resolve the templates in question. */
19867 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19868 arg
= TREE_OPERAND (arg
, 0);
19870 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19873 tree subargs
, elem
;
19875 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19878 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19879 expl_subargs
, NULL_TREE
, tf_none
,
19880 /*require_all_args=*/true,
19881 /*use_default_args=*/true);
19882 if (subargs
!= error_mark_node
19883 && !any_dependent_template_arguments_p (subargs
))
19885 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19886 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19887 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19888 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19897 /* If no templates (or more than one) are fully resolved by the
19898 explicit arguments, this template-id is a non-deduced context; it
19899 could still be OK if we deduce all template arguments for the
19900 enclosing call through other arguments. */
19904 else if (TREE_CODE (arg
) != OVERLOAD
19905 && TREE_CODE (arg
) != FUNCTION_DECL
)
19906 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19907 -- but the deduction does not succeed because the expression is
19908 not just the function on its own. */
19911 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19914 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19915 strict
, sub_strict
, addr_p
, explain_p
)
19916 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19923 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19924 to function or pointer to member function argument if the set of
19925 overloaded functions does not contain function templates and at most
19926 one of a set of overloaded functions provides a unique match.
19928 So if we found multiple possibilities, we return success but don't
19929 deduce anything. */
19933 int i
= TREE_VEC_LENGTH (targs
);
19935 if (TREE_VEC_ELT (tempargs
, i
))
19937 tree old
= TREE_VEC_ELT (targs
, i
);
19938 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19939 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19940 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19941 /* Don't forget explicit template arguments in a pack. */
19942 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19943 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19944 TREE_VEC_ELT (targs
, i
) = new_
;
19953 /* Core DR 115: In contexts where deduction is done and fails, or in
19954 contexts where deduction is not done, if a template argument list is
19955 specified and it, along with any default template arguments, identifies
19956 a single function template specialization, then the template-id is an
19957 lvalue for the function template specialization. */
19960 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19962 tree expr
, offset
, baselink
;
19965 if (!type_unknown_p (orig_expr
))
19970 offset
= NULL_TREE
;
19971 baselink
= NULL_TREE
;
19973 if (TREE_CODE (expr
) == ADDR_EXPR
)
19975 expr
= TREE_OPERAND (expr
, 0);
19978 if (TREE_CODE (expr
) == OFFSET_REF
)
19981 expr
= TREE_OPERAND (expr
, 1);
19983 if (BASELINK_P (expr
))
19986 expr
= BASELINK_FUNCTIONS (expr
);
19989 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19992 tree goodfn
= NULL_TREE
;
19994 /* If we got some explicit template args, we need to plug them into
19995 the affected templates before we try to unify, in case the
19996 explicit args will completely resolve the templates in question. */
19998 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19999 tree arg
= TREE_OPERAND (expr
, 0);
20000 tree badfn
= NULL_TREE
;
20001 tree badargs
= NULL_TREE
;
20003 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20006 tree subargs
, elem
;
20008 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20011 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20012 expl_subargs
, NULL_TREE
, tf_none
,
20013 /*require_all_args=*/true,
20014 /*use_default_args=*/true);
20015 if (subargs
!= error_mark_node
20016 && !any_dependent_template_arguments_p (subargs
))
20018 elem
= instantiate_template (fn
, subargs
, tf_none
);
20019 if (elem
== error_mark_node
)
20024 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20033 mark_used (goodfn
);
20036 expr
= build_baselink (BASELINK_BINFO (baselink
),
20037 BASELINK_ACCESS_BINFO (baselink
),
20038 expr
, BASELINK_OPTYPE (baselink
));
20042 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20043 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20046 expr
= cp_build_addr_expr (expr
, complain
);
20049 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20050 /* There were no good options and at least one bad one, so let the
20051 user know what the problem is. */
20052 instantiate_template (badfn
, badargs
, complain
);
20057 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20058 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20059 different overloads deduce different arguments for a given parm.
20060 ADDR_P is true if the expression for which deduction is being
20061 performed was of the form "& fn" rather than simply "fn".
20063 Returns 1 on success. */
20066 try_one_overload (tree tparms
,
20071 unification_kind_t strict
,
20080 if (arg
== error_mark_node
)
20083 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20084 to function or pointer to member function argument if the set of
20085 overloaded functions does not contain function templates and at most
20086 one of a set of overloaded functions provides a unique match.
20088 So if this is a template, just return success. */
20090 if (uses_template_parms (arg
))
20093 if (TREE_CODE (arg
) == METHOD_TYPE
)
20094 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20096 arg
= build_pointer_type (arg
);
20098 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20100 /* We don't copy orig_targs for this because if we have already deduced
20101 some template args from previous args, unify would complain when we
20102 try to deduce a template parameter for the same argument, even though
20103 there isn't really a conflict. */
20104 nargs
= TREE_VEC_LENGTH (targs
);
20105 tempargs
= make_tree_vec (nargs
);
20107 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20110 /* First make sure we didn't deduce anything that conflicts with
20111 explicitly specified args. */
20112 for (i
= nargs
; i
--; )
20114 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20115 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20119 else if (uses_template_parms (elt
))
20120 /* Since we're unifying against ourselves, we will fill in
20121 template args used in the function parm list with our own
20122 template parms. Discard them. */
20123 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20124 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20126 /* Check that the argument at each index of the deduced argument pack
20127 is equivalent to the corresponding explicitly specified argument.
20128 We may have deduced more arguments than were explicitly specified,
20131 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20132 that's wrong if we deduce the same argument pack from multiple
20133 function arguments: it's only incomplete the first time. */
20135 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20136 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20138 if (TREE_VEC_LENGTH (deduced_pack
)
20139 < TREE_VEC_LENGTH (explicit_pack
))
20142 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20143 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20144 TREE_VEC_ELT (deduced_pack
, j
)))
20147 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20151 for (i
= nargs
; i
--; )
20153 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20156 TREE_VEC_ELT (targs
, i
) = elt
;
20162 /* PARM is a template class (perhaps with unbound template
20163 parameters). ARG is a fully instantiated type. If ARG can be
20164 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20165 TARGS are as for unify. */
20168 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20171 tree copy_of_targs
;
20173 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20175 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20176 /* Matches anything. */;
20177 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20178 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20181 /* We need to make a new template argument vector for the call to
20182 unify. If we used TARGS, we'd clutter it up with the result of
20183 the attempted unification, even if this class didn't work out.
20184 We also don't want to commit ourselves to all the unifications
20185 we've already done, since unification is supposed to be done on
20186 an argument-by-argument basis. In other words, consider the
20187 following pathological case:
20189 template <int I, int J, int K>
20192 template <int I, int J>
20193 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20195 template <int I, int J, int K>
20196 void f(S<I, J, K>, S<I, I, I>);
20205 Now, by the time we consider the unification involving `s2', we
20206 already know that we must have `f<0, 0, 0>'. But, even though
20207 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20208 because there are two ways to unify base classes of S<0, 1, 2>
20209 with S<I, I, I>. If we kept the already deduced knowledge, we
20210 would reject the possibility I=1. */
20211 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20213 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20215 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20220 /* If unification failed, we're done. */
20221 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20222 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20228 /* Given a template type PARM and a class type ARG, find the unique
20229 base type in ARG that is an instance of PARM. We do not examine
20230 ARG itself; only its base-classes. If there is not exactly one
20231 appropriate base class, return NULL_TREE. PARM may be the type of
20232 a partial specialization, as well as a plain template type. Used
20235 static enum template_base_result
20236 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20237 bool explain_p
, tree
*result
)
20239 tree rval
= NULL_TREE
;
20242 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20244 binfo
= TYPE_BINFO (complete_type (arg
));
20247 /* The type could not be completed. */
20248 *result
= NULL_TREE
;
20249 return tbr_incomplete_type
;
20252 /* Walk in inheritance graph order. The search order is not
20253 important, and this avoids multiple walks of virtual bases. */
20254 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20256 tree r
= try_class_unification (tparms
, targs
, parm
,
20257 BINFO_TYPE (binfo
), explain_p
);
20261 /* If there is more than one satisfactory baseclass, then:
20265 If they yield more than one possible deduced A, the type
20269 if (rval
&& !same_type_p (r
, rval
))
20271 *result
= NULL_TREE
;
20272 return tbr_ambiguous_baseclass
;
20280 return tbr_success
;
20283 /* Returns the level of DECL, which declares a template parameter. */
20286 template_decl_level (tree decl
)
20288 switch (TREE_CODE (decl
))
20291 case TEMPLATE_DECL
:
20292 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20295 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20298 gcc_unreachable ();
20303 /* Decide whether ARG can be unified with PARM, considering only the
20304 cv-qualifiers of each type, given STRICT as documented for unify.
20305 Returns nonzero iff the unification is OK on that basis. */
20308 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20310 int arg_quals
= cp_type_quals (arg
);
20311 int parm_quals
= cp_type_quals (parm
);
20313 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20314 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20316 /* Although a CVR qualifier is ignored when being applied to a
20317 substituted template parameter ([8.3.2]/1 for example), that
20318 does not allow us to unify "const T" with "int&" because both
20319 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20320 It is ok when we're allowing additional CV qualifiers
20321 at the outer level [14.8.2.1]/3,1st bullet. */
20322 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20323 || TREE_CODE (arg
) == FUNCTION_TYPE
20324 || TREE_CODE (arg
) == METHOD_TYPE
)
20325 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20328 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20329 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20333 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20334 && (arg_quals
& parm_quals
) != parm_quals
)
20337 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20338 && (parm_quals
& arg_quals
) != arg_quals
)
20344 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20346 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20348 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20349 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20350 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20352 *index
= TEMPLATE_TYPE_IDX (parm
);
20353 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20357 *index
= TEMPLATE_PARM_IDX (parm
);
20358 *level
= TEMPLATE_PARM_LEVEL (parm
);
20362 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20364 if (unify (TP, TA, P, A, S, EP)) \
20368 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20369 expansion at the end of PACKED_PARMS. Returns 0 if the type
20370 deduction succeeds, 1 otherwise. STRICT is the same as in
20371 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20372 function call argument list. We'll need to adjust the arguments to make them
20373 types. SUBR tells us if this is from a recursive call to
20374 type_unification_real, or for comparing two template argument
20378 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20379 tree packed_args
, unification_kind_t strict
,
20380 bool subr
, bool explain_p
)
20383 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20384 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20385 tree pack
, packs
= NULL_TREE
;
20386 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20388 /* Add in any args remembered from an earlier partial instantiation. */
20389 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20390 int levels
= TMPL_ARGS_DEPTH (targs
);
20392 packed_args
= expand_template_argument_pack (packed_args
);
20394 int len
= TREE_VEC_LENGTH (packed_args
);
20396 /* Determine the parameter packs we will be deducing from the
20397 pattern, and record their current deductions. */
20398 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20399 pack
; pack
= TREE_CHAIN (pack
))
20401 tree parm_pack
= TREE_VALUE (pack
);
20404 /* Determine the index and level of this parameter pack. */
20405 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20406 if (level
< levels
)
20409 /* Keep track of the parameter packs and their corresponding
20411 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20412 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20415 /* Loop through all of the arguments that have not yet been
20416 unified and unify each with the pattern. */
20417 for (i
= start
; i
< len
; i
++)
20420 bool any_explicit
= false;
20421 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20423 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20424 or the element of its argument pack at the current index if
20425 this argument was explicitly specified. */
20426 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20430 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20433 if (TREE_VALUE (pack
)
20434 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20435 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20437 any_explicit
= true;
20438 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20440 TMPL_ARG (targs
, level
, idx
) = arg
;
20443 /* If we had explicit template arguments, substitute them into the
20444 pattern before deduction. */
20447 /* Some arguments might still be unspecified or dependent. */
20449 ++processing_template_decl
;
20450 dependent
= any_dependent_template_arguments_p (targs
);
20452 --processing_template_decl
;
20453 parm
= tsubst (pattern
, targs
,
20454 explain_p
? tf_warning_or_error
: tf_none
,
20457 --processing_template_decl
;
20458 if (parm
== error_mark_node
)
20464 /* Unify the pattern with the current argument. */
20465 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20469 /* For each parameter pack, collect the deduced value. */
20470 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20473 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20475 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20476 TMPL_ARG (targs
, level
, idx
);
20480 /* Verify that the results of unification with the parameter packs
20481 produce results consistent with what we've seen before, and make
20482 the deduced argument packs available. */
20483 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20485 tree old_pack
= TREE_VALUE (pack
);
20486 tree new_args
= TREE_TYPE (pack
);
20487 int i
, len
= TREE_VEC_LENGTH (new_args
);
20489 bool nondeduced_p
= false;
20491 /* By default keep the original deduced argument pack.
20492 If necessary, more specific code is going to update the
20493 resulting deduced argument later down in this function. */
20494 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20495 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20497 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20498 actually deduce anything. */
20499 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20500 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20501 nondeduced_p
= true;
20505 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20507 /* If we had fewer function args than explicit template args,
20508 just use the explicits. */
20509 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20510 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20511 if (len
< explicit_len
)
20512 new_args
= explicit_args
;
20518 /* Build the deduced *_ARGUMENT_PACK. */
20519 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20521 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20522 TREE_CONSTANT (result
) = 1;
20525 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20527 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20529 /* Note the deduced argument packs for this parameter
20531 TMPL_ARG (targs
, level
, idx
) = result
;
20533 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20534 && (ARGUMENT_PACK_ARGS (old_pack
)
20535 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20537 /* We only had the explicitly-provided arguments before, but
20538 now we have a complete set of arguments. */
20539 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20541 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20542 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20543 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20547 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20548 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20550 if (!comp_template_args (old_args
, new_args
,
20551 &bad_old_arg
, &bad_new_arg
))
20552 /* Inconsistent unification of this parameter pack. */
20553 return unify_parameter_pack_inconsistent (explain_p
,
20559 return unify_success (explain_p
);
20562 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20563 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20564 parameters and return value are as for unify. */
20567 unify_array_domain (tree tparms
, tree targs
,
20568 tree parm_dom
, tree arg_dom
,
20576 /* Our representation of array types uses "N - 1" as the
20577 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20578 not an integer constant. We cannot unify arbitrarily
20579 complex expressions, so we eliminate the MINUS_EXPRs
20581 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20582 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20585 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20586 parm_max
= TREE_OPERAND (parm_max
, 0);
20588 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20589 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20592 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20593 trying to unify the type of a variable with the type
20594 of a template parameter. For example:
20596 template <unsigned int N>
20597 void f (char (&) [N]);
20604 Here, the type of the ARG will be "int [g(i)]", and
20605 may be a SAVE_EXPR, etc. */
20606 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20607 return unify_vla_arg (explain_p
, arg_dom
);
20608 arg_max
= TREE_OPERAND (arg_max
, 0);
20611 /* If only one of the bounds used a MINUS_EXPR, compensate
20612 by adding one to the other bound. */
20613 if (parm_cst
&& !arg_cst
)
20614 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20618 else if (arg_cst
&& !parm_cst
)
20619 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20624 return unify (tparms
, targs
, parm_max
, arg_max
,
20625 UNIFY_ALLOW_INTEGER
, explain_p
);
20628 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20630 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20635 if (PACK_EXPANSION_P (t
))
20636 t
= PACK_EXPANSION_PATTERN (t
);
20637 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20638 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20639 || DECL_TYPE_TEMPLATE_P (t
))
20641 else if (TYPE_P (t
))
20647 /* Deduce the value of template parameters. TPARMS is the (innermost)
20648 set of template parameters to a template. TARGS is the bindings
20649 for those template parameters, as determined thus far; TARGS may
20650 include template arguments for outer levels of template parameters
20651 as well. PARM is a parameter to a template function, or a
20652 subcomponent of that parameter; ARG is the corresponding argument.
20653 This function attempts to match PARM with ARG in a manner
20654 consistent with the existing assignments in TARGS. If more values
20655 are deduced, then TARGS is updated.
20657 Returns 0 if the type deduction succeeds, 1 otherwise. The
20658 parameter STRICT is a bitwise or of the following flags:
20661 Require an exact match between PARM and ARG.
20662 UNIFY_ALLOW_MORE_CV_QUAL:
20663 Allow the deduced ARG to be more cv-qualified (by qualification
20664 conversion) than ARG.
20665 UNIFY_ALLOW_LESS_CV_QUAL:
20666 Allow the deduced ARG to be less cv-qualified than ARG.
20667 UNIFY_ALLOW_DERIVED:
20668 Allow the deduced ARG to be a template base class of ARG,
20669 or a pointer to a template base class of the type pointed to by
20671 UNIFY_ALLOW_INTEGER:
20672 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20673 case for more information.
20674 UNIFY_ALLOW_OUTER_LEVEL:
20675 This is the outermost level of a deduction. Used to determine validity
20676 of qualification conversions. A valid qualification conversion must
20677 have const qualified pointers leading up to the inner type which
20678 requires additional CV quals, except at the outer level, where const
20679 is not required [conv.qual]. It would be normal to set this flag in
20680 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20681 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20682 This is the outermost level of a deduction, and PARM can be more CV
20683 qualified at this point.
20684 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20685 This is the outermost level of a deduction, and PARM can be less CV
20686 qualified at this point. */
20689 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20695 int strict_in
= strict
;
20696 tsubst_flags_t complain
= (explain_p
20697 ? tf_warning_or_error
20700 /* I don't think this will do the right thing with respect to types.
20701 But the only case I've seen it in so far has been array bounds, where
20702 signedness is the only information lost, and I think that will be
20704 while (CONVERT_EXPR_P (parm
))
20705 parm
= TREE_OPERAND (parm
, 0);
20707 if (arg
== error_mark_node
)
20708 return unify_invalid (explain_p
);
20709 if (arg
== unknown_type_node
20710 || arg
== init_list_type_node
)
20711 /* We can't deduce anything from this, but we might get all the
20712 template args from other function args. */
20713 return unify_success (explain_p
);
20715 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20716 return unify_success (explain_p
);
20718 /* If PARM uses template parameters, then we can't bail out here,
20719 even if ARG == PARM, since we won't record unifications for the
20720 template parameters. We might need them if we're trying to
20721 figure out which of two things is more specialized. */
20722 if (arg
== parm
&& !uses_template_parms (parm
))
20723 return unify_success (explain_p
);
20725 /* Handle init lists early, so the rest of the function can assume
20726 we're dealing with a type. */
20727 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20731 tree orig_parm
= parm
;
20733 /* Replace T with std::initializer_list<T> for deduction. */
20734 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20735 && flag_deduce_init_list
)
20736 parm
= listify (parm
);
20738 if (!is_std_init_list (parm
)
20739 && TREE_CODE (parm
) != ARRAY_TYPE
)
20740 /* We can only deduce from an initializer list argument if the
20741 parameter is std::initializer_list or an array; otherwise this
20742 is a non-deduced context. */
20743 return unify_success (explain_p
);
20745 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20746 elttype
= TREE_TYPE (parm
);
20749 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20750 /* Deduction is defined in terms of a single type, so just punt
20751 on the (bizarre) std::initializer_list<T...>. */
20752 if (PACK_EXPANSION_P (elttype
))
20753 return unify_success (explain_p
);
20756 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20758 int elt_strict
= strict
;
20760 if (elt
== error_mark_node
)
20761 return unify_invalid (explain_p
);
20763 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20765 tree type
= TREE_TYPE (elt
);
20766 if (type
== error_mark_node
)
20767 return unify_invalid (explain_p
);
20768 /* It should only be possible to get here for a call. */
20769 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20770 elt_strict
|= maybe_adjust_types_for_deduction
20771 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20775 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20779 if (TREE_CODE (parm
) == ARRAY_TYPE
20780 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20782 /* Also deduce from the length of the initializer list. */
20783 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20784 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20785 if (idx
== error_mark_node
)
20786 return unify_invalid (explain_p
);
20787 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20791 /* If the std::initializer_list<T> deduction worked, replace the
20792 deduced A with std::initializer_list<A>. */
20793 if (orig_parm
!= parm
)
20795 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20796 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20797 targ
= listify (targ
);
20798 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20800 return unify_success (explain_p
);
20803 /* If parm and arg aren't the same kind of thing (template, type, or
20804 expression), fail early. */
20805 if (pa_kind (parm
) != pa_kind (arg
))
20806 return unify_invalid (explain_p
);
20808 /* Immediately reject some pairs that won't unify because of
20809 cv-qualification mismatches. */
20810 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20812 /* It is the elements of the array which hold the cv quals of an array
20813 type, and the elements might be template type parms. We'll check
20814 when we recurse. */
20815 && TREE_CODE (arg
) != ARRAY_TYPE
20816 /* We check the cv-qualifiers when unifying with template type
20817 parameters below. We want to allow ARG `const T' to unify with
20818 PARM `T' for example, when computing which of two templates
20819 is more specialized, for example. */
20820 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20821 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20822 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20824 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20825 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20826 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20827 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20828 strict
&= ~UNIFY_ALLOW_DERIVED
;
20829 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20830 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20832 switch (TREE_CODE (parm
))
20834 case TYPENAME_TYPE
:
20836 case UNBOUND_CLASS_TEMPLATE
:
20837 /* In a type which contains a nested-name-specifier, template
20838 argument values cannot be deduced for template parameters used
20839 within the nested-name-specifier. */
20840 return unify_success (explain_p
);
20842 case TEMPLATE_TYPE_PARM
:
20843 case TEMPLATE_TEMPLATE_PARM
:
20844 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20845 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20846 if (error_operand_p (tparm
))
20847 return unify_invalid (explain_p
);
20849 if (TEMPLATE_TYPE_LEVEL (parm
)
20850 != template_decl_level (tparm
))
20851 /* The PARM is not one we're trying to unify. Just check
20852 to see if it matches ARG. */
20854 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20855 && (is_auto (parm
) ? is_auto (arg
)
20856 : same_type_p (parm
, arg
)))
20857 return unify_success (explain_p
);
20859 return unify_type_mismatch (explain_p
, parm
, arg
);
20861 idx
= TEMPLATE_TYPE_IDX (parm
);
20862 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20863 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20864 if (error_operand_p (tparm
))
20865 return unify_invalid (explain_p
);
20867 /* Check for mixed types and values. */
20868 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20869 && TREE_CODE (tparm
) != TYPE_DECL
)
20870 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20871 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20872 gcc_unreachable ();
20874 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20876 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20877 && CLASS_TYPE_P (arg
))
20879 /* First try to match ARG directly. */
20880 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20884 /* Otherwise, look for a suitable base of ARG, as below. */
20885 enum template_base_result r
;
20886 r
= get_template_base (tparms
, targs
, parm
, arg
,
20889 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20893 /* ARG must be constructed from a template class or a template
20894 template parameter. */
20895 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20896 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20897 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20899 /* Deduce arguments T, i from TT<T> or TT<i>. */
20900 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20903 arg
= TYPE_TI_TEMPLATE (arg
);
20905 /* Fall through to deduce template name. */
20908 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20909 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20911 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20913 /* Simple cases: Value already set, does match or doesn't. */
20914 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20915 return unify_success (explain_p
);
20917 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20921 /* If PARM is `const T' and ARG is only `int', we don't have
20922 a match unless we are allowing additional qualification.
20923 If ARG is `const int' and PARM is just `T' that's OK;
20924 that binds `const int' to `T'. */
20925 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20927 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20929 /* Consider the case where ARG is `const volatile int' and
20930 PARM is `const T'. Then, T should be `volatile int'. */
20931 arg
= cp_build_qualified_type_real
20932 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20933 if (arg
== error_mark_node
)
20934 return unify_invalid (explain_p
);
20936 /* Simple cases: Value already set, does match or doesn't. */
20937 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20938 return unify_success (explain_p
);
20940 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20942 /* Make sure that ARG is not a variable-sized array. (Note
20943 that were talking about variable-sized arrays (like
20944 `int[n]'), rather than arrays of unknown size (like
20945 `int[]').) We'll get very confused by such a type since
20946 the bound of the array is not constant, and therefore
20947 not mangleable. Besides, such types are not allowed in
20948 ISO C++, so we can do as we please here. We do allow
20949 them for 'auto' deduction, since that isn't ABI-exposed. */
20950 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20951 return unify_vla_arg (explain_p
, arg
);
20953 /* Strip typedefs as in convert_template_argument. */
20954 arg
= canonicalize_type_argument (arg
, tf_none
);
20957 /* If ARG is a parameter pack or an expansion, we cannot unify
20958 against it unless PARM is also a parameter pack. */
20959 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20960 && !template_parameter_pack_p (parm
))
20961 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20963 /* If the argument deduction results is a METHOD_TYPE,
20964 then there is a problem.
20965 METHOD_TYPE doesn't map to any real C++ type the result of
20966 the deduction can not be of that type. */
20967 if (TREE_CODE (arg
) == METHOD_TYPE
)
20968 return unify_method_type_error (explain_p
, arg
);
20970 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20971 return unify_success (explain_p
);
20973 case TEMPLATE_PARM_INDEX
:
20974 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20975 if (error_operand_p (tparm
))
20976 return unify_invalid (explain_p
);
20978 if (TEMPLATE_PARM_LEVEL (parm
)
20979 != template_decl_level (tparm
))
20981 /* The PARM is not one we're trying to unify. Just check
20982 to see if it matches ARG. */
20983 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20984 && cp_tree_equal (parm
, arg
));
20986 unify_expression_unequal (explain_p
, parm
, arg
);
20990 idx
= TEMPLATE_PARM_IDX (parm
);
20991 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20995 if ((strict
& UNIFY_ALLOW_INTEGER
)
20996 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
20997 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
20998 /* We're deducing from an array bound, the type doesn't matter. */
20999 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21000 int x
= !cp_tree_equal (targ
, arg
);
21002 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21006 /* [temp.deduct.type] If, in the declaration of a function template
21007 with a non-type template-parameter, the non-type
21008 template-parameter is used in an expression in the function
21009 parameter-list and, if the corresponding template-argument is
21010 deduced, the template-argument type shall match the type of the
21011 template-parameter exactly, except that a template-argument
21012 deduced from an array bound may be of any integral type.
21013 The non-type parameter might use already deduced type parameters. */
21014 ++processing_template_decl
;
21015 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
21016 --processing_template_decl
;
21017 if (tree a
= type_uses_auto (tparm
))
21019 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21020 if (tparm
== error_mark_node
)
21024 if (!TREE_TYPE (arg
))
21025 /* Template-parameter dependent expression. Just accept it for now.
21026 It will later be processed in convert_template_argument. */
21028 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21029 non_reference (tparm
)))
21031 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21032 && CP_INTEGRAL_TYPE_P (tparm
))
21033 /* Convert the ARG to the type of PARM; the deduced non-type
21034 template argument must exactly match the types of the
21035 corresponding parameter. */
21036 arg
= fold (build_nop (tparm
, arg
));
21037 else if (uses_template_parms (tparm
))
21039 /* We haven't deduced the type of this parameter yet. */
21040 if (cxx_dialect
>= cxx17
21041 /* We deduce from array bounds in try_array_deduction. */
21042 && !(strict
& UNIFY_ALLOW_INTEGER
))
21044 /* Deduce it from the non-type argument. */
21045 tree atype
= TREE_TYPE (arg
);
21046 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21048 UNIFY_ALLOW_NONE
, explain_p
);
21051 /* Try again later. */
21052 return unify_success (explain_p
);
21055 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21057 /* If ARG is a parameter pack or an expansion, we cannot unify
21058 against it unless PARM is also a parameter pack. */
21059 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21060 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21061 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21064 bool removed_attr
= false;
21065 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21067 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21068 return unify_success (explain_p
);
21072 /* A pointer-to-member constant can be unified only with
21073 another constant. */
21074 if (TREE_CODE (arg
) != PTRMEM_CST
)
21075 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21077 /* Just unify the class member. It would be useless (and possibly
21078 wrong, depending on the strict flags) to unify also
21079 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21080 arg refer to the same variable, even if through different
21081 classes. For instance:
21083 struct A { int x; };
21086 Unification of &A::x and &B::x must succeed. */
21087 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21088 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21093 if (!TYPE_PTR_P (arg
))
21094 return unify_type_mismatch (explain_p
, parm
, arg
);
21096 /* [temp.deduct.call]
21098 A can be another pointer or pointer to member type that can
21099 be converted to the deduced A via a qualification
21100 conversion (_conv.qual_).
21102 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21103 This will allow for additional cv-qualification of the
21104 pointed-to types if appropriate. */
21106 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21107 /* The derived-to-base conversion only persists through one
21108 level of pointers. */
21109 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21111 return unify (tparms
, targs
, TREE_TYPE (parm
),
21112 TREE_TYPE (arg
), strict
, explain_p
);
21115 case REFERENCE_TYPE
:
21116 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21117 return unify_type_mismatch (explain_p
, parm
, arg
);
21118 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21119 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21122 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21123 return unify_type_mismatch (explain_p
, parm
, arg
);
21124 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21125 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21126 return unify_type_mismatch (explain_p
, parm
, arg
);
21127 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21128 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21129 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21130 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21131 TYPE_DOMAIN (arg
), explain_p
);
21132 return unify_success (explain_p
);
21139 case ENUMERAL_TYPE
:
21142 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21143 return unify_type_mismatch (explain_p
, parm
, arg
);
21145 /* We have already checked cv-qualification at the top of the
21147 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21148 return unify_type_mismatch (explain_p
, parm
, arg
);
21150 /* As far as unification is concerned, this wins. Later checks
21151 will invalidate it if necessary. */
21152 return unify_success (explain_p
);
21154 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21155 /* Type INTEGER_CST can come from ordinary constant template args. */
21157 while (CONVERT_EXPR_P (arg
))
21158 arg
= TREE_OPERAND (arg
, 0);
21160 if (TREE_CODE (arg
) != INTEGER_CST
)
21161 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21162 return (tree_int_cst_equal (parm
, arg
)
21163 ? unify_success (explain_p
)
21164 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21168 int i
, len
, argslen
;
21169 int parm_variadic_p
= 0;
21171 if (TREE_CODE (arg
) != TREE_VEC
)
21172 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21174 len
= TREE_VEC_LENGTH (parm
);
21175 argslen
= TREE_VEC_LENGTH (arg
);
21177 /* Check for pack expansions in the parameters. */
21178 for (i
= 0; i
< len
; ++i
)
21180 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21183 /* We can unify against something with a trailing
21185 parm_variadic_p
= 1;
21187 /* [temp.deduct.type]/9: If the template argument list of
21188 P contains a pack expansion that is not the last
21189 template argument, the entire template argument list
21190 is a non-deduced context. */
21191 return unify_success (explain_p
);
21195 /* If we don't have enough arguments to satisfy the parameters
21196 (not counting the pack expression at the end), or we have
21197 too many arguments for a parameter list that doesn't end in
21198 a pack expression, we can't unify. */
21199 if (parm_variadic_p
21200 ? argslen
< len
- parm_variadic_p
21202 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21204 /* Unify all of the parameters that precede the (optional)
21205 pack expression. */
21206 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21208 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21209 TREE_VEC_ELT (parm
, i
),
21210 TREE_VEC_ELT (arg
, i
),
21211 UNIFY_ALLOW_NONE
, explain_p
);
21213 if (parm_variadic_p
)
21214 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21216 /*subr=*/true, explain_p
);
21217 return unify_success (explain_p
);
21222 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21223 return unify_type_mismatch (explain_p
, parm
, arg
);
21225 if (TYPE_PTRMEMFUNC_P (parm
))
21227 if (!TYPE_PTRMEMFUNC_P (arg
))
21228 return unify_type_mismatch (explain_p
, parm
, arg
);
21230 return unify (tparms
, targs
,
21231 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21232 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21233 strict
, explain_p
);
21235 else if (TYPE_PTRMEMFUNC_P (arg
))
21236 return unify_type_mismatch (explain_p
, parm
, arg
);
21238 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21240 tree t
= NULL_TREE
;
21242 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21244 /* First, we try to unify the PARM and ARG directly. */
21245 t
= try_class_unification (tparms
, targs
,
21246 parm
, arg
, explain_p
);
21250 /* Fallback to the special case allowed in
21251 [temp.deduct.call]:
21253 If P is a class, and P has the form
21254 template-id, then A can be a derived class of
21255 the deduced A. Likewise, if P is a pointer to
21256 a class of the form template-id, A can be a
21257 pointer to a derived class pointed to by the
21259 enum template_base_result r
;
21260 r
= get_template_base (tparms
, targs
, parm
, arg
,
21265 /* Don't give the derived diagnostic if we're
21266 already dealing with the same template. */
21268 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21269 && (CLASSTYPE_TI_TEMPLATE (parm
)
21270 == CLASSTYPE_TI_TEMPLATE (arg
)));
21271 return unify_no_common_base (explain_p
&& !same_template
,
21276 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21277 && (CLASSTYPE_TI_TEMPLATE (parm
)
21278 == CLASSTYPE_TI_TEMPLATE (arg
)))
21279 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21280 Then, we should unify `int' and `U'. */
21283 /* There's no chance of unification succeeding. */
21284 return unify_type_mismatch (explain_p
, parm
, arg
);
21286 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21287 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21289 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21290 return unify_type_mismatch (explain_p
, parm
, arg
);
21291 return unify_success (explain_p
);
21294 case FUNCTION_TYPE
:
21296 unsigned int nargs
;
21301 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21302 return unify_type_mismatch (explain_p
, parm
, arg
);
21304 /* CV qualifications for methods can never be deduced, they must
21305 match exactly. We need to check them explicitly here,
21306 because type_unification_real treats them as any other
21307 cv-qualified parameter. */
21308 if (TREE_CODE (parm
) == METHOD_TYPE
21309 && (!check_cv_quals_for_unify
21311 class_of_this_parm (arg
),
21312 class_of_this_parm (parm
))))
21313 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21314 if (TREE_CODE (arg
) == FUNCTION_TYPE
21315 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21316 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21317 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21318 return unify_type_mismatch (explain_p
, parm
, arg
);
21320 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21321 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21323 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21324 args
= XALLOCAVEC (tree
, nargs
);
21325 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21326 a
!= NULL_TREE
&& a
!= void_list_node
;
21327 a
= TREE_CHAIN (a
), ++i
)
21328 args
[i
] = TREE_VALUE (a
);
21331 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21332 args
, nargs
, 1, DEDUCE_EXACT
,
21333 LOOKUP_NORMAL
, NULL
, explain_p
))
21336 if (flag_noexcept_type
)
21338 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21339 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21340 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21341 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21342 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21343 && uses_template_parms (TREE_PURPOSE (pspec
)))
21344 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21345 TREE_PURPOSE (aspec
),
21346 UNIFY_ALLOW_NONE
, explain_p
);
21347 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21348 return unify_type_mismatch (explain_p
, parm
, arg
);
21355 /* Unify a pointer to member with a pointer to member function, which
21356 deduces the type of the member as a function type. */
21357 if (TYPE_PTRMEMFUNC_P (arg
))
21359 /* Check top-level cv qualifiers */
21360 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21361 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21363 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21364 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21365 UNIFY_ALLOW_NONE
, explain_p
);
21367 /* Determine the type of the function we are unifying against. */
21368 tree fntype
= static_fn_type (arg
);
21370 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21373 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21374 return unify_type_mismatch (explain_p
, parm
, arg
);
21375 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21376 TYPE_OFFSET_BASETYPE (arg
),
21377 UNIFY_ALLOW_NONE
, explain_p
);
21378 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21379 strict
, explain_p
);
21382 if (DECL_TEMPLATE_PARM_P (parm
))
21383 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21384 if (arg
!= scalar_constant_value (parm
))
21385 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21386 return unify_success (explain_p
);
21389 case TEMPLATE_DECL
:
21390 /* Matched cases are handled by the ARG == PARM test above. */
21391 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21394 /* We might get a variable as a non-type template argument in parm if the
21395 corresponding parameter is type-dependent. Make any necessary
21396 adjustments based on whether arg is a reference. */
21397 if (CONSTANT_CLASS_P (arg
))
21398 parm
= fold_non_dependent_expr (parm
);
21399 else if (REFERENCE_REF_P (arg
))
21401 tree sub
= TREE_OPERAND (arg
, 0);
21403 if (TREE_CODE (sub
) == ADDR_EXPR
)
21404 arg
= TREE_OPERAND (sub
, 0);
21406 /* Now use the normal expression code to check whether they match. */
21409 case TYPE_ARGUMENT_PACK
:
21410 case NONTYPE_ARGUMENT_PACK
:
21411 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21412 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21415 case DECLTYPE_TYPE
:
21416 case UNDERLYING_TYPE
:
21417 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21418 or UNDERLYING_TYPE nodes. */
21419 return unify_success (explain_p
);
21422 /* Unification fails if we hit an error node. */
21423 return unify_invalid (explain_p
);
21426 if (REFERENCE_REF_P (parm
))
21428 bool pexp
= PACK_EXPANSION_P (arg
);
21430 arg
= PACK_EXPANSION_PATTERN (arg
);
21431 if (REFERENCE_REF_P (arg
))
21432 arg
= TREE_OPERAND (arg
, 0);
21434 arg
= make_pack_expansion (arg
, complain
);
21435 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21436 strict
, explain_p
);
21441 /* An unresolved overload is a nondeduced context. */
21442 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21443 return unify_success (explain_p
);
21444 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21446 /* We must be looking at an expression. This can happen with
21450 void foo(S<I>, S<I + 2>);
21452 This is a "nondeduced context":
21456 The nondeduced contexts are:
21458 --A type that is a template-id in which one or more of
21459 the template-arguments is an expression that references
21460 a template-parameter.
21462 In these cases, we assume deduction succeeded, but don't
21463 actually infer any unifications. */
21465 if (!uses_template_parms (parm
)
21466 && !template_args_equal (parm
, arg
))
21467 return unify_expression_unequal (explain_p
, parm
, arg
);
21469 return unify_success (explain_p
);
21472 #undef RECUR_AND_CHECK_FAILURE
21474 /* Note that DECL can be defined in this translation unit, if
21478 mark_definable (tree decl
)
21481 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21482 FOR_EACH_CLONE (clone
, decl
)
21483 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21486 /* Called if RESULT is explicitly instantiated, or is a member of an
21487 explicitly instantiated class. */
21490 mark_decl_instantiated (tree result
, int extern_p
)
21492 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21494 /* If this entity has already been written out, it's too late to
21495 make any modifications. */
21496 if (TREE_ASM_WRITTEN (result
))
21499 /* For anonymous namespace we don't need to do anything. */
21500 if (decl_anon_ns_mem_p (result
))
21502 gcc_assert (!TREE_PUBLIC (result
));
21506 if (TREE_CODE (result
) != FUNCTION_DECL
)
21507 /* The TREE_PUBLIC flag for function declarations will have been
21508 set correctly by tsubst. */
21509 TREE_PUBLIC (result
) = 1;
21511 /* This might have been set by an earlier implicit instantiation. */
21512 DECL_COMDAT (result
) = 0;
21515 DECL_NOT_REALLY_EXTERN (result
) = 0;
21518 mark_definable (result
);
21519 mark_needed (result
);
21520 /* Always make artificials weak. */
21521 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21522 comdat_linkage (result
);
21523 /* For WIN32 we also want to put explicit instantiations in
21524 linkonce sections. */
21525 else if (TREE_PUBLIC (result
))
21526 maybe_make_one_only (result
);
21529 /* If EXTERN_P, then this function will not be emitted -- unless
21530 followed by an explicit instantiation, at which point its linkage
21531 will be adjusted. If !EXTERN_P, then this function will be
21532 emitted here. In neither circumstance do we want
21533 import_export_decl to adjust the linkage. */
21534 DECL_INTERFACE_KNOWN (result
) = 1;
21537 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21538 important template arguments. If any are missing, we check whether
21539 they're important by using error_mark_node for substituting into any
21540 args that were used for partial ordering (the ones between ARGS and END)
21541 and seeing if it bubbles up. */
21544 check_undeduced_parms (tree targs
, tree args
, tree end
)
21546 bool found
= false;
21548 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21549 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21552 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21556 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21557 if (substed
== error_mark_node
)
21563 /* Given two function templates PAT1 and PAT2, return:
21565 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21566 -1 if PAT2 is more specialized than PAT1.
21567 0 if neither is more specialized.
21569 LEN indicates the number of parameters we should consider
21570 (defaulted parameters should not be considered).
21572 The 1998 std underspecified function template partial ordering, and
21573 DR214 addresses the issue. We take pairs of arguments, one from
21574 each of the templates, and deduce them against each other. One of
21575 the templates will be more specialized if all the *other*
21576 template's arguments deduce against its arguments and at least one
21577 of its arguments *does* *not* deduce against the other template's
21578 corresponding argument. Deduction is done as for class templates.
21579 The arguments used in deduction have reference and top level cv
21580 qualifiers removed. Iff both arguments were originally reference
21581 types *and* deduction succeeds in both directions, an lvalue reference
21582 wins against an rvalue reference and otherwise the template
21583 with the more cv-qualified argument wins for that pairing (if
21584 neither is more cv-qualified, they both are equal). Unlike regular
21585 deduction, after all the arguments have been deduced in this way,
21586 we do *not* verify the deduced template argument values can be
21587 substituted into non-deduced contexts.
21589 The logic can be a bit confusing here, because we look at deduce1 and
21590 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21591 can find template arguments for pat1 to make arg1 look like arg2, that
21592 means that arg2 is at least as specialized as arg1. */
21595 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21597 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21598 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21599 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21600 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21601 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21602 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21603 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21604 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21605 tree origs1
, origs2
;
21606 bool lose1
= false;
21607 bool lose2
= false;
21609 /* Remove the this parameter from non-static member functions. If
21610 one is a non-static member function and the other is not a static
21611 member function, remove the first parameter from that function
21612 also. This situation occurs for operator functions where we
21613 locate both a member function (with this pointer) and non-member
21614 operator (with explicit first operand). */
21615 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21617 len
--; /* LEN is the number of significant arguments for DECL1 */
21618 args1
= TREE_CHAIN (args1
);
21619 if (!DECL_STATIC_FUNCTION_P (decl2
))
21620 args2
= TREE_CHAIN (args2
);
21622 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21624 args2
= TREE_CHAIN (args2
);
21625 if (!DECL_STATIC_FUNCTION_P (decl1
))
21628 args1
= TREE_CHAIN (args1
);
21632 /* If only one is a conversion operator, they are unordered. */
21633 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21636 /* Consider the return type for a conversion function */
21637 if (DECL_CONV_FN_P (decl1
))
21639 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21640 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21644 processing_template_decl
++;
21650 /* Stop when an ellipsis is seen. */
21651 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21653 tree arg1
= TREE_VALUE (args1
);
21654 tree arg2
= TREE_VALUE (args2
);
21655 int deduce1
, deduce2
;
21661 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21662 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21664 /* When both arguments are pack expansions, we need only
21665 unify the patterns themselves. */
21666 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21667 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21669 /* This is the last comparison we need to do. */
21673 /* DR 1847: If a particular P contains no template-parameters that
21674 participate in template argument deduction, that P is not used to
21675 determine the ordering. */
21676 if (!uses_deducible_template_parms (arg1
)
21677 && !uses_deducible_template_parms (arg2
))
21680 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21682 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21683 arg1
= TREE_TYPE (arg1
);
21684 quals1
= cp_type_quals (arg1
);
21687 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21689 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21690 arg2
= TREE_TYPE (arg2
);
21691 quals2
= cp_type_quals (arg2
);
21694 arg1
= TYPE_MAIN_VARIANT (arg1
);
21695 arg2
= TYPE_MAIN_VARIANT (arg2
);
21697 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21699 int i
, len2
= remaining_arguments (args2
);
21700 tree parmvec
= make_tree_vec (1);
21701 tree argvec
= make_tree_vec (len2
);
21704 /* Setup the parameter vector, which contains only ARG1. */
21705 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21707 /* Setup the argument vector, which contains the remaining
21709 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21710 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21712 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21713 argvec
, DEDUCE_EXACT
,
21714 /*subr=*/true, /*explain_p=*/false)
21717 /* We cannot deduce in the other direction, because ARG1 is
21718 a pack expansion but ARG2 is not. */
21721 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21723 int i
, len1
= remaining_arguments (args1
);
21724 tree parmvec
= make_tree_vec (1);
21725 tree argvec
= make_tree_vec (len1
);
21728 /* Setup the parameter vector, which contains only ARG1. */
21729 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21731 /* Setup the argument vector, which contains the remaining
21733 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21734 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21736 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21737 argvec
, DEDUCE_EXACT
,
21738 /*subr=*/true, /*explain_p=*/false)
21741 /* We cannot deduce in the other direction, because ARG2 is
21742 a pack expansion but ARG1 is not.*/
21748 /* The normal case, where neither argument is a pack
21750 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21751 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21753 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21754 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21758 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21759 arg2, then arg2 is not as specialized as arg1. */
21765 /* "If, for a given type, deduction succeeds in both directions
21766 (i.e., the types are identical after the transformations above)
21767 and both P and A were reference types (before being replaced with
21768 the type referred to above):
21769 - if the type from the argument template was an lvalue reference and
21770 the type from the parameter template was not, the argument type is
21771 considered to be more specialized than the other; otherwise,
21772 - if the type from the argument template is more cv-qualified
21773 than the type from the parameter template (as described above),
21774 the argument type is considered to be more specialized than the other;
21776 - neither type is more specialized than the other." */
21778 if (deduce1
&& deduce2
)
21780 if (ref1
&& ref2
&& ref1
!= ref2
)
21787 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21789 if ((quals1
& quals2
) == quals2
)
21791 if ((quals1
& quals2
) == quals1
)
21796 if (lose1
&& lose2
)
21797 /* We've failed to deduce something in either direction.
21798 These must be unordered. */
21803 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21804 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21805 /* We have already processed all of the arguments in our
21806 handing of the pack expansion type. */
21809 args1
= TREE_CHAIN (args1
);
21810 args2
= TREE_CHAIN (args2
);
21813 /* "In most cases, all template parameters must have values in order for
21814 deduction to succeed, but for partial ordering purposes a template
21815 parameter may remain without a value provided it is not used in the
21816 types being used for partial ordering."
21818 Thus, if we are missing any of the targs1 we need to substitute into
21819 origs1, then pat2 is not as specialized as pat1. This can happen when
21820 there is a nondeduced context. */
21821 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21823 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21826 processing_template_decl
--;
21828 /* If both deductions succeed, the partial ordering selects the more
21829 constrained template. */
21830 if (!lose1
&& !lose2
)
21832 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21833 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21834 lose1
= !subsumes_constraints (c1
, c2
);
21835 lose2
= !subsumes_constraints (c2
, c1
);
21838 /* All things being equal, if the next argument is a pack expansion
21839 for one function but not for the other, prefer the
21840 non-variadic function. FIXME this is bogus; see c++/41958. */
21842 && args1
&& TREE_VALUE (args1
)
21843 && args2
&& TREE_VALUE (args2
))
21845 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21846 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21849 if (lose1
== lose2
)
21857 /* Determine which of two partial specializations of TMPL is more
21860 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21861 to the first partial specialization. The TREE_PURPOSE is the
21862 innermost set of template parameters for the partial
21863 specialization. PAT2 is similar, but for the second template.
21865 Return 1 if the first partial specialization is more specialized;
21866 -1 if the second is more specialized; 0 if neither is more
21869 See [temp.class.order] for information about determining which of
21870 two templates is more specialized. */
21873 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21877 bool any_deductions
= false;
21879 tree tmpl1
= TREE_VALUE (pat1
);
21880 tree tmpl2
= TREE_VALUE (pat2
);
21881 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21882 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21884 /* Just like what happens for functions, if we are ordering between
21885 different template specializations, we may encounter dependent
21886 types in the arguments, and we need our dependency check functions
21887 to behave correctly. */
21888 ++processing_template_decl
;
21889 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21893 any_deductions
= true;
21896 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21900 any_deductions
= true;
21902 --processing_template_decl
;
21904 /* If both deductions succeed, the partial ordering selects the more
21905 constrained template. */
21906 if (!winner
&& any_deductions
)
21907 return more_constrained (tmpl1
, tmpl2
);
21909 /* In the case of a tie where at least one of the templates
21910 has a parameter pack at the end, the template with the most
21911 non-packed parameters wins. */
21914 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21915 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21917 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21918 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21919 int len1
= TREE_VEC_LENGTH (args1
);
21920 int len2
= TREE_VEC_LENGTH (args2
);
21922 /* We don't count the pack expansion at the end. */
21923 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21925 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21930 else if (len1
< len2
)
21937 /* Return the template arguments that will produce the function signature
21938 DECL from the function template FN, with the explicit template
21939 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21940 also match. Return NULL_TREE if no satisfactory arguments could be
21944 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21946 int ntparms
= DECL_NTPARMS (fn
);
21947 tree targs
= make_tree_vec (ntparms
);
21948 tree decl_type
= TREE_TYPE (decl
);
21949 tree decl_arg_types
;
21951 unsigned int nargs
, ix
;
21954 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21956 /* Never do unification on the 'this' parameter. */
21957 decl_arg_types
= skip_artificial_parms_for (decl
,
21958 TYPE_ARG_TYPES (decl_type
));
21960 nargs
= list_length (decl_arg_types
);
21961 args
= XALLOCAVEC (tree
, nargs
);
21962 for (arg
= decl_arg_types
, ix
= 0;
21963 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21964 arg
= TREE_CHAIN (arg
), ++ix
)
21965 args
[ix
] = TREE_VALUE (arg
);
21967 if (fn_type_unification (fn
, explicit_args
, targs
,
21969 (check_rettype
|| DECL_CONV_FN_P (fn
)
21970 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21971 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21973 == error_mark_node
)
21979 /* Return the innermost template arguments that, when applied to a partial
21980 specialization SPEC_TMPL of TMPL, yield the ARGS.
21982 For example, suppose we have:
21984 template <class T, class U> struct S {};
21985 template <class T> struct S<T*, int> {};
21987 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21988 partial specialization and the ARGS will be {double*, int}. The resulting
21989 vector will be {double}, indicating that `T' is bound to `double'. */
21992 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21994 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21996 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21997 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21999 tree innermost_deduced_args
;
22001 innermost_deduced_args
= make_tree_vec (ntparms
);
22002 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22004 deduced_args
= copy_node (args
);
22005 SET_TMPL_ARGS_LEVEL (deduced_args
,
22006 TMPL_ARGS_DEPTH (deduced_args
),
22007 innermost_deduced_args
);
22010 deduced_args
= innermost_deduced_args
;
22012 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22014 if (unify (tparms
, deduced_args
,
22015 INNERMOST_TEMPLATE_ARGS (spec_args
),
22016 INNERMOST_TEMPLATE_ARGS (args
),
22017 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22020 for (i
= 0; i
< ntparms
; ++i
)
22021 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22023 if (!tried_array_deduction
)
22025 try_array_deduction (tparms
, innermost_deduced_args
,
22026 INNERMOST_TEMPLATE_ARGS (spec_args
));
22027 tried_array_deduction
= true;
22028 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22034 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22035 if (!push_tinst_level (tinst
))
22037 excessive_deduction_depth
= true;
22041 /* Verify that nondeduced template arguments agree with the type
22042 obtained from argument deduction.
22046 struct A { typedef int X; };
22047 template <class T, class U> struct C {};
22048 template <class T> struct C<T, typename T::X> {};
22050 Then with the instantiation `C<A, int>', we can deduce that
22051 `T' is `A' but unify () does not check whether `typename T::X'
22053 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22055 if (spec_args
!= error_mark_node
)
22056 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22057 INNERMOST_TEMPLATE_ARGS (spec_args
),
22058 tmpl
, tf_none
, false, false);
22060 pop_tinst_level ();
22062 if (spec_args
== error_mark_node
22063 /* We only need to check the innermost arguments; the other
22064 arguments will always agree. */
22065 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22066 INNERMOST_TEMPLATE_ARGS (args
)))
22069 /* Now that we have bindings for all of the template arguments,
22070 ensure that the arguments deduced for the template template
22071 parameters have compatible template parameter lists. See the use
22072 of template_template_parm_bindings_ok_p in fn_type_unification
22073 for more information. */
22074 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22077 return deduced_args
;
22080 // Compare two function templates T1 and T2 by deducing bindings
22081 // from one against the other. If both deductions succeed, compare
22082 // constraints to see which is more constrained.
22084 more_specialized_inst (tree t1
, tree t2
)
22089 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22095 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22101 // If both deductions succeed, then one may be more constrained.
22102 if (count
== 2 && fate
== 0)
22103 fate
= more_constrained (t1
, t2
);
22108 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22109 Return the TREE_LIST node with the most specialized template, if
22110 any. If there is no most specialized template, the error_mark_node
22113 Note that this function does not look at, or modify, the
22114 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22115 returned is one of the elements of INSTANTIATIONS, callers may
22116 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22117 and retrieve it from the value returned. */
22120 most_specialized_instantiation (tree templates
)
22124 ++processing_template_decl
;
22127 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22129 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22130 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22135 /* Equally specialized, move to next function. If there
22136 is no next function, nothing's most specialized. */
22137 fn
= TREE_CHAIN (fn
);
22145 /* Now verify that champ is better than everything earlier in the
22146 instantiation list. */
22147 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22148 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22155 processing_template_decl
--;
22158 return error_mark_node
;
22163 /* If DECL is a specialization of some template, return the most
22164 general such template. Otherwise, returns NULL_TREE.
22166 For example, given:
22168 template <class T> struct S { template <class U> void f(U); };
22170 if TMPL is `template <class U> void S<int>::f(U)' this will return
22171 the full template. This function will not trace past partial
22172 specializations, however. For example, given in addition:
22174 template <class T> struct S<T*> { template <class U> void f(U); };
22176 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22177 `template <class T> template <class U> S<T*>::f(U)'. */
22180 most_general_template (tree decl
)
22182 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22184 if (tree tinfo
= get_template_info (decl
))
22185 decl
= TI_TEMPLATE (tinfo
);
22186 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22187 template friend, or a FIELD_DECL for a capture pack. */
22188 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22192 /* Look for more and more general templates. */
22193 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22195 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22196 (See cp-tree.h for details.) */
22197 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22200 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22201 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22202 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22205 /* Stop if we run into an explicitly specialized class template. */
22206 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22207 && DECL_CONTEXT (decl
)
22208 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22211 decl
= DECL_TI_TEMPLATE (decl
);
22217 /* Return the most specialized of the template partial specializations
22218 which can produce TARGET, a specialization of some class or variable
22219 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22220 a TEMPLATE_DECL node corresponding to the partial specialization, while
22221 the TREE_PURPOSE is the set of template arguments that must be
22222 substituted into the template pattern in order to generate TARGET.
22224 If the choice of partial specialization is ambiguous, a diagnostic
22225 is issued, and the error_mark_node is returned. If there are no
22226 partial specializations matching TARGET, then NULL_TREE is
22227 returned, indicating that the primary template should be used. */
22230 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22232 tree list
= NULL_TREE
;
22237 tree outer_args
= NULL_TREE
;
22240 if (TYPE_P (target
))
22242 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22243 tmpl
= TI_TEMPLATE (tinfo
);
22244 args
= TI_ARGS (tinfo
);
22246 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22248 tmpl
= TREE_OPERAND (target
, 0);
22249 args
= TREE_OPERAND (target
, 1);
22251 else if (VAR_P (target
))
22253 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22254 tmpl
= TI_TEMPLATE (tinfo
);
22255 args
= TI_ARGS (tinfo
);
22258 gcc_unreachable ();
22260 tree main_tmpl
= most_general_template (tmpl
);
22262 /* For determining which partial specialization to use, only the
22263 innermost args are interesting. */
22264 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22266 outer_args
= strip_innermost_template_args (args
, 1);
22267 args
= INNERMOST_TEMPLATE_ARGS (args
);
22270 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22273 tree spec_tmpl
= TREE_VALUE (t
);
22277 /* Substitute in the template args from the enclosing class. */
22278 ++processing_template_decl
;
22279 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22280 --processing_template_decl
;
22283 if (spec_tmpl
== error_mark_node
)
22284 return error_mark_node
;
22286 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22290 spec_args
= add_to_template_args (outer_args
, spec_args
);
22292 /* Keep the candidate only if the constraints are satisfied,
22293 or if we're not compiling with concepts. */
22295 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22297 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22298 TREE_TYPE (list
) = TREE_TYPE (t
);
22306 ambiguous_p
= false;
22309 t
= TREE_CHAIN (t
);
22310 for (; t
; t
= TREE_CHAIN (t
))
22312 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22319 t
= TREE_CHAIN (t
);
22322 ambiguous_p
= true;
22331 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22333 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22336 ambiguous_p
= true;
22344 char *spaces
= NULL
;
22345 if (!(complain
& tf_error
))
22346 return error_mark_node
;
22347 if (TYPE_P (target
))
22348 error ("ambiguous template instantiation for %q#T", target
);
22350 error ("ambiguous template instantiation for %q#D", target
);
22351 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22352 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22354 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22355 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22356 "%s %#qS", spaces
? spaces
: str
, subst
);
22357 spaces
= spaces
? spaces
: get_spaces (str
);
22360 return error_mark_node
;
22366 /* Explicitly instantiate DECL. */
22369 do_decl_instantiation (tree decl
, tree storage
)
22371 tree result
= NULL_TREE
;
22374 if (!decl
|| decl
== error_mark_node
)
22375 /* An error occurred, for which grokdeclarator has already issued
22376 an appropriate message. */
22378 else if (! DECL_LANG_SPECIFIC (decl
))
22380 error ("explicit instantiation of non-template %q#D", decl
);
22384 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22385 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22387 if (VAR_P (decl
) && !var_templ
)
22389 /* There is an asymmetry here in the way VAR_DECLs and
22390 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22391 the latter, the DECL we get back will be marked as a
22392 template instantiation, and the appropriate
22393 DECL_TEMPLATE_INFO will be set up. This does not happen for
22394 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22395 should handle VAR_DECLs as it currently handles
22397 if (!DECL_CLASS_SCOPE_P (decl
))
22399 error ("%qD is not a static data member of a class template", decl
);
22402 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22403 if (!result
|| !VAR_P (result
))
22405 error ("no matching template for %qD found", decl
);
22408 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22410 error ("type %qT for explicit instantiation %qD does not match "
22411 "declared type %qT", TREE_TYPE (result
), decl
,
22416 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22418 error ("explicit instantiation of %q#D", decl
);
22424 /* Check for various error cases. Note that if the explicit
22425 instantiation is valid the RESULT will currently be marked as an
22426 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22427 until we get here. */
22429 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22431 /* DR 259 [temp.spec].
22433 Both an explicit instantiation and a declaration of an explicit
22434 specialization shall not appear in a program unless the explicit
22435 instantiation follows a declaration of the explicit specialization.
22437 For a given set of template parameters, if an explicit
22438 instantiation of a template appears after a declaration of an
22439 explicit specialization for that template, the explicit
22440 instantiation has no effect. */
22443 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22447 No program shall explicitly instantiate any template more
22450 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22451 the first instantiation was `extern' and the second is not,
22452 and EXTERN_P for the opposite case. */
22453 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22454 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22455 /* If an "extern" explicit instantiation follows an ordinary
22456 explicit instantiation, the template is instantiated. */
22460 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22462 error ("no matching template for %qD found", result
);
22465 else if (!DECL_TEMPLATE_INFO (result
))
22467 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22471 if (storage
== NULL_TREE
)
22473 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22475 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22476 pedwarn (input_location
, OPT_Wpedantic
,
22477 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22482 error ("storage class %qD applied to template instantiation", storage
);
22484 check_explicit_instantiation_namespace (result
);
22485 mark_decl_instantiated (result
, extern_p
);
22487 instantiate_decl (result
, /*defer_ok=*/true,
22488 /*expl_inst_class_mem_p=*/false);
22492 mark_class_instantiated (tree t
, int extern_p
)
22494 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22495 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22496 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22497 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22500 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22501 rest_of_type_compilation (t
, 1);
22505 /* Called from do_type_instantiation through binding_table_foreach to
22506 do recursive instantiation for the type bound in ENTRY. */
22508 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22510 tree storage
= *(tree
*) data
;
22512 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22513 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22514 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22517 /* Perform an explicit instantiation of template class T. STORAGE, if
22518 non-null, is the RID for extern, inline or static. COMPLAIN is
22519 nonzero if this is called from the parser, zero if called recursively,
22520 since the standard is unclear (as detailed below). */
22523 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22528 int previous_instantiation_extern_p
= 0;
22530 if (TREE_CODE (t
) == TYPE_DECL
)
22533 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22536 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22538 error ("explicit instantiation of non-class template %qD", tmpl
);
22540 error ("explicit instantiation of non-template type %qT", t
);
22546 if (!COMPLETE_TYPE_P (t
))
22548 if (complain
& tf_error
)
22549 error ("explicit instantiation of %q#T before definition of template",
22554 if (storage
!= NULL_TREE
)
22556 if (!in_system_header_at (input_location
))
22558 if (storage
== ridpointers
[(int) RID_EXTERN
])
22560 if (cxx_dialect
== cxx98
)
22561 pedwarn (input_location
, OPT_Wpedantic
,
22562 "ISO C++ 1998 forbids the use of %<extern%> on "
22563 "explicit instantiations");
22566 pedwarn (input_location
, OPT_Wpedantic
,
22567 "ISO C++ forbids the use of %qE"
22568 " on explicit instantiations", storage
);
22571 if (storage
== ridpointers
[(int) RID_INLINE
])
22573 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22575 else if (storage
== ridpointers
[(int) RID_STATIC
])
22579 error ("storage class %qD applied to template instantiation",
22585 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22587 /* DR 259 [temp.spec].
22589 Both an explicit instantiation and a declaration of an explicit
22590 specialization shall not appear in a program unless the explicit
22591 instantiation follows a declaration of the explicit specialization.
22593 For a given set of template parameters, if an explicit
22594 instantiation of a template appears after a declaration of an
22595 explicit specialization for that template, the explicit
22596 instantiation has no effect. */
22599 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22603 No program shall explicitly instantiate any template more
22606 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22607 instantiation was `extern'. If EXTERN_P then the second is.
22608 These cases are OK. */
22609 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22611 if (!previous_instantiation_extern_p
&& !extern_p
22612 && (complain
& tf_error
))
22613 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22615 /* If we've already instantiated the template, just return now. */
22616 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22620 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22621 mark_class_instantiated (t
, extern_p
);
22626 /* In contrast to implicit instantiation, where only the
22627 declarations, and not the definitions, of members are
22628 instantiated, we have here:
22632 The explicit instantiation of a class template specialization
22633 implies the instantiation of all of its members not
22634 previously explicitly specialized in the translation unit
22635 containing the explicit instantiation.
22637 Of course, we can't instantiate member template classes, since we
22638 don't have any arguments for them. Note that the standard is
22639 unclear on whether the instantiation of the members are
22640 *explicit* instantiations or not. However, the most natural
22641 interpretation is that it should be an explicit
22643 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22645 || (TREE_CODE (fld
) == FUNCTION_DECL
22647 && user_provided_p (fld
)))
22648 && DECL_TEMPLATE_INSTANTIATION (fld
))
22650 mark_decl_instantiated (fld
, extern_p
);
22652 instantiate_decl (fld
, /*defer_ok=*/true,
22653 /*expl_inst_class_mem_p=*/true);
22656 if (CLASSTYPE_NESTED_UTDS (t
))
22657 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22658 bt_instantiate_type_proc
, &storage
);
22661 /* Given a function DECL, which is a specialization of TMPL, modify
22662 DECL to be a re-instantiation of TMPL with the same template
22663 arguments. TMPL should be the template into which tsubst'ing
22664 should occur for DECL, not the most general template.
22666 One reason for doing this is a scenario like this:
22669 void f(const T&, int i);
22671 void g() { f(3, 7); }
22674 void f(const T& t, const int i) { }
22676 Note that when the template is first instantiated, with
22677 instantiate_template, the resulting DECL will have no name for the
22678 first parameter, and the wrong type for the second. So, when we go
22679 to instantiate the DECL, we regenerate it. */
22682 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22684 /* The arguments used to instantiate DECL, from the most general
22688 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22690 /* Make sure that we can see identifiers, and compute access
22692 push_access_scope (decl
);
22694 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22702 args_depth
= TMPL_ARGS_DEPTH (args
);
22703 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22704 if (args_depth
> parms_depth
)
22705 args
= get_innermost_template_args (args
, parms_depth
);
22707 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22708 args
, tf_error
, NULL_TREE
,
22709 /*defer_ok*/false);
22710 if (specs
&& specs
!= error_mark_node
)
22711 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22714 /* Merge parameter declarations. */
22715 decl_parm
= skip_artificial_parms_for (decl
,
22716 DECL_ARGUMENTS (decl
));
22718 = skip_artificial_parms_for (code_pattern
,
22719 DECL_ARGUMENTS (code_pattern
));
22720 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22725 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22726 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22727 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22729 parm_type
= type_decays_to (parm_type
);
22730 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22731 TREE_TYPE (decl_parm
) = parm_type
;
22732 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22733 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22735 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22736 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22738 decl_parm
= DECL_CHAIN (decl_parm
);
22739 pattern_parm
= DECL_CHAIN (pattern_parm
);
22741 /* Merge any parameters that match with the function parameter
22743 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22746 tree expanded_types
;
22747 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22748 the parameters in this function parameter pack. */
22749 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22750 args
, tf_error
, NULL_TREE
);
22751 len
= TREE_VEC_LENGTH (expanded_types
);
22752 for (i
= 0; i
< len
; i
++)
22757 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22758 /* Rename the parameter to include the index. */
22759 DECL_NAME (decl_parm
) =
22760 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22761 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22762 parm_type
= type_decays_to (parm_type
);
22763 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22764 TREE_TYPE (decl_parm
) = parm_type
;
22765 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22766 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22768 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22769 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22771 decl_parm
= DECL_CHAIN (decl_parm
);
22774 /* Merge additional specifiers from the CODE_PATTERN. */
22775 if (DECL_DECLARED_INLINE_P (code_pattern
)
22776 && !DECL_DECLARED_INLINE_P (decl
))
22777 DECL_DECLARED_INLINE_P (decl
) = 1;
22779 else if (VAR_P (decl
))
22781 start_lambda_scope (decl
);
22782 DECL_INITIAL (decl
) =
22783 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22784 tf_error
, DECL_TI_TEMPLATE (decl
),
22785 /*integral_constant_expression_p=*/false);
22786 finish_lambda_scope ();
22787 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22788 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22789 tf_error
, DECL_TI_TEMPLATE (decl
));
22792 gcc_unreachable ();
22794 pop_access_scope (decl
);
22797 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22798 substituted to get DECL. */
22801 template_for_substitution (tree decl
)
22803 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22805 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22806 for the instantiation. This is not always the most general
22807 template. Consider, for example:
22810 struct S { template <class U> void f();
22811 template <> void f<int>(); };
22813 and an instantiation of S<double>::f<int>. We want TD to be the
22814 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22815 while (/* An instantiation cannot have a definition, so we need a
22816 more general template. */
22817 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22818 /* We must also deal with friend templates. Given:
22820 template <class T> struct S {
22821 template <class U> friend void f() {};
22824 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22825 so far as the language is concerned, but that's still
22826 where we get the pattern for the instantiation from. On
22827 other hand, if the definition comes outside the class, say:
22829 template <class T> struct S {
22830 template <class U> friend void f();
22832 template <class U> friend void f() {}
22834 we don't need to look any further. That's what the check for
22835 DECL_INITIAL is for. */
22836 || (TREE_CODE (decl
) == FUNCTION_DECL
22837 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22838 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22840 /* The present template, TD, should not be a definition. If it
22841 were a definition, we should be using it! Note that we
22842 cannot restructure the loop to just keep going until we find
22843 a template with a definition, since that might go too far if
22844 a specialization was declared, but not defined. */
22846 /* Fetch the more general template. */
22847 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22853 /* Returns true if we need to instantiate this template instance even if we
22854 know we aren't going to emit it. */
22857 always_instantiate_p (tree decl
)
22859 /* We always instantiate inline functions so that we can inline them. An
22860 explicit instantiation declaration prohibits implicit instantiation of
22861 non-inline functions. With high levels of optimization, we would
22862 normally inline non-inline functions -- but we're not allowed to do
22863 that for "extern template" functions. Therefore, we check
22864 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22865 return ((TREE_CODE (decl
) == FUNCTION_DECL
22866 && (DECL_DECLARED_INLINE_P (decl
)
22867 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22868 /* And we need to instantiate static data members so that
22869 their initializers are available in integral constant
22872 && decl_maybe_constant_var_p (decl
)));
22875 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22876 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22877 error, true otherwise. */
22880 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
22882 tree fntype
, spec
, noex
, clone
;
22884 /* Don't instantiate a noexcept-specification from template context. */
22885 if (processing_template_decl
)
22888 if (DECL_CLONED_FUNCTION_P (fn
))
22889 fn
= DECL_CLONED_FUNCTION (fn
);
22890 fntype
= TREE_TYPE (fn
);
22891 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22893 if (!spec
|| !TREE_PURPOSE (spec
))
22896 noex
= TREE_PURPOSE (spec
);
22898 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22900 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
22901 bool added
= false;
22902 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22903 spec
= get_defaulted_eh_spec (fn
, complain
);
22904 else if (!(added
= !fns
->add (fn
)))
22906 /* If hash_set::add returns true, the element was already there. */
22907 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
22908 DECL_SOURCE_LOCATION (fn
));
22910 "exception specification of %qD depends on itself",
22912 spec
= noexcept_false_spec
;
22914 else if (push_tinst_level (fn
))
22916 push_access_scope (fn
);
22917 push_deferring_access_checks (dk_no_deferred
);
22918 input_location
= DECL_SOURCE_LOCATION (fn
);
22919 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22920 DEFERRED_NOEXCEPT_ARGS (noex
),
22921 tf_warning_or_error
, fn
,
22922 /*function_p=*/false,
22923 /*integral_constant_expression_p=*/true);
22924 pop_deferring_access_checks ();
22925 pop_access_scope (fn
);
22926 pop_tinst_level ();
22927 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22928 if (spec
== error_mark_node
)
22929 spec
= noexcept_false_spec
;
22932 spec
= noexcept_false_spec
;
22937 if (spec
== error_mark_node
)
22940 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22943 FOR_EACH_CLONE (clone
, fn
)
22945 if (TREE_TYPE (clone
) == fntype
)
22946 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22948 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22954 /* We're starting to process the function INST, an instantiation of PATTERN;
22955 add their parameters to local_specializations. */
22958 register_parameter_specializations (tree pattern
, tree inst
)
22960 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
22961 tree spec_parm
= DECL_ARGUMENTS (inst
);
22962 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
22964 register_local_specialization (spec_parm
, tmpl_parm
);
22965 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
22966 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
22968 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22970 if (!DECL_PACK_P (tmpl_parm
))
22972 register_local_specialization (spec_parm
, tmpl_parm
);
22973 spec_parm
= DECL_CHAIN (spec_parm
);
22977 /* Register the (value) argument pack as a specialization of
22978 TMPL_PARM, then move on. */
22979 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22980 register_local_specialization (argpack
, tmpl_parm
);
22983 gcc_assert (!spec_parm
);
22986 /* Produce the definition of D, a _DECL generated from a template. If
22987 DEFER_OK is true, then we don't have to actually do the
22988 instantiation now; we just have to do it sometime. Normally it is
22989 an error if this is an explicit instantiation but D is undefined.
22990 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22991 instantiated class template. */
22994 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
22996 tree tmpl
= DECL_TI_TEMPLATE (d
);
23003 bool pattern_defined
;
23004 location_t saved_loc
= input_location
;
23005 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23006 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23010 /* This function should only be used to instantiate templates for
23011 functions and static member variables. */
23012 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23014 /* A concept is never instantiated. */
23015 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23017 /* Variables are never deferred; if instantiation is required, they
23018 are instantiated right away. That allows for better code in the
23019 case that an expression refers to the value of the variable --
23020 if the variable has a constant value the referring expression can
23021 take advantage of that fact. */
23025 /* Don't instantiate cloned functions. Instead, instantiate the
23026 functions they cloned. */
23027 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23028 d
= DECL_CLONED_FUNCTION (d
);
23030 if (DECL_TEMPLATE_INSTANTIATED (d
)
23031 || (TREE_CODE (d
) == FUNCTION_DECL
23032 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23033 || DECL_TEMPLATE_SPECIALIZATION (d
))
23034 /* D has already been instantiated or explicitly specialized, so
23035 there's nothing for us to do here.
23037 It might seem reasonable to check whether or not D is an explicit
23038 instantiation, and, if so, stop here. But when an explicit
23039 instantiation is deferred until the end of the compilation,
23040 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23041 the instantiation. */
23044 /* Check to see whether we know that this template will be
23045 instantiated in some other file, as with "extern template"
23047 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23049 /* In general, we do not instantiate such templates. */
23050 if (external_p
&& !always_instantiate_p (d
))
23053 gen_tmpl
= most_general_template (tmpl
);
23054 gen_args
= DECL_TI_ARGS (d
);
23056 if (tmpl
!= gen_tmpl
)
23057 /* We should already have the extra args. */
23058 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23059 == TMPL_ARGS_DEPTH (gen_args
));
23060 /* And what's in the hash table should match D. */
23061 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23062 || spec
== NULL_TREE
);
23064 /* This needs to happen before any tsubsting. */
23065 if (! push_tinst_level (d
))
23068 timevar_push (TV_TEMPLATE_INST
);
23070 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23071 for the instantiation. */
23072 td
= template_for_substitution (d
);
23077 /* Look up an explicit specialization, if any. */
23078 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23079 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23080 if (elt
&& elt
!= error_mark_node
)
23082 td
= TREE_VALUE (elt
);
23083 args
= TREE_PURPOSE (elt
);
23087 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23089 /* We should never be trying to instantiate a member of a class
23090 template or partial specialization. */
23091 gcc_assert (d
!= code_pattern
);
23093 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23094 || DECL_TEMPLATE_SPECIALIZATION (td
))
23095 /* In the case of a friend template whose definition is provided
23096 outside the class, we may have too many arguments. Drop the
23097 ones we don't need. The same is true for specializations. */
23098 args
= get_innermost_template_args
23099 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23101 if (TREE_CODE (d
) == FUNCTION_DECL
)
23103 deleted_p
= DECL_DELETED_FN (code_pattern
);
23104 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23105 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23106 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23112 if (DECL_CLASS_SCOPE_P (code_pattern
))
23113 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23114 || DECL_INLINE_VAR_P (code_pattern
));
23116 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23119 /* We may be in the middle of deferred access check. Disable it now. */
23120 push_deferring_access_checks (dk_no_deferred
);
23122 /* Unless an explicit instantiation directive has already determined
23123 the linkage of D, remember that a definition is available for
23125 if (pattern_defined
23126 && !DECL_INTERFACE_KNOWN (d
)
23127 && !DECL_NOT_REALLY_EXTERN (d
))
23128 mark_definable (d
);
23130 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23131 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23132 input_location
= DECL_SOURCE_LOCATION (d
);
23134 /* If D is a member of an explicitly instantiated class template,
23135 and no definition is available, treat it like an implicit
23137 if (!pattern_defined
&& expl_inst_class_mem_p
23138 && DECL_EXPLICIT_INSTANTIATION (d
))
23140 /* Leave linkage flags alone on instantiations with anonymous
23142 if (TREE_PUBLIC (d
))
23144 DECL_NOT_REALLY_EXTERN (d
) = 0;
23145 DECL_INTERFACE_KNOWN (d
) = 0;
23147 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23150 /* Defer all other templates, unless we have been explicitly
23151 forbidden from doing so. */
23152 if (/* If there is no definition, we cannot instantiate the
23155 /* If it's OK to postpone instantiation, do so. */
23157 /* If this is a static data member that will be defined
23158 elsewhere, we don't want to instantiate the entire data
23159 member, but we do want to instantiate the initializer so that
23160 we can substitute that elsewhere. */
23161 || (external_p
&& VAR_P (d
))
23162 /* Handle here a deleted function too, avoid generating
23163 its body (c++/61080). */
23166 /* The definition of the static data member is now required so
23167 we must substitute the initializer. */
23169 && !DECL_INITIAL (d
)
23170 && DECL_INITIAL (code_pattern
))
23174 bool const_init
= false;
23175 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23177 ns
= decl_namespace_context (d
);
23178 push_nested_namespace (ns
);
23180 push_nested_class (DECL_CONTEXT (d
));
23181 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23183 tf_warning_or_error
, NULL_TREE
,
23184 /*integral_constant_expression_p=*/false);
23185 /* If instantiating the initializer involved instantiating this
23186 again, don't call cp_finish_decl twice. */
23187 if (!DECL_INITIAL (d
))
23189 /* Make sure the initializer is still constant, in case of
23190 circular dependency (template/instantiate6.C). */
23192 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23193 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23194 /*asmspec_tree=*/NULL_TREE
,
23195 LOOKUP_ONLYCONVERTING
);
23198 pop_nested_class ();
23199 pop_nested_namespace (ns
);
23202 /* We restore the source position here because it's used by
23203 add_pending_template. */
23204 input_location
= saved_loc
;
23206 if (at_eof
&& !pattern_defined
23207 && DECL_EXPLICIT_INSTANTIATION (d
)
23208 && DECL_NOT_REALLY_EXTERN (d
))
23211 The definition of a non-exported function template, a
23212 non-exported member function template, or a non-exported
23213 member function or static data member of a class template
23214 shall be present in every translation unit in which it is
23215 explicitly instantiated. */
23216 permerror (input_location
, "explicit instantiation of %qD "
23217 "but no definition available", d
);
23219 /* If we're in unevaluated context, we just wanted to get the
23220 constant value; this isn't an odr use, so don't queue
23221 a full instantiation. */
23222 if (cp_unevaluated_operand
!= 0)
23224 /* ??? Historically, we have instantiated inline functions, even
23225 when marked as "extern template". */
23226 if (!(external_p
&& VAR_P (d
)))
23227 add_pending_template (d
);
23230 /* Tell the repository that D is available in this translation unit
23231 -- and see if it is supposed to be instantiated here. */
23232 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23234 /* In a PCH file, despite the fact that the repository hasn't
23235 requested instantiation in the PCH it is still possible that
23236 an instantiation will be required in a file that includes the
23239 add_pending_template (d
);
23240 /* Instantiate inline functions so that the inliner can do its
23241 job, even though we'll not be emitting a copy of this
23243 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23247 bool push_to_top
, nested
;
23249 fn_context
= decl_function_context (d
);
23250 nested
= current_function_decl
!= NULL_TREE
;
23251 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23253 vec
<tree
> omp_privatization_save
;
23255 save_omp_privatization_clauses (omp_privatization_save
);
23258 push_to_top_level ();
23261 push_function_context ();
23262 cp_unevaluated_operand
= 0;
23263 c_inhibit_evaluation_warnings
= 0;
23266 /* Mark D as instantiated so that recursive calls to
23267 instantiate_decl do not try to instantiate it again. */
23268 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23270 /* Regenerate the declaration in case the template has been modified
23271 by a subsequent redeclaration. */
23272 regenerate_decl_from_template (d
, td
, args
);
23274 /* We already set the file and line above. Reset them now in case
23275 they changed as a result of calling regenerate_decl_from_template. */
23276 input_location
= DECL_SOURCE_LOCATION (d
);
23281 bool const_init
= false;
23283 /* Clear out DECL_RTL; whatever was there before may not be right
23284 since we've reset the type of the declaration. */
23285 SET_DECL_RTL (d
, NULL
);
23286 DECL_IN_AGGR_P (d
) = 0;
23288 /* The initializer is placed in DECL_INITIAL by
23289 regenerate_decl_from_template so we don't need to
23290 push/pop_access_scope again here. Pull it out so that
23291 cp_finish_decl can process it. */
23292 init
= DECL_INITIAL (d
);
23293 DECL_INITIAL (d
) = NULL_TREE
;
23294 DECL_INITIALIZED_P (d
) = 0;
23296 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23297 initializer. That function will defer actual emission until
23298 we have a chance to determine linkage. */
23299 DECL_EXTERNAL (d
) = 0;
23301 /* Enter the scope of D so that access-checking works correctly. */
23302 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23304 push_nested_class (DECL_CONTEXT (d
));
23306 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23307 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23310 pop_nested_class ();
23312 if (variable_template_p (gen_tmpl
))
23313 note_variable_template_instantiation (d
);
23315 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23316 synthesize_method (d
);
23317 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23319 /* Set up the list of local specializations. */
23320 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23321 tree block
= NULL_TREE
;
23323 /* Set up context. */
23324 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23325 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23326 block
= push_stmt_list ();
23328 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23330 /* Some typedefs referenced from within the template code need to be
23331 access checked at template instantiation time, i.e now. These
23332 types were added to the template at parsing time. Let's get those
23333 and perform the access checks then. */
23334 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23337 /* Create substitution entries for the parameters. */
23338 register_parameter_specializations (code_pattern
, d
);
23340 /* Substitute into the body of the function. */
23341 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23342 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23343 tf_warning_or_error
, tmpl
);
23346 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23347 tf_warning_or_error
, tmpl
,
23348 /*integral_constant_expression_p=*/false);
23350 /* Set the current input_location to the end of the function
23351 so that finish_function knows where we are. */
23353 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23355 /* Remember if we saw an infinite loop in the template. */
23356 current_function_infinite_loop
23357 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23360 /* Finish the function. */
23361 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23362 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23363 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23366 d
= finish_function (/*inline_p=*/false);
23367 expand_or_defer_fn (d
);
23370 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23371 cp_check_omp_declare_reduction (d
);
23374 /* We're not deferring instantiation any more. */
23375 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23378 pop_from_top_level ();
23380 pop_function_context ();
23383 restore_omp_privatization_clauses (omp_privatization_save
);
23386 pop_deferring_access_checks ();
23387 timevar_pop (TV_TEMPLATE_INST
);
23388 pop_tinst_level ();
23389 input_location
= saved_loc
;
23390 cp_unevaluated_operand
= saved_unevaluated_operand
;
23391 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23396 /* Run through the list of templates that we wish we could
23397 instantiate, and instantiate any we can. RETRIES is the
23398 number of times we retry pending template instantiation. */
23401 instantiate_pending_templates (int retries
)
23404 location_t saved_loc
= input_location
;
23406 /* Instantiating templates may trigger vtable generation. This in turn
23407 may require further template instantiations. We place a limit here
23408 to avoid infinite loop. */
23409 if (pending_templates
&& retries
>= max_tinst_depth
)
23411 tree decl
= pending_templates
->tinst
->decl
;
23413 fatal_error (input_location
,
23414 "template instantiation depth exceeds maximum of %d"
23415 " instantiating %q+D, possibly from virtual table generation"
23416 " (use -ftemplate-depth= to increase the maximum)",
23417 max_tinst_depth
, decl
);
23418 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23419 /* Pretend that we defined it. */
23420 DECL_INITIAL (decl
) = error_mark_node
;
23426 struct pending_template
**t
= &pending_templates
;
23427 struct pending_template
*last
= NULL
;
23431 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23432 bool complete
= false;
23434 if (TYPE_P (instantiation
))
23436 if (!COMPLETE_TYPE_P (instantiation
))
23438 instantiate_class_template (instantiation
);
23439 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23440 for (tree fld
= TYPE_FIELDS (instantiation
);
23441 fld
; fld
= TREE_CHAIN (fld
))
23443 || (TREE_CODE (fld
) == FUNCTION_DECL
23444 && !DECL_ARTIFICIAL (fld
)))
23445 && DECL_TEMPLATE_INSTANTIATION (fld
))
23446 instantiate_decl (fld
,
23447 /*defer_ok=*/false,
23448 /*expl_inst_class_mem_p=*/false);
23450 if (COMPLETE_TYPE_P (instantiation
))
23454 complete
= COMPLETE_TYPE_P (instantiation
);
23458 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23459 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23462 = instantiate_decl (instantiation
,
23463 /*defer_ok=*/false,
23464 /*expl_inst_class_mem_p=*/false);
23465 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23469 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23470 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23474 /* If INSTANTIATION has been instantiated, then we don't
23475 need to consider it again in the future. */
23483 current_tinst_level
= NULL
;
23485 last_pending_template
= last
;
23487 while (reconsider
);
23489 input_location
= saved_loc
;
23492 /* Substitute ARGVEC into T, which is a list of initializers for
23493 either base class or a non-static data member. The TREE_PURPOSEs
23494 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23495 instantiate_decl. */
23498 tsubst_initializer_list (tree t
, tree argvec
)
23500 tree inits
= NULL_TREE
;
23502 for (; t
; t
= TREE_CHAIN (t
))
23506 tree expanded_bases
= NULL_TREE
;
23507 tree expanded_arguments
= NULL_TREE
;
23510 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23515 /* Expand the base class expansion type into separate base
23517 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23518 tf_warning_or_error
,
23520 if (expanded_bases
== error_mark_node
)
23523 /* We'll be building separate TREE_LISTs of arguments for
23525 len
= TREE_VEC_LENGTH (expanded_bases
);
23526 expanded_arguments
= make_tree_vec (len
);
23527 for (i
= 0; i
< len
; i
++)
23528 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23530 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23531 expand each argument in the TREE_VALUE of t. */
23532 expr
= make_node (EXPR_PACK_EXPANSION
);
23533 PACK_EXPANSION_LOCAL_P (expr
) = true;
23534 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23535 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23537 if (TREE_VALUE (t
) == void_type_node
)
23538 /* VOID_TYPE_NODE is used to indicate
23539 value-initialization. */
23541 for (i
= 0; i
< len
; i
++)
23542 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23546 /* Substitute parameter packs into each argument in the
23548 in_base_initializer
= 1;
23549 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23551 tree expanded_exprs
;
23553 /* Expand the argument. */
23554 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23556 = tsubst_pack_expansion (expr
, argvec
,
23557 tf_warning_or_error
,
23559 if (expanded_exprs
== error_mark_node
)
23562 /* Prepend each of the expanded expressions to the
23563 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23564 for (i
= 0; i
< len
; i
++)
23566 TREE_VEC_ELT (expanded_arguments
, i
) =
23567 tree_cons (NULL_TREE
,
23568 TREE_VEC_ELT (expanded_exprs
, i
),
23569 TREE_VEC_ELT (expanded_arguments
, i
));
23572 in_base_initializer
= 0;
23574 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23575 since we built them backwards. */
23576 for (i
= 0; i
< len
; i
++)
23578 TREE_VEC_ELT (expanded_arguments
, i
) =
23579 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23584 for (i
= 0; i
< len
; ++i
)
23586 if (expanded_bases
)
23588 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23589 decl
= expand_member_init (decl
);
23590 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23595 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23596 tf_warning_or_error
, NULL_TREE
);
23598 decl
= expand_member_init (decl
);
23599 if (decl
&& !DECL_P (decl
))
23600 in_base_initializer
= 1;
23602 init
= TREE_VALUE (t
);
23604 if (init
!= void_type_node
)
23605 init
= tsubst_expr (init
, argvec
,
23606 tf_warning_or_error
, NULL_TREE
,
23607 /*integral_constant_expression_p=*/false);
23608 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23609 /* If we had an initializer but it instantiated to nothing,
23610 value-initialize the object. This will only occur when
23611 the initializer was a pack expansion where the parameter
23612 packs used in that expansion were of length zero. */
23613 init
= void_type_node
;
23614 in_base_initializer
= 0;
23619 init
= build_tree_list (decl
, init
);
23620 TREE_CHAIN (init
) = inits
;
23628 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23631 set_current_access_from_decl (tree decl
)
23633 if (TREE_PRIVATE (decl
))
23634 current_access_specifier
= access_private_node
;
23635 else if (TREE_PROTECTED (decl
))
23636 current_access_specifier
= access_protected_node
;
23638 current_access_specifier
= access_public_node
;
23641 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23642 is the instantiation (which should have been created with
23643 start_enum) and ARGS are the template arguments to use. */
23646 tsubst_enum (tree tag
, tree newtag
, tree args
)
23650 if (SCOPED_ENUM_P (newtag
))
23651 begin_scope (sk_scoped_enum
, newtag
);
23653 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23658 decl
= TREE_VALUE (e
);
23659 /* Note that in a template enum, the TREE_VALUE is the
23660 CONST_DECL, not the corresponding INTEGER_CST. */
23661 value
= tsubst_expr (DECL_INITIAL (decl
),
23662 args
, tf_warning_or_error
, NULL_TREE
,
23663 /*integral_constant_expression_p=*/true);
23665 /* Give this enumeration constant the correct access. */
23666 set_current_access_from_decl (decl
);
23668 /* Actually build the enumerator itself. Here we're assuming that
23669 enumerators can't have dependent attributes. */
23670 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23671 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23674 if (SCOPED_ENUM_P (newtag
))
23677 finish_enum_value_list (newtag
);
23678 finish_enum (newtag
);
23680 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23681 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23684 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23685 its type -- but without substituting the innermost set of template
23686 arguments. So, innermost set of template parameters will appear in
23690 get_mostly_instantiated_function_type (tree decl
)
23692 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23693 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23696 /* Return truthvalue if we're processing a template different from
23697 the last one involved in diagnostics. */
23699 problematic_instantiation_changed (void)
23701 return current_tinst_level
!= last_error_tinst_level
;
23704 /* Remember current template involved in diagnostics. */
23706 record_last_problematic_instantiation (void)
23708 last_error_tinst_level
= current_tinst_level
;
23711 struct tinst_level
*
23712 current_instantiation (void)
23714 return current_tinst_level
;
23717 /* Return TRUE if current_function_decl is being instantiated, false
23721 instantiating_current_function_p (void)
23723 return (current_instantiation ()
23724 && current_instantiation ()->decl
== current_function_decl
);
23727 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23728 type. Return false for ok, true for disallowed. Issue error and
23729 inform messages under control of COMPLAIN. */
23732 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23734 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23736 else if (POINTER_TYPE_P (type
))
23738 else if (TYPE_PTRMEM_P (type
))
23740 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23742 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23744 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23746 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23748 /* A bound template template parm could later be instantiated to have a valid
23749 nontype parm type via an alias template. */
23750 else if (cxx_dialect
>= cxx11
23751 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23754 if (complain
& tf_error
)
23756 if (type
== error_mark_node
)
23757 inform (input_location
, "invalid template non-type parameter");
23759 error ("%q#T is not a valid type for a template non-type parameter",
23765 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23766 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23769 dependent_type_p_r (tree type
)
23775 A type is dependent if it is:
23777 -- a template parameter. Template template parameters are types
23778 for us (since TYPE_P holds true for them) so we handle
23780 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23781 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23783 /* -- a qualified-id with a nested-name-specifier which contains a
23784 class-name that names a dependent type or whose unqualified-id
23785 names a dependent type. */
23786 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23789 /* An alias template specialization can be dependent even if the
23790 resulting type is not. */
23791 if (dependent_alias_template_spec_p (type
))
23794 /* -- a cv-qualified type where the cv-unqualified type is
23796 No code is necessary for this bullet; the code below handles
23797 cv-qualified types, and we don't want to strip aliases with
23798 TYPE_MAIN_VARIANT because of DR 1558. */
23799 /* -- a compound type constructed from any dependent type. */
23800 if (TYPE_PTRMEM_P (type
))
23801 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23802 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23804 else if (TYPE_PTR_P (type
)
23805 || TREE_CODE (type
) == REFERENCE_TYPE
)
23806 return dependent_type_p (TREE_TYPE (type
));
23807 else if (TREE_CODE (type
) == FUNCTION_TYPE
23808 || TREE_CODE (type
) == METHOD_TYPE
)
23812 if (dependent_type_p (TREE_TYPE (type
)))
23814 for (arg_type
= TYPE_ARG_TYPES (type
);
23816 arg_type
= TREE_CHAIN (arg_type
))
23817 if (dependent_type_p (TREE_VALUE (arg_type
)))
23819 if (cxx_dialect
>= cxx17
)
23820 /* A value-dependent noexcept-specifier makes the type dependent. */
23821 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
23822 if (tree noex
= TREE_PURPOSE (spec
))
23823 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23824 affect overload resolution and treating it as dependent breaks
23826 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
23827 && value_dependent_expression_p (noex
))
23831 /* -- an array type constructed from any dependent type or whose
23832 size is specified by a constant expression that is
23835 We checked for type- and value-dependence of the bounds in
23836 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23837 if (TREE_CODE (type
) == ARRAY_TYPE
)
23839 if (TYPE_DOMAIN (type
)
23840 && dependent_type_p (TYPE_DOMAIN (type
)))
23842 return dependent_type_p (TREE_TYPE (type
));
23845 /* -- a template-id in which either the template name is a template
23847 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23849 /* ... or any of the template arguments is a dependent type or
23850 an expression that is type-dependent or value-dependent. */
23851 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23852 && (any_dependent_template_arguments_p
23853 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23856 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23857 dependent; if the argument of the `typeof' expression is not
23858 type-dependent, then it should already been have resolved. */
23859 if (TREE_CODE (type
) == TYPEOF_TYPE
23860 || TREE_CODE (type
) == DECLTYPE_TYPE
23861 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23864 /* A template argument pack is dependent if any of its packed
23866 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23868 tree args
= ARGUMENT_PACK_ARGS (type
);
23869 int i
, len
= TREE_VEC_LENGTH (args
);
23870 for (i
= 0; i
< len
; ++i
)
23871 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23875 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23876 be template parameters. */
23877 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23880 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23883 /* The standard does not specifically mention types that are local
23884 to template functions or local classes, but they should be
23885 considered dependent too. For example:
23887 template <int I> void f() {
23892 The size of `E' cannot be known until the value of `I' has been
23893 determined. Therefore, `E' must be considered dependent. */
23894 scope
= TYPE_CONTEXT (type
);
23895 if (scope
&& TYPE_P (scope
))
23896 return dependent_type_p (scope
);
23897 /* Don't use type_dependent_expression_p here, as it can lead
23898 to infinite recursion trying to determine whether a lambda
23899 nested in a lambda is dependent (c++/47687). */
23900 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23901 && DECL_LANG_SPECIFIC (scope
)
23902 && DECL_TEMPLATE_INFO (scope
)
23903 && (any_dependent_template_arguments_p
23904 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23907 /* Other types are non-dependent. */
23911 /* Returns TRUE if TYPE is dependent, in the sense of
23912 [temp.dep.type]. Note that a NULL type is considered dependent. */
23915 dependent_type_p (tree type
)
23917 /* If there are no template parameters in scope, then there can't be
23918 any dependent types. */
23919 if (!processing_template_decl
)
23921 /* If we are not processing a template, then nobody should be
23922 providing us with a dependent type. */
23924 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23928 /* If the type is NULL, we have not computed a type for the entity
23929 in question; in that case, the type is dependent. */
23933 /* Erroneous types can be considered non-dependent. */
23934 if (type
== error_mark_node
)
23937 /* Getting here with global_type_node means we improperly called this
23938 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23939 gcc_checking_assert (type
!= global_type_node
);
23941 /* If we have not already computed the appropriate value for TYPE,
23943 if (!TYPE_DEPENDENT_P_VALID (type
))
23945 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23946 TYPE_DEPENDENT_P_VALID (type
) = 1;
23949 return TYPE_DEPENDENT_P (type
);
23952 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23953 lookup. In other words, a dependent type that is not the current
23957 dependent_scope_p (tree scope
)
23959 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23960 && !currently_open_class (scope
));
23963 /* T is a SCOPE_REF; return whether we need to consider it
23964 instantiation-dependent so that we can check access at instantiation
23965 time even though we know which member it resolves to. */
23968 instantiation_dependent_scope_ref_p (tree t
)
23970 if (DECL_P (TREE_OPERAND (t
, 1))
23971 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23972 /* A dependent base could make a member inaccessible in the current
23974 && !any_dependent_bases_p ()
23975 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23976 TREE_OPERAND (t
, 1)))
23982 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23983 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23986 /* Note that this predicate is not appropriate for general expressions;
23987 only constant expressions (that satisfy potential_constant_expression)
23988 can be tested for value dependence. */
23991 value_dependent_expression_p (tree expression
)
23993 if (!processing_template_decl
|| expression
== NULL_TREE
)
23996 /* A type-dependent expression is also value-dependent. */
23997 if (type_dependent_expression_p (expression
))
24000 switch (TREE_CODE (expression
))
24003 /* A dependent member function of the current instantiation. */
24004 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24006 case FUNCTION_DECL
:
24007 /* A dependent member function of the current instantiation. */
24008 if (DECL_CLASS_SCOPE_P (expression
)
24009 && dependent_type_p (DECL_CONTEXT (expression
)))
24013 case IDENTIFIER_NODE
:
24014 /* A name that has not been looked up -- must be dependent. */
24017 case TEMPLATE_PARM_INDEX
:
24018 /* A non-type template parm. */
24022 /* A non-type template parm. */
24023 if (DECL_TEMPLATE_PARM_P (expression
))
24025 return value_dependent_expression_p (DECL_INITIAL (expression
));
24028 /* A constant with literal type and is initialized
24029 with an expression that is value-dependent. */
24030 if (DECL_DEPENDENT_INIT_P (expression
)
24031 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24032 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24034 if (DECL_HAS_VALUE_EXPR_P (expression
))
24036 tree value_expr
= DECL_VALUE_EXPR (expression
);
24037 if (value_dependent_expression_p (value_expr
))
24042 case DYNAMIC_CAST_EXPR
:
24043 case STATIC_CAST_EXPR
:
24044 case CONST_CAST_EXPR
:
24045 case REINTERPRET_CAST_EXPR
:
24047 case IMPLICIT_CONV_EXPR
:
24048 /* These expressions are value-dependent if the type to which
24049 the cast occurs is dependent or the expression being casted
24050 is value-dependent. */
24052 tree type
= TREE_TYPE (expression
);
24054 if (dependent_type_p (type
))
24057 /* A functional cast has a list of operands. */
24058 expression
= TREE_OPERAND (expression
, 0);
24061 /* If there are no operands, it must be an expression such
24062 as "int()". This should not happen for aggregate types
24063 because it would form non-constant expressions. */
24064 gcc_assert (cxx_dialect
>= cxx11
24065 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24070 if (TREE_CODE (expression
) == TREE_LIST
)
24071 return any_value_dependent_elements_p (expression
);
24073 return value_dependent_expression_p (expression
);
24077 if (SIZEOF_EXPR_TYPE_P (expression
))
24078 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24082 /* A `sizeof' expression is value-dependent if the operand is
24083 type-dependent or is a pack expansion. */
24084 expression
= TREE_OPERAND (expression
, 0);
24085 if (PACK_EXPANSION_P (expression
))
24087 else if (TYPE_P (expression
))
24088 return dependent_type_p (expression
);
24089 return instantiation_dependent_uneval_expression_p (expression
);
24091 case AT_ENCODE_EXPR
:
24092 /* An 'encode' expression is value-dependent if the operand is
24094 expression
= TREE_OPERAND (expression
, 0);
24095 return dependent_type_p (expression
);
24097 case NOEXCEPT_EXPR
:
24098 expression
= TREE_OPERAND (expression
, 0);
24099 return instantiation_dependent_uneval_expression_p (expression
);
24102 /* All instantiation-dependent expressions should also be considered
24103 value-dependent. */
24104 return instantiation_dependent_scope_ref_p (expression
);
24106 case COMPONENT_REF
:
24107 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24108 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24110 case NONTYPE_ARGUMENT_PACK
:
24111 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24112 is value-dependent. */
24114 tree values
= ARGUMENT_PACK_ARGS (expression
);
24115 int i
, len
= TREE_VEC_LENGTH (values
);
24117 for (i
= 0; i
< len
; ++i
)
24118 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24126 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24128 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24134 if (TREE_CODE (type2
) != TREE_LIST
)
24135 return dependent_type_p (type2
);
24137 for (; type2
; type2
= TREE_CHAIN (type2
))
24138 if (dependent_type_p (TREE_VALUE (type2
)))
24145 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24146 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24149 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24150 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24154 tree op
= TREE_OPERAND (expression
, 0);
24155 return (value_dependent_expression_p (op
)
24156 || has_value_dependent_address (op
));
24159 case REQUIRES_EXPR
:
24160 /* Treat all requires-expressions as value-dependent so
24161 we don't try to fold them. */
24165 return dependent_type_p (TREE_OPERAND (expression
, 0));
24169 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24171 tree fn
= get_callee_fndecl (expression
);
24173 nargs
= call_expr_nargs (expression
);
24174 for (i
= 0; i
< nargs
; ++i
)
24176 tree op
= CALL_EXPR_ARG (expression
, i
);
24177 /* In a call to a constexpr member function, look through the
24178 implicit ADDR_EXPR on the object argument so that it doesn't
24179 cause the call to be considered value-dependent. We also
24180 look through it in potential_constant_expression. */
24181 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24182 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24183 && TREE_CODE (op
) == ADDR_EXPR
)
24184 op
= TREE_OPERAND (op
, 0);
24185 if (value_dependent_expression_p (op
))
24191 case TEMPLATE_ID_EXPR
:
24192 return variable_concept_p (TREE_OPERAND (expression
, 0));
24198 if (dependent_type_p (TREE_TYPE (expression
)))
24200 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24201 if (value_dependent_expression_p (val
))
24207 /* Treat a GNU statement expression as dependent to avoid crashing
24208 under instantiate_non_dependent_expr; it can't be constant. */
24212 /* A constant expression is value-dependent if any subexpression is
24213 value-dependent. */
24214 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24216 case tcc_reference
:
24218 case tcc_comparison
:
24220 case tcc_expression
:
24223 int i
, len
= cp_tree_operand_length (expression
);
24225 for (i
= 0; i
< len
; i
++)
24227 tree t
= TREE_OPERAND (expression
, i
);
24229 /* In some cases, some of the operands may be missing.
24230 (For example, in the case of PREDECREMENT_EXPR, the
24231 amount to increment by may be missing.) That doesn't
24232 make the expression dependent. */
24233 if (t
&& value_dependent_expression_p (t
))
24244 /* The expression is not value-dependent. */
24248 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24249 [temp.dep.expr]. Note that an expression with no type is
24250 considered dependent. Other parts of the compiler arrange for an
24251 expression with type-dependent subexpressions to have no type, so
24252 this function doesn't have to be fully recursive. */
24255 type_dependent_expression_p (tree expression
)
24257 if (!processing_template_decl
)
24260 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24263 STRIP_ANY_LOCATION_WRAPPER (expression
);
24265 /* An unresolved name is always dependent. */
24266 if (identifier_p (expression
)
24267 || TREE_CODE (expression
) == USING_DECL
24268 || TREE_CODE (expression
) == WILDCARD_DECL
)
24271 /* A fold expression is type-dependent. */
24272 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24273 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24274 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24275 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24278 /* Some expression forms are never type-dependent. */
24279 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24280 || TREE_CODE (expression
) == SIZEOF_EXPR
24281 || TREE_CODE (expression
) == ALIGNOF_EXPR
24282 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24283 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24284 || TREE_CODE (expression
) == TRAIT_EXPR
24285 || TREE_CODE (expression
) == TYPEID_EXPR
24286 || TREE_CODE (expression
) == DELETE_EXPR
24287 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24288 || TREE_CODE (expression
) == THROW_EXPR
24289 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24292 /* The types of these expressions depends only on the type to which
24293 the cast occurs. */
24294 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24295 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24296 || TREE_CODE (expression
) == CONST_CAST_EXPR
24297 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24298 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24299 || TREE_CODE (expression
) == CAST_EXPR
)
24300 return dependent_type_p (TREE_TYPE (expression
));
24302 /* The types of these expressions depends only on the type created
24303 by the expression. */
24304 if (TREE_CODE (expression
) == NEW_EXPR
24305 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24307 /* For NEW_EXPR tree nodes created inside a template, either
24308 the object type itself or a TREE_LIST may appear as the
24310 tree type
= TREE_OPERAND (expression
, 1);
24311 if (TREE_CODE (type
) == TREE_LIST
)
24312 /* This is an array type. We need to check array dimensions
24314 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24315 || value_dependent_expression_p
24316 (TREE_OPERAND (TREE_VALUE (type
), 1));
24318 return dependent_type_p (type
);
24321 if (TREE_CODE (expression
) == SCOPE_REF
)
24323 tree scope
= TREE_OPERAND (expression
, 0);
24324 tree name
= TREE_OPERAND (expression
, 1);
24326 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24327 contains an identifier associated by name lookup with one or more
24328 declarations declared with a dependent type, or...a
24329 nested-name-specifier or qualified-id that names a member of an
24330 unknown specialization. */
24331 return (type_dependent_expression_p (name
)
24332 || dependent_scope_p (scope
));
24335 if (TREE_CODE (expression
) == TEMPLATE_DECL
24336 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24337 return uses_outer_template_parms (expression
);
24339 if (TREE_CODE (expression
) == STMT_EXPR
)
24340 expression
= stmt_expr_value_expr (expression
);
24342 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24347 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24349 if (type_dependent_expression_p (elt
))
24355 /* A static data member of the current instantiation with incomplete
24356 array type is type-dependent, as the definition and specializations
24357 can have different bounds. */
24358 if (VAR_P (expression
)
24359 && DECL_CLASS_SCOPE_P (expression
)
24360 && dependent_type_p (DECL_CONTEXT (expression
))
24361 && VAR_HAD_UNKNOWN_BOUND (expression
))
24364 /* An array of unknown bound depending on a variadic parameter, eg:
24366 template<typename... Args>
24367 void foo (Args... args)
24369 int arr[] = { args... };
24372 template<int... vals>
24375 int arr[] = { vals... };
24378 If the array has no length and has an initializer, it must be that
24379 we couldn't determine its length in cp_complete_array_type because
24380 it is dependent. */
24381 if (VAR_P (expression
)
24382 && TREE_TYPE (expression
) != NULL_TREE
24383 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24384 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24385 && DECL_INITIAL (expression
))
24388 /* A function or variable template-id is type-dependent if it has any
24389 dependent template arguments. */
24390 if (VAR_OR_FUNCTION_DECL_P (expression
)
24391 && DECL_LANG_SPECIFIC (expression
)
24392 && DECL_TEMPLATE_INFO (expression
))
24394 /* Consider the innermost template arguments, since those are the ones
24395 that come from the template-id; the template arguments for the
24396 enclosing class do not make it type-dependent unless they are used in
24397 the type of the decl. */
24398 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24399 && (any_dependent_template_arguments_p
24400 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24404 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24405 type-dependent. Checking this is important for functions with auto return
24406 type, which looks like a dependent type. */
24407 if (TREE_CODE (expression
) == FUNCTION_DECL
24408 && !(DECL_CLASS_SCOPE_P (expression
)
24409 && dependent_type_p (DECL_CONTEXT (expression
)))
24410 && !(DECL_FRIEND_P (expression
)
24411 && (!DECL_FRIEND_CONTEXT (expression
)
24412 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24413 && !DECL_LOCAL_FUNCTION_P (expression
))
24415 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24416 || undeduced_auto_decl (expression
));
24420 /* Always dependent, on the number of arguments if nothing else. */
24421 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24424 if (TREE_TYPE (expression
) == unknown_type_node
)
24426 if (TREE_CODE (expression
) == ADDR_EXPR
)
24427 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24428 if (TREE_CODE (expression
) == COMPONENT_REF
24429 || TREE_CODE (expression
) == OFFSET_REF
)
24431 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24433 expression
= TREE_OPERAND (expression
, 1);
24434 if (identifier_p (expression
))
24437 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24438 if (TREE_CODE (expression
) == SCOPE_REF
)
24441 if (BASELINK_P (expression
))
24443 if (BASELINK_OPTYPE (expression
)
24444 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24446 expression
= BASELINK_FUNCTIONS (expression
);
24449 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24451 if (any_dependent_template_arguments_p
24452 (TREE_OPERAND (expression
, 1)))
24454 expression
= TREE_OPERAND (expression
, 0);
24455 if (identifier_p (expression
))
24459 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24460 || TREE_CODE (expression
) == FUNCTION_DECL
);
24462 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24463 if (type_dependent_expression_p (*iter
))
24469 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24471 /* Dependent type attributes might not have made it from the decl to
24473 if (DECL_P (expression
)
24474 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24477 return (dependent_type_p (TREE_TYPE (expression
)));
24480 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24481 type-dependent if the expression refers to a member of the current
24482 instantiation and the type of the referenced member is dependent, or the
24483 class member access expression refers to a member of an unknown
24486 This function returns true if the OBJECT in such a class member access
24487 expression is of an unknown specialization. */
24490 type_dependent_object_expression_p (tree object
)
24492 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24494 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24496 tree scope
= TREE_TYPE (object
);
24497 return (!scope
|| dependent_scope_p (scope
));
24500 /* walk_tree callback function for instantiation_dependent_expression_p,
24501 below. Returns non-zero if a dependent subexpression is found. */
24504 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24509 /* We don't have to worry about decltype currently because decltype
24510 of an instantiation-dependent expr is a dependent type. This
24511 might change depending on the resolution of DR 1172. */
24512 *walk_subtrees
= false;
24515 enum tree_code code
= TREE_CODE (*tp
);
24518 /* Don't treat an argument list as dependent just because it has no
24524 case TEMPLATE_PARM_INDEX
:
24527 /* Handle expressions with type operands. */
24531 case AT_ENCODE_EXPR
:
24533 tree op
= TREE_OPERAND (*tp
, 0);
24534 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24535 op
= TREE_TYPE (op
);
24538 if (dependent_type_p (op
))
24542 *walk_subtrees
= false;
24549 case COMPONENT_REF
:
24550 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24551 /* In a template, finish_class_member_access_expr creates a
24552 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24553 type-dependent, so that we can check access control at
24554 instantiation time (PR 42277). See also Core issue 1273. */
24559 if (instantiation_dependent_scope_ref_p (*tp
))
24564 /* Treat statement-expressions as dependent. */
24568 /* Treat requires-expressions as dependent. */
24569 case REQUIRES_EXPR
:
24573 /* Treat calls to function concepts as dependent. */
24574 if (function_concept_check_p (*tp
))
24578 case TEMPLATE_ID_EXPR
:
24579 /* And variable concepts. */
24580 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24588 if (type_dependent_expression_p (*tp
))
24594 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24595 sense defined by the ABI:
24597 "An expression is instantiation-dependent if it is type-dependent
24598 or value-dependent, or it has a subexpression that is type-dependent
24599 or value-dependent."
24601 Except don't actually check value-dependence for unevaluated expressions,
24602 because in sizeof(i) we don't care about the value of i. Checking
24603 type-dependence will in turn check value-dependence of array bounds/template
24604 arguments as needed. */
24607 instantiation_dependent_uneval_expression_p (tree expression
)
24611 if (!processing_template_decl
)
24614 if (expression
== error_mark_node
)
24617 result
= cp_walk_tree_without_duplicates (&expression
,
24618 instantiation_dependent_r
, NULL
);
24619 return result
!= NULL_TREE
;
24622 /* As above, but also check value-dependence of the expression as a whole. */
24625 instantiation_dependent_expression_p (tree expression
)
24627 return (instantiation_dependent_uneval_expression_p (expression
)
24628 || value_dependent_expression_p (expression
));
24631 /* Like type_dependent_expression_p, but it also works while not processing
24632 a template definition, i.e. during substitution or mangling. */
24635 type_dependent_expression_p_push (tree expr
)
24638 ++processing_template_decl
;
24639 b
= type_dependent_expression_p (expr
);
24640 --processing_template_decl
;
24644 /* Returns TRUE if ARGS contains a type-dependent expression. */
24647 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24652 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24654 if (type_dependent_expression_p (arg
))
24660 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24661 expressions) contains any type-dependent expressions. */
24664 any_type_dependent_elements_p (const_tree list
)
24666 for (; list
; list
= TREE_CHAIN (list
))
24667 if (type_dependent_expression_p (TREE_VALUE (list
)))
24673 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24674 expressions) contains any value-dependent expressions. */
24677 any_value_dependent_elements_p (const_tree list
)
24679 for (; list
; list
= TREE_CHAIN (list
))
24680 if (value_dependent_expression_p (TREE_VALUE (list
)))
24686 /* Returns TRUE if the ARG (a template argument) is dependent. */
24689 dependent_template_arg_p (tree arg
)
24691 if (!processing_template_decl
)
24694 /* Assume a template argument that was wrongly written by the user
24695 is dependent. This is consistent with what
24696 any_dependent_template_arguments_p [that calls this function]
24698 if (!arg
|| arg
== error_mark_node
)
24701 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24702 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24704 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24706 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24708 if (DECL_TEMPLATE_PARM_P (arg
))
24710 /* A member template of a dependent class is not necessarily
24711 type-dependent, but it is a dependent template argument because it
24712 will be a member of an unknown specialization to that template. */
24713 tree scope
= CP_DECL_CONTEXT (arg
);
24714 return TYPE_P (scope
) && dependent_type_p (scope
);
24716 else if (ARGUMENT_PACK_P (arg
))
24718 tree args
= ARGUMENT_PACK_ARGS (arg
);
24719 int i
, len
= TREE_VEC_LENGTH (args
);
24720 for (i
= 0; i
< len
; ++i
)
24722 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24728 else if (TYPE_P (arg
))
24729 return dependent_type_p (arg
);
24731 return (type_dependent_expression_p (arg
)
24732 || value_dependent_expression_p (arg
));
24735 /* Returns true if ARGS (a collection of template arguments) contains
24736 any types that require structural equality testing. */
24739 any_template_arguments_need_structural_equality_p (tree args
)
24746 if (args
== error_mark_node
)
24749 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24751 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24752 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24754 tree arg
= TREE_VEC_ELT (level
, j
);
24755 tree packed_args
= NULL_TREE
;
24758 if (ARGUMENT_PACK_P (arg
))
24760 /* Look inside the argument pack. */
24761 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24762 len
= TREE_VEC_LENGTH (packed_args
);
24765 for (k
= 0; k
< len
; ++k
)
24768 arg
= TREE_VEC_ELT (packed_args
, k
);
24770 if (error_operand_p (arg
))
24772 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24774 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24776 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24777 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24786 /* Returns true if ARGS (a collection of template arguments) contains
24787 any dependent arguments. */
24790 any_dependent_template_arguments_p (const_tree args
)
24797 if (args
== error_mark_node
)
24800 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24802 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24803 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24804 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24811 /* Returns TRUE if the template TMPL is type-dependent. */
24814 dependent_template_p (tree tmpl
)
24816 if (TREE_CODE (tmpl
) == OVERLOAD
)
24818 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24819 if (dependent_template_p (*iter
))
24824 /* Template template parameters are dependent. */
24825 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24826 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24828 /* So are names that have not been looked up. */
24829 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24834 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24837 dependent_template_id_p (tree tmpl
, tree args
)
24839 return (dependent_template_p (tmpl
)
24840 || any_dependent_template_arguments_p (args
));
24843 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24847 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24851 if (!processing_template_decl
)
24854 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24856 tree decl
= TREE_VEC_ELT (declv
, i
);
24857 tree init
= TREE_VEC_ELT (initv
, i
);
24858 tree cond
= TREE_VEC_ELT (condv
, i
);
24859 tree incr
= TREE_VEC_ELT (incrv
, i
);
24861 if (type_dependent_expression_p (decl
)
24862 || TREE_CODE (decl
) == SCOPE_REF
)
24865 if (init
&& type_dependent_expression_p (init
))
24868 if (type_dependent_expression_p (cond
))
24871 if (COMPARISON_CLASS_P (cond
)
24872 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24873 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24876 if (TREE_CODE (incr
) == MODOP_EXPR
)
24878 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24879 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24882 else if (type_dependent_expression_p (incr
))
24884 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24886 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24888 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24890 tree t
= TREE_OPERAND (incr
, 1);
24891 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24892 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24901 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24902 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24903 no such TYPE can be found. Note that this function peers inside
24904 uninstantiated templates and therefore should be used only in
24905 extremely limited situations. ONLY_CURRENT_P restricts this
24906 peering to the currently open classes hierarchy (which is required
24907 when comparing types). */
24910 resolve_typename_type (tree type
, bool only_current_p
)
24919 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24921 scope
= TYPE_CONTEXT (type
);
24922 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24923 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24924 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24925 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24926 identifier of the TYPENAME_TYPE anymore.
24927 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24928 TYPENAME_TYPE instead, we avoid messing up with a possible
24929 typedef variant case. */
24930 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24932 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24933 it first before we can figure out what NAME refers to. */
24934 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24936 if (TYPENAME_IS_RESOLVING_P (scope
))
24937 /* Given a class template A with a dependent base with nested type C,
24938 typedef typename A::C::C C will land us here, as trying to resolve
24939 the initial A::C leads to the local C typedef, which leads back to
24940 A::C::C. So we break the recursion now. */
24943 scope
= resolve_typename_type (scope
, only_current_p
);
24945 /* If we don't know what SCOPE refers to, then we cannot resolve the
24947 if (!CLASS_TYPE_P (scope
))
24949 /* If this is a typedef, we don't want to look inside (c++/11987). */
24950 if (typedef_variant_p (type
))
24952 /* If SCOPE isn't the template itself, it will not have a valid
24953 TYPE_FIELDS list. */
24954 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24955 /* scope is either the template itself or a compatible instantiation
24956 like X<T>, so look up the name in the original template. */
24957 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24958 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24959 gcc_checking_assert (uses_template_parms (scope
));
24960 /* If scope has no fields, it can't be a current instantiation. Check this
24961 before currently_open_class to avoid infinite recursion (71515). */
24962 if (!TYPE_FIELDS (scope
))
24964 /* If the SCOPE is not the current instantiation, there's no reason
24965 to look inside it. */
24966 if (only_current_p
&& !currently_open_class (scope
))
24968 /* Enter the SCOPE so that name lookup will be resolved as if we
24969 were in the class definition. In particular, SCOPE will no
24970 longer be considered a dependent type. */
24971 pushed_scope
= push_scope (scope
);
24972 /* Look up the declaration. */
24973 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24974 tf_warning_or_error
);
24976 result
= NULL_TREE
;
24978 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24979 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24980 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
24983 else if (identifier_p (fullname
)
24984 && TREE_CODE (decl
) == TYPE_DECL
)
24986 result
= TREE_TYPE (decl
);
24987 if (result
== error_mark_node
)
24988 result
= NULL_TREE
;
24990 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
24991 && DECL_CLASS_TEMPLATE_P (decl
))
24993 /* Obtain the template and the arguments. */
24994 tree tmpl
= TREE_OPERAND (fullname
, 0);
24995 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
24997 /* We get here with a plain identifier because a previous tentative
24998 parse of the nested-name-specifier as part of a ptr-operator saw
24999 ::template X<A>. The use of ::template is necessary in a
25000 ptr-operator, but wrong in a declarator-id.
25002 [temp.names]: In a qualified-id of a declarator-id, the keyword
25003 template shall not appear at the top level. */
25004 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25005 "keyword %<template%> not allowed in declarator-id");
25008 tree args
= TREE_OPERAND (fullname
, 1);
25009 /* Instantiate the template. */
25010 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25011 /*entering_scope=*/true,
25012 tf_error
| tf_user
);
25013 if (result
== error_mark_node
)
25014 result
= NULL_TREE
;
25017 /* Leave the SCOPE. */
25019 pop_scope (pushed_scope
);
25021 /* If we failed to resolve it, return the original typename. */
25025 /* If lookup found a typename type, resolve that too. */
25026 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25028 /* Ill-formed programs can cause infinite recursion here, so we
25029 must catch that. */
25030 TYPENAME_IS_RESOLVING_P (result
) = 1;
25031 result
= resolve_typename_type (result
, only_current_p
);
25032 TYPENAME_IS_RESOLVING_P (result
) = 0;
25035 /* Qualify the resulting type. */
25036 quals
= cp_type_quals (type
);
25038 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25043 /* EXPR is an expression which is not type-dependent. Return a proxy
25044 for EXPR that can be used to compute the types of larger
25045 expressions containing EXPR. */
25048 build_non_dependent_expr (tree expr
)
25050 tree orig_expr
= expr
;
25053 /* When checking, try to get a constant value for all non-dependent
25054 expressions in order to expose bugs in *_dependent_expression_p
25055 and constexpr. This can affect code generation, see PR70704, so
25056 only do this for -fchecking=2. */
25057 if (flag_checking
> 1
25058 && cxx_dialect
>= cxx11
25059 /* Don't do this during nsdmi parsing as it can lead to
25060 unexpected recursive instantiations. */
25061 && !parsing_nsdmi ()
25062 /* Don't do this during concept expansion either and for
25063 the same reason. */
25064 && !expanding_concept ())
25065 fold_non_dependent_expr (expr
);
25067 STRIP_ANY_LOCATION_WRAPPER (expr
);
25069 /* Preserve OVERLOADs; the functions must be available to resolve
25072 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25073 inner_expr
= stmt_expr_value_expr (inner_expr
);
25074 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25075 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25076 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25077 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25078 if (is_overloaded_fn (inner_expr
)
25079 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25081 /* There is no need to return a proxy for a variable. */
25084 /* Preserve string constants; conversions from string constants to
25085 "char *" are allowed, even though normally a "const char *"
25086 cannot be used to initialize a "char *". */
25087 if (TREE_CODE (expr
) == STRING_CST
)
25089 /* Preserve void and arithmetic constants, as an optimization -- there is no
25090 reason to create a new node. */
25091 if (TREE_CODE (expr
) == VOID_CST
25092 || TREE_CODE (expr
) == INTEGER_CST
25093 || TREE_CODE (expr
) == REAL_CST
)
25095 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25096 There is at least one place where we want to know that a
25097 particular expression is a throw-expression: when checking a ?:
25098 expression, there are special rules if the second or third
25099 argument is a throw-expression. */
25100 if (TREE_CODE (expr
) == THROW_EXPR
)
25103 /* Don't wrap an initializer list, we need to be able to look inside. */
25104 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25107 /* Don't wrap a dummy object, we need to be able to test for it. */
25108 if (is_dummy_object (expr
))
25111 if (TREE_CODE (expr
) == COND_EXPR
)
25112 return build3 (COND_EXPR
,
25114 TREE_OPERAND (expr
, 0),
25115 (TREE_OPERAND (expr
, 1)
25116 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25117 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25118 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25119 if (TREE_CODE (expr
) == COMPOUND_EXPR
25120 && !COMPOUND_EXPR_OVERLOADED (expr
))
25121 return build2 (COMPOUND_EXPR
,
25123 TREE_OPERAND (expr
, 0),
25124 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25126 /* If the type is unknown, it can't really be non-dependent */
25127 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25129 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25130 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25133 /* ARGS is a vector of expressions as arguments to a function call.
25134 Replace the arguments with equivalent non-dependent expressions.
25135 This modifies ARGS in place. */
25138 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25143 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25145 tree newarg
= build_non_dependent_expr (arg
);
25147 (*args
)[ix
] = newarg
;
25151 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25152 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25153 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25156 make_auto_1 (tree name
, bool set_canonical
)
25158 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25159 TYPE_NAME (au
) = build_decl (input_location
,
25160 TYPE_DECL
, name
, au
);
25161 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25162 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25163 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25164 TYPE_NAME (au
), NULL_TREE
);
25166 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25167 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25168 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25174 make_decltype_auto (void)
25176 return make_auto_1 (decltype_auto_identifier
, true);
25182 return make_auto_1 (auto_identifier
, true);
25185 /* Return a C++17 deduction placeholder for class template TMPL. */
25188 make_template_placeholder (tree tmpl
)
25190 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25191 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25195 /* True iff T is a C++17 class template deduction placeholder. */
25198 template_placeholder_p (tree t
)
25200 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25203 /* Make a "constrained auto" type-specifier. This is an
25204 auto type with constraints that must be associated after
25205 deduction. The constraint is formed from the given
25206 CONC and its optional sequence of arguments, which are
25207 non-null if written as partial-concept-id. */
25210 make_constrained_auto (tree con
, tree args
)
25212 tree type
= make_auto_1 (auto_identifier
, false);
25214 /* Build the constraint. */
25215 tree tmpl
= DECL_TI_TEMPLATE (con
);
25216 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25217 expr
= build_concept_check (expr
, type
, args
);
25219 tree constr
= normalize_expression (expr
);
25220 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25222 /* Our canonical type depends on the constraint. */
25223 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25225 /* Attach the constraint to the type declaration. */
25226 tree decl
= TYPE_NAME (type
);
25230 /* Given type ARG, return std::initializer_list<ARG>. */
25235 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25237 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25239 gcc_rich_location
richloc (input_location
);
25240 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25241 error_at (&richloc
,
25242 "deducing from brace-enclosed initializer list"
25243 " requires %<#include <initializer_list>%>");
25245 return error_mark_node
;
25247 tree argvec
= make_tree_vec (1);
25248 TREE_VEC_ELT (argvec
, 0) = arg
;
25250 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25251 NULL_TREE
, 0, tf_warning_or_error
);
25254 /* Replace auto in TYPE with std::initializer_list<auto>. */
25257 listify_autos (tree type
, tree auto_node
)
25259 tree init_auto
= listify (auto_node
);
25260 tree argvec
= make_tree_vec (1);
25261 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25262 if (processing_template_decl
)
25263 argvec
= add_to_template_args (current_template_args (), argvec
);
25264 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25267 /* Hash traits for hashing possibly constrained 'auto'
25268 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25270 struct auto_hash
: default_hash_traits
<tree
>
25272 static inline hashval_t
hash (tree
);
25273 static inline bool equal (tree
, tree
);
25276 /* Hash the 'auto' T. */
25279 auto_hash::hash (tree t
)
25281 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25282 /* Matching constrained-type-specifiers denote the same template
25283 parameter, so hash the constraint. */
25284 return hash_placeholder_constraint (c
);
25286 /* But unconstrained autos are all separate, so just hash the pointer. */
25287 return iterative_hash_object (t
, 0);
25290 /* Compare two 'auto's. */
25293 auto_hash::equal (tree t1
, tree t2
)
25298 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25299 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25301 /* Two unconstrained autos are distinct. */
25305 return equivalent_placeholder_constraints (c1
, c2
);
25308 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25309 constrained) auto, add it to the vector. */
25312 extract_autos_r (tree t
, void *data
)
25314 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25317 /* All the autos were built with index 0; fix that up now. */
25318 tree
*p
= hash
.find_slot (t
, INSERT
);
25321 /* If this is a repeated constrained-type-specifier, use the index we
25323 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25326 /* Otherwise this is new, so use the current count. */
25328 idx
= hash
.elements () - 1;
25330 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25333 /* Always keep walking. */
25337 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25338 says they can appear anywhere in the type. */
25341 extract_autos (tree type
)
25343 hash_set
<tree
> visited
;
25344 hash_table
<auto_hash
> hash (2);
25346 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25348 tree tree_vec
= make_tree_vec (hash
.elements());
25349 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25350 iter
!= hash
.end(); ++iter
)
25353 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25354 TREE_VEC_ELT (tree_vec
, i
)
25355 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25361 /* The stem for deduction guide names. */
25362 const char *const dguide_base
= "__dguide_";
25364 /* Return the name for a deduction guide for class template TMPL. */
25367 dguide_name (tree tmpl
)
25369 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25370 tree tname
= TYPE_IDENTIFIER (type
);
25371 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25372 + IDENTIFIER_LENGTH (tname
));
25373 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25374 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25375 IDENTIFIER_LENGTH (tname
) + 1);
25376 tree dname
= get_identifier (buf
);
25377 TREE_TYPE (dname
) = type
;
25381 /* True if NAME is the name of a deduction guide. */
25384 dguide_name_p (tree name
)
25386 return (TREE_TYPE (name
)
25387 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25388 strlen (dguide_base
)));
25391 /* True if FN is a deduction guide. */
25394 deduction_guide_p (const_tree fn
)
25397 if (tree name
= DECL_NAME (fn
))
25398 return dguide_name_p (name
);
25402 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25405 copy_guide_p (const_tree fn
)
25407 gcc_assert (deduction_guide_p (fn
));
25408 if (!DECL_ARTIFICIAL (fn
))
25410 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25411 return (TREE_CHAIN (parms
) == void_list_node
25412 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25415 /* True if FN is a guide generated from a constructor template. */
25418 template_guide_p (const_tree fn
)
25420 gcc_assert (deduction_guide_p (fn
));
25421 if (!DECL_ARTIFICIAL (fn
))
25423 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25424 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25425 return PRIMARY_TEMPLATE_P (org
);
25429 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25430 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25431 template parameter types. Note that the handling of template template
25432 parameters relies on current_template_parms being set appropriately for the
25436 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25437 tree tsubst_args
, tsubst_flags_t complain
)
25439 tree oldidx
= get_template_parm_index (olddecl
);
25442 if (TREE_CODE (olddecl
) == TYPE_DECL
25443 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25445 tree oldtype
= TREE_TYPE (olddecl
);
25446 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25447 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25448 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25449 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25450 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25453 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25454 complain
, NULL_TREE
);
25457 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25458 DECL_NAME (olddecl
), newtype
);
25459 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25462 if (TREE_CODE (olddecl
) == TYPE_DECL
25463 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25465 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25466 = build_template_parm_index (index
, level
, level
,
25468 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25469 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25470 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25471 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25473 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25475 DECL_TEMPLATE_RESULT (newdecl
)
25476 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25477 DECL_NAME (olddecl
), newtype
);
25478 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25479 // First create a copy (ttargs) of tsubst_args with an
25480 // additional level for the template template parameter's own
25481 // template parameters (ttparms).
25482 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25483 (DECL_TEMPLATE_PARMS (olddecl
)));
25484 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25485 tree ttargs
= make_tree_vec (depth
+ 1);
25486 for (int i
= 0; i
< depth
; ++i
)
25487 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25488 TREE_VEC_ELT (ttargs
, depth
)
25489 = template_parms_level_to_args (ttparms
);
25490 // Substitute ttargs into ttparms to fix references to
25491 // other template parameters.
25492 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25494 // Now substitute again with args based on tparms, to reduce
25495 // the level of the ttparms.
25496 ttargs
= current_template_args ();
25497 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25499 // Finally, tack the adjusted parms onto tparms.
25500 ttparms
= tree_cons (size_int (depth
), ttparms
,
25501 current_template_parms
);
25502 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25507 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25509 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25510 TREE_CODE (oldconst
),
25511 DECL_NAME (oldconst
), newtype
);
25512 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25513 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25514 SET_DECL_TEMPLATE_PARM_P (newconst
);
25515 newidx
= build_template_parm_index (index
, level
, level
,
25516 newconst
, newtype
);
25517 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25518 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25519 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25525 /* Returns a C++17 class deduction guide template based on the constructor
25526 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25527 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25530 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25532 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25533 bool memtmpl
= false;
25536 tree fn_tmpl
= NULL_TREE
;
25541 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25544 type
= TREE_TYPE (type
);
25545 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25548 fparms
= void_list_node
;
25550 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25551 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25552 targs
= CLASSTYPE_TI_ARGS (type
);
25555 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25556 explicit_p
= false;
25560 ++processing_template_decl
;
25563 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25564 : DECL_TI_TEMPLATE (ctor
));
25566 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25567 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25569 type
= DECL_CONTEXT (ctor
);
25571 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25572 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25573 fully specialized args for the enclosing class. Strip those off, as
25574 the deduction guide won't have those template parameters. */
25575 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25576 TMPL_PARMS_DEPTH (tparms
));
25577 /* Discard the 'this' parameter. */
25578 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25579 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25580 ci
= get_constraints (ctor
);
25581 loc
= DECL_SOURCE_LOCATION (ctor
);
25582 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25584 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25588 /* For a member template constructor, we need to flatten the two
25589 template parameter lists into one, and then adjust the function
25590 signature accordingly. This gets...complicated. */
25591 tree save_parms
= current_template_parms
;
25593 /* For a member template we should have two levels of parms/args, one
25594 for the class and one for the constructor. We stripped
25595 specialized args for further enclosing classes above. */
25596 const int depth
= 2;
25597 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25599 /* Template args for translating references to the two-level template
25600 parameters into references to the one-level template parameters we
25602 tree tsubst_args
= copy_node (targs
);
25603 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25604 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25606 /* Template parms for the constructor template. */
25607 tree ftparms
= TREE_VALUE (tparms
);
25608 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25609 /* Template parms for the class template. */
25610 tparms
= TREE_CHAIN (tparms
);
25611 tree ctparms
= TREE_VALUE (tparms
);
25612 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25613 /* Template parms for the deduction guide start as a copy of the
25614 template parms for the class. We set current_template_parms for
25615 lookup_template_class_1. */
25616 current_template_parms
= tparms
= copy_node (tparms
);
25617 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25618 for (unsigned i
= 0; i
< clen
; ++i
)
25619 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25621 /* Now we need to rewrite the constructor parms to append them to the
25623 for (unsigned i
= 0; i
< flen
; ++i
)
25625 unsigned index
= i
+ clen
;
25626 unsigned level
= 1;
25627 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25628 tree olddecl
= TREE_VALUE (oldelt
);
25629 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25630 tsubst_args
, complain
);
25631 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25632 tsubst_args
, complain
, ctor
);
25633 tree list
= build_tree_list (newdef
, newdecl
);
25634 TEMPLATE_PARM_CONSTRAINTS (list
)
25635 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25636 tsubst_args
, complain
, ctor
);
25637 TREE_VEC_ELT (new_vec
, index
) = list
;
25638 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25641 /* Now we have a final set of template parms to substitute into the
25642 function signature. */
25643 targs
= template_parms_to_args (tparms
);
25644 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25646 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25648 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25650 current_template_parms
= save_parms
;
25652 --processing_template_decl
;
25657 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25658 tparms
= copy_node (tparms
);
25659 INNERMOST_TEMPLATE_PARMS (tparms
)
25660 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25663 tree fntype
= build_function_type (type
, fparms
);
25664 tree ded_fn
= build_lang_decl_loc (loc
,
25666 dguide_name (type
), fntype
);
25667 DECL_ARGUMENTS (ded_fn
) = fargs
;
25668 DECL_ARTIFICIAL (ded_fn
) = true;
25669 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25670 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25671 DECL_ARTIFICIAL (ded_tmpl
) = true;
25672 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25673 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25674 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25675 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25677 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25679 set_constraints (ded_tmpl
, ci
);
25684 /* Deduce template arguments for the class template placeholder PTYPE for
25685 template TMPL based on the initializer INIT, and return the resulting
25689 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25690 tsubst_flags_t complain
)
25692 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25694 /* We should have handled this in the caller. */
25695 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25697 if (complain
& tf_error
)
25698 error ("non-class template %qT used without template arguments", tmpl
);
25699 return error_mark_node
;
25702 tree type
= TREE_TYPE (tmpl
);
25704 bool try_list_ctor
= false;
25706 vec
<tree
,va_gc
> *args
;
25707 if (init
== NULL_TREE
25708 || TREE_CODE (init
) == TREE_LIST
)
25709 args
= make_tree_vector_from_list (init
);
25710 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25712 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25713 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25715 /* As an exception, the first phase in 16.3.1.7 (considering the
25716 initializer list as a single argument) is omitted if the
25717 initializer list consists of a single expression of type cv U,
25718 where U is a specialization of C or a class derived from a
25719 specialization of C. */
25720 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25721 tree etype
= TREE_TYPE (elt
);
25723 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25724 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25725 int err
= unify (tparms
, targs
, type
, etype
,
25726 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25728 try_list_ctor
= false;
25731 if (try_list_ctor
|| is_std_init_list (type
))
25732 args
= make_tree_vector_single (init
);
25734 args
= make_tree_vector_from_ctor (init
);
25737 args
= make_tree_vector_single (init
);
25739 tree dname
= dguide_name (tmpl
);
25740 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25741 /*type*/false, /*complain*/false,
25743 bool elided
= false;
25744 if (cands
== error_mark_node
)
25747 /* Prune explicit deduction guides in copy-initialization context. */
25748 if (flags
& LOOKUP_ONLYCONVERTING
)
25750 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25751 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25756 /* Found a nonconverting guide, prune the candidates. */
25757 tree pruned
= NULL_TREE
;
25758 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25759 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25760 pruned
= lookup_add (*iter
, pruned
);
25766 tree outer_args
= NULL_TREE
;
25767 if (DECL_CLASS_SCOPE_P (tmpl
)
25768 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25770 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25771 type
= TREE_TYPE (most_general_template (tmpl
));
25774 bool saw_ctor
= false;
25775 // FIXME cache artificial deduction guides
25776 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
25778 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25779 if ((flags
& LOOKUP_ONLYCONVERTING
)
25780 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25783 cands
= lookup_add (guide
, cands
);
25788 tree call
= error_mark_node
;
25790 /* If this is list-initialization and the class has a list constructor, first
25791 try deducing from the list as a single argument, as [over.match.list]. */
25792 tree list_cands
= NULL_TREE
;
25793 if (try_list_ctor
&& cands
)
25794 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25797 if (is_list_ctor (dg
))
25798 list_cands
= lookup_add (dg
, list_cands
);
25802 ++cp_unevaluated_operand
;
25803 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
25804 --cp_unevaluated_operand
;
25806 if (call
== error_mark_node
)
25808 /* That didn't work, now try treating the list as a sequence of
25810 release_tree_vector (args
);
25811 args
= make_tree_vector_from_ctor (init
);
25815 /* Maybe generate an implicit deduction guide. */
25816 if (call
== error_mark_node
&& args
->length () < 2)
25818 tree gtype
= NULL_TREE
;
25820 if (args
->length () == 1)
25821 /* Generate a copy guide. */
25822 gtype
= build_reference_type (type
);
25823 else if (!saw_ctor
)
25824 /* Generate a default guide. */
25829 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25830 cands
= lookup_add (guide
, cands
);
25834 if (elided
&& !cands
)
25836 error ("cannot deduce template arguments for copy-initialization"
25837 " of %qT, as it has no non-explicit deduction guides or "
25838 "user-declared constructors", type
);
25839 return error_mark_node
;
25841 else if (!cands
&& call
== error_mark_node
)
25843 error ("cannot deduce template arguments of %qT, as it has no viable "
25844 "deduction guides", type
);
25845 return error_mark_node
;
25848 if (call
== error_mark_node
)
25850 ++cp_unevaluated_operand
;
25851 call
= build_new_function_call (cands
, &args
, tf_decltype
);
25852 --cp_unevaluated_operand
;
25855 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
25857 error ("class template argument deduction failed:");
25859 ++cp_unevaluated_operand
;
25860 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25861 --cp_unevaluated_operand
;
25864 inform (input_location
, "explicit deduction guides not considered "
25865 "for copy-initialization");
25868 release_tree_vector (args
);
25870 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
25873 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25874 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25877 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25879 return do_auto_deduction (type
, init
, auto_node
,
25880 tf_warning_or_error
,
25884 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25885 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25886 The CONTEXT determines the context in which auto deduction is performed
25887 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25888 OUTER_TARGS are used during template argument deduction
25889 (context == adc_unify) to properly substitute the result, and is ignored
25892 For partial-concept-ids, extra args may be appended to the list of deduced
25893 template arguments prior to determining constraint satisfaction. */
25896 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25897 tsubst_flags_t complain
, auto_deduction_context context
,
25898 tree outer_targs
, int flags
)
25902 if (init
== error_mark_node
)
25903 return error_mark_node
;
25905 if (init
&& type_dependent_expression_p (init
)
25906 && context
!= adc_unify
)
25907 /* Defining a subset of type-dependent expressions that we can deduce
25908 from ahead of time isn't worth the trouble. */
25911 /* Similarly, we can't deduce from another undeduced decl. */
25912 if (init
&& undeduced_auto_decl (init
))
25915 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25916 /* C++17 class template argument deduction. */
25917 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25919 if (TREE_TYPE (init
) == NULL_TREE
)
25920 /* Nothing we can do with this, even in deduction context. */
25923 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25924 with either a new invented type template parameter U or, if the
25925 initializer is a braced-init-list (8.5.4), with
25926 std::initializer_list<U>. */
25927 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25929 if (!DIRECT_LIST_INIT_P (init
))
25930 type
= listify_autos (type
, auto_node
);
25931 else if (CONSTRUCTOR_NELTS (init
) == 1)
25932 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25935 if (complain
& tf_warning_or_error
)
25937 if (permerror (input_location
, "direct-list-initialization of "
25938 "%<auto%> requires exactly one element"))
25939 inform (input_location
,
25940 "for deduction to %<std::initializer_list%>, use copy-"
25941 "list-initialization (i.e. add %<=%> before the %<{%>)");
25943 type
= listify_autos (type
, auto_node
);
25947 if (type
== error_mark_node
)
25948 return error_mark_node
;
25950 init
= resolve_nondeduced_context (init
, complain
);
25952 if (context
== adc_decomp_type
25953 && auto_node
== type
25954 && init
!= error_mark_node
25955 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
25956 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25957 and initializer has array type, deduce cv-qualified array type. */
25958 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
25960 else if (AUTO_IS_DECLTYPE (auto_node
))
25962 bool id
= (DECL_P (init
)
25963 || ((TREE_CODE (init
) == COMPONENT_REF
25964 || TREE_CODE (init
) == SCOPE_REF
)
25965 && !REF_PARENTHESIZED_P (init
)));
25966 targs
= make_tree_vec (1);
25967 TREE_VEC_ELT (targs
, 0)
25968 = finish_decltype_type (init
, id
, tf_warning_or_error
);
25969 if (type
!= auto_node
)
25971 if (complain
& tf_error
)
25972 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
25973 return error_mark_node
;
25978 tree parms
= build_tree_list (NULL_TREE
, type
);
25982 tparms
= extract_autos (type
);
25985 tparms
= make_tree_vec (1);
25986 TREE_VEC_ELT (tparms
, 0)
25987 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
25990 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25991 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25992 DEDUCE_CALL
, LOOKUP_NORMAL
,
25993 NULL
, /*explain_p=*/false);
25996 if (processing_template_decl
)
25997 /* Try again at instantiation time. */
25999 if (type
&& type
!= error_mark_node
26000 && (complain
& tf_error
))
26001 /* If type is error_mark_node a diagnostic must have been
26002 emitted by now. Also, having a mention to '<type error>'
26003 in the diagnostic is not really useful to the user. */
26005 if (cfun
&& auto_node
== current_function_auto_return_pattern
26006 && LAMBDA_FUNCTION_P (current_function_decl
))
26007 error ("unable to deduce lambda return type from %qE", init
);
26009 error ("unable to deduce %qT from %qE", type
, init
);
26010 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26011 DEDUCE_CALL
, LOOKUP_NORMAL
,
26012 NULL
, /*explain_p=*/true);
26014 return error_mark_node
;
26018 /* Check any placeholder constraints against the deduced type. */
26019 if (flag_concepts
&& !processing_template_decl
)
26020 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26022 /* Use the deduced type to check the associated constraints. If we
26023 have a partial-concept-id, rebuild the argument list so that
26024 we check using the extra arguments. */
26025 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26026 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26027 if (TREE_VEC_LENGTH (cargs
) > 1)
26029 cargs
= copy_node (cargs
);
26030 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26034 if (!constraints_satisfied_p (constr
, cargs
))
26036 if (complain
& tf_warning_or_error
)
26040 case adc_unspecified
:
26042 error("placeholder constraints not satisfied");
26044 case adc_variable_type
:
26045 case adc_decomp_type
:
26046 error ("deduced initializer does not satisfy "
26047 "placeholder constraints");
26049 case adc_return_type
:
26050 error ("deduced return type does not satisfy "
26051 "placeholder constraints");
26053 case adc_requirement
:
26054 error ("deduced expression type does not satisfy "
26055 "placeholder constraints");
26058 diagnose_constraints (input_location
, constr
, targs
);
26060 return error_mark_node
;
26064 if (processing_template_decl
&& context
!= adc_unify
)
26065 outer_targs
= current_template_args ();
26066 targs
= add_to_template_args (outer_targs
, targs
);
26067 return tsubst (type
, targs
, complain
, NULL_TREE
);
26070 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26074 splice_late_return_type (tree type
, tree late_return_type
)
26076 if (is_auto (type
))
26078 if (late_return_type
)
26079 return late_return_type
;
26081 tree idx
= get_template_parm_index (type
);
26082 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26083 /* In an abbreviated function template we didn't know we were dealing
26084 with a function template when we saw the auto return type, so update
26085 it to have the correct level. */
26086 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26091 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26092 'decltype(auto)' or a deduced class template. */
26095 is_auto (const_tree type
)
26097 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26098 && (TYPE_IDENTIFIER (type
) == auto_identifier
26099 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26100 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26106 /* for_each_template_parm callback for type_uses_auto. */
26109 is_auto_r (tree tp
, void */
*data*/
)
26111 return is_auto (tp
);
26114 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26115 a use of `auto'. Returns NULL_TREE otherwise. */
26118 type_uses_auto (tree type
)
26120 if (type
== NULL_TREE
)
26122 else if (flag_concepts
)
26124 /* The Concepts TS allows multiple autos in one type-specifier; just
26125 return the first one we find, do_auto_deduction will collect all of
26127 if (uses_template_parms (type
))
26128 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26129 /*visited*/NULL
, /*nondeduced*/true);
26134 return find_type_usage (type
, is_auto
);
26137 /* For a given template T, return the vector of typedefs referenced
26138 in T for which access check is needed at T instantiation time.
26139 T is either a FUNCTION_DECL or a RECORD_TYPE.
26140 Those typedefs were added to T by the function
26141 append_type_to_template_for_access_check. */
26143 vec
<qualified_typedef_usage_t
, va_gc
> *
26144 get_types_needing_access_check (tree t
)
26147 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26149 if (!t
|| t
== error_mark_node
)
26152 if (!(ti
= get_template_info (t
)))
26155 if (CLASS_TYPE_P (t
)
26156 || TREE_CODE (t
) == FUNCTION_DECL
)
26158 if (!TI_TEMPLATE (ti
))
26161 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26167 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26168 tied to T. That list of typedefs will be access checked at
26169 T instantiation time.
26170 T is either a FUNCTION_DECL or a RECORD_TYPE.
26171 TYPE_DECL is a TYPE_DECL node representing a typedef.
26172 SCOPE is the scope through which TYPE_DECL is accessed.
26173 LOCATION is the location of the usage point of TYPE_DECL.
26175 This function is a subroutine of
26176 append_type_to_template_for_access_check. */
26179 append_type_to_template_for_access_check_1 (tree t
,
26182 location_t location
)
26184 qualified_typedef_usage_t typedef_usage
;
26187 if (!t
|| t
== error_mark_node
)
26190 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26191 || CLASS_TYPE_P (t
))
26193 && TREE_CODE (type_decl
) == TYPE_DECL
26196 if (!(ti
= get_template_info (t
)))
26199 gcc_assert (TI_TEMPLATE (ti
));
26201 typedef_usage
.typedef_decl
= type_decl
;
26202 typedef_usage
.context
= scope
;
26203 typedef_usage
.locus
= location
;
26205 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26208 /* Append TYPE_DECL to the template TEMPL.
26209 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26210 At TEMPL instanciation time, TYPE_DECL will be checked to see
26211 if it can be accessed through SCOPE.
26212 LOCATION is the location of the usage point of TYPE_DECL.
26214 e.g. consider the following code snippet:
26221 template<class U> struct S
26223 C::myint mi; // <-- usage point of the typedef C::myint
26228 At S<char> instantiation time, we need to check the access of C::myint
26229 In other words, we need to check the access of the myint typedef through
26230 the C scope. For that purpose, this function will add the myint typedef
26231 and the scope C through which its being accessed to a list of typedefs
26232 tied to the template S. That list will be walked at template instantiation
26233 time and access check performed on each typedefs it contains.
26234 Note that this particular code snippet should yield an error because
26235 myint is private to C. */
26238 append_type_to_template_for_access_check (tree templ
,
26241 location_t location
)
26243 qualified_typedef_usage_t
*iter
;
26246 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26248 /* Make sure we don't append the type to the template twice. */
26249 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26250 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26253 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26257 /* Convert the generic type parameters in PARM that match the types given in the
26258 range [START_IDX, END_IDX) from the current_template_parms into generic type
26262 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26264 tree current
= current_template_parms
;
26265 int depth
= TMPL_PARMS_DEPTH (current
);
26266 current
= INNERMOST_TEMPLATE_PARMS (current
);
26267 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26269 for (int i
= 0; i
< start_idx
; ++i
)
26270 TREE_VEC_ELT (replacement
, i
)
26271 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26273 for (int i
= start_idx
; i
< end_idx
; ++i
)
26275 /* Create a distinct parameter pack type from the current parm and add it
26276 to the replacement args to tsubst below into the generic function
26279 tree o
= TREE_TYPE (TREE_VALUE
26280 (TREE_VEC_ELT (current
, i
)));
26281 tree t
= copy_type (o
);
26282 TEMPLATE_TYPE_PARM_INDEX (t
)
26283 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26285 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26286 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26287 TYPE_MAIN_VARIANT (t
) = t
;
26288 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26289 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26290 TREE_VEC_ELT (replacement
, i
) = t
;
26291 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26294 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26295 TREE_VEC_ELT (replacement
, i
)
26296 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26298 /* If there are more levels then build up the replacement with the outer
26301 replacement
= add_to_template_args (template_parms_to_args
26302 (TREE_CHAIN (current_template_parms
)),
26305 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26308 /* Entries in the decl_constraint hash table. */
26309 struct GTY((for_user
)) constr_entry
26315 /* Hashing function and equality for constraint entries. */
26316 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26318 static hashval_t
hash (constr_entry
*e
)
26320 return (hashval_t
)DECL_UID (e
->decl
);
26323 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26325 return e1
->decl
== e2
->decl
;
26329 /* A mapping from declarations to constraint information. Note that
26330 both templates and their underlying declarations are mapped to the
26331 same constraint information.
26333 FIXME: This is defined in pt.c because garbage collection
26334 code is not being generated for constraint.cc. */
26336 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26338 /* Returns the template constraints of declaration T. If T is not
26339 constrained, return NULL_TREE. Note that T must be non-null. */
26342 get_constraints (tree t
)
26344 if (!flag_concepts
)
26347 gcc_assert (DECL_P (t
));
26348 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26349 t
= DECL_TEMPLATE_RESULT (t
);
26350 constr_entry elt
= { t
, NULL_TREE
};
26351 constr_entry
* found
= decl_constraints
->find (&elt
);
26358 /* Associate the given constraint information CI with the declaration
26359 T. If T is a template, then the constraints are associated with
26360 its underlying declaration. Don't build associations if CI is
26364 set_constraints (tree t
, tree ci
)
26368 gcc_assert (t
&& flag_concepts
);
26369 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26370 t
= DECL_TEMPLATE_RESULT (t
);
26371 gcc_assert (!get_constraints (t
));
26372 constr_entry elt
= {t
, ci
};
26373 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26374 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26379 /* Remove the associated constraints of the declaration T. */
26382 remove_constraints (tree t
)
26384 gcc_assert (DECL_P (t
));
26385 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26386 t
= DECL_TEMPLATE_RESULT (t
);
26388 constr_entry elt
= {t
, NULL_TREE
};
26389 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26391 decl_constraints
->clear_slot (slot
);
26394 /* Memoized satisfaction results for declarations. This
26395 maps the pair (constraint_info, arguments) to the result computed
26396 by constraints_satisfied_p. */
26398 struct GTY((for_user
)) constraint_sat_entry
26405 /* Hashing function and equality for constraint entries. */
26407 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26409 static hashval_t
hash (constraint_sat_entry
*e
)
26411 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26412 return iterative_hash_template_arg (e
->args
, val
);
26415 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26417 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26421 /* Memoized satisfaction results for concept checks. */
26423 struct GTY((for_user
)) concept_spec_entry
26430 /* Hashing function and equality for constraint entries. */
26432 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26434 static hashval_t
hash (concept_spec_entry
*e
)
26436 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26439 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26441 ++comparing_specializations
;
26442 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26443 --comparing_specializations
;
26448 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26449 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26451 /* Search for a memoized satisfaction result. Returns one of the
26452 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26455 lookup_constraint_satisfaction (tree ci
, tree args
)
26457 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26458 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26460 return found
->result
;
26465 /* Memoize the result of a satisfication test. Returns the saved result. */
26468 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26470 constraint_sat_entry elt
= {ci
, args
, result
};
26471 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26472 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26478 /* Search for a memoized satisfaction result for a concept. */
26481 lookup_concept_satisfaction (tree tmpl
, tree args
)
26483 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26484 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26486 return found
->result
;
26491 /* Memoize the result of a concept check. Returns the saved result. */
26494 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26496 concept_spec_entry elt
= {tmpl
, args
, result
};
26497 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26498 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26504 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26506 /* Returns a prior concept specialization. This returns the substituted
26507 and normalized constraints defined by the concept. */
26510 get_concept_expansion (tree tmpl
, tree args
)
26512 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26513 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26515 return found
->result
;
26520 /* Save a concept expansion for later. */
26523 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26525 concept_spec_entry elt
= {tmpl
, args
, def
};
26526 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26527 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26534 hash_subsumption_args (tree t1
, tree t2
)
26536 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26537 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26539 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26540 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26541 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26542 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26546 /* Compare the constraints of two subsumption entries. The LEFT1 and
26547 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26548 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26551 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26553 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26554 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26555 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26556 CHECK_CONSTR_ARGS (right1
)))
26557 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26558 CHECK_CONSTR_ARGS (right2
));
26562 /* Key/value pair for learning and memoizing subsumption results. This
26563 associates a pair of check constraints (including arguments) with
26564 a boolean value indicating the result. */
26566 struct GTY((for_user
)) subsumption_entry
26573 /* Hashing function and equality for constraint entries. */
26575 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26577 static hashval_t
hash (subsumption_entry
*e
)
26579 return hash_subsumption_args (e
->t1
, e
->t2
);
26582 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26584 ++comparing_specializations
;
26585 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26586 --comparing_specializations
;
26591 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26593 /* Search for a previously cached subsumption result. */
26596 lookup_subsumption_result (tree t1
, tree t2
)
26598 subsumption_entry elt
= { t1
, t2
, false };
26599 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26601 return &found
->result
;
26606 /* Save a subsumption result. */
26609 save_subsumption_result (tree t1
, tree t2
, bool result
)
26611 subsumption_entry elt
= {t1
, t2
, result
};
26612 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26613 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26619 /* Set up the hash table for constraint association. */
26622 init_constraint_processing (void)
26624 if (!flag_concepts
)
26627 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26628 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26629 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26630 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26631 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26634 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26638 declare_integer_pack (void)
26640 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26641 build_function_type_list (integer_type_node
,
26644 NULL_TREE
, ECF_CONST
);
26645 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26646 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26649 /* Set up the hash tables for template instantiations. */
26652 init_template_processing (void)
26654 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26655 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26657 if (cxx_dialect
>= cxx11
)
26658 declare_integer_pack ();
26661 /* Print stats about the template hash tables for -fstats. */
26664 print_template_statistics (void)
26666 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26667 "%f collisions\n", (long) decl_specializations
->size (),
26668 (long) decl_specializations
->elements (),
26669 decl_specializations
->collisions ());
26670 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26671 "%f collisions\n", (long) type_specializations
->size (),
26672 (long) type_specializations
->elements (),
26673 type_specializations
->collisions ());
26678 namespace selftest
{
26680 /* Verify that build_non_dependent_expr () works, for various expressions,
26681 and that location wrappers don't affect the results. */
26684 test_build_non_dependent_expr ()
26686 location_t loc
= BUILTINS_LOCATION
;
26688 /* Verify constants, without and with location wrappers. */
26689 tree int_cst
= build_int_cst (integer_type_node
, 42);
26690 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26692 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26693 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26694 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26696 tree string_lit
= build_string (4, "foo");
26697 TREE_TYPE (string_lit
) = char_array_type_node
;
26698 string_lit
= fix_string_type (string_lit
);
26699 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26701 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26702 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26703 ASSERT_EQ (wrapped_string_lit
,
26704 build_non_dependent_expr (wrapped_string_lit
));
26707 /* Verify that type_dependent_expression_p () works correctly, even
26708 in the presence of location wrapper nodes. */
26711 test_type_dependent_expression_p ()
26713 location_t loc
= BUILTINS_LOCATION
;
26715 tree name
= get_identifier ("foo");
26717 /* If no templates are involved, nothing is type-dependent. */
26718 gcc_assert (!processing_template_decl
);
26719 ASSERT_FALSE (type_dependent_expression_p (name
));
26721 ++processing_template_decl
;
26723 /* Within a template, an unresolved name is always type-dependent. */
26724 ASSERT_TRUE (type_dependent_expression_p (name
));
26726 /* Ensure it copes with NULL_TREE and errors. */
26727 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26728 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26730 /* A USING_DECL in a template should be type-dependent, even if wrapped
26731 with a location wrapper (PR c++/83799). */
26732 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26733 TREE_TYPE (using_decl
) = integer_type_node
;
26734 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26735 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26736 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26737 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26739 --processing_template_decl
;
26742 /* Run all of the selftests within this file. */
26747 test_build_non_dependent_expr ();
26748 test_type_dependent_expression_p ();
26751 } // namespace selftest
26753 #endif /* #if CHECKING_P */
26755 #include "gt-cp-pt.h"