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_1 (tree inner
)
1139 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1141 tree arg
= TREE_VEC_ELT (inner
, i
);
1142 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1144 else if (TYPE_P (arg
))
1145 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1146 else if (ARGUMENT_PACK_P (arg
))
1147 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1148 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1152 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1157 verify_unstripped_args (tree args
)
1159 ++processing_template_decl
;
1160 if (!any_dependent_template_arguments_p (args
))
1161 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1162 --processing_template_decl
;
1165 /* Retrieve the specialization (in the sense of [temp.spec] - a
1166 specialization is either an instantiation or an explicit
1167 specialization) of TMPL for the given template ARGS. If there is
1168 no such specialization, return NULL_TREE. The ARGS are a vector of
1169 arguments, or a vector of vectors of arguments, in the case of
1170 templates with more than one level of parameters.
1172 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1173 then we search for a partial specialization matching ARGS. This
1174 parameter is ignored if TMPL is not a class template.
1176 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1177 result is a NONTYPE_ARGUMENT_PACK. */
1180 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1182 if (tmpl
== NULL_TREE
)
1185 if (args
== error_mark_node
)
1188 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1189 || TREE_CODE (tmpl
) == FIELD_DECL
);
1191 /* There should be as many levels of arguments as there are
1192 levels of parameters. */
1193 gcc_assert (TMPL_ARGS_DEPTH (args
)
1194 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1195 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1196 : template_class_depth (DECL_CONTEXT (tmpl
))));
1199 verify_unstripped_args (args
);
1201 /* Lambda functions in templates aren't instantiated normally, but through
1202 tsubst_lambda_expr. */
1203 if (lambda_fn_in_template_p (tmpl
))
1206 if (optimize_specialization_lookup_p (tmpl
))
1208 /* The template arguments actually apply to the containing
1209 class. Find the class specialization with those
1211 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1212 tree class_specialization
1213 = retrieve_specialization (class_template
, args
, 0);
1214 if (!class_specialization
)
1217 /* Find the instance of TMPL. */
1218 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1219 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1222 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1223 /* using-declarations can add base methods to the method vec,
1224 and we don't want those here. */
1225 && DECL_CONTEXT (fn
) == class_specialization
)
1234 hash_table
<spec_hasher
> *specializations
;
1238 elt
.spec
= NULL_TREE
;
1240 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1241 specializations
= type_specializations
;
1243 specializations
= decl_specializations
;
1246 hash
= spec_hasher::hash (&elt
);
1247 found
= specializations
->find_with_hash (&elt
, hash
);
1255 /* Like retrieve_specialization, but for local declarations. */
1258 retrieve_local_specialization (tree tmpl
)
1260 if (local_specializations
== NULL
)
1263 tree
*slot
= local_specializations
->get (tmpl
);
1264 return slot
? *slot
: NULL_TREE
;
1267 /* Returns nonzero iff DECL is a specialization of TMPL. */
1270 is_specialization_of (tree decl
, tree tmpl
)
1274 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1278 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1284 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1286 for (t
= TREE_TYPE (decl
);
1288 t
= CLASSTYPE_USE_TEMPLATE (t
)
1289 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1290 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1297 /* Returns nonzero iff DECL is a specialization of friend declaration
1298 FRIEND_DECL according to [temp.friend]. */
1301 is_specialization_of_friend (tree decl
, tree friend_decl
)
1303 bool need_template
= true;
1306 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1307 || TREE_CODE (decl
) == TYPE_DECL
);
1309 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1310 of a template class, we want to check if DECL is a specialization
1312 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1313 && DECL_TEMPLATE_INFO (friend_decl
)
1314 && !DECL_USE_TEMPLATE (friend_decl
))
1316 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1317 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1318 need_template
= false;
1320 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1321 && !PRIMARY_TEMPLATE_P (friend_decl
))
1322 need_template
= false;
1324 /* There is nothing to do if this is not a template friend. */
1325 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1328 if (is_specialization_of (decl
, friend_decl
))
1332 A member of a class template may be declared to be a friend of a
1333 non-template class. In this case, the corresponding member of
1334 every specialization of the class template is a friend of the
1335 class granting friendship.
1337 For example, given a template friend declaration
1339 template <class T> friend void A<T>::f();
1341 the member function below is considered a friend
1343 template <> struct A<int> {
1347 For this type of template friend, TEMPLATE_DEPTH below will be
1348 nonzero. To determine if DECL is a friend of FRIEND, we first
1349 check if the enclosing class is a specialization of another. */
1351 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1353 && DECL_CLASS_SCOPE_P (decl
)
1354 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1355 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1357 /* Next, we check the members themselves. In order to handle
1358 a few tricky cases, such as when FRIEND_DECL's are
1360 template <class T> friend void A<T>::g(T t);
1361 template <class T> template <T t> friend void A<T>::h();
1365 void A<int>::g(int);
1366 template <int> void A<int>::h();
1368 we need to figure out ARGS, the template arguments from
1369 the context of DECL. This is required for template substitution
1370 of `T' in the function parameter of `g' and template parameter
1371 of `h' in the above examples. Here ARGS corresponds to `int'. */
1373 tree context
= DECL_CONTEXT (decl
);
1374 tree args
= NULL_TREE
;
1375 int current_depth
= 0;
1377 while (current_depth
< template_depth
)
1379 if (CLASSTYPE_TEMPLATE_INFO (context
))
1381 if (current_depth
== 0)
1382 args
= TYPE_TI_ARGS (context
);
1384 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1387 context
= TYPE_CONTEXT (context
);
1390 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1395 tree friend_args_type
;
1396 tree decl_args_type
;
1398 /* Make sure that both DECL and FRIEND_DECL are templates or
1400 is_template
= DECL_TEMPLATE_INFO (decl
)
1401 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1402 if (need_template
^ is_template
)
1404 else if (is_template
)
1406 /* If both are templates, check template parameter list. */
1408 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1410 if (!comp_template_parms
1411 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1415 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1418 decl_type
= TREE_TYPE (decl
);
1420 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1421 tf_none
, NULL_TREE
);
1422 if (friend_type
== error_mark_node
)
1425 /* Check if return types match. */
1426 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1429 /* Check if function parameter types match, ignoring the
1430 `this' parameter. */
1431 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1432 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1434 friend_args_type
= TREE_CHAIN (friend_args_type
);
1435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1436 decl_args_type
= TREE_CHAIN (decl_args_type
);
1438 return compparms (decl_args_type
, friend_args_type
);
1442 /* DECL is a TYPE_DECL */
1444 tree decl_type
= TREE_TYPE (decl
);
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1449 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1452 if (need_template
^ is_template
)
1454 else if (is_template
)
1457 /* If both are templates, check the name of the two
1458 TEMPLATE_DECL's first because is_friend didn't. */
1459 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1460 != DECL_NAME (friend_decl
))
1463 /* Now check template parameter list. */
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1467 return comp_template_parms
1468 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1472 return (DECL_NAME (decl
)
1473 == DECL_NAME (friend_decl
));
1479 /* Register the specialization SPEC as a specialization of TMPL with
1480 the indicated ARGS. IS_FRIEND indicates whether the specialization
1481 is actually just a friend declaration. ATTRLIST is the list of
1482 attributes that the specialization is declared with or NULL when
1483 it isn't. Returns SPEC, or an equivalent prior declaration, if
1486 We also store instantiations of field packs in the hash table, even
1487 though they are not themselves templates, to make lookup easier. */
1490 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1494 spec_entry
**slot
= NULL
;
1497 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1498 || (TREE_CODE (tmpl
) == FIELD_DECL
1499 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1501 if (TREE_CODE (spec
) == FUNCTION_DECL
1502 && uses_template_parms (DECL_TI_ARGS (spec
)))
1503 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1504 register it; we want the corresponding TEMPLATE_DECL instead.
1505 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1506 the more obvious `uses_template_parms (spec)' to avoid problems
1507 with default function arguments. In particular, given
1508 something like this:
1510 template <class T> void f(T t1, T t = T())
1512 the default argument expression is not substituted for in an
1513 instantiation unless and until it is actually needed. */
1516 if (optimize_specialization_lookup_p (tmpl
))
1517 /* We don't put these specializations in the hash table, but we might
1518 want to give an error about a mismatch. */
1519 fn
= retrieve_specialization (tmpl
, args
, 0);
1527 hash
= spec_hasher::hash (&elt
);
1530 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1532 fn
= ((spec_entry
*) *slot
)->spec
;
1537 /* We can sometimes try to re-register a specialization that we've
1538 already got. In particular, regenerate_decl_from_template calls
1539 duplicate_decls which will update the specialization list. But,
1540 we'll still get called again here anyhow. It's more convenient
1541 to simply allow this than to try to prevent it. */
1544 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1546 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1548 if (DECL_ODR_USED (fn
)
1549 || DECL_EXPLICIT_INSTANTIATION (fn
))
1551 error ("specialization of %qD after instantiation",
1553 return error_mark_node
;
1558 /* This situation should occur only if the first
1559 specialization is an implicit instantiation, the
1560 second is an explicit specialization, and the
1561 implicit instantiation has not yet been used. That
1562 situation can occur if we have implicitly
1563 instantiated a member function and then specialized
1566 We can also wind up here if a friend declaration that
1567 looked like an instantiation turns out to be a
1570 template <class T> void foo(T);
1571 class S { friend void foo<>(int) };
1572 template <> void foo(int);
1574 We transform the existing DECL in place so that any
1575 pointers to it become pointers to the updated
1578 If there was a definition for the template, but not
1579 for the specialization, we want this to look as if
1580 there were no definition, and vice versa. */
1581 DECL_INITIAL (fn
) = NULL_TREE
;
1582 duplicate_decls (spec
, fn
, is_friend
);
1583 /* The call to duplicate_decls will have applied
1586 An explicit specialization of a function template
1587 is inline only if it is explicitly declared to be,
1588 and independently of whether its function template
1591 to the primary function; now copy the inline bits to
1592 the various clones. */
1593 FOR_EACH_CLONE (clone
, fn
)
1595 DECL_DECLARED_INLINE_P (clone
)
1596 = DECL_DECLARED_INLINE_P (fn
);
1597 DECL_SOURCE_LOCATION (clone
)
1598 = DECL_SOURCE_LOCATION (fn
);
1599 DECL_DELETED_FN (clone
)
1600 = DECL_DELETED_FN (fn
);
1602 check_specialization_namespace (tmpl
);
1607 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1609 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1610 if (dd
== error_mark_node
)
1611 /* We've already complained in duplicate_decls. */
1612 return error_mark_node
;
1614 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1615 /* Dup decl failed, but this is a new definition. Set the
1616 line number so any errors match this new
1618 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1624 return duplicate_decls (spec
, fn
, is_friend
);
1626 /* A specialization must be declared in the same namespace as the
1627 template it is specializing. */
1628 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1629 && !check_specialization_namespace (tmpl
))
1630 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1632 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1634 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1635 gcc_assert (tmpl
&& args
&& spec
);
1638 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1639 && PRIMARY_TEMPLATE_P (tmpl
)
1640 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1641 || variable_template_p (tmpl
))
1642 /* If TMPL is a forward declaration of a template function, keep a list
1643 of all specializations in case we need to reassign them to a friend
1644 template later in tsubst_friend_function.
1646 Also keep a list of all variable template instantiations so that
1647 process_partial_specialization can check whether a later partial
1648 specialization would have used it. */
1649 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1650 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1656 /* Returns true iff two spec_entry nodes are equivalent. */
1658 int comparing_specializations
;
1661 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1665 ++comparing_specializations
;
1666 equal
= (e1
->tmpl
== e2
->tmpl
1667 && comp_template_args (e1
->args
, e2
->args
));
1668 if (equal
&& flag_concepts
1669 /* tmpl could be a FIELD_DECL for a capture pack. */
1670 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1671 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1672 && uses_template_parms (e1
->args
))
1674 /* Partial specializations of a variable template can be distinguished by
1676 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1677 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1678 equal
= equivalent_constraints (c1
, c2
);
1680 --comparing_specializations
;
1685 /* Returns a hash for a template TMPL and template arguments ARGS. */
1688 hash_tmpl_and_args (tree tmpl
, tree args
)
1690 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1691 return iterative_hash_template_arg (args
, val
);
1694 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1698 spec_hasher::hash (spec_entry
*e
)
1700 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1703 /* Recursively calculate a hash value for a template argument ARG, for use
1704 in the hash tables of template specializations. */
1707 iterative_hash_template_arg (tree arg
, hashval_t val
)
1709 unsigned HOST_WIDE_INT i
;
1710 enum tree_code code
;
1713 if (arg
== NULL_TREE
)
1714 return iterative_hash_object (arg
, val
);
1719 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1722 code
= TREE_CODE (arg
);
1723 tclass
= TREE_CODE_CLASS (code
);
1725 val
= iterative_hash_object (code
, val
);
1732 case IDENTIFIER_NODE
:
1733 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1737 int i
, len
= TREE_VEC_LENGTH (arg
);
1738 for (i
= 0; i
< len
; ++i
)
1739 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1743 case TYPE_PACK_EXPANSION
:
1744 case EXPR_PACK_EXPANSION
:
1745 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1746 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1748 case TYPE_ARGUMENT_PACK
:
1749 case NONTYPE_ARGUMENT_PACK
:
1750 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1753 for (; arg
; arg
= TREE_CHAIN (arg
))
1754 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1758 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1759 val
= iterative_hash_template_arg (*iter
, val
);
1765 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1768 val
= iterative_hash_template_arg (field
, val
);
1769 val
= iterative_hash_template_arg (value
, val
);
1775 if (!DECL_ARTIFICIAL (arg
))
1777 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1778 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1780 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1783 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1786 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1787 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1789 case TEMPLATE_PARM_INDEX
:
1790 val
= iterative_hash_template_arg
1791 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1792 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1793 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1796 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1797 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1798 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1801 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1803 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1807 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1808 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1809 val
= iterative_hash_object (code
, val
);
1810 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1813 /* A lambda can't appear in a template arg, but don't crash on
1815 gcc_assert (seen_error ());
1819 case IMPLICIT_CONV_EXPR
:
1820 case STATIC_CAST_EXPR
:
1821 case REINTERPRET_CAST_EXPR
:
1822 case CONST_CAST_EXPR
:
1823 case DYNAMIC_CAST_EXPR
:
1825 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1826 /* Now hash operands as usual. */
1836 if (alias_template_specialization_p (arg
))
1838 // We want an alias specialization that survived strip_typedefs
1839 // to hash differently from its TYPE_CANONICAL, to avoid hash
1840 // collisions that compare as different in template_args_equal.
1841 // These could be dependent specializations that strip_typedefs
1842 // left alone, or untouched specializations because
1843 // coerce_template_parms returns the unconverted template
1844 // arguments if it sees incomplete argument packs.
1845 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1846 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1848 if (TYPE_CANONICAL (arg
))
1849 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1851 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1852 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1853 /* Otherwise just compare the types during lookup. */
1856 case tcc_declaration
:
1858 return iterative_hash_expr (arg
, val
);
1861 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1863 unsigned n
= cp_tree_operand_length (arg
);
1864 for (i
= 0; i
< n
; ++i
)
1865 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1873 /* Unregister the specialization SPEC as a specialization of TMPL.
1874 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1875 if the SPEC was listed as a specialization of TMPL.
1877 Note that SPEC has been ggc_freed, so we can't look inside it. */
1880 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1885 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1886 elt
.args
= TI_ARGS (tinfo
);
1887 elt
.spec
= NULL_TREE
;
1889 entry
= decl_specializations
->find (&elt
);
1892 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1893 gcc_assert (new_spec
!= NULL_TREE
);
1894 entry
->spec
= new_spec
;
1901 /* Like register_specialization, but for local declarations. We are
1902 registering SPEC, an instantiation of TMPL. */
1905 register_local_specialization (tree spec
, tree tmpl
)
1907 gcc_assert (tmpl
!= spec
);
1908 local_specializations
->put (tmpl
, spec
);
1911 /* TYPE is a class type. Returns true if TYPE is an explicitly
1912 specialized class. */
1915 explicit_class_specialization_p (tree type
)
1917 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1919 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1922 /* Print the list of functions at FNS, going through all the overloads
1923 for each element of the list. Alternatively, FNS can not be a
1924 TREE_LIST, in which case it will be printed together with all the
1927 MORE and *STR should respectively be FALSE and NULL when the function
1928 is called from the outside. They are used internally on recursive
1929 calls. print_candidates manages the two parameters and leaves NULL
1930 in *STR when it ends. */
1933 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1935 if (TREE_CODE (fns
) == TREE_LIST
)
1936 for (; fns
; fns
= TREE_CHAIN (fns
))
1937 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1939 for (lkp_iterator
iter (fns
); iter
;)
1944 const char *pfx
= *str
;
1948 pfx
= _("candidates are:");
1950 pfx
= _("candidate is:");
1951 *str
= get_spaces (pfx
);
1953 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1961 print_candidates (tree fns
)
1964 print_candidates_1 (fns
, &str
);
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1971 get_template_for_ordering (tree list
)
1973 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1974 tree f
= TREE_VALUE (list
);
1975 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1976 return TI_TEMPLATE (ti
);
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1989 most_constrained_function (tree candidates
)
1991 // Try to find the best candidate in a first pass.
1992 tree champ
= candidates
;
1993 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1995 int winner
= more_constrained (get_template_for_ordering (champ
),
1996 get_template_for_ordering (c
));
1998 champ
= c
; // The candidate is more constrained
1999 else if (winner
== 0)
2000 return NULL_TREE
; // Neither is more constrained
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2005 if (!more_constrained (get_template_for_ordering (champ
),
2006 get_template_for_ordering (c
)))
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2039 determine_specialization (tree template_id
,
2042 int need_member_template
,
2048 tree explicit_targs
;
2049 tree candidates
= NULL_TREE
;
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates
= NULL_TREE
;
2058 cp_binding_level
*b
;
2060 *targs_out
= NULL_TREE
;
2062 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2063 return error_mark_node
;
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2069 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2070 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2072 gcc_assert (errorcount
);
2073 return error_mark_node
;
2076 fns
= TREE_OPERAND (template_id
, 0);
2077 explicit_targs
= TREE_OPERAND (template_id
, 1);
2079 if (fns
== error_mark_node
)
2080 return error_mark_node
;
2082 /* Check for baselinks. */
2083 if (BASELINK_P (fns
))
2084 fns
= BASELINK_FUNCTIONS (fns
);
2086 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2088 error ("%qD is not a function template", fns
);
2089 return error_mark_node
;
2091 else if (VAR_P (decl
) && !variable_template_p (fns
))
2093 error ("%qD is not a variable template", fns
);
2094 return error_mark_node
;
2097 /* Count the number of template headers specified for this
2100 for (b
= current_binding_level
;
2101 b
->kind
== sk_template_parms
;
2105 tree orig_fns
= fns
;
2107 if (variable_template_p (fns
))
2109 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2110 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2111 tf_warning_or_error
,
2112 /*req_all*/true, /*use_defarg*/true);
2113 if (targs
!= error_mark_node
)
2114 templates
= tree_cons (targs
, fns
, templates
);
2116 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2120 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2122 tree decl_arg_types
;
2126 /* In case of explicit specialization, we need to check if
2127 the number of template headers appearing in the specialization
2128 is correct. This is usually done in check_explicit_specialization,
2129 but the check done there cannot be exhaustive when specializing
2130 member functions. Consider the following code:
2132 template <> void A<int>::f(int);
2133 template <> template <> void A<int>::f(int);
2135 Assuming that A<int> is not itself an explicit specialization
2136 already, the first line specializes "f" which is a non-template
2137 member function, whilst the second line specializes "f" which
2138 is a template member function. So both lines are syntactically
2139 correct, and check_explicit_specialization does not reject
2142 Here, we can do better, as we are matching the specialization
2143 against the declarations. We count the number of template
2144 headers, and we check if they match TEMPLATE_COUNT + 1
2145 (TEMPLATE_COUNT is the number of qualifying template classes,
2146 plus there must be another header for the member template
2149 Notice that if header_count is zero, this is not a
2150 specialization but rather a template instantiation, so there
2151 is no check we can perform here. */
2152 if (header_count
&& header_count
!= template_count
+ 1)
2155 /* Check that the number of template arguments at the
2156 innermost level for DECL is the same as for FN. */
2157 if (current_binding_level
->kind
== sk_template_parms
2158 && !current_binding_level
->explicit_spec_p
2159 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2160 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2161 (current_template_parms
))))
2164 /* DECL might be a specialization of FN. */
2165 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2166 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2168 /* For a non-static member function, we need to make sure
2169 that the const qualification is the same. Since
2170 get_bindings does not try to merge the "this" parameter,
2171 we must do the comparison explicitly. */
2172 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2174 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2175 TREE_VALUE (decl_arg_types
)))
2178 /* And the ref-qualification. */
2179 if (type_memfn_rqual (TREE_TYPE (decl
))
2180 != type_memfn_rqual (TREE_TYPE (fn
)))
2184 /* Skip the "this" parameter and, for constructors of
2185 classes with virtual bases, the VTT parameter. A
2186 full specialization of a constructor will have a VTT
2187 parameter, but a template never will. */
2189 = skip_artificial_parms_for (decl
, decl_arg_types
);
2191 = skip_artificial_parms_for (fn
, fn_arg_types
);
2193 /* Function templates cannot be specializations; there are
2194 no partial specializations of functions. Therefore, if
2195 the type of DECL does not match FN, there is no
2198 Note that it should never be the case that we have both
2199 candidates added here, and for regular member functions
2201 if (tsk
== tsk_template
)
2203 if (compparms (fn_arg_types
, decl_arg_types
))
2204 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2208 /* See whether this function might be a specialization of this
2209 template. Suppress access control because we might be trying
2210 to make this specialization a friend, and we have already done
2211 access control for the declaration of the specialization. */
2212 push_deferring_access_checks (dk_no_check
);
2213 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2214 pop_deferring_access_checks ();
2217 /* We cannot deduce template arguments that when used to
2218 specialize TMPL will produce DECL. */
2221 if (uses_template_parms (targs
))
2222 /* We deduced something involving 'auto', which isn't a valid
2223 template argument. */
2226 /* Remove, from the set of candidates, all those functions
2227 whose constraints are not satisfied. */
2228 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2231 // Then, try to form the new function type.
2232 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2233 if (insttype
== error_mark_node
)
2236 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2237 if (!compparms (fn_arg_types
, decl_arg_types
))
2240 /* Save this template, and the arguments deduced. */
2241 templates
= tree_cons (targs
, fn
, templates
);
2243 else if (need_member_template
)
2244 /* FN is an ordinary member function, and we need a
2245 specialization of a member template. */
2247 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2248 /* We can get IDENTIFIER_NODEs here in certain erroneous
2251 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2252 /* This is just an ordinary non-member function. Nothing can
2253 be a specialization of that. */
2255 else if (DECL_ARTIFICIAL (fn
))
2256 /* Cannot specialize functions that are created implicitly. */
2260 tree decl_arg_types
;
2262 /* This is an ordinary member function. However, since
2263 we're here, we can assume its enclosing class is a
2264 template class. For example,
2266 template <typename T> struct S { void f(); };
2267 template <> void S<int>::f() {}
2269 Here, S<int>::f is a non-template, but S<int> is a
2270 template class. If FN has the same type as DECL, we
2271 might be in business. */
2273 if (!DECL_TEMPLATE_INFO (fn
))
2274 /* Its enclosing class is an explicit specialization
2275 of a template class. This is not a candidate. */
2278 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2279 TREE_TYPE (TREE_TYPE (fn
))))
2280 /* The return types differ. */
2283 /* Adjust the type of DECL in case FN is a static member. */
2284 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2285 if (DECL_STATIC_FUNCTION_P (fn
)
2286 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2287 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2289 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2293 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2294 && (type_memfn_rqual (TREE_TYPE (decl
))
2295 != type_memfn_rqual (TREE_TYPE (fn
))))
2298 // If the deduced arguments do not satisfy the constraints,
2299 // this is not a candidate.
2300 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2303 // Add the candidate.
2304 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2308 if (templates
&& TREE_CHAIN (templates
))
2314 It is possible for a specialization with a given function
2315 signature to be instantiated from more than one function
2316 template. In such cases, explicit specification of the
2317 template arguments must be used to uniquely identify the
2318 function template specialization being specialized.
2320 Note that here, there's no suggestion that we're supposed to
2321 determine which of the candidate templates is most
2322 specialized. However, we, also have:
2326 Partial ordering of overloaded function template
2327 declarations is used in the following contexts to select
2328 the function template to which a function template
2329 specialization refers:
2331 -- when an explicit specialization refers to a function
2334 So, we do use the partial ordering rules, at least for now.
2335 This extension can only serve to make invalid programs valid,
2336 so it's safe. And, there is strong anecdotal evidence that
2337 the committee intended the partial ordering rules to apply;
2338 the EDG front end has that behavior, and John Spicer claims
2339 that the committee simply forgot to delete the wording in
2340 [temp.expl.spec]. */
2341 tree tmpl
= most_specialized_instantiation (templates
);
2342 if (tmpl
!= error_mark_node
)
2345 TREE_CHAIN (templates
) = NULL_TREE
;
2349 // Concepts allows multiple declarations of member functions
2350 // with the same signature. Like above, we need to rely on
2351 // on the partial ordering of those candidates to determine which
2353 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2355 if (tree cand
= most_constrained_function (candidates
))
2358 TREE_CHAIN (cand
) = NULL_TREE
;
2362 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2364 error ("template-id %qD for %q+D does not match any template "
2365 "declaration", template_id
, decl
);
2366 if (header_count
&& header_count
!= template_count
+ 1)
2367 inform (input_location
, "saw %d %<template<>%>, need %d for "
2368 "specializing a member function template",
2369 header_count
, template_count
+ 1);
2371 print_candidates (orig_fns
);
2372 return error_mark_node
;
2374 else if ((templates
&& TREE_CHAIN (templates
))
2375 || (candidates
&& TREE_CHAIN (candidates
))
2376 || (templates
&& candidates
))
2378 error ("ambiguous template specialization %qD for %q+D",
2380 candidates
= chainon (candidates
, templates
);
2381 print_candidates (candidates
);
2382 return error_mark_node
;
2385 /* We have one, and exactly one, match. */
2388 tree fn
= TREE_VALUE (candidates
);
2389 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2391 // Propagate the candidate's constraints to the declaration.
2392 set_constraints (decl
, get_constraints (fn
));
2394 /* DECL is a re-declaration or partial instantiation of a template
2396 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2398 /* It was a specialization of an ordinary member function in a
2400 return DECL_TI_TEMPLATE (fn
);
2403 /* It was a specialization of a template. */
2404 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2405 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2407 *targs_out
= copy_node (targs
);
2408 SET_TMPL_ARGS_LEVEL (*targs_out
,
2409 TMPL_ARGS_DEPTH (*targs_out
),
2410 TREE_PURPOSE (templates
));
2413 *targs_out
= TREE_PURPOSE (templates
);
2414 return TREE_VALUE (templates
);
2417 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2418 but with the default argument values filled in from those in the
2422 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2425 tree new_spec_types
;
2430 if (spec_types
== void_list_node
)
2431 return void_list_node
;
2433 /* Substitute into the rest of the list. */
2435 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2436 TREE_CHAIN (tmpl_types
));
2438 /* Add the default argument for this parameter. */
2439 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2440 TREE_VALUE (spec_types
),
2444 /* DECL is an explicit specialization. Replicate default arguments
2445 from the template it specializes. (That way, code like:
2447 template <class T> void f(T = 3);
2448 template <> void f(double);
2451 works, as required.) An alternative approach would be to look up
2452 the correct default arguments at the call-site, but this approach
2453 is consistent with how implicit instantiations are handled. */
2456 copy_default_args_to_explicit_spec (tree decl
)
2461 tree new_spec_types
;
2465 tree object_type
= NULL_TREE
;
2466 tree in_charge
= NULL_TREE
;
2467 tree vtt
= NULL_TREE
;
2469 /* See if there's anything we need to do. */
2470 tmpl
= DECL_TI_TEMPLATE (decl
);
2471 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2472 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2473 if (TREE_PURPOSE (t
))
2478 old_type
= TREE_TYPE (decl
);
2479 spec_types
= TYPE_ARG_TYPES (old_type
);
2481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2483 /* Remove the this pointer, but remember the object's type for
2485 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2486 spec_types
= TREE_CHAIN (spec_types
);
2487 tmpl_types
= TREE_CHAIN (tmpl_types
);
2489 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2491 /* DECL may contain more parameters than TMPL due to the extra
2492 in-charge parameter in constructors and destructors. */
2493 in_charge
= spec_types
;
2494 spec_types
= TREE_CHAIN (spec_types
);
2496 if (DECL_HAS_VTT_PARM_P (decl
))
2499 spec_types
= TREE_CHAIN (spec_types
);
2503 /* Compute the merged default arguments. */
2505 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2507 /* Compute the new FUNCTION_TYPE. */
2511 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2516 /* Put the in-charge parameter back. */
2517 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2518 TREE_VALUE (in_charge
),
2521 new_type
= build_method_type_directly (object_type
,
2522 TREE_TYPE (old_type
),
2526 new_type
= build_function_type (TREE_TYPE (old_type
),
2528 new_type
= cp_build_type_attribute_variant (new_type
,
2529 TYPE_ATTRIBUTES (old_type
));
2530 new_type
= build_exception_variant (new_type
,
2531 TYPE_RAISES_EXCEPTIONS (old_type
));
2533 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2534 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2536 TREE_TYPE (decl
) = new_type
;
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2543 num_template_headers_for_class (tree ctype
)
2545 int num_templates
= 0;
2547 while (ctype
&& CLASS_TYPE_P (ctype
))
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2564 if (explicit_class_specialization_p (ctype
))
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2569 ctype
= TYPE_CONTEXT (ctype
);
2572 return num_templates
;
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2579 check_template_variable (tree decl
)
2581 tree ctx
= CP_DECL_CONTEXT (decl
);
2582 int wanted
= num_template_headers_for_class (ctx
);
2583 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2586 if (cxx_dialect
< cxx14
)
2587 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2591 // Namespace-scope variable templates should have a template header.
2594 if (template_header_count
> wanted
)
2596 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2597 "too many template headers for %qD "
2600 if (warned
&& CLASS_TYPE_P (ctx
)
2601 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2602 inform (DECL_SOURCE_LOCATION (decl
),
2603 "members of an explicitly specialized class are defined "
2604 "without a template header");
2608 /* An explicit specialization whose declarator-id or class-head-name is not
2609 qualified shall be declared in the nearest enclosing namespace of the
2610 template, or, if the namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set.
2613 If the name declared in the explicit instantiation is an unqualified name,
2614 the explicit instantiation shall appear in the namespace where its template
2615 is declared or, if that namespace is inline (7.3.1), any namespace from its
2616 enclosing namespace set. */
2619 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2621 tree tmpl
= most_general_template (t
);
2622 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2623 && !is_nested_namespace (current_namespace
,
2624 CP_DECL_CONTEXT (tmpl
), true))
2626 if (processing_specialization
)
2627 permerror (loc
, "explicit specialization of %qD outside its "
2628 "namespace must use a nested-name-specifier", tmpl
);
2629 else if (processing_explicit_instantiation
2630 && cxx_dialect
>= cxx11
)
2631 /* This was allowed in C++98, so only pedwarn. */
2632 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2633 "outside its namespace must use a nested-name-"
2638 /* Warn for a template specialization SPEC that is missing some of a set
2639 of function or type attributes that the template TEMPL is declared with.
2640 ATTRLIST is a list of additional attributes that SPEC should be taken
2641 to ultimately be declared with. */
2644 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2646 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2647 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2649 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2652 /* Avoid warning if either declaration or its type is deprecated. */
2653 if (TREE_DEPRECATED (tmpl
)
2654 || TREE_DEPRECATED (spec
))
2657 tree tmpl_type
= TREE_TYPE (tmpl
);
2658 tree spec_type
= TREE_TYPE (spec
);
2660 if (TREE_DEPRECATED (tmpl_type
)
2661 || TREE_DEPRECATED (spec_type
)
2662 || TREE_DEPRECATED (TREE_TYPE (tmpl_type
))
2663 || TREE_DEPRECATED (TREE_TYPE (spec_type
)))
2666 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpl_type
) };
2667 tree spec_attrs
[] = { DECL_ATTRIBUTES (spec
), TYPE_ATTRIBUTES (spec_type
) };
2670 spec_attrs
[0] = attrlist
;
2671 else if (!spec_attrs
[1])
2672 spec_attrs
[1] = attrlist
;
2674 /* Avoid warning if the primary has no attributes. */
2675 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2678 /* Avoid warning if either declaration contains an attribute on
2679 the white list below. */
2680 const char* const whitelist
[] = {
2684 for (unsigned i
= 0; i
!= 2; ++i
)
2685 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
2686 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2687 || lookup_attribute (whitelist
[j
], spec_attrs
[i
]))
2690 /* Avoid warning if the difference between the primary and
2691 the specialization is not in one of the attributes below. */
2692 const char* const blacklist
[] = {
2693 "alloc_align", "alloc_size", "assume_aligned", "format",
2694 "format_arg", "malloc", "nonnull"
2697 /* Put together a list of the black listed attributes that the primary
2698 template is declared with that the specialization is not, in case
2699 it's not apparent from the most recent declaration of the primary. */
2700 unsigned nattrs
= 0;
2703 for (unsigned i
= 0; i
!= sizeof blacklist
/ sizeof *blacklist
; ++i
)
2705 for (unsigned j
= 0; j
!= 2; ++j
)
2707 if (!lookup_attribute (blacklist
[i
], tmpl_attrs
[j
]))
2710 for (unsigned k
= 0; k
!= 1 + !!spec_attrs
[1]; ++k
)
2712 if (lookup_attribute (blacklist
[i
], spec_attrs
[k
]))
2716 pp_string (&str
, ", ");
2717 pp_begin_quote (&str
, pp_show_color (global_dc
->printer
));
2718 pp_string (&str
, blacklist
[i
]);
2719 pp_end_quote (&str
, pp_show_color (global_dc
->printer
));
2728 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2729 "explicit specialization %q#D may be missing attributes",
2731 inform (DECL_SOURCE_LOCATION (tmpl
),
2733 ? G_("missing primary template attributes %s")
2734 : G_("missing primary template attribute %s"),
2735 pp_formatted_text (&str
));
2738 /* Check to see if the function just declared, as indicated in
2739 DECLARATOR, and in DECL, is a specialization of a function
2740 template. We may also discover that the declaration is an explicit
2741 instantiation at this point.
2743 Returns DECL, or an equivalent declaration that should be used
2744 instead if all goes well. Issues an error message if something is
2745 amiss. Returns error_mark_node if the error is not easily
2748 FLAGS is a bitmask consisting of the following flags:
2750 2: The function has a definition.
2751 4: The function is a friend.
2753 The TEMPLATE_COUNT is the number of references to qualifying
2754 template classes that appeared in the name of the function. For
2757 template <class T> struct S { void f(); };
2760 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2761 classes are not counted in the TEMPLATE_COUNT, so that in
2763 template <class T> struct S {};
2764 template <> struct S<int> { void f(); }
2765 template <> void S<int>::f();
2767 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2768 invalid; there should be no template <>.)
2770 If the function is a specialization, it is marked as such via
2771 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2772 is set up correctly, and it is added to the list of specializations
2773 for that template. */
2776 check_explicit_specialization (tree declarator
,
2782 int have_def
= flags
& 2;
2783 int is_friend
= flags
& 4;
2784 bool is_concept
= flags
& 8;
2785 int specialization
= 0;
2786 int explicit_instantiation
= 0;
2787 int member_specialization
= 0;
2788 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2789 tree dname
= DECL_NAME (decl
);
2794 if (!processing_specialization
)
2797 tsk
= tsk_excessive_parms
;
2800 tsk
= current_tmpl_spec_kind (template_count
);
2805 if (processing_specialization
&& !VAR_P (decl
))
2808 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2810 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2813 /* This could be something like:
2815 template <class T> void f(T);
2816 class S { friend void f<>(int); } */
2820 /* This case handles bogus declarations like template <>
2821 template <class T> void f<int>(); */
2823 error ("template-id %qD in declaration of primary template",
2830 case tsk_invalid_member_spec
:
2831 /* The error has already been reported in
2832 check_specialization_scope. */
2833 return error_mark_node
;
2835 case tsk_invalid_expl_inst
:
2836 error ("template parameter list used in explicit instantiation");
2842 error ("definition provided for explicit instantiation");
2844 explicit_instantiation
= 1;
2847 case tsk_excessive_parms
:
2848 case tsk_insufficient_parms
:
2849 if (tsk
== tsk_excessive_parms
)
2850 error ("too many template parameter lists in declaration of %qD",
2852 else if (template_header_count
)
2853 error("too few template parameter lists in declaration of %qD", decl
);
2855 error("explicit specialization of %qD must be introduced by "
2856 "%<template <>%>", decl
);
2861 error ("explicit specialization declared %<concept%>");
2863 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2864 /* In cases like template<> constexpr bool v = true;
2865 We'll give an error in check_template_variable. */
2868 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2870 member_specialization
= 1;
2876 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2878 /* This case handles bogus declarations like template <>
2879 template <class T> void f<int>(); */
2881 if (!uses_template_parms (declarator
))
2882 error ("template-id %qD in declaration of primary template",
2884 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2886 /* Partial specialization of variable template. */
2887 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2891 else if (cxx_dialect
< cxx14
)
2892 error ("non-type partial specialization %qD "
2893 "is not allowed", declarator
);
2895 error ("non-class, non-variable partial specialization %qD "
2896 "is not allowed", declarator
);
2901 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2902 /* This is a specialization of a member template, without
2903 specialization the containing class. Something like:
2905 template <class T> struct S {
2906 template <class U> void f (U);
2908 template <> template <class U> void S<int>::f(U) {}
2910 That's a specialization -- but of the entire template. */
2918 if ((specialization
|| member_specialization
)
2919 /* This doesn't apply to variable templates. */
2920 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2921 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2923 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2924 for (; t
; t
= TREE_CHAIN (t
))
2925 if (TREE_PURPOSE (t
))
2927 permerror (input_location
,
2928 "default argument specified in explicit specialization");
2933 if (specialization
|| member_specialization
|| explicit_instantiation
)
2935 tree tmpl
= NULL_TREE
;
2936 tree targs
= NULL_TREE
;
2937 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2939 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2940 if (!was_template_id
)
2944 gcc_assert (identifier_p (declarator
));
2949 /* If there is no class context, the explicit instantiation
2950 must be at namespace scope. */
2951 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2953 /* Find the namespace binding, using the declaration
2955 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2957 if (fns
== error_mark_node
)
2958 /* If lookup fails, look for a friend declaration so we can
2959 give a better diagnostic. */
2960 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2961 /*type*/false, /*complain*/true,
2964 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2966 error ("%qD is not a template function", dname
);
2967 fns
= error_mark_node
;
2971 declarator
= lookup_template_function (fns
, NULL_TREE
);
2974 if (declarator
== error_mark_node
)
2975 return error_mark_node
;
2977 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2979 if (!explicit_instantiation
)
2980 /* A specialization in class scope. This is invalid,
2981 but the error will already have been flagged by
2982 check_specialization_scope. */
2983 return error_mark_node
;
2986 /* It's not valid to write an explicit instantiation in
2989 class C { template void f(); }
2991 This case is caught by the parser. However, on
2994 template class C { void f(); };
2996 (which is invalid) we can get here. The error will be
3003 else if (ctype
!= NULL_TREE
3004 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3006 // We'll match variable templates in start_decl.
3010 /* Find the list of functions in ctype that have the same
3011 name as the declared function. */
3012 tree name
= TREE_OPERAND (declarator
, 0);
3014 if (constructor_name_p (name
, ctype
))
3016 if (DECL_CONSTRUCTOR_P (decl
)
3017 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3018 : !CLASSTYPE_DESTRUCTOR (ctype
))
3020 /* From [temp.expl.spec]:
3022 If such an explicit specialization for the member
3023 of a class template names an implicitly-declared
3024 special member function (clause _special_), the
3025 program is ill-formed.
3027 Similar language is found in [temp.explicit]. */
3028 error ("specialization of implicitly-declared special member function");
3029 return error_mark_node
;
3032 name
= DECL_NAME (decl
);
3035 /* For a type-conversion operator, We might be looking for
3036 `operator int' which will be a specialization of
3037 `operator T'. Grab all the conversion operators, and
3038 then select from them. */
3039 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3040 ? conv_op_identifier
: name
);
3042 if (fns
== NULL_TREE
)
3044 error ("no member function %qD declared in %qT", name
, ctype
);
3045 return error_mark_node
;
3048 TREE_OPERAND (declarator
, 0) = fns
;
3051 /* Figure out what exactly is being specialized at this point.
3052 Note that for an explicit instantiation, even one for a
3053 member function, we cannot tell a priori whether the
3054 instantiation is for a member template, or just a member
3055 function of a template class. Even if a member template is
3056 being instantiated, the member template arguments may be
3057 elided if they can be deduced from the rest of the
3059 tmpl
= determine_specialization (declarator
, decl
,
3061 member_specialization
,
3065 if (!tmpl
|| tmpl
== error_mark_node
)
3066 /* We couldn't figure out what this declaration was
3068 return error_mark_node
;
3071 if (TREE_CODE (decl
) == FUNCTION_DECL
3072 && DECL_HIDDEN_FRIEND_P (tmpl
))
3074 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3075 "friend declaration %qD is not visible to "
3076 "explicit specialization", tmpl
))
3077 inform (DECL_SOURCE_LOCATION (tmpl
),
3078 "friend declaration here");
3080 else if (!ctype
&& !is_friend
3081 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3082 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3084 tree gen_tmpl
= most_general_template (tmpl
);
3086 if (explicit_instantiation
)
3088 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3089 is done by do_decl_instantiation later. */
3091 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3092 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3094 if (arg_depth
> parm_depth
)
3096 /* If TMPL is not the most general template (for
3097 example, if TMPL is a friend template that is
3098 injected into namespace scope), then there will
3099 be too many levels of TARGS. Remove some of them
3104 new_targs
= make_tree_vec (parm_depth
);
3105 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3106 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3107 = TREE_VEC_ELT (targs
, i
);
3111 return instantiate_template (tmpl
, targs
, tf_error
);
3114 /* If we thought that the DECL was a member function, but it
3115 turns out to be specializing a static member function,
3116 make DECL a static member function as well. */
3117 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3118 && DECL_STATIC_FUNCTION_P (tmpl
)
3119 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3120 revert_static_member_fn (decl
);
3122 /* If this is a specialization of a member template of a
3123 template class, we want to return the TEMPLATE_DECL, not
3124 the specialization of it. */
3125 if (tsk
== tsk_template
&& !was_template_id
)
3127 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3128 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3129 DECL_INITIAL (result
) = NULL_TREE
;
3133 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3134 DECL_SOURCE_LOCATION (result
)
3135 = DECL_SOURCE_LOCATION (decl
);
3136 /* We want to use the argument list specified in the
3137 definition, not in the original declaration. */
3138 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3139 for (parm
= DECL_ARGUMENTS (result
); parm
;
3140 parm
= DECL_CHAIN (parm
))
3141 DECL_CONTEXT (parm
) = result
;
3143 return register_specialization (tmpl
, gen_tmpl
, targs
,
3147 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3148 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3150 if (was_template_id
)
3151 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3153 /* Inherit default function arguments from the template
3154 DECL is specializing. */
3155 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3156 copy_default_args_to_explicit_spec (decl
);
3158 /* This specialization has the same protection as the
3159 template it specializes. */
3160 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3161 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3163 /* 7.1.1-1 [dcl.stc]
3165 A storage-class-specifier shall not be specified in an
3166 explicit specialization...
3168 The parser rejects these, so unless action is taken here,
3169 explicit function specializations will always appear with
3172 The action recommended by the C++ CWG in response to C++
3173 defect report 605 is to make the storage class and linkage
3174 of the explicit specialization match the templated function:
3176 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3178 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3180 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3181 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3183 /* A concept cannot be specialized. */
3184 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3186 error ("explicit specialization of function concept %qD",
3188 return error_mark_node
;
3191 /* This specialization has the same linkage and visibility as
3192 the function template it specializes. */
3193 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3194 if (! TREE_PUBLIC (decl
))
3196 DECL_INTERFACE_KNOWN (decl
) = 1;
3197 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3199 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3200 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3202 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3203 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3207 /* If DECL is a friend declaration, declared using an
3208 unqualified name, the namespace associated with DECL may
3209 have been set incorrectly. For example, in:
3211 template <typename T> void f(T);
3213 struct S { friend void f<int>(int); }
3216 we will have set the DECL_CONTEXT for the friend
3217 declaration to N, rather than to the global namespace. */
3218 if (DECL_NAMESPACE_SCOPE_P (decl
))
3219 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3221 if (is_friend
&& !have_def
)
3222 /* This is not really a declaration of a specialization.
3223 It's just the name of an instantiation. But, it's not
3224 a request for an instantiation, either. */
3225 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3226 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3227 /* A specialization is not necessarily COMDAT. */
3228 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3229 && DECL_DECLARED_INLINE_P (decl
));
3230 else if (VAR_P (decl
))
3231 DECL_COMDAT (decl
) = false;
3233 /* If this is a full specialization, register it so that we can find
3234 it again. Partial specializations will be registered in
3235 process_partial_specialization. */
3236 if (!processing_template_decl
)
3238 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3240 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3245 /* A 'structor should already have clones. */
3246 gcc_assert (decl
== error_mark_node
3247 || variable_template_p (tmpl
)
3248 || !(DECL_CONSTRUCTOR_P (decl
)
3249 || DECL_DESTRUCTOR_P (decl
))
3250 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3257 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3258 parameters. These are represented in the same format used for
3259 DECL_TEMPLATE_PARMS. */
3262 comp_template_parms (const_tree parms1
, const_tree parms2
)
3267 if (parms1
== parms2
)
3270 for (p1
= parms1
, p2
= parms2
;
3271 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3272 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3274 tree t1
= TREE_VALUE (p1
);
3275 tree t2
= TREE_VALUE (p2
);
3278 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3279 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3281 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3284 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3286 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3287 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3289 /* If either of the template parameters are invalid, assume
3290 they match for the sake of error recovery. */
3291 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3294 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3297 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3298 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3299 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3301 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3306 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3307 /* One set of parameters has more parameters lists than the
3314 /* Determine whether PARM is a parameter pack. */
3317 template_parameter_pack_p (const_tree parm
)
3319 /* Determine if we have a non-type template parameter pack. */
3320 if (TREE_CODE (parm
) == PARM_DECL
)
3321 return (DECL_TEMPLATE_PARM_P (parm
)
3322 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3323 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3324 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3326 /* If this is a list of template parameters, we could get a
3327 TYPE_DECL or a TEMPLATE_DECL. */
3328 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3329 parm
= TREE_TYPE (parm
);
3331 /* Otherwise it must be a type template parameter. */
3332 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3333 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3334 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3337 /* Determine if T is a function parameter pack. */
3340 function_parameter_pack_p (const_tree t
)
3342 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3343 return DECL_PACK_P (t
);
3347 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3348 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3351 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3353 if (! primary_func_tmpl_inst
3354 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3355 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3358 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3361 /* Return true iff the function parameter PARAM_DECL was expanded
3362 from the function parameter pack PACK. */
3365 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3367 if (DECL_ARTIFICIAL (param_decl
)
3368 || !function_parameter_pack_p (pack
))
3371 /* The parameter pack and its pack arguments have the same
3373 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3376 /* Determine whether ARGS describes a variadic template args list,
3377 i.e., one that is terminated by a template argument pack. */
3380 template_args_variadic_p (tree args
)
3385 if (args
== NULL_TREE
)
3388 args
= INNERMOST_TEMPLATE_ARGS (args
);
3389 nargs
= TREE_VEC_LENGTH (args
);
3394 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3396 return ARGUMENT_PACK_P (last_parm
);
3399 /* Generate a new name for the parameter pack name NAME (an
3400 IDENTIFIER_NODE) that incorporates its */
3403 make_ith_pack_parameter_name (tree name
, int i
)
3405 /* Munge the name to include the parameter index. */
3406 #define NUMBUF_LEN 128
3407 char numbuf
[NUMBUF_LEN
];
3411 if (name
== NULL_TREE
)
3413 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3414 newname_len
= IDENTIFIER_LENGTH (name
)
3415 + strlen (numbuf
) + 2;
3416 newname
= (char*)alloca (newname_len
);
3417 snprintf (newname
, newname_len
,
3418 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3419 return get_identifier (newname
);
3422 /* Return true if T is a primary function, class or alias template
3423 specialization, not including the template pattern. */
3426 primary_template_specialization_p (const_tree t
)
3431 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3432 return (DECL_LANG_SPECIFIC (t
)
3433 && DECL_USE_TEMPLATE (t
)
3434 && DECL_TEMPLATE_INFO (t
)
3435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3436 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3437 return (CLASSTYPE_TEMPLATE_INFO (t
)
3438 && CLASSTYPE_USE_TEMPLATE (t
)
3439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3440 else if (alias_template_specialization_p (t
))
3445 /* Return true if PARM is a template template parameter. */
3448 template_template_parameter_p (const_tree parm
)
3450 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3453 /* Return true iff PARM is a DECL representing a type template
3457 template_type_parameter_p (const_tree parm
)
3460 && (TREE_CODE (parm
) == TYPE_DECL
3461 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3462 && DECL_TEMPLATE_PARM_P (parm
));
3465 /* Return the template parameters of T if T is a
3466 primary template instantiation, NULL otherwise. */
3469 get_primary_template_innermost_parameters (const_tree t
)
3471 tree parms
= NULL
, template_info
= NULL
;
3473 if ((template_info
= get_template_info (t
))
3474 && primary_template_specialization_p (t
))
3475 parms
= INNERMOST_TEMPLATE_PARMS
3476 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3481 /* Return the template parameters of the LEVELth level from the full list
3482 of template parameters PARMS. */
3485 get_template_parms_at_level (tree parms
, int level
)
3489 || TREE_CODE (parms
) != TREE_LIST
3490 || level
> TMPL_PARMS_DEPTH (parms
))
3493 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3494 if (TMPL_PARMS_DEPTH (p
) == level
)
3500 /* Returns the template arguments of T if T is a template instantiation,
3504 get_template_innermost_arguments (const_tree t
)
3506 tree args
= NULL
, template_info
= NULL
;
3508 if ((template_info
= get_template_info (t
))
3509 && TI_ARGS (template_info
))
3510 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3515 /* Return the argument pack elements of T if T is a template argument pack,
3519 get_template_argument_pack_elems (const_tree t
)
3521 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3522 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3525 return ARGUMENT_PACK_ARGS (t
);
3528 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3529 ARGUMENT_PACK_SELECT represents. */
3532 argument_pack_select_arg (tree t
)
3534 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3535 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3537 /* If the selected argument is an expansion E, that most likely means we were
3538 called from gen_elem_of_pack_expansion_instantiation during the
3539 substituting of an argument pack (of which the Ith element is a pack
3540 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3541 In this case, the Ith element resulting from this substituting is going to
3542 be a pack expansion, which pattern is the pattern of E. Let's return the
3543 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3544 resulting pack expansion from it. */
3545 if (PACK_EXPANSION_P (arg
))
3547 /* Make sure we aren't throwing away arg info. */
3548 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3549 arg
= PACK_EXPANSION_PATTERN (arg
);
3556 /* True iff FN is a function representing a built-in variadic parameter
3560 builtin_pack_fn_p (tree fn
)
3563 || TREE_CODE (fn
) != FUNCTION_DECL
3564 || !DECL_IS_BUILTIN (fn
))
3567 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3573 /* True iff CALL is a call to a function representing a built-in variadic
3577 builtin_pack_call_p (tree call
)
3579 if (TREE_CODE (call
) != CALL_EXPR
)
3581 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3584 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3587 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3590 tree ohi
= CALL_EXPR_ARG (call
, 0);
3591 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3592 false/*fn*/, true/*int_cst*/);
3594 if (value_dependent_expression_p (hi
))
3598 call
= copy_node (call
);
3599 CALL_EXPR_ARG (call
, 0) = hi
;
3601 tree ex
= make_pack_expansion (call
, complain
);
3602 tree vec
= make_tree_vec (1);
3603 TREE_VEC_ELT (vec
, 0) = ex
;
3608 hi
= cxx_constant_value (hi
);
3609 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3611 /* Calculate the largest value of len that won't make the size of the vec
3612 overflow an int. The compiler will exceed resource limits long before
3613 this, but it seems a decent place to diagnose. */
3614 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3616 if (len
< 0 || len
> max
)
3618 if ((complain
& tf_error
)
3619 && hi
!= error_mark_node
)
3620 error ("argument to __integer_pack must be between 0 and %d", max
);
3621 return error_mark_node
;
3624 tree vec
= make_tree_vec (len
);
3626 for (int i
= 0; i
< len
; ++i
)
3627 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3633 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3637 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3640 if (!builtin_pack_call_p (call
))
3643 tree fn
= CALL_EXPR_FN (call
);
3645 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3646 return expand_integer_pack (call
, args
, complain
, in_decl
);
3651 /* Structure used to track the progress of find_parameter_packs_r. */
3652 struct find_parameter_pack_data
3654 /* TREE_LIST that will contain all of the parameter packs found by
3656 tree
* parameter_packs
;
3658 /* Set of AST nodes that have been visited by the traversal. */
3659 hash_set
<tree
> *visited
;
3661 /* True iff we're making a type pack expansion. */
3662 bool type_pack_expansion_p
;
3665 /* Identifies all of the argument packs that occur in a template
3666 argument and appends them to the TREE_LIST inside DATA, which is a
3667 find_parameter_pack_data structure. This is a subroutine of
3668 make_pack_expansion and uses_parameter_packs. */
3670 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3673 struct find_parameter_pack_data
* ppd
=
3674 (struct find_parameter_pack_data
*)data
;
3675 bool parameter_pack_p
= false;
3677 /* Handle type aliases/typedefs. */
3678 if (TYPE_ALIAS_P (t
))
3680 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3681 cp_walk_tree (&TI_ARGS (tinfo
),
3682 &find_parameter_packs_r
,
3688 /* Identify whether this is a parameter pack or not. */
3689 switch (TREE_CODE (t
))
3691 case TEMPLATE_PARM_INDEX
:
3692 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3693 parameter_pack_p
= true;
3696 case TEMPLATE_TYPE_PARM
:
3697 t
= TYPE_MAIN_VARIANT (t
);
3699 case TEMPLATE_TEMPLATE_PARM
:
3700 /* If the placeholder appears in the decl-specifier-seq of a function
3701 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3702 is a pack expansion, the invented template parameter is a template
3704 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3705 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3706 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3707 parameter_pack_p
= true;
3712 if (DECL_PACK_P (t
))
3714 /* We don't want to walk into the type of a PARM_DECL,
3715 because we don't want to see the type parameter pack. */
3717 parameter_pack_p
= true;
3722 if (DECL_PACK_P (t
))
3724 /* We don't want to walk into the type of a variadic capture proxy,
3725 because we don't want to see the type parameter pack. */
3727 parameter_pack_p
= true;
3729 else if (variable_template_specialization_p (t
))
3731 cp_walk_tree (&DECL_TI_ARGS (t
),
3732 find_parameter_packs_r
,
3739 if (builtin_pack_call_p (t
))
3740 parameter_pack_p
= true;
3744 parameter_pack_p
= true;
3747 /* Not a parameter pack. */
3751 if (parameter_pack_p
)
3753 /* Add this parameter pack to the list. */
3754 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3758 cp_walk_tree (&TYPE_CONTEXT (t
),
3759 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3761 /* This switch statement will return immediately if we don't find a
3763 switch (TREE_CODE (t
))
3765 case TEMPLATE_PARM_INDEX
:
3768 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3769 /* Check the template itself. */
3770 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3771 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3772 /* Check the template arguments. */
3773 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3778 case TEMPLATE_TYPE_PARM
:
3779 case TEMPLATE_TEMPLATE_PARM
:
3786 /* Ignore the declaration of a capture proxy for a parameter pack. */
3787 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3792 if (TYPE_PTRMEMFUNC_P (t
))
3798 if (TYPE_TEMPLATE_INFO (t
))
3799 cp_walk_tree (&TYPE_TI_ARGS (t
),
3800 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3806 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3811 cp_walk_tree (&TREE_TYPE (t
),
3812 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3816 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3821 case TYPE_PACK_EXPANSION
:
3822 case EXPR_PACK_EXPANSION
:
3827 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3832 case IDENTIFIER_NODE
:
3833 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3840 /* Look at explicit captures. */
3841 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3842 cap
; cap
= TREE_CHAIN (cap
))
3843 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3845 /* Since we defer implicit capture, look in the body as well. */
3846 tree fn
= lambda_function (t
);
3847 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3855 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3856 type_pack_expansion_p to false so that any placeholders
3857 within the expression don't get marked as parameter packs. */
3858 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3859 ppd
->type_pack_expansion_p
= false;
3860 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3862 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3874 /* Determines if the expression or type T uses any parameter packs. */
3876 uses_parameter_packs (tree t
)
3878 tree parameter_packs
= NULL_TREE
;
3879 struct find_parameter_pack_data ppd
;
3880 ppd
.parameter_packs
= ¶meter_packs
;
3881 ppd
.visited
= new hash_set
<tree
>;
3882 ppd
.type_pack_expansion_p
= false;
3883 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3885 return parameter_packs
!= NULL_TREE
;
3888 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3889 representation a base-class initializer into a parameter pack
3890 expansion. If all goes well, the resulting node will be an
3891 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3894 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3897 tree parameter_packs
= NULL_TREE
;
3898 bool for_types
= false;
3899 struct find_parameter_pack_data ppd
;
3901 if (!arg
|| arg
== error_mark_node
)
3904 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3906 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3907 class initializer. In this case, the TREE_PURPOSE will be a
3908 _TYPE node (representing the base class expansion we're
3909 initializing) and the TREE_VALUE will be a TREE_LIST
3910 containing the initialization arguments.
3912 The resulting expansion looks somewhat different from most
3913 expansions. Rather than returning just one _EXPANSION, we
3914 return a TREE_LIST whose TREE_PURPOSE is a
3915 TYPE_PACK_EXPANSION containing the bases that will be
3916 initialized. The TREE_VALUE will be identical to the
3917 original TREE_VALUE, which is a list of arguments that will
3918 be passed to each base. We do not introduce any new pack
3919 expansion nodes into the TREE_VALUE (although it is possible
3920 that some already exist), because the TREE_PURPOSE and
3921 TREE_VALUE all need to be expanded together with the same
3922 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3923 resulting TREE_PURPOSE will mention the parameter packs in
3924 both the bases and the arguments to the bases. */
3927 tree parameter_packs
= NULL_TREE
;
3929 /* Determine which parameter packs will be used by the base
3931 ppd
.visited
= new hash_set
<tree
>;
3932 ppd
.parameter_packs
= ¶meter_packs
;
3933 ppd
.type_pack_expansion_p
= true;
3934 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3935 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3938 if (parameter_packs
== NULL_TREE
)
3940 if (complain
& tf_error
)
3941 error ("base initializer expansion %qT contains no parameter packs",
3944 return error_mark_node
;
3947 if (TREE_VALUE (arg
) != void_type_node
)
3949 /* Collect the sets of parameter packs used in each of the
3950 initialization arguments. */
3951 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3953 /* Determine which parameter packs will be expanded in this
3955 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3962 /* Create the pack expansion type for the base type. */
3963 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3964 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3965 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3966 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3968 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3969 they will rarely be compared to anything. */
3970 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3972 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3975 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3978 /* Build the PACK_EXPANSION_* node. */
3980 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3981 : make_node (EXPR_PACK_EXPANSION
);
3982 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3983 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3985 /* Propagate type and const-expression information. */
3986 TREE_TYPE (result
) = TREE_TYPE (arg
);
3987 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3988 /* Mark this read now, since the expansion might be length 0. */
3989 mark_exp_read (arg
);
3992 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3993 they will rarely be compared to anything. */
3994 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3996 /* Determine which parameter packs will be expanded. */
3997 ppd
.parameter_packs
= ¶meter_packs
;
3998 ppd
.visited
= new hash_set
<tree
>;
3999 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4000 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4003 /* Make sure we found some parameter packs. */
4004 if (parameter_packs
== NULL_TREE
)
4006 if (complain
& tf_error
)
4009 error ("expansion pattern %qT contains no argument packs", arg
);
4011 error ("expansion pattern %qE contains no argument packs", arg
);
4013 return error_mark_node
;
4015 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4017 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4022 /* Checks T for any "bare" parameter packs, which have not yet been
4023 expanded, and issues an error if any are found. This operation can
4024 only be done on full expressions or types (e.g., an expression
4025 statement, "if" condition, etc.), because we could have expressions like:
4027 foo(f(g(h(args)))...)
4029 where "args" is a parameter pack. check_for_bare_parameter_packs
4030 should not be called for the subexpressions args, h(args),
4031 g(h(args)), or f(g(h(args))), because we would produce erroneous
4034 Returns TRUE and emits an error if there were bare parameter packs,
4035 returns FALSE otherwise. */
4037 check_for_bare_parameter_packs (tree t
)
4039 tree parameter_packs
= NULL_TREE
;
4040 struct find_parameter_pack_data ppd
;
4042 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4045 /* A lambda might use a parameter pack from the containing context. */
4046 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4047 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4050 if (TREE_CODE (t
) == TYPE_DECL
)
4053 ppd
.parameter_packs
= ¶meter_packs
;
4054 ppd
.visited
= new hash_set
<tree
>;
4055 ppd
.type_pack_expansion_p
= false;
4056 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4059 if (parameter_packs
)
4061 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
4062 error_at (loc
, "parameter packs not expanded with %<...%>:");
4063 while (parameter_packs
)
4065 tree pack
= TREE_VALUE (parameter_packs
);
4066 tree name
= NULL_TREE
;
4068 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4069 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4070 name
= TYPE_NAME (pack
);
4071 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4072 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4073 else if (TREE_CODE (pack
) == CALL_EXPR
)
4074 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4076 name
= DECL_NAME (pack
);
4079 inform (loc
, " %qD", name
);
4081 inform (loc
, " <anonymous>");
4083 parameter_packs
= TREE_CHAIN (parameter_packs
);
4092 /* Expand any parameter packs that occur in the template arguments in
4095 expand_template_argument_pack (tree args
)
4097 if (args
== error_mark_node
)
4098 return error_mark_node
;
4100 tree result_args
= NULL_TREE
;
4101 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4102 int num_result_args
= -1;
4103 int non_default_args_count
= -1;
4105 /* First, determine if we need to expand anything, and the number of
4106 slots we'll need. */
4107 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4109 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4110 if (arg
== NULL_TREE
)
4112 if (ARGUMENT_PACK_P (arg
))
4114 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4115 if (num_result_args
< 0)
4116 num_result_args
= in_arg
+ num_packed
;
4118 num_result_args
+= num_packed
;
4122 if (num_result_args
>= 0)
4127 /* If no expansion is necessary, we're done. */
4128 if (num_result_args
< 0)
4131 /* Expand arguments. */
4132 result_args
= make_tree_vec (num_result_args
);
4133 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4134 non_default_args_count
=
4135 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4136 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4138 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4139 if (ARGUMENT_PACK_P (arg
))
4141 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4142 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4143 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4144 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4145 if (non_default_args_count
> 0)
4146 non_default_args_count
+= num_packed
- 1;
4150 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4154 if (non_default_args_count
>= 0)
4155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4159 /* Checks if DECL shadows a template parameter.
4161 [temp.local]: A template-parameter shall not be redeclared within its
4162 scope (including nested scopes).
4164 Emits an error and returns TRUE if the DECL shadows a parameter,
4165 returns FALSE otherwise. */
4168 check_template_shadow (tree decl
)
4172 /* If we're not in a template, we can't possibly shadow a template
4174 if (!current_template_parms
)
4177 /* Figure out what we're shadowing. */
4178 decl
= OVL_FIRST (decl
);
4179 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4181 /* If there's no previous binding for this name, we're not shadowing
4182 anything, let alone a template parameter. */
4186 /* If we're not shadowing a template parameter, we're done. Note
4187 that OLDDECL might be an OVERLOAD (or perhaps even an
4188 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4190 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4193 /* We check for decl != olddecl to avoid bogus errors for using a
4194 name inside a class. We check TPFI to avoid duplicate errors for
4195 inline member templates. */
4197 || (DECL_TEMPLATE_PARM_P (decl
)
4198 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4201 /* Don't complain about the injected class name, as we've already
4202 complained about the class itself. */
4203 if (DECL_SELF_REFERENCE_P (decl
))
4206 if (DECL_TEMPLATE_PARM_P (decl
))
4207 error ("declaration of template parameter %q+D shadows "
4208 "template parameter", decl
);
4210 error ("declaration of %q+#D shadows template parameter", decl
);
4211 inform (DECL_SOURCE_LOCATION (olddecl
),
4212 "template parameter %qD declared here", olddecl
);
4216 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4217 ORIG_LEVEL, DECL, and TYPE. */
4220 build_template_parm_index (int index
,
4226 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4227 TEMPLATE_PARM_IDX (t
) = index
;
4228 TEMPLATE_PARM_LEVEL (t
) = level
;
4229 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4230 TEMPLATE_PARM_DECL (t
) = decl
;
4231 TREE_TYPE (t
) = type
;
4232 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4233 TREE_READONLY (t
) = TREE_READONLY (decl
);
4238 /* Find the canonical type parameter for the given template type
4239 parameter. Returns the canonical type parameter, which may be TYPE
4240 if no such parameter existed. */
4243 canonical_type_parameter (tree type
)
4246 int idx
= TEMPLATE_TYPE_IDX (type
);
4247 if (!canonical_template_parms
)
4248 vec_alloc (canonical_template_parms
, idx
+ 1);
4250 if (canonical_template_parms
->length () <= (unsigned) idx
)
4251 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4253 list
= (*canonical_template_parms
)[idx
];
4254 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4255 list
= TREE_CHAIN (list
);
4258 return TREE_VALUE (list
);
4261 (*canonical_template_parms
)[idx
]
4262 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4267 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4268 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4269 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4270 new one is created. */
4273 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4274 tsubst_flags_t complain
)
4276 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4277 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4278 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4279 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4281 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4284 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4285 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4286 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4287 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4288 DECL_ARTIFICIAL (decl
) = 1;
4289 SET_DECL_TEMPLATE_PARM_P (decl
);
4291 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4292 TEMPLATE_PARM_LEVEL (index
) - levels
,
4293 TEMPLATE_PARM_ORIG_LEVEL (index
),
4295 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4296 TEMPLATE_PARM_PARAMETER_PACK (t
)
4297 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4299 /* Template template parameters need this. */
4300 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4302 DECL_TEMPLATE_RESULT (decl
)
4303 = build_decl (DECL_SOURCE_LOCATION (decl
),
4304 TYPE_DECL
, DECL_NAME (decl
), type
);
4305 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4306 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4307 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4311 return TEMPLATE_PARM_DESCENDANTS (index
);
4314 /* Process information from new template parameter PARM and append it
4315 to the LIST being built. This new parameter is a non-type
4316 parameter iff IS_NON_TYPE is true. This new parameter is a
4317 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4321 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4322 bool is_non_type
, bool is_parameter_pack
)
4327 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4328 tree defval
= TREE_PURPOSE (parm
);
4329 tree constr
= TREE_TYPE (parm
);
4333 tree p
= tree_last (list
);
4335 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4338 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4339 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4341 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4349 parm
= TREE_VALUE (parm
);
4351 SET_DECL_TEMPLATE_PARM_P (parm
);
4353 if (TREE_TYPE (parm
) != error_mark_node
)
4357 The top-level cv-qualifiers on the template-parameter are
4358 ignored when determining its type. */
4359 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4360 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4361 TREE_TYPE (parm
) = error_mark_node
;
4362 else if (uses_parameter_packs (TREE_TYPE (parm
))
4363 && !is_parameter_pack
4364 /* If we're in a nested template parameter list, the template
4365 template parameter could be a parameter pack. */
4366 && processing_template_parmlist
== 1)
4368 /* This template parameter is not a parameter pack, but it
4369 should be. Complain about "bare" parameter packs. */
4370 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4372 /* Recover by calling this a parameter pack. */
4373 is_parameter_pack
= true;
4377 /* A template parameter is not modifiable. */
4378 TREE_CONSTANT (parm
) = 1;
4379 TREE_READONLY (parm
) = 1;
4380 decl
= build_decl (parm_loc
,
4381 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4382 TREE_CONSTANT (decl
) = 1;
4383 TREE_READONLY (decl
) = 1;
4384 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4385 = build_template_parm_index (idx
, processing_template_decl
,
4386 processing_template_decl
,
4387 decl
, TREE_TYPE (parm
));
4389 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4390 = is_parameter_pack
;
4395 parm
= TREE_VALUE (TREE_VALUE (parm
));
4397 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4399 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4400 /* This is for distinguishing between real templates and template
4401 template parameters */
4402 TREE_TYPE (parm
) = t
;
4403 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4408 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4409 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4410 decl
= build_decl (parm_loc
,
4411 TYPE_DECL
, parm
, t
);
4414 TYPE_NAME (t
) = decl
;
4415 TYPE_STUB_DECL (t
) = decl
;
4417 TEMPLATE_TYPE_PARM_INDEX (t
)
4418 = build_template_parm_index (idx
, processing_template_decl
,
4419 processing_template_decl
,
4420 decl
, TREE_TYPE (parm
));
4421 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4422 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4424 DECL_ARTIFICIAL (decl
) = 1;
4425 SET_DECL_TEMPLATE_PARM_P (decl
);
4427 /* Build requirements for the type/template parameter.
4428 This must be done after SET_DECL_TEMPLATE_PARM_P or
4429 process_template_parm could fail. */
4430 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4434 if (defval
&& TREE_CODE (defval
) == OVERLOAD
)
4435 lookup_keep (defval
, true);
4437 /* Build the parameter node linking the parameter declaration,
4438 its default argument (if any), and its constraints (if any). */
4439 parm
= build_tree_list (defval
, parm
);
4440 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4442 return chainon (list
, parm
);
4445 /* The end of a template parameter list has been reached. Process the
4446 tree list into a parameter vector, converting each parameter into a more
4447 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4451 end_template_parm_list (tree parms
)
4455 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4457 /* Pop the dummy parameter level and add the real one. */
4458 current_template_parms
= TREE_CHAIN (current_template_parms
);
4460 current_template_parms
4461 = tree_cons (size_int (processing_template_decl
),
4462 saved_parmlist
, current_template_parms
);
4464 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4466 next
= TREE_CHAIN (parm
);
4467 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4468 TREE_CHAIN (parm
) = NULL_TREE
;
4471 --processing_template_parmlist
;
4473 return saved_parmlist
;
4476 // Explicitly indicate the end of the template parameter list. We assume
4477 // that the current template parameters have been constructed and/or
4478 // managed explicitly, as when creating new template template parameters
4479 // from a shorthand constraint.
4481 end_template_parm_list ()
4483 --processing_template_parmlist
;
4486 /* end_template_decl is called after a template declaration is seen. */
4489 end_template_decl (void)
4491 reset_specialization ();
4493 if (! processing_template_decl
)
4496 /* This matches the pushlevel in begin_template_parm_list. */
4499 --processing_template_decl
;
4500 current_template_parms
= TREE_CHAIN (current_template_parms
);
4503 /* Takes a TREE_LIST representing a template parameter and convert it
4504 into an argument suitable to be passed to the type substitution
4505 functions. Note that If the TREE_LIST contains an error_mark
4506 node, the returned argument is error_mark_node. */
4509 template_parm_to_arg (tree t
)
4513 || TREE_CODE (t
) != TREE_LIST
)
4516 if (error_operand_p (TREE_VALUE (t
)))
4517 return error_mark_node
;
4521 if (TREE_CODE (t
) == TYPE_DECL
4522 || TREE_CODE (t
) == TEMPLATE_DECL
)
4526 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4528 /* Turn this argument into a TYPE_ARGUMENT_PACK
4529 with a single element, which expands T. */
4530 tree vec
= make_tree_vec (1);
4532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4534 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4536 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4537 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4542 t
= DECL_INITIAL (t
);
4544 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4546 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4547 with a single element, which expands T. */
4548 tree vec
= make_tree_vec (1);
4550 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4552 t
= convert_from_reference (t
);
4553 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4555 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4556 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4559 t
= convert_from_reference (t
);
4564 /* Given a single level of template parameters (a TREE_VEC), return it
4565 as a set of template arguments. */
4568 template_parms_level_to_args (tree parms
)
4570 tree a
= copy_node (parms
);
4571 TREE_TYPE (a
) = NULL_TREE
;
4572 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4573 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4576 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4581 /* Given a set of template parameters, return them as a set of template
4582 arguments. The template parameters are represented as a TREE_VEC, in
4583 the form documented in cp-tree.h for template arguments. */
4586 template_parms_to_args (tree parms
)
4589 tree args
= NULL_TREE
;
4590 int length
= TMPL_PARMS_DEPTH (parms
);
4593 /* If there is only one level of template parameters, we do not
4594 create a TREE_VEC of TREE_VECs. Instead, we return a single
4595 TREE_VEC containing the arguments. */
4597 args
= make_tree_vec (length
);
4599 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4601 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4604 TREE_VEC_ELT (args
, --l
) = a
;
4612 /* Within the declaration of a template, return the currently active
4613 template parameters as an argument TREE_VEC. */
4616 current_template_args (void)
4618 return template_parms_to_args (current_template_parms
);
4621 /* Update the declared TYPE by doing any lookups which were thought to be
4622 dependent, but are not now that we know the SCOPE of the declarator. */
4625 maybe_update_decl_type (tree orig_type
, tree scope
)
4627 tree type
= orig_type
;
4629 if (type
== NULL_TREE
)
4632 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4633 type
= TREE_TYPE (type
);
4635 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4636 && dependent_type_p (type
)
4637 /* Don't bother building up the args in this case. */
4638 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4640 /* tsubst in the args corresponding to the template parameters,
4641 including auto if present. Most things will be unchanged, but
4642 make_typename_type and tsubst_qualified_id will resolve
4643 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4644 tree args
= current_template_args ();
4645 tree auto_node
= type_uses_auto (type
);
4649 tree auto_vec
= make_tree_vec (1);
4650 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4651 args
= add_to_template_args (args
, auto_vec
);
4653 pushed
= push_scope (scope
);
4654 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4659 if (type
== error_mark_node
)
4662 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4664 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4667 type
= TYPE_NAME (type
);
4672 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4673 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4674 the new template is a member template. */
4677 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4679 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4680 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4681 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4682 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4683 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4684 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4689 struct template_parm_data
4691 /* The level of the template parameters we are currently
4695 /* The index of the specialization argument we are currently
4699 /* An array whose size is the number of template parameters. The
4700 elements are nonzero if the parameter has been used in any one
4701 of the arguments processed so far. */
4704 /* An array whose size is the number of template arguments. The
4705 elements are nonzero if the argument makes use of template
4706 parameters of this level. */
4707 int* arg_uses_template_parms
;
4710 /* Subroutine of push_template_decl used to see if each template
4711 parameter in a partial specialization is used in the explicit
4712 argument list. If T is of the LEVEL given in DATA (which is
4713 treated as a template_parm_data*), then DATA->PARMS is marked
4717 mark_template_parm (tree t
, void* data
)
4721 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4723 template_parm_level_and_index (t
, &level
, &idx
);
4725 if (level
== tpd
->level
)
4727 tpd
->parms
[idx
] = 1;
4728 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4731 /* In C++17 the type of a non-type argument is a deduced context. */
4732 if (cxx_dialect
>= cxx17
4733 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4734 for_each_template_parm (TREE_TYPE (t
),
4735 &mark_template_parm
,
4738 /*include_nondeduced_p=*/false);
4740 /* Return zero so that for_each_template_parm will continue the
4741 traversal of the tree; we want to mark *every* template parm. */
4745 /* Process the partial specialization DECL. */
4748 process_partial_specialization (tree decl
)
4750 tree type
= TREE_TYPE (decl
);
4751 tree tinfo
= get_template_info (decl
);
4752 tree maintmpl
= TI_TEMPLATE (tinfo
);
4753 tree specargs
= TI_ARGS (tinfo
);
4754 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4755 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4758 int nargs
= TREE_VEC_LENGTH (inner_args
);
4761 bool did_error_intro
= false;
4762 struct template_parm_data tpd
;
4763 struct template_parm_data tpd2
;
4765 gcc_assert (current_template_parms
);
4767 /* A concept cannot be specialized. */
4768 if (flag_concepts
&& variable_concept_p (maintmpl
))
4770 error ("specialization of variable concept %q#D", maintmpl
);
4771 return error_mark_node
;
4774 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4775 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4777 /* We check that each of the template parameters given in the
4778 partial specialization is used in the argument list to the
4779 specialization. For example:
4781 template <class T> struct S;
4782 template <class T> struct S<T*>;
4784 The second declaration is OK because `T*' uses the template
4785 parameter T, whereas
4787 template <class T> struct S<int>;
4789 is no good. Even trickier is:
4800 The S2<T> declaration is actually invalid; it is a
4801 full-specialization. Of course,
4804 struct S2<T (*)(U)>;
4806 or some such would have been OK. */
4807 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4808 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4809 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4811 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4812 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4813 for (i
= 0; i
< nargs
; ++i
)
4815 tpd
.current_arg
= i
;
4816 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4817 &mark_template_parm
,
4820 /*include_nondeduced_p=*/false);
4822 for (i
= 0; i
< ntparms
; ++i
)
4823 if (tpd
.parms
[i
] == 0)
4825 /* One of the template parms was not used in a deduced context in the
4827 if (!did_error_intro
)
4829 error ("template parameters not deducible in "
4830 "partial specialization:");
4831 did_error_intro
= true;
4834 inform (input_location
, " %qD",
4835 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4838 if (did_error_intro
)
4839 return error_mark_node
;
4841 /* [temp.class.spec]
4843 The argument list of the specialization shall not be identical to
4844 the implicit argument list of the primary template. */
4846 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4847 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4849 || !strictly_subsumes (current_template_constraints (),
4850 get_constraints (maintmpl
))))
4853 error ("partial specialization %q+D does not specialize "
4854 "any template arguments; to define the primary template, "
4855 "remove the template argument list", decl
);
4857 error ("partial specialization %q+D does not specialize any "
4858 "template arguments and is not more constrained than "
4859 "the primary template; to define the primary template, "
4860 "remove the template argument list", decl
);
4861 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4864 /* A partial specialization that replaces multiple parameters of the
4865 primary template with a pack expansion is less specialized for those
4867 if (nargs
< DECL_NTPARMS (maintmpl
))
4869 error ("partial specialization is not more specialized than the "
4870 "primary template because it replaces multiple parameters "
4871 "with a pack expansion");
4872 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4873 /* Avoid crash in process_partial_specialization. */
4877 /* If we aren't in a dependent class, we can actually try deduction. */
4878 else if (tpd
.level
== 1
4879 /* FIXME we should be able to handle a partial specialization of a
4880 partial instantiation, but currently we can't (c++/41727). */
4881 && TMPL_ARGS_DEPTH (specargs
) == 1
4882 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4884 if (permerror (input_location
, "partial specialization %qD is not "
4885 "more specialized than", decl
))
4886 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4890 /* [temp.class.spec]
4892 A partially specialized non-type argument expression shall not
4893 involve template parameters of the partial specialization except
4894 when the argument expression is a simple identifier.
4896 The type of a template parameter corresponding to a specialized
4897 non-type argument shall not be dependent on a parameter of the
4900 Also, we verify that pack expansions only occur at the
4901 end of the argument list. */
4902 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4904 for (i
= 0; i
< nargs
; ++i
)
4906 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4907 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4908 tree packed_args
= NULL_TREE
;
4911 if (ARGUMENT_PACK_P (arg
))
4913 /* Extract the arguments from the argument pack. We'll be
4914 iterating over these in the following loop. */
4915 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4916 len
= TREE_VEC_LENGTH (packed_args
);
4919 for (j
= 0; j
< len
; j
++)
4922 /* Get the Jth argument in the parameter pack. */
4923 arg
= TREE_VEC_ELT (packed_args
, j
);
4925 if (PACK_EXPANSION_P (arg
))
4927 /* Pack expansions must come at the end of the
4929 if ((packed_args
&& j
< len
- 1)
4930 || (!packed_args
&& i
< nargs
- 1))
4932 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4933 error ("parameter pack argument %qE must be at the "
4934 "end of the template argument list", arg
);
4936 error ("parameter pack argument %qT must be at the "
4937 "end of the template argument list", arg
);
4941 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4942 /* We only care about the pattern. */
4943 arg
= PACK_EXPANSION_PATTERN (arg
);
4945 if (/* These first two lines are the `non-type' bit. */
4947 && TREE_CODE (arg
) != TEMPLATE_DECL
4948 /* This next two lines are the `argument expression is not just a
4949 simple identifier' condition and also the `specialized
4950 non-type argument' bit. */
4951 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4952 && !(REFERENCE_REF_P (arg
)
4953 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4955 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4956 || (packed_args
&& uses_template_parms (arg
)))
4957 error ("template argument %qE involves template parameter(s)",
4961 /* Look at the corresponding template parameter,
4962 marking which template parameters its type depends
4964 tree type
= TREE_TYPE (parm
);
4968 /* We haven't yet initialized TPD2. Do so now. */
4969 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4970 /* The number of parameters here is the number in the
4971 main template, which, as checked in the assertion
4973 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4975 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4978 /* Mark the template parameters. But this time, we're
4979 looking for the template parameters of the main
4980 template, not in the specialization. */
4981 tpd2
.current_arg
= i
;
4982 tpd2
.arg_uses_template_parms
[i
] = 0;
4983 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4984 for_each_template_parm (type
,
4985 &mark_template_parm
,
4988 /*include_nondeduced_p=*/false);
4990 if (tpd2
.arg_uses_template_parms
[i
])
4992 /* The type depended on some template parameters.
4993 If they are fully specialized in the
4994 specialization, that's OK. */
4997 for (j
= 0; j
< nargs
; ++j
)
4998 if (tpd2
.parms
[j
] != 0
4999 && tpd
.arg_uses_template_parms
[j
])
5002 error_n (input_location
, count
,
5003 "type %qT of template argument %qE depends "
5004 "on a template parameter",
5005 "type %qT of template argument %qE depends "
5006 "on template parameters",
5015 /* We should only get here once. */
5016 if (TREE_CODE (decl
) == TYPE_DECL
)
5017 gcc_assert (!COMPLETE_TYPE_P (type
));
5019 // Build the template decl.
5020 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5021 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5022 TREE_TYPE (tmpl
) = type
;
5023 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5025 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5026 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5028 /* Give template template parms a DECL_CONTEXT of the template
5029 for which they are a parameter. */
5030 for (i
= 0; i
< ntparms
; ++i
)
5032 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5033 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5034 DECL_CONTEXT (parm
) = tmpl
;
5038 /* We didn't register this in check_explicit_specialization so we could
5039 wait until the constraints were set. */
5040 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5042 associate_classtype_constraints (type
);
5044 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5045 = tree_cons (specargs
, tmpl
,
5046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5047 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5049 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5050 inst
= TREE_CHAIN (inst
))
5052 tree instance
= TREE_VALUE (inst
);
5053 if (TYPE_P (instance
)
5054 ? (COMPLETE_TYPE_P (instance
)
5055 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5056 : DECL_TEMPLATE_INSTANTIATION (instance
))
5058 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5059 tree inst_decl
= (DECL_P (instance
)
5060 ? instance
: TYPE_NAME (instance
));
5063 else if (spec
== error_mark_node
)
5064 permerror (input_location
,
5065 "declaration of %qD ambiguates earlier template "
5066 "instantiation for %qD", decl
, inst_decl
);
5067 else if (TREE_VALUE (spec
) == tmpl
)
5068 permerror (input_location
,
5069 "partial specialization of %qD after instantiation "
5070 "of %qD", decl
, inst_decl
);
5077 /* PARM is a template parameter of some form; return the corresponding
5078 TEMPLATE_PARM_INDEX. */
5081 get_template_parm_index (tree parm
)
5083 if (TREE_CODE (parm
) == PARM_DECL
5084 || TREE_CODE (parm
) == CONST_DECL
)
5085 parm
= DECL_INITIAL (parm
);
5086 else if (TREE_CODE (parm
) == TYPE_DECL
5087 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5088 parm
= TREE_TYPE (parm
);
5089 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5090 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5091 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5092 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5093 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5097 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5098 parameter packs used by the template parameter PARM. */
5101 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5103 /* A type parm can't refer to another parm. */
5104 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5106 else if (TREE_CODE (parm
) == PARM_DECL
)
5108 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5113 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5115 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5116 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5117 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5120 /* PARM is a template parameter pack. Return any parameter packs used in
5121 its type or the type of any of its template parameters. If there are
5122 any such packs, it will be instantiated into a fixed template parameter
5123 list by partial instantiation rather than be fully deduced. */
5126 fixed_parameter_pack_p (tree parm
)
5128 /* This can only be true in a member template. */
5129 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5131 /* This can only be true for a parameter pack. */
5132 if (!template_parameter_pack_p (parm
))
5134 /* A type parm can't refer to another parm. */
5135 if (TREE_CODE (parm
) == TYPE_DECL
)
5138 tree parameter_packs
= NULL_TREE
;
5139 struct find_parameter_pack_data ppd
;
5140 ppd
.parameter_packs
= ¶meter_packs
;
5141 ppd
.visited
= new hash_set
<tree
>;
5142 ppd
.type_pack_expansion_p
= false;
5144 fixed_parameter_pack_p_1 (parm
, &ppd
);
5147 return parameter_packs
;
5150 /* Check that a template declaration's use of default arguments and
5151 parameter packs is not invalid. Here, PARMS are the template
5152 parameters. IS_PRIMARY is true if DECL is the thing declared by
5153 a primary template. IS_PARTIAL is true if DECL is a partial
5156 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5157 function template declaration or a friend class template
5158 declaration. In the function case, 1 indicates a declaration, 2
5159 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5160 emitted for extraneous default arguments.
5162 Returns TRUE if there were no errors found, FALSE otherwise. */
5165 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5166 bool is_partial
, int is_friend_decl
)
5169 int last_level_to_check
;
5171 bool no_errors
= true;
5175 A default template-argument shall not be specified in a
5176 function template declaration or a function template definition, nor
5177 in the template-parameter-list of the definition of a member of a
5180 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5181 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5182 /* You can't have a function template declaration in a local
5183 scope, nor you can you define a member of a class template in a
5187 if ((TREE_CODE (decl
) == TYPE_DECL
5189 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5190 || (TREE_CODE (decl
) == FUNCTION_DECL
5191 && LAMBDA_FUNCTION_P (decl
)))
5192 /* A lambda doesn't have an explicit declaration; don't complain
5193 about the parms of the enclosing class. */
5196 if (current_class_type
5197 && !TYPE_BEING_DEFINED (current_class_type
)
5198 && DECL_LANG_SPECIFIC (decl
)
5199 && DECL_DECLARES_FUNCTION_P (decl
)
5200 /* If this is either a friend defined in the scope of the class
5201 or a member function. */
5202 && (DECL_FUNCTION_MEMBER_P (decl
)
5203 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5204 : DECL_FRIEND_CONTEXT (decl
)
5205 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5207 /* And, if it was a member function, it really was defined in
5208 the scope of the class. */
5209 && (!DECL_FUNCTION_MEMBER_P (decl
)
5210 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5211 /* We already checked these parameters when the template was
5212 declared, so there's no need to do it again now. This function
5213 was defined in class scope, but we're processing its body now
5214 that the class is complete. */
5217 /* Core issue 226 (C++0x only): the following only applies to class
5220 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5224 If a template-parameter has a default template-argument, all
5225 subsequent template-parameters shall have a default
5226 template-argument supplied. */
5227 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5229 tree inner_parms
= TREE_VALUE (parm_level
);
5230 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5231 int seen_def_arg_p
= 0;
5234 for (i
= 0; i
< ntparms
; ++i
)
5236 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5238 if (parm
== error_mark_node
)
5241 if (TREE_PURPOSE (parm
))
5243 else if (seen_def_arg_p
5244 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5246 error ("no default argument for %qD", TREE_VALUE (parm
));
5247 /* For better subsequent error-recovery, we indicate that
5248 there should have been a default argument. */
5249 TREE_PURPOSE (parm
) = error_mark_node
;
5252 else if (!is_partial
5254 /* Don't complain about an enclosing partial
5256 && parm_level
== parms
5257 && TREE_CODE (decl
) == TYPE_DECL
5259 && template_parameter_pack_p (TREE_VALUE (parm
))
5260 /* A fixed parameter pack will be partially
5261 instantiated into a fixed length list. */
5262 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5264 /* A primary class template can only have one
5265 parameter pack, at the end of the template
5268 error ("parameter pack %q+D must be at the end of the"
5269 " template parameter list", TREE_VALUE (parm
));
5271 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5279 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5283 /* For an ordinary class template, default template arguments are
5284 allowed at the innermost level, e.g.:
5285 template <class T = int>
5287 but, in a partial specialization, they're not allowed even
5288 there, as we have in [temp.class.spec]:
5290 The template parameter list of a specialization shall not
5291 contain default template argument values.
5293 So, for a partial specialization, or for a function template
5294 (in C++98/C++03), we look at all of them. */
5297 /* But, for a primary class template that is not a partial
5298 specialization we look at all template parameters except the
5300 parms
= TREE_CHAIN (parms
);
5302 /* Figure out what error message to issue. */
5303 if (is_friend_decl
== 2)
5304 msg
= G_("default template arguments may not be used in function template "
5305 "friend re-declaration");
5306 else if (is_friend_decl
)
5307 msg
= G_("default template arguments may not be used in template "
5308 "friend declarations");
5309 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5310 msg
= G_("default template arguments may not be used in function templates "
5311 "without -std=c++11 or -std=gnu++11");
5312 else if (is_partial
)
5313 msg
= G_("default template arguments may not be used in "
5314 "partial specializations");
5315 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5316 msg
= G_("default argument for template parameter for class enclosing %qD");
5318 /* Per [temp.param]/9, "A default template-argument shall not be
5319 specified in the template-parameter-lists of the definition of
5320 a member of a class template that appears outside of the member's
5321 class.", thus if we aren't handling a member of a class template
5322 there is no need to examine the parameters. */
5325 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5326 /* If we're inside a class definition, there's no need to
5327 examine the parameters to the class itself. On the one
5328 hand, they will be checked when the class is defined, and,
5329 on the other, default arguments are valid in things like:
5330 template <class T = double>
5331 struct S { template <class U> void f(U); };
5332 Here the default argument for `S' has no bearing on the
5333 declaration of `f'. */
5334 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5336 /* Check everything. */
5337 last_level_to_check
= 0;
5339 for (parm_level
= parms
;
5340 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5341 parm_level
= TREE_CHAIN (parm_level
))
5343 tree inner_parms
= TREE_VALUE (parm_level
);
5347 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5348 for (i
= 0; i
< ntparms
; ++i
)
5350 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5353 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5358 if (is_friend_decl
== 2)
5365 /* Clear out the default argument so that we are not
5367 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5371 /* At this point, if we're still interested in issuing messages,
5372 they must apply to classes surrounding the object declared. */
5374 msg
= G_("default argument for template parameter for class "
5381 /* Worker for push_template_decl_real, called via
5382 for_each_template_parm. DATA is really an int, indicating the
5383 level of the parameters we are interested in. If T is a template
5384 parameter of that level, return nonzero. */
5387 template_parm_this_level_p (tree t
, void* data
)
5389 int this_level
= *(int *)data
;
5392 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5393 level
= TEMPLATE_PARM_LEVEL (t
);
5395 level
= TEMPLATE_TYPE_LEVEL (t
);
5396 return level
== this_level
;
5399 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5400 DATA is really an int, indicating the innermost outer level of parameters.
5401 If T is a template parameter of that level or further out, return
5405 template_parm_outer_level (tree t
, void *data
)
5407 int this_level
= *(int *)data
;
5410 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5411 level
= TEMPLATE_PARM_LEVEL (t
);
5413 level
= TEMPLATE_TYPE_LEVEL (t
);
5414 return level
<= this_level
;
5417 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5418 parameters given by current_template_args, or reuses a
5419 previously existing one, if appropriate. Returns the DECL, or an
5420 equivalent one, if it is replaced via a call to duplicate_decls.
5422 If IS_FRIEND is true, DECL is a friend declaration. */
5425 push_template_decl_real (tree decl
, bool is_friend
)
5433 int new_template_p
= 0;
5434 /* True if the template is a member template, in the sense of
5436 bool member_template_p
= false;
5438 if (decl
== error_mark_node
|| !current_template_parms
)
5439 return error_mark_node
;
5441 /* See if this is a partial specialization. */
5442 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5443 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5444 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5446 && DECL_LANG_SPECIFIC (decl
)
5447 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5448 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5450 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5454 /* For a friend, we want the context of the friend, not
5455 the type of which it is a friend. */
5456 ctx
= CP_DECL_CONTEXT (decl
);
5457 else if (CP_DECL_CONTEXT (decl
)
5458 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5459 /* In the case of a virtual function, we want the class in which
5461 ctx
= CP_DECL_CONTEXT (decl
);
5463 /* Otherwise, if we're currently defining some class, the DECL
5464 is assumed to be a member of the class. */
5465 ctx
= current_scope ();
5467 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5470 if (!DECL_CONTEXT (decl
))
5471 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5473 /* See if this is a primary template. */
5474 if (is_friend
&& ctx
5475 && uses_template_parms_level (ctx
, processing_template_decl
))
5476 /* A friend template that specifies a class context, i.e.
5477 template <typename T> friend void A<T>::f();
5480 else if (TREE_CODE (decl
) == TYPE_DECL
5481 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5484 is_primary
= template_parm_scope_p ();
5488 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5490 if (DECL_CLASS_SCOPE_P (decl
))
5491 member_template_p
= true;
5492 if (TREE_CODE (decl
) == TYPE_DECL
5493 && anon_aggrname_p (DECL_NAME (decl
)))
5495 error ("template class without a name");
5496 return error_mark_node
;
5498 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5500 if (member_template_p
)
5502 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5503 error ("member template %qD may not have virt-specifiers", decl
);
5505 if (DECL_DESTRUCTOR_P (decl
))
5509 A destructor shall not be a member template. */
5510 error ("destructor %qD declared as member template", decl
);
5511 return error_mark_node
;
5513 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5514 && (!prototype_p (TREE_TYPE (decl
))
5515 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5516 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5517 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5518 == void_list_node
)))
5520 /* [basic.stc.dynamic.allocation]
5522 An allocation function can be a function
5523 template. ... Template allocation functions shall
5524 have two or more parameters. */
5525 error ("invalid template declaration of %qD", decl
);
5526 return error_mark_node
;
5529 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5530 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5532 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5533 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5534 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5536 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5537 if (TREE_CODE (t
) == TYPE_DECL
)
5539 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5540 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5543 else if (TREE_CODE (decl
) == TYPE_DECL
5544 && TYPE_DECL_ALIAS_P (decl
))
5545 /* alias-declaration */
5546 gcc_assert (!DECL_ARTIFICIAL (decl
));
5547 else if (VAR_P (decl
))
5548 /* C++14 variable template. */;
5551 error ("template declaration of %q#D", decl
);
5552 return error_mark_node
;
5556 /* Check to see that the rules regarding the use of default
5557 arguments are not being violated. We check args for a friend
5558 functions when we know whether it's a definition, introducing
5559 declaration or re-declaration. */
5560 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5561 check_default_tmpl_args (decl
, current_template_parms
,
5562 is_primary
, is_partial
, is_friend
);
5564 /* Ensure that there are no parameter packs in the type of this
5565 declaration that have not been expanded. */
5566 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5568 /* Check each of the arguments individually to see if there are
5569 any bare parameter packs. */
5570 tree type
= TREE_TYPE (decl
);
5571 tree arg
= DECL_ARGUMENTS (decl
);
5572 tree argtype
= TYPE_ARG_TYPES (type
);
5574 while (arg
&& argtype
)
5576 if (!DECL_PACK_P (arg
)
5577 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5579 /* This is a PARM_DECL that contains unexpanded parameter
5580 packs. We have already complained about this in the
5581 check_for_bare_parameter_packs call, so just replace
5582 these types with ERROR_MARK_NODE. */
5583 TREE_TYPE (arg
) = error_mark_node
;
5584 TREE_VALUE (argtype
) = error_mark_node
;
5587 arg
= DECL_CHAIN (arg
);
5588 argtype
= TREE_CHAIN (argtype
);
5591 /* Check for bare parameter packs in the return type and the
5592 exception specifiers. */
5593 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5594 /* Errors were already issued, set return type to int
5595 as the frontend doesn't expect error_mark_node as
5597 TREE_TYPE (type
) = integer_type_node
;
5598 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5599 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5601 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5602 && TYPE_DECL_ALIAS_P (decl
))
5603 ? DECL_ORIGINAL_TYPE (decl
)
5604 : TREE_TYPE (decl
)))
5606 TREE_TYPE (decl
) = error_mark_node
;
5607 return error_mark_node
;
5611 return process_partial_specialization (decl
);
5613 args
= current_template_args ();
5616 || TREE_CODE (ctx
) == FUNCTION_DECL
5617 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5618 || (TREE_CODE (decl
) == TYPE_DECL
5619 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5620 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5622 if (DECL_LANG_SPECIFIC (decl
)
5623 && DECL_TEMPLATE_INFO (decl
)
5624 && DECL_TI_TEMPLATE (decl
))
5625 tmpl
= DECL_TI_TEMPLATE (decl
);
5626 /* If DECL is a TYPE_DECL for a class-template, then there won't
5627 be DECL_LANG_SPECIFIC. The information equivalent to
5628 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5629 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5630 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5631 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5633 /* Since a template declaration already existed for this
5634 class-type, we must be redeclaring it here. Make sure
5635 that the redeclaration is valid. */
5636 redeclare_class_template (TREE_TYPE (decl
),
5637 current_template_parms
,
5638 current_template_constraints ());
5639 /* We don't need to create a new TEMPLATE_DECL; just use the
5640 one we already had. */
5641 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5645 tmpl
= build_template_decl (decl
, current_template_parms
,
5649 if (DECL_LANG_SPECIFIC (decl
)
5650 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5652 /* A specialization of a member template of a template
5654 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5655 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5656 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5662 tree a
, t
, current
, parms
;
5664 tree tinfo
= get_template_info (decl
);
5668 error ("template definition of non-template %q#D", decl
);
5669 return error_mark_node
;
5672 tmpl
= TI_TEMPLATE (tinfo
);
5674 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5675 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5676 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5677 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5681 /* The declaration is a specialization of a member
5682 template, declared outside the class. Therefore, the
5683 innermost template arguments will be NULL, so we
5684 replace them with the arguments determined by the
5685 earlier call to check_explicit_specialization. */
5686 args
= DECL_TI_ARGS (decl
);
5689 = build_template_decl (decl
, current_template_parms
,
5691 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5692 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5693 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5694 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5695 DECL_TEMPLATE_INFO (new_tmpl
)
5696 = build_template_info (tmpl
, args
);
5698 register_specialization (new_tmpl
,
5699 most_general_template (tmpl
),
5705 /* Make sure the template headers we got make sense. */
5707 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5708 i
= TMPL_PARMS_DEPTH (parms
);
5709 if (TMPL_ARGS_DEPTH (args
) != i
)
5711 error ("expected %d levels of template parms for %q#D, got %d",
5712 i
, decl
, TMPL_ARGS_DEPTH (args
));
5713 DECL_INTERFACE_KNOWN (decl
) = 1;
5714 return error_mark_node
;
5717 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5719 a
= TMPL_ARGS_LEVEL (args
, i
);
5720 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5722 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5724 if (current
== decl
)
5725 error ("got %d template parameters for %q#D",
5726 TREE_VEC_LENGTH (a
), decl
);
5728 error ("got %d template parameters for %q#T",
5729 TREE_VEC_LENGTH (a
), current
);
5730 error (" but %d required", TREE_VEC_LENGTH (t
));
5731 /* Avoid crash in import_export_decl. */
5732 DECL_INTERFACE_KNOWN (decl
) = 1;
5733 return error_mark_node
;
5736 if (current
== decl
)
5738 else if (current
== NULL_TREE
)
5739 /* Can happen in erroneous input. */
5742 current
= get_containing_scope (current
);
5745 /* Check that the parms are used in the appropriate qualifying scopes
5746 in the declarator. */
5747 if (!comp_template_args
5749 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5751 error ("template arguments to %qD do not match original "
5752 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5753 if (!uses_template_parms (TI_ARGS (tinfo
)))
5754 inform (input_location
, "use %<template<>%> for"
5755 " an explicit specialization");
5756 /* Avoid crash in import_export_decl. */
5757 DECL_INTERFACE_KNOWN (decl
) = 1;
5758 return error_mark_node
;
5762 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5763 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5765 /* Push template declarations for global functions and types. Note
5766 that we do not try to push a global template friend declared in a
5767 template class; such a thing may well depend on the template
5768 parameters of the class. */
5769 if (new_template_p
&& !ctx
5770 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5772 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5773 if (tmpl
== error_mark_node
)
5774 return error_mark_node
;
5776 /* Hide template friend classes that haven't been declared yet. */
5777 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5779 DECL_ANTICIPATED (tmpl
) = 1;
5780 DECL_FRIEND_P (tmpl
) = 1;
5786 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5788 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5790 /* Give template template parms a DECL_CONTEXT of the template
5791 for which they are a parameter. */
5792 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5793 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5795 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5796 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5797 DECL_CONTEXT (parm
) = tmpl
;
5800 if (TREE_CODE (decl
) == TYPE_DECL
5801 && TYPE_DECL_ALIAS_P (decl
)
5802 && complex_alias_template_p (tmpl
))
5803 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5806 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5807 back to its most general template. If TMPL is a specialization,
5808 ARGS may only have the innermost set of arguments. Add the missing
5809 argument levels if necessary. */
5810 if (DECL_TEMPLATE_INFO (tmpl
))
5811 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5813 info
= build_template_info (tmpl
, args
);
5815 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5816 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5820 retrofit_lang_decl (decl
);
5821 if (DECL_LANG_SPECIFIC (decl
))
5822 DECL_TEMPLATE_INFO (decl
) = info
;
5825 if (flag_implicit_templates
5827 && TREE_PUBLIC (decl
)
5828 && VAR_OR_FUNCTION_DECL_P (decl
))
5829 /* Set DECL_COMDAT on template instantiations; if we force
5830 them to be emitted by explicit instantiation or -frepo,
5831 mark_needed will tell cgraph to do the right thing. */
5832 DECL_COMDAT (decl
) = true;
5834 return DECL_TEMPLATE_RESULT (tmpl
);
5838 push_template_decl (tree decl
)
5840 return push_template_decl_real (decl
, false);
5843 /* FN is an inheriting constructor that inherits from the constructor
5844 template INHERITED; turn FN into a constructor template with a matching
5848 add_inherited_template_parms (tree fn
, tree inherited
)
5851 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5852 inner_parms
= copy_node (inner_parms
);
5854 = tree_cons (size_int (processing_template_decl
+ 1),
5855 inner_parms
, current_template_parms
);
5856 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5857 tree args
= template_parms_to_args (parms
);
5858 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5859 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5860 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5861 DECL_ARTIFICIAL (tmpl
) = true;
5862 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5866 /* Called when a class template TYPE is redeclared with the indicated
5867 template PARMS, e.g.:
5869 template <class T> struct S;
5870 template <class T> struct S {}; */
5873 redeclare_class_template (tree type
, tree parms
, tree cons
)
5879 if (!TYPE_TEMPLATE_INFO (type
))
5881 error ("%qT is not a template type", type
);
5885 tmpl
= TYPE_TI_TEMPLATE (type
);
5886 if (!PRIMARY_TEMPLATE_P (tmpl
))
5887 /* The type is nested in some template class. Nothing to worry
5888 about here; there are no new template parameters for the nested
5894 error ("template specifiers not specified in declaration of %qD",
5899 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5900 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5902 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5904 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5905 "redeclared with %d template parameter",
5906 "redeclared with %d template parameters",
5907 TREE_VEC_LENGTH (parms
));
5908 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5909 "previous declaration %qD used %d template parameter",
5910 "previous declaration %qD used %d template parameters",
5911 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5915 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5922 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5923 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5926 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5927 if (error_operand_p (tmpl_parm
))
5930 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5931 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5932 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5934 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5936 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5937 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5938 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5939 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5940 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5941 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5942 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5943 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5944 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5946 error ("template parameter %q+#D", tmpl_parm
);
5947 error ("redeclared here as %q#D", parm
);
5951 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5953 /* We have in [temp.param]:
5955 A template-parameter may not be given default arguments
5956 by two different declarations in the same scope. */
5957 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5958 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5959 "original definition appeared here");
5963 if (parm_default
!= NULL_TREE
)
5964 /* Update the previous template parameters (which are the ones
5965 that will really count) with the new default value. */
5966 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5967 else if (tmpl_default
!= NULL_TREE
)
5968 /* Update the new parameters, too; they'll be used as the
5969 parameters for any members. */
5970 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5972 /* Give each template template parm in this redeclaration a
5973 DECL_CONTEXT of the template for which they are a parameter. */
5974 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5976 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5977 DECL_CONTEXT (parm
) = tmpl
;
5980 if (TREE_CODE (parm
) == TYPE_DECL
)
5981 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5984 // Cannot redeclare a class template with a different set of constraints.
5985 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5987 error_at (input_location
, "redeclaration %q#D with different "
5988 "constraints", tmpl
);
5989 inform (DECL_SOURCE_LOCATION (tmpl
),
5990 "original declaration appeared here");
5996 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5997 to be used when the caller has already checked
5998 (processing_template_decl
5999 && !instantiation_dependent_expression_p (expr)
6000 && potential_constant_expression (expr))
6001 and cleared processing_template_decl. */
6004 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6006 return tsubst_copy_and_build (expr
,
6009 /*in_decl=*/NULL_TREE
,
6010 /*function_p=*/false,
6011 /*integral_constant_expression_p=*/true);
6014 /* Simplify EXPR if it is a non-dependent expression. Returns the
6015 (possibly simplified) expression. */
6018 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6020 if (expr
== NULL_TREE
)
6023 /* If we're in a template, but EXPR isn't value dependent, simplify
6024 it. We're supposed to treat:
6026 template <typename T> void f(T[1 + 1]);
6027 template <typename T> void f(T[2]);
6029 as two declarations of the same function, for example. */
6030 if (processing_template_decl
6031 && is_nondependent_constant_expression (expr
))
6033 processing_template_decl_sentinel s
;
6034 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6040 instantiate_non_dependent_expr (tree expr
)
6042 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6045 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6046 an uninstantiated expression. */
6049 instantiate_non_dependent_or_null (tree expr
)
6051 if (expr
== NULL_TREE
)
6053 if (processing_template_decl
)
6055 if (!is_nondependent_constant_expression (expr
))
6059 processing_template_decl_sentinel s
;
6060 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6066 /* True iff T is a specialization of a variable template. */
6069 variable_template_specialization_p (tree t
)
6071 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6073 tree tmpl
= DECL_TI_TEMPLATE (t
);
6074 return variable_template_p (tmpl
);
6077 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6078 template declaration, or a TYPE_DECL for an alias declaration. */
6081 alias_type_or_template_p (tree t
)
6085 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6088 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6089 || DECL_ALIAS_TEMPLATE_P (t
));
6092 /* Return TRUE iff T is a specialization of an alias template. */
6095 alias_template_specialization_p (const_tree t
)
6097 /* It's an alias template specialization if it's an alias and its
6098 TYPE_NAME is a specialization of a primary template. */
6099 if (TYPE_ALIAS_P (t
))
6100 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6101 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6106 /* An alias template is complex from a SFINAE perspective if a template-id
6107 using that alias can be ill-formed when the expansion is not, as with
6108 the void_t template. We determine this by checking whether the
6109 expansion for the alias template uses all its template parameters. */
6111 struct uses_all_template_parms_data
6118 uses_all_template_parms_r (tree t
, void *data_
)
6120 struct uses_all_template_parms_data
&data
6121 = *(struct uses_all_template_parms_data
*)data_
;
6122 tree idx
= get_template_parm_index (t
);
6124 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6125 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6130 complex_alias_template_p (const_tree tmpl
)
6132 struct uses_all_template_parms_data data
;
6133 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6134 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6135 data
.level
= TMPL_PARMS_DEPTH (parms
);
6136 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6137 data
.seen
= XALLOCAVEC (bool, len
);
6138 for (int i
= 0; i
< len
; ++i
)
6139 data
.seen
[i
] = false;
6141 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6142 for (int i
= 0; i
< len
; ++i
)
6148 /* Return TRUE iff T is a specialization of a complex alias template with
6149 dependent template-arguments. */
6152 dependent_alias_template_spec_p (const_tree t
)
6154 if (!alias_template_specialization_p (t
))
6157 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6158 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6161 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6162 if (!any_dependent_template_arguments_p (args
))
6168 /* Return the number of innermost template parameters in TMPL. */
6171 num_innermost_template_parms (tree tmpl
)
6173 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6174 return TREE_VEC_LENGTH (parms
);
6177 /* Return either TMPL or another template that it is equivalent to under DR
6178 1286: An alias that just changes the name of a template is equivalent to
6179 the other template. */
6182 get_underlying_template (tree tmpl
)
6184 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6185 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6187 /* Determine if the alias is equivalent to an underlying template. */
6188 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6189 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6193 tree underlying
= TI_TEMPLATE (tinfo
);
6194 if (!PRIMARY_TEMPLATE_P (underlying
)
6195 || (num_innermost_template_parms (tmpl
)
6196 != num_innermost_template_parms (underlying
)))
6199 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6200 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6201 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6204 /* Alias is equivalent. Strip it and repeat. */
6211 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6212 must be a reference-to-function or a pointer-to-function type, as specified
6213 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6214 and check that the resulting function has external linkage. */
6217 convert_nontype_argument_function (tree type
, tree expr
,
6218 tsubst_flags_t complain
)
6222 linkage_kind linkage
;
6224 fn
= instantiate_type (type
, fns
, tf_none
);
6225 if (fn
== error_mark_node
)
6226 return error_mark_node
;
6228 if (value_dependent_expression_p (fn
))
6231 fn_no_ptr
= strip_fnptr_conv (fn
);
6232 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6233 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6234 if (BASELINK_P (fn_no_ptr
))
6235 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6237 /* [temp.arg.nontype]/1
6239 A template-argument for a non-type, non-template template-parameter
6242 -- the address of an object or function with external [C++11: or
6243 internal] linkage. */
6245 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6247 if (complain
& tf_error
)
6249 error ("%qE is not a valid template argument for type %qT",
6251 if (TYPE_PTR_P (type
))
6252 inform (input_location
, "it must be the address of a function "
6253 "with external linkage");
6255 inform (input_location
, "it must be the name of a function with "
6256 "external linkage");
6261 linkage
= decl_linkage (fn_no_ptr
);
6262 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6264 if (complain
& tf_error
)
6266 if (cxx_dialect
>= cxx11
)
6267 error ("%qE is not a valid template argument for type %qT "
6268 "because %qD has no linkage",
6269 expr
, type
, fn_no_ptr
);
6271 error ("%qE is not a valid template argument for type %qT "
6272 "because %qD does not have external linkage",
6273 expr
, type
, fn_no_ptr
);
6279 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6281 if (REFERENCE_REF_P (fn
))
6282 fn
= TREE_OPERAND (fn
, 0);
6284 fn
= build_address (fn
);
6286 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6287 fn
= build_nop (type
, fn
);
6292 /* Subroutine of convert_nontype_argument.
6293 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6294 Emit an error otherwise. */
6297 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6298 tsubst_flags_t complain
)
6300 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6301 tree orig_expr
= expr
;
6303 if (null_ptr_cst_p (expr
))
6305 if (TREE_CODE (expr
) == PTRMEM_CST
6306 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6307 PTRMEM_CST_CLASS (expr
)))
6309 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6311 if (processing_template_decl
6312 && TREE_CODE (expr
) == ADDR_EXPR
6313 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6315 if (complain
& tf_error
)
6317 error_at (loc
, "%qE is not a valid template argument for type %qT",
6319 if (TREE_CODE (expr
) != PTRMEM_CST
)
6320 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6322 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6327 /* Returns TRUE iff the address of OP is value-dependent.
6329 14.6.2.4 [temp.dep.temp]:
6330 A non-integral non-type template-argument is dependent if its type is
6331 dependent or it has either of the following forms
6334 and contains a nested-name-specifier which specifies a class-name that
6335 names a dependent type.
6337 We generalize this to just say that the address of a member of a
6338 dependent class is value-dependent; the above doesn't cover the
6339 address of a static data member named with an unqualified-id. */
6342 has_value_dependent_address (tree op
)
6344 /* We could use get_inner_reference here, but there's no need;
6345 this is only relevant for template non-type arguments, which
6346 can only be expressed as &id-expression. */
6349 tree ctx
= CP_DECL_CONTEXT (op
);
6350 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6357 /* The next set of functions are used for providing helpful explanatory
6358 diagnostics for failed overload resolution. Their messages should be
6359 indented by two spaces for consistency with the messages in
6363 unify_success (bool /*explain_p*/)
6368 /* Other failure functions should call this one, to provide a single function
6369 for setting a breakpoint on. */
6372 unify_invalid (bool /*explain_p*/)
6378 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6381 inform (input_location
,
6382 " couldn't deduce template parameter %qD", parm
);
6383 return unify_invalid (explain_p
);
6387 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6390 inform (input_location
,
6391 " types %qT and %qT have incompatible cv-qualifiers",
6393 return unify_invalid (explain_p
);
6397 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6400 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6401 return unify_invalid (explain_p
);
6405 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6408 inform (input_location
,
6409 " template parameter %qD is not a parameter pack, but "
6412 return unify_invalid (explain_p
);
6416 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6419 inform (input_location
,
6420 " template argument %qE does not match "
6421 "pointer-to-member constant %qE",
6423 return unify_invalid (explain_p
);
6427 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6430 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6431 return unify_invalid (explain_p
);
6435 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6438 inform (input_location
,
6439 " inconsistent parameter pack deduction with %qT and %qT",
6441 return unify_invalid (explain_p
);
6445 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6450 inform (input_location
,
6451 " deduced conflicting types for parameter %qT (%qT and %qT)",
6452 parm
, first
, second
);
6454 inform (input_location
,
6455 " deduced conflicting values for non-type parameter "
6456 "%qE (%qE and %qE)", parm
, first
, second
);
6458 return unify_invalid (explain_p
);
6462 unify_vla_arg (bool explain_p
, tree arg
)
6465 inform (input_location
,
6466 " variable-sized array type %qT is not "
6467 "a valid template argument",
6469 return unify_invalid (explain_p
);
6473 unify_method_type_error (bool explain_p
, tree arg
)
6476 inform (input_location
,
6477 " member function type %qT is not a valid template argument",
6479 return unify_invalid (explain_p
);
6483 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6488 inform_n (input_location
, wanted
,
6489 " candidate expects at least %d argument, %d provided",
6490 " candidate expects at least %d arguments, %d provided",
6493 inform_n (input_location
, wanted
,
6494 " candidate expects %d argument, %d provided",
6495 " candidate expects %d arguments, %d provided",
6498 return unify_invalid (explain_p
);
6502 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6504 return unify_arity (explain_p
, have
, wanted
);
6508 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6509 bool least_p
= false)
6511 return unify_arity (explain_p
, have
, wanted
, least_p
);
6515 unify_arg_conversion (bool explain_p
, tree to_type
,
6516 tree from_type
, tree arg
)
6519 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6520 " cannot convert %qE (type %qT) to type %qT",
6521 arg
, from_type
, to_type
);
6522 return unify_invalid (explain_p
);
6526 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6527 tree parm
, tree arg
)
6532 case tbr_ambiguous_baseclass
:
6533 inform (input_location
, " %qT is an ambiguous base class of %qT",
6537 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6540 return unify_invalid (explain_p
);
6544 unify_inconsistent_template_template_parameters (bool explain_p
)
6547 inform (input_location
,
6548 " template parameters of a template template argument are "
6549 "inconsistent with other deduced template arguments");
6550 return unify_invalid (explain_p
);
6554 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6557 inform (input_location
,
6558 " can't deduce a template for %qT from non-template type %qT",
6560 return unify_invalid (explain_p
);
6564 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6567 inform (input_location
,
6568 " template argument %qE does not match %qE", arg
, parm
);
6569 return unify_invalid (explain_p
);
6572 /* Attempt to convert the non-type template parameter EXPR to the
6573 indicated TYPE. If the conversion is successful, return the
6574 converted value. If the conversion is unsuccessful, return
6575 NULL_TREE if we issued an error message, or error_mark_node if we
6576 did not. We issue error messages for out-and-out bad template
6577 parameters, but not simply because the conversion failed, since we
6578 might be just trying to do argument deduction. Both TYPE and EXPR
6579 must be non-dependent.
6581 The conversion follows the special rules described in
6582 [temp.arg.nontype], and it is much more strict than an implicit
6585 This function is called twice for each template argument (see
6586 lookup_template_class for a more accurate description of this
6587 problem). This means that we need to handle expressions which
6588 are not valid in a C++ source, but can be created from the
6589 first call (for instance, casts to perform conversions). These
6590 hacks can go away after we fix the double coercion problem. */
6593 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6596 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6597 tree orig_expr
= expr
;
6599 /* Detect immediately string literals as invalid non-type argument.
6600 This special-case is not needed for correctness (we would easily
6601 catch this later), but only to provide better diagnostic for this
6602 common user mistake. As suggested by DR 100, we do not mention
6603 linkage issues in the diagnostic as this is not the point. */
6604 /* FIXME we're making this OK. */
6605 if (TREE_CODE (expr
) == STRING_CST
)
6607 if (complain
& tf_error
)
6608 error ("%qE is not a valid template argument for type %qT "
6609 "because string literals can never be used in this context",
6614 /* Add the ADDR_EXPR now for the benefit of
6615 value_dependent_expression_p. */
6616 if (TYPE_PTROBV_P (type
)
6617 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6619 expr
= decay_conversion (expr
, complain
);
6620 if (expr
== error_mark_node
)
6621 return error_mark_node
;
6624 /* If we are in a template, EXPR may be non-dependent, but still
6625 have a syntactic, rather than semantic, form. For example, EXPR
6626 might be a SCOPE_REF, rather than the VAR_DECL to which the
6627 SCOPE_REF refers. Preserving the qualifying scope is necessary
6628 so that access checking can be performed when the template is
6629 instantiated -- but here we need the resolved form so that we can
6630 convert the argument. */
6631 bool non_dep
= false;
6632 if (TYPE_REF_OBJ_P (type
)
6633 && has_value_dependent_address (expr
))
6634 /* If we want the address and it's value-dependent, don't fold. */;
6635 else if (processing_template_decl
6636 && is_nondependent_constant_expression (expr
))
6638 if (error_operand_p (expr
))
6639 return error_mark_node
;
6640 expr_type
= TREE_TYPE (expr
);
6642 /* If the argument is non-dependent, perform any conversions in
6643 non-dependent context as well. */
6644 processing_template_decl_sentinel
s (non_dep
);
6646 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6648 if (value_dependent_expression_p (expr
))
6649 expr
= canonicalize_expr_argument (expr
, complain
);
6651 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6652 to a non-type argument of "nullptr". */
6653 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6654 expr
= fold_simple (convert (type
, expr
));
6656 /* In C++11, integral or enumeration non-type template arguments can be
6657 arbitrary constant expressions. Pointer and pointer to
6658 member arguments can be general constant expressions that evaluate
6659 to a null value, but otherwise still need to be of a specific form. */
6660 if (cxx_dialect
>= cxx11
)
6662 if (TREE_CODE (expr
) == PTRMEM_CST
)
6663 /* A PTRMEM_CST is already constant, and a valid template
6664 argument for a parameter of pointer to member type, we just want
6665 to leave it in that form rather than lower it to a
6667 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6668 || cxx_dialect
>= cxx17
)
6670 /* C++17: A template-argument for a non-type template-parameter shall
6671 be a converted constant expression (8.20) of the type of the
6672 template-parameter. */
6673 expr
= build_converted_constant_expr (type
, expr
, complain
);
6674 if (expr
== error_mark_node
)
6675 return error_mark_node
;
6676 expr
= maybe_constant_value (expr
);
6677 expr
= convert_from_reference (expr
);
6679 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6681 tree folded
= maybe_constant_value (expr
);
6682 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6683 : null_member_pointer_value_p (folded
))
6688 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6689 expr
= mark_lvalue_use (expr
);
6691 expr
= mark_rvalue_use (expr
);
6693 /* HACK: Due to double coercion, we can get a
6694 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6695 which is the tree that we built on the first call (see
6696 below when coercing to reference to object or to reference to
6697 function). We just strip everything and get to the arg.
6698 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6700 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6702 tree probe_type
, probe
= expr
;
6703 if (REFERENCE_REF_P (probe
))
6704 probe
= TREE_OPERAND (probe
, 0);
6705 probe_type
= TREE_TYPE (probe
);
6706 if (TREE_CODE (probe
) == NOP_EXPR
)
6708 /* ??? Maybe we could use convert_from_reference here, but we
6709 would need to relax its constraints because the NOP_EXPR
6710 could actually change the type to something more cv-qualified,
6711 and this is not folded by convert_from_reference. */
6712 tree addr
= TREE_OPERAND (probe
, 0);
6713 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6714 && TREE_CODE (addr
) == ADDR_EXPR
6715 && TYPE_PTR_P (TREE_TYPE (addr
))
6716 && (same_type_ignoring_top_level_qualifiers_p
6717 (TREE_TYPE (probe_type
),
6718 TREE_TYPE (TREE_TYPE (addr
)))))
6720 expr
= TREE_OPERAND (addr
, 0);
6721 expr_type
= TREE_TYPE (probe_type
);
6726 /* [temp.arg.nontype]/5, bullet 1
6728 For a non-type template-parameter of integral or enumeration type,
6729 integral promotions (_conv.prom_) and integral conversions
6730 (_conv.integral_) are applied. */
6731 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6733 if (cxx_dialect
< cxx11
)
6735 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6736 t
= maybe_constant_value (t
);
6737 if (t
!= error_mark_node
)
6741 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6742 return error_mark_node
;
6744 /* Notice that there are constant expressions like '4 % 0' which
6745 do not fold into integer constants. */
6746 if (TREE_CODE (expr
) != INTEGER_CST
6747 && !value_dependent_expression_p (expr
))
6749 if (complain
& tf_error
)
6751 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6752 if (!require_potential_constant_expression (expr
))
6753 expr
= error_mark_node
;
6755 expr
= cxx_constant_value (expr
);
6756 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6757 inform (loc
, "in template argument for type %qT", type
);
6758 if (expr
== error_mark_node
)
6760 /* else cxx_constant_value complained but gave us
6761 a real constant, so go ahead. */
6762 if (TREE_CODE (expr
) != INTEGER_CST
)
6764 /* Some assemble time constant expressions like
6765 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6766 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6767 as we can emit them into .rodata initializers of
6768 variables, yet they can't fold into an INTEGER_CST at
6769 compile time. Refuse them here. */
6770 gcc_checking_assert (reduced_constant_expression_p (expr
));
6771 error_at (loc
, "template argument %qE for type %qT not "
6772 "a constant integer", expr
, type
);
6780 /* Avoid typedef problems. */
6781 if (TREE_TYPE (expr
) != type
)
6782 expr
= fold_convert (type
, expr
);
6784 /* [temp.arg.nontype]/5, bullet 2
6786 For a non-type template-parameter of type pointer to object,
6787 qualification conversions (_conv.qual_) and the array-to-pointer
6788 conversion (_conv.array_) are applied. */
6789 else if (TYPE_PTROBV_P (type
))
6791 tree decayed
= expr
;
6793 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6794 decay_conversion or an explicit cast. If it's a problematic cast,
6795 we'll complain about it below. */
6796 if (TREE_CODE (expr
) == NOP_EXPR
)
6800 if (TREE_CODE (probe
) == ADDR_EXPR
6801 && TYPE_PTR_P (TREE_TYPE (probe
)))
6804 expr_type
= TREE_TYPE (expr
);
6808 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6810 A template-argument for a non-type, non-template template-parameter
6811 shall be one of: [...]
6813 -- the name of a non-type template-parameter;
6814 -- the address of an object or function with external linkage, [...]
6815 expressed as "& id-expression" where the & is optional if the name
6816 refers to a function or array, or if the corresponding
6817 template-parameter is a reference.
6819 Here, we do not care about functions, as they are invalid anyway
6820 for a parameter of type pointer-to-object. */
6822 if (value_dependent_expression_p (expr
))
6823 /* Non-type template parameters are OK. */
6825 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6826 /* Null pointer values are OK in C++11. */;
6827 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6831 if (complain
& tf_error
)
6832 error ("%qD is not a valid template argument "
6833 "because %qD is a variable, not the address of "
6834 "a variable", orig_expr
, expr
);
6837 if (POINTER_TYPE_P (expr_type
))
6839 if (complain
& tf_error
)
6840 error ("%qE is not a valid template argument for %qT "
6841 "because it is not the address of a variable",
6845 /* Other values, like integer constants, might be valid
6846 non-type arguments of some other type. */
6847 return error_mark_node
;
6851 tree decl
= TREE_OPERAND (expr
, 0);
6855 if (complain
& tf_error
)
6856 error ("%qE is not a valid template argument of type %qT "
6857 "because %qE is not a variable", orig_expr
, type
, decl
);
6860 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6862 if (complain
& tf_error
)
6863 error ("%qE is not a valid template argument of type %qT "
6864 "because %qD does not have external linkage",
6865 orig_expr
, type
, decl
);
6868 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6869 && decl_linkage (decl
) == lk_none
)
6871 if (complain
& tf_error
)
6872 error ("%qE is not a valid template argument of type %qT "
6873 "because %qD has no linkage", orig_expr
, type
, decl
);
6876 /* C++17: For a non-type template-parameter of reference or pointer
6877 type, the value of the constant expression shall not refer to (or
6878 for a pointer type, shall not be the address of):
6879 * a subobject (4.5),
6880 * a temporary object (15.2),
6881 * a string literal (5.13.5),
6882 * the result of a typeid expression (8.2.8), or
6883 * a predefined __func__ variable (11.4.1). */
6884 else if (DECL_ARTIFICIAL (decl
))
6886 if (complain
& tf_error
)
6887 error ("the address of %qD is not a valid template argument",
6891 else if (!same_type_ignoring_top_level_qualifiers_p
6892 (strip_array_types (TREE_TYPE (type
)),
6893 strip_array_types (TREE_TYPE (decl
))))
6895 if (complain
& tf_error
)
6896 error ("the address of the %qT subobject of %qD is not a "
6897 "valid template argument", TREE_TYPE (type
), decl
);
6900 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6902 if (complain
& tf_error
)
6903 error ("the address of %qD is not a valid template argument "
6904 "because it does not have static storage duration",
6912 expr
= perform_qualification_conversions (type
, expr
);
6913 if (expr
== error_mark_node
)
6914 return error_mark_node
;
6916 /* [temp.arg.nontype]/5, bullet 3
6918 For a non-type template-parameter of type reference to object, no
6919 conversions apply. The type referred to by the reference may be more
6920 cv-qualified than the (otherwise identical) type of the
6921 template-argument. The template-parameter is bound directly to the
6922 template-argument, which must be an lvalue. */
6923 else if (TYPE_REF_OBJ_P (type
))
6925 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6927 return error_mark_node
;
6929 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6931 if (complain
& tf_error
)
6932 error ("%qE is not a valid template argument for type %qT "
6933 "because of conflicts in cv-qualification", expr
, type
);
6937 if (!lvalue_p (expr
))
6939 if (complain
& tf_error
)
6940 error ("%qE is not a valid template argument for type %qT "
6941 "because it is not an lvalue", expr
, type
);
6945 /* [temp.arg.nontype]/1
6947 A template-argument for a non-type, non-template template-parameter
6948 shall be one of: [...]
6950 -- the address of an object or function with external linkage. */
6951 if (INDIRECT_REF_P (expr
)
6952 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6954 expr
= TREE_OPERAND (expr
, 0);
6957 if (complain
& tf_error
)
6958 error ("%q#D is not a valid template argument for type %qT "
6959 "because a reference variable does not have a constant "
6960 "address", expr
, type
);
6965 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6966 && value_dependent_expression_p (expr
))
6967 /* OK, dependent reference. We don't want to ask whether a DECL is
6968 itself value-dependent, since what we want here is its address. */;
6973 if (complain
& tf_error
)
6974 error ("%qE is not a valid template argument for type %qT "
6975 "because it is not an object with linkage",
6980 /* DR 1155 allows internal linkage in C++11 and up. */
6981 linkage_kind linkage
= decl_linkage (expr
);
6982 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6984 if (complain
& tf_error
)
6985 error ("%qE is not a valid template argument for type %qT "
6986 "because object %qD does not have linkage",
6991 expr
= build_address (expr
);
6994 if (!same_type_p (type
, TREE_TYPE (expr
)))
6995 expr
= build_nop (type
, expr
);
6997 /* [temp.arg.nontype]/5, bullet 4
6999 For a non-type template-parameter of type pointer to function, only
7000 the function-to-pointer conversion (_conv.func_) is applied. If the
7001 template-argument represents a set of overloaded functions (or a
7002 pointer to such), the matching function is selected from the set
7004 else if (TYPE_PTRFN_P (type
))
7006 /* If the argument is a template-id, we might not have enough
7007 context information to decay the pointer. */
7008 if (!type_unknown_p (expr_type
))
7010 expr
= decay_conversion (expr
, complain
);
7011 if (expr
== error_mark_node
)
7012 return error_mark_node
;
7015 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7016 /* Null pointer values are OK in C++11. */
7017 return perform_qualification_conversions (type
, expr
);
7019 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7020 if (!expr
|| expr
== error_mark_node
)
7023 /* [temp.arg.nontype]/5, bullet 5
7025 For a non-type template-parameter of type reference to function, no
7026 conversions apply. If the template-argument represents a set of
7027 overloaded functions, the matching function is selected from the set
7029 else if (TYPE_REFFN_P (type
))
7031 if (TREE_CODE (expr
) == ADDR_EXPR
)
7033 if (complain
& tf_error
)
7035 error ("%qE is not a valid template argument for type %qT "
7036 "because it is a pointer", expr
, type
);
7037 inform (input_location
, "try using %qE instead",
7038 TREE_OPERAND (expr
, 0));
7043 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7044 if (!expr
|| expr
== error_mark_node
)
7047 /* [temp.arg.nontype]/5, bullet 6
7049 For a non-type template-parameter of type pointer to member function,
7050 no conversions apply. If the template-argument represents a set of
7051 overloaded member functions, the matching member function is selected
7052 from the set (_over.over_). */
7053 else if (TYPE_PTRMEMFUNC_P (type
))
7055 expr
= instantiate_type (type
, expr
, tf_none
);
7056 if (expr
== error_mark_node
)
7057 return error_mark_node
;
7059 /* [temp.arg.nontype] bullet 1 says the pointer to member
7060 expression must be a pointer-to-member constant. */
7061 if (!value_dependent_expression_p (expr
)
7062 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7065 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7066 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7067 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7068 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7070 /* [temp.arg.nontype]/5, bullet 7
7072 For a non-type template-parameter of type pointer to data member,
7073 qualification conversions (_conv.qual_) are applied. */
7074 else if (TYPE_PTRDATAMEM_P (type
))
7076 /* [temp.arg.nontype] bullet 1 says the pointer to member
7077 expression must be a pointer-to-member constant. */
7078 if (!value_dependent_expression_p (expr
)
7079 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7082 expr
= perform_qualification_conversions (type
, expr
);
7083 if (expr
== error_mark_node
)
7086 else if (NULLPTR_TYPE_P (type
))
7088 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7090 if (complain
& tf_error
)
7091 error ("%qE is not a valid template argument for type %qT "
7092 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7097 /* A template non-type parameter must be one of the above. */
7101 /* Sanity check: did we actually convert the argument to the
7103 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7104 (type
, TREE_TYPE (expr
)));
7105 return convert_from_reference (expr
);
7108 /* Subroutine of coerce_template_template_parms, which returns 1 if
7109 PARM_PARM and ARG_PARM match using the rule for the template
7110 parameters of template template parameters. Both PARM and ARG are
7111 template parameters; the rest of the arguments are the same as for
7112 coerce_template_template_parms.
7115 coerce_template_template_parm (tree parm
,
7117 tsubst_flags_t complain
,
7121 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7122 || parm
== NULL_TREE
|| error_operand_p (parm
))
7125 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7128 switch (TREE_CODE (parm
))
7131 /* We encounter instantiations of templates like
7132 template <template <template <class> class> class TT>
7135 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7136 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7138 if (!coerce_template_template_parms
7139 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7145 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7146 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7147 /* Argument is a parameter pack but parameter is not. */
7152 /* The tsubst call is used to handle cases such as
7154 template <int> class C {};
7155 template <class T, template <T> class TT> class D {};
7158 i.e. the parameter list of TT depends on earlier parameters. */
7159 if (!uses_template_parms (TREE_TYPE (arg
)))
7161 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7162 if (!uses_template_parms (t
)
7163 && !same_type_p (t
, TREE_TYPE (arg
)))
7167 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7168 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7169 /* Argument is a parameter pack but parameter is not. */
7181 /* Coerce template argument list ARGLIST for use with template
7182 template-parameter TEMPL. */
7185 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7186 tsubst_flags_t complain
)
7188 /* Consider an example where a template template parameter declared as
7190 template <class T, class U = std::allocator<T> > class TT
7192 The template parameter level of T and U are one level larger than
7193 of TT. To proper process the default argument of U, say when an
7194 instantiation `TT<int>' is seen, we need to build the full
7195 arguments containing {int} as the innermost level. Outer levels,
7196 available when not appearing as default template argument, can be
7197 obtained from the arguments of the enclosing template.
7199 Suppose that TT is later substituted with std::vector. The above
7200 instantiation is `TT<int, std::allocator<T> >' with TT at
7201 level 1, and T at level 2, while the template arguments at level 1
7202 becomes {std::vector} and the inner level 2 is {int}. */
7204 tree outer
= DECL_CONTEXT (templ
);
7207 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7208 /* We want arguments for the partial specialization, not arguments for
7209 the primary template. */
7210 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7212 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7214 else if (current_template_parms
)
7216 /* This is an argument of the current template, so we haven't set
7217 DECL_CONTEXT yet. */
7218 tree relevant_template_parms
;
7220 /* Parameter levels that are greater than the level of the given
7221 template template parm are irrelevant. */
7222 relevant_template_parms
= current_template_parms
;
7223 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7224 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7225 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7227 outer
= template_parms_to_args (relevant_template_parms
);
7231 arglist
= add_to_template_args (outer
, arglist
);
7233 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7234 return coerce_template_parms (parmlist
, arglist
, templ
,
7236 /*require_all_args=*/true,
7237 /*use_default_args=*/true);
7240 /* A cache of template template parameters with match-all default
7242 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7244 store_defaulted_ttp (tree v
, tree t
)
7246 if (!defaulted_ttp_cache
)
7247 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7248 defaulted_ttp_cache
->put (v
, t
);
7251 lookup_defaulted_ttp (tree v
)
7253 if (defaulted_ttp_cache
)
7254 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7259 /* T is a bound template template-parameter. Copy its arguments into default
7260 arguments of the template template-parameter's template parameters. */
7263 add_defaults_to_ttp (tree otmpl
)
7265 if (tree c
= lookup_defaulted_ttp (otmpl
))
7268 tree ntmpl
= copy_node (otmpl
);
7270 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7271 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7272 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7273 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7274 TYPE_NAME (ntype
) = ntmpl
;
7275 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7277 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7278 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7279 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7280 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7282 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7283 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7284 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7285 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7286 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7288 tree o
= TREE_VEC_ELT (vec
, i
);
7289 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7291 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7292 TREE_PURPOSE (n
) = any_targ_node
;
7296 store_defaulted_ttp (otmpl
, ntmpl
);
7300 /* ARG is a bound potential template template-argument, and PARGS is a list
7301 of arguments for the corresponding template template-parameter. Adjust
7302 PARGS as appropriate for application to ARG's template, and if ARG is a
7303 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7304 arguments to the template template parameter. */
7307 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7309 ++processing_template_decl
;
7310 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7313 /* When comparing two template template-parameters in partial ordering,
7314 rewrite the one currently being used as an argument to have default
7315 arguments for all parameters. */
7316 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7317 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7318 if (pargs
!= error_mark_node
)
7319 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7320 TYPE_TI_ARGS (arg
));
7325 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7326 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7327 /*require_all*/true,
7328 /*use_default*/true);
7330 --processing_template_decl
;
7334 /* Subroutine of unify for the case when PARM is a
7335 BOUND_TEMPLATE_TEMPLATE_PARM. */
7338 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7341 tree parmvec
= TYPE_TI_ARGS (parm
);
7342 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7344 /* The template template parm might be variadic and the argument
7345 not, so flatten both argument lists. */
7346 parmvec
= expand_template_argument_pack (parmvec
);
7347 argvec
= expand_template_argument_pack (argvec
);
7351 /* In keeping with P0522R0, adjust P's template arguments
7352 to apply to A's template; then flatten it again. */
7353 tree nparmvec
= parmvec
;
7354 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7355 nparmvec
= expand_template_argument_pack (nparmvec
);
7357 if (unify (tparms
, targs
, nparmvec
, argvec
,
7358 UNIFY_ALLOW_NONE
, explain_p
))
7361 /* If the P0522 adjustment eliminated a pack expansion, deduce
7364 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7365 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7366 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7371 /* Deduce arguments T, i from TT<T> or TT<i>.
7372 We check each element of PARMVEC and ARGVEC individually
7373 rather than the whole TREE_VEC since they can have
7374 different number of elements, which is allowed under N2555. */
7376 int len
= TREE_VEC_LENGTH (parmvec
);
7378 /* Check if the parameters end in a pack, making them
7380 int parm_variadic_p
= 0;
7382 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7383 parm_variadic_p
= 1;
7385 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7386 /* If the template argument list of P contains a pack
7387 expansion that is not the last template argument, the
7388 entire template argument list is a non-deduced
7390 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7391 return unify_success (explain_p
);
7393 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7394 return unify_too_few_arguments (explain_p
,
7395 TREE_VEC_LENGTH (argvec
), len
);
7397 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7398 if (unify (tparms
, targs
,
7399 TREE_VEC_ELT (parmvec
, i
),
7400 TREE_VEC_ELT (argvec
, i
),
7401 UNIFY_ALLOW_NONE
, explain_p
))
7405 && unify_pack_expansion (tparms
, targs
,
7408 /*subr=*/true, explain_p
))
7415 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7416 template template parameters. Both PARM_PARMS and ARG_PARMS are
7417 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7420 Consider the example:
7421 template <class T> class A;
7422 template<template <class U> class TT> class B;
7424 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7425 the parameters to A, and OUTER_ARGS contains A. */
7428 coerce_template_template_parms (tree parm_parms
,
7430 tsubst_flags_t complain
,
7434 int nparms
, nargs
, i
;
7438 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7439 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7441 nparms
= TREE_VEC_LENGTH (parm_parms
);
7442 nargs
= TREE_VEC_LENGTH (arg_parms
);
7446 /* P0522R0: A template template-parameter P is at least as specialized as
7447 a template template-argument A if, given the following rewrite to two
7448 function templates, the function template corresponding to P is at
7449 least as specialized as the function template corresponding to A
7450 according to the partial ordering rules for function templates
7451 ([temp.func.order]). Given an invented class template X with the
7452 template parameter list of A (including default arguments):
7454 * Each of the two function templates has the same template parameters,
7455 respectively, as P or A.
7457 * Each function template has a single function parameter whose type is
7458 a specialization of X with template arguments corresponding to the
7459 template parameters from the respective function template where, for
7460 each template parameter PP in the template parameter list of the
7461 function template, a corresponding template argument AA is formed. If
7462 PP declares a parameter pack, then AA is the pack expansion
7463 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7465 If the rewrite produces an invalid type, then P is not at least as
7466 specialized as A. */
7468 /* So coerce P's args to apply to A's parms, and then deduce between A's
7469 args and the converted args. If that succeeds, A is at least as
7470 specialized as P, so they match.*/
7471 tree pargs
= template_parms_level_to_args (parm_parms
);
7472 ++processing_template_decl
;
7473 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7474 /*require_all*/true, /*use_default*/true);
7475 --processing_template_decl
;
7476 if (pargs
!= error_mark_node
)
7478 tree targs
= make_tree_vec (nargs
);
7479 tree aargs
= template_parms_level_to_args (arg_parms
);
7480 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7486 /* Determine whether we have a parameter pack at the end of the
7487 template template parameter's template parameter list. */
7488 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7490 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7492 if (error_operand_p (parm
))
7495 switch (TREE_CODE (parm
))
7499 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7504 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7514 && !(variadic_p
&& nargs
>= nparms
- 1))
7517 /* Check all of the template parameters except the parameter pack at
7518 the end (if any). */
7519 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7521 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7522 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7525 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7526 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7528 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7536 /* Check each of the template parameters in the template
7537 argument against the template parameter pack at the end of
7538 the template template parameter. */
7539 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7542 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7544 for (; i
< nargs
; ++i
)
7546 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7549 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7551 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7560 /* Verifies that the deduced template arguments (in TARGS) for the
7561 template template parameters (in TPARMS) represent valid bindings,
7562 by comparing the template parameter list of each template argument
7563 to the template parameter list of its corresponding template
7564 template parameter, in accordance with DR150. This
7565 routine can only be called after all template arguments have been
7566 deduced. It will return TRUE if all of the template template
7567 parameter bindings are okay, FALSE otherwise. */
7569 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7571 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7574 /* We're dealing with template parms in this process. */
7575 ++processing_template_decl
;
7577 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7579 for (i
= 0; i
< ntparms
; ++i
)
7581 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7582 tree targ
= TREE_VEC_ELT (targs
, i
);
7584 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7586 tree packed_args
= NULL_TREE
;
7589 if (ARGUMENT_PACK_P (targ
))
7591 /* Look inside the argument pack. */
7592 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7593 len
= TREE_VEC_LENGTH (packed_args
);
7596 for (idx
= 0; idx
< len
; ++idx
)
7598 tree targ_parms
= NULL_TREE
;
7601 /* Extract the next argument from the argument
7603 targ
= TREE_VEC_ELT (packed_args
, idx
);
7605 if (PACK_EXPANSION_P (targ
))
7606 /* Look at the pattern of the pack expansion. */
7607 targ
= PACK_EXPANSION_PATTERN (targ
);
7609 /* Extract the template parameters from the template
7611 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7612 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7613 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7614 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7616 /* Verify that we can coerce the template template
7617 parameters from the template argument to the template
7618 parameter. This requires an exact match. */
7620 && !coerce_template_template_parms
7621 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7636 --processing_template_decl
;
7640 /* Since type attributes aren't mangled, we need to strip them from
7641 template type arguments. */
7644 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7646 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7648 bool removed_attributes
= false;
7649 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7650 if (removed_attributes
7651 && (complain
& tf_warning
))
7652 warning (OPT_Wignored_attributes
,
7653 "ignoring attributes on template argument %qT", arg
);
7657 /* And from inside dependent non-type arguments like sizeof(Type). */
7660 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7662 if (!arg
|| arg
== error_mark_node
)
7664 bool removed_attributes
= false;
7665 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7666 if (removed_attributes
7667 && (complain
& tf_warning
))
7668 warning (OPT_Wignored_attributes
,
7669 "ignoring attributes in template argument %qE", arg
);
7673 // A template declaration can be substituted for a constrained
7674 // template template parameter only when the argument is more
7675 // constrained than the parameter.
7677 is_compatible_template_arg (tree parm
, tree arg
)
7679 tree parm_cons
= get_constraints (parm
);
7681 /* For now, allow constrained template template arguments
7682 and unconstrained template template parameters. */
7683 if (parm_cons
== NULL_TREE
)
7686 tree arg_cons
= get_constraints (arg
);
7688 // If the template parameter is constrained, we need to rewrite its
7689 // constraints in terms of the ARG's template parameters. This ensures
7690 // that all of the template parameter types will have the same depth.
7692 // Note that this is only valid when coerce_template_template_parm is
7693 // true for the innermost template parameters of PARM and ARG. In other
7694 // words, because coercion is successful, this conversion will be valid.
7697 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7698 parm_cons
= tsubst_constraint_info (parm_cons
,
7699 INNERMOST_TEMPLATE_ARGS (args
),
7700 tf_none
, NULL_TREE
);
7701 if (parm_cons
== error_mark_node
)
7705 return subsumes (parm_cons
, arg_cons
);
7708 // Convert a placeholder argument into a binding to the original
7709 // parameter. The original parameter is saved as the TREE_TYPE of
7712 convert_wildcard_argument (tree parm
, tree arg
)
7714 TREE_TYPE (arg
) = parm
;
7718 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7719 because one of them is dependent. But we need to represent the
7720 conversion for the benefit of cp_tree_equal. */
7723 maybe_convert_nontype_argument (tree type
, tree arg
)
7725 /* Auto parms get no conversion. */
7726 if (type_uses_auto (type
))
7728 /* We don't need or want to add this conversion now if we're going to use the
7729 argument for deduction. */
7730 if (value_dependent_expression_p (arg
))
7733 type
= cv_unqualified (type
);
7734 tree argtype
= TREE_TYPE (arg
);
7735 if (same_type_p (type
, argtype
))
7738 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7739 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7743 /* Convert the indicated template ARG as necessary to match the
7744 indicated template PARM. Returns the converted ARG, or
7745 error_mark_node if the conversion was unsuccessful. Error and
7746 warning messages are issued under control of COMPLAIN. This
7747 conversion is for the Ith parameter in the parameter list. ARGS is
7748 the full set of template arguments deduced so far. */
7751 convert_template_argument (tree parm
,
7754 tsubst_flags_t complain
,
7760 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7762 if (parm
== error_mark_node
)
7763 return error_mark_node
;
7765 /* Trivially convert placeholders. */
7766 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7767 return convert_wildcard_argument (parm
, arg
);
7769 if (arg
== any_targ_node
)
7772 if (TREE_CODE (arg
) == TREE_LIST
7773 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7775 /* The template argument was the name of some
7776 member function. That's usually
7777 invalid, but static members are OK. In any
7778 case, grab the underlying fields/functions
7779 and issue an error later if required. */
7780 orig_arg
= TREE_VALUE (arg
);
7781 TREE_TYPE (arg
) = unknown_type_node
;
7786 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7787 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7788 || requires_tmpl_type
);
7790 /* When determining whether an argument pack expansion is a template,
7791 look at the pattern. */
7792 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7793 arg
= PACK_EXPANSION_PATTERN (arg
);
7795 /* Deal with an injected-class-name used as a template template arg. */
7796 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7798 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7799 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7801 if (cxx_dialect
>= cxx11
)
7802 /* OK under DR 1004. */;
7803 else if (complain
& tf_warning_or_error
)
7804 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7805 " used as template template argument", TYPE_NAME (arg
));
7806 else if (flag_pedantic_errors
)
7814 ((TREE_CODE (arg
) == TEMPLATE_DECL
7815 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7816 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7817 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7818 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7821 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7822 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7823 arg
= TYPE_STUB_DECL (arg
);
7825 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7827 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7828 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7830 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7832 if (complain
& tf_error
)
7833 error ("invalid use of destructor %qE as a type", orig_arg
);
7834 return error_mark_node
;
7837 permerror (input_location
,
7838 "to refer to a type member of a template parameter, "
7839 "use %<typename %E%>", orig_arg
);
7841 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7842 TREE_OPERAND (arg
, 1),
7848 if (is_type
!= requires_type
)
7852 if (complain
& tf_error
)
7854 error ("type/value mismatch at argument %d in template "
7855 "parameter list for %qD",
7858 inform (input_location
,
7859 " expected a constant of type %qT, got %qT",
7861 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7862 else if (requires_tmpl_type
)
7863 inform (input_location
,
7864 " expected a class template, got %qE", orig_arg
);
7866 inform (input_location
,
7867 " expected a type, got %qE", orig_arg
);
7870 return error_mark_node
;
7872 if (is_tmpl_type
^ requires_tmpl_type
)
7874 if (in_decl
&& (complain
& tf_error
))
7876 error ("type/value mismatch at argument %d in template "
7877 "parameter list for %qD",
7880 inform (input_location
,
7881 " expected a type, got %qT", DECL_NAME (arg
));
7883 inform (input_location
,
7884 " expected a class template, got %qT", orig_arg
);
7886 return error_mark_node
;
7889 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7890 /* We already did the appropriate conversion when packing args. */
7894 if (requires_tmpl_type
)
7896 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7897 /* The number of argument required is not known yet.
7898 Just accept it for now. */
7902 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7905 /* Strip alias templates that are equivalent to another
7907 arg
= get_underlying_template (arg
);
7908 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7910 if (coerce_template_template_parms (parmparm
, argparm
,
7916 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7918 if (val
!= error_mark_node
)
7920 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7921 val
= TREE_TYPE (val
);
7922 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7923 val
= make_pack_expansion (val
, complain
);
7928 if (in_decl
&& (complain
& tf_error
))
7930 error ("type/value mismatch at argument %d in "
7931 "template parameter list for %qD",
7933 inform (input_location
,
7934 " expected a template of type %qD, got %qT",
7938 val
= error_mark_node
;
7941 // Check that the constraints are compatible before allowing the
7943 if (val
!= error_mark_node
)
7944 if (!is_compatible_template_arg (parm
, arg
))
7946 if (in_decl
&& (complain
& tf_error
))
7948 error ("constraint mismatch at argument %d in "
7949 "template parameter list for %qD",
7951 inform (input_location
, " expected %qD but got %qD",
7954 val
= error_mark_node
;
7960 /* We only form one instance of each template specialization.
7961 Therefore, if we use a non-canonical variant (i.e., a
7962 typedef), any future messages referring to the type will use
7963 the typedef, which is confusing if those future uses do not
7964 themselves also use the typedef. */
7966 val
= canonicalize_type_argument (val
, complain
);
7970 tree t
= TREE_TYPE (parm
);
7972 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
7973 > TMPL_ARGS_DEPTH (args
))
7974 /* We don't have enough levels of args to do any substitution. This
7975 can happen in the context of -fnew-ttp-matching. */;
7976 else if (tree a
= type_uses_auto (t
))
7978 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7979 if (t
== error_mark_node
)
7980 return error_mark_node
;
7983 t
= tsubst (t
, args
, complain
, in_decl
);
7985 if (invalid_nontype_parm_type_p (t
, complain
))
7986 return error_mark_node
;
7988 if (!type_dependent_expression_p (orig_arg
)
7989 && !uses_template_parms (t
))
7990 /* We used to call digest_init here. However, digest_init
7991 will report errors, which we don't want when complain
7992 is zero. More importantly, digest_init will try too
7993 hard to convert things: for example, `0' should not be
7994 converted to pointer type at this point according to
7995 the standard. Accepting this is not merely an
7996 extension, since deciding whether or not these
7997 conversions can occur is part of determining which
7998 function template to call, or whether a given explicit
7999 argument specification is valid. */
8000 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8003 val
= canonicalize_expr_argument (orig_arg
, complain
);
8004 val
= maybe_convert_nontype_argument (t
, val
);
8008 if (val
== NULL_TREE
)
8009 val
= error_mark_node
;
8010 else if (val
== error_mark_node
&& (complain
& tf_error
))
8011 error ("could not convert template argument %qE from %qT to %qT",
8012 orig_arg
, TREE_TYPE (orig_arg
), t
);
8014 if (INDIRECT_REF_P (val
))
8016 /* Reject template arguments that are references to built-in
8017 functions with no library fallbacks. */
8018 const_tree inner
= TREE_OPERAND (val
, 0);
8019 const_tree innertype
= TREE_TYPE (inner
);
8021 && TREE_CODE (innertype
) == REFERENCE_TYPE
8022 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8023 && TREE_OPERAND_LENGTH (inner
) > 0
8024 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8025 return error_mark_node
;
8028 if (TREE_CODE (val
) == SCOPE_REF
)
8030 /* Strip typedefs from the SCOPE_REF. */
8031 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8032 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8034 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8035 QUALIFIED_NAME_IS_TEMPLATE (val
));
8042 /* Coerces the remaining template arguments in INNER_ARGS (from
8043 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8044 Returns the coerced argument pack. PARM_IDX is the position of this
8045 parameter in the template parameter list. ARGS is the original
8046 template argument list. */
8048 coerce_template_parameter_pack (tree parms
,
8056 tsubst_flags_t complain
)
8058 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8059 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8062 tree packed_parms
= NULL_TREE
;
8064 if (arg_idx
> nargs
)
8067 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8069 /* When the template parameter is a non-type template parameter pack
8070 or template template parameter pack whose type or template
8071 parameters use parameter packs, we know exactly how many arguments
8072 we are looking for. Build a vector of the instantiated decls for
8073 these template parameters in PACKED_PARMS. */
8074 /* We can't use make_pack_expansion here because it would interpret a
8075 _DECL as a use rather than a declaration. */
8076 tree decl
= TREE_VALUE (parm
);
8077 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8078 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8079 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8080 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8082 TREE_VEC_LENGTH (args
)--;
8083 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8084 TREE_VEC_LENGTH (args
)++;
8086 if (packed_parms
== error_mark_node
)
8087 return error_mark_node
;
8089 /* If we're doing a partial instantiation of a member template,
8090 verify that all of the types used for the non-type
8091 template parameter pack are, in fact, valid for non-type
8092 template parameters. */
8094 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8096 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8097 for (j
= 0; j
< len
; ++j
)
8099 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8100 if (invalid_nontype_parm_type_p (t
, complain
))
8101 return error_mark_node
;
8103 /* We don't know how many args we have yet, just
8104 use the unconverted ones for now. */
8108 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8110 /* Check if we have a placeholder pack, which indicates we're
8111 in the context of a introduction list. In that case we want
8112 to match this pack to the single placeholder. */
8113 else if (arg_idx
< nargs
8114 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8115 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8117 nargs
= arg_idx
+ 1;
8118 packed_args
= make_tree_vec (1);
8121 packed_args
= make_tree_vec (nargs
- arg_idx
);
8123 /* Convert the remaining arguments, which will be a part of the
8124 parameter pack "parm". */
8125 int first_pack_arg
= arg_idx
;
8126 for (; arg_idx
< nargs
; ++arg_idx
)
8128 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8129 tree actual_parm
= TREE_VALUE (parm
);
8130 int pack_idx
= arg_idx
- first_pack_arg
;
8134 /* Once we've packed as many args as we have types, stop. */
8135 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8137 else if (PACK_EXPANSION_P (arg
))
8138 /* We don't know how many args we have yet, just
8139 use the unconverted ones for now. */
8142 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8145 if (arg
== error_mark_node
)
8147 if (complain
& tf_error
)
8148 error ("template argument %d is invalid", arg_idx
+ 1);
8151 arg
= convert_template_argument (actual_parm
,
8152 arg
, new_args
, complain
, parm_idx
,
8154 if (arg
== error_mark_node
)
8156 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8159 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8160 && TREE_VEC_LENGTH (packed_args
) > 0)
8162 if (complain
& tf_error
)
8163 error ("wrong number of template arguments (%d, should be %d)",
8164 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8165 return error_mark_node
;
8168 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8169 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8170 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8173 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8174 TREE_CONSTANT (argument_pack
) = 1;
8177 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8179 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8180 TREE_VEC_LENGTH (packed_args
));
8181 return argument_pack
;
8184 /* Returns the number of pack expansions in the template argument vector
8188 pack_expansion_args_count (tree args
)
8193 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8195 tree elt
= TREE_VEC_ELT (args
, i
);
8196 if (elt
&& PACK_EXPANSION_P (elt
))
8202 /* Convert all template arguments to their appropriate types, and
8203 return a vector containing the innermost resulting template
8204 arguments. If any error occurs, return error_mark_node. Error and
8205 warning messages are issued under control of COMPLAIN.
8207 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8208 for arguments not specified in ARGS. Otherwise, if
8209 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8210 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8211 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8215 coerce_template_parms (tree parms
,
8218 tsubst_flags_t complain
,
8219 bool require_all_args
,
8220 bool use_default_args
)
8222 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8223 tree orig_inner_args
;
8226 tree new_inner_args
;
8227 int saved_unevaluated_operand
;
8228 int saved_inhibit_evaluation_warnings
;
8230 /* When used as a boolean value, indicates whether this is a
8231 variadic template parameter list. Since it's an int, we can also
8232 subtract it from nparms to get the number of non-variadic
8235 int variadic_args_p
= 0;
8236 int post_variadic_parms
= 0;
8238 /* Adjustment to nparms for fixed parameter packs. */
8239 int fixed_pack_adjust
= 0;
8240 int fixed_packs
= 0;
8243 /* Likewise for parameters with default arguments. */
8246 if (args
== error_mark_node
)
8247 return error_mark_node
;
8249 nparms
= TREE_VEC_LENGTH (parms
);
8251 /* Determine if there are any parameter packs or default arguments. */
8252 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8254 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8256 ++post_variadic_parms
;
8257 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8259 if (TREE_PURPOSE (parm
))
8263 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8264 /* If there are no parameters that follow a parameter pack, we need to
8265 expand any argument packs so that we can deduce a parameter pack from
8266 some non-packed args followed by an argument pack, as in variadic85.C.
8267 If there are such parameters, we need to leave argument packs intact
8268 so the arguments are assigned properly. This can happen when dealing
8269 with a nested class inside a partial specialization of a class
8270 template, as in variadic92.C, or when deducing a template parameter pack
8271 from a sub-declarator, as in variadic114.C. */
8272 if (!post_variadic_parms
)
8273 inner_args
= expand_template_argument_pack (inner_args
);
8275 /* Count any pack expansion args. */
8276 variadic_args_p
= pack_expansion_args_count (inner_args
);
8278 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8279 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8280 || (nargs
< nparms
- variadic_p
8283 && (!use_default_args
8284 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8285 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8288 if (complain
& tf_error
)
8290 if (variadic_p
|| default_p
)
8292 nparms
-= variadic_p
+ default_p
;
8293 error ("wrong number of template arguments "
8294 "(%d, should be at least %d)", nargs
, nparms
);
8297 error ("wrong number of template arguments "
8298 "(%d, should be %d)", nargs
, nparms
);
8301 inform (DECL_SOURCE_LOCATION (in_decl
),
8302 "provided for %qD", in_decl
);
8305 return error_mark_node
;
8307 /* We can't pass a pack expansion to a non-pack parameter of an alias
8308 template (DR 1430). */
8310 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8311 || concept_template_p (in_decl
))
8313 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8315 if (complain
& tf_error
)
8317 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8319 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8320 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8322 if (PACK_EXPANSION_P (arg
)
8323 && !template_parameter_pack_p (parm
))
8325 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8326 error_at (location_of (arg
),
8327 "pack expansion argument for non-pack parameter "
8328 "%qD of alias template %qD", parm
, in_decl
);
8330 error_at (location_of (arg
),
8331 "pack expansion argument for non-pack parameter "
8332 "%qD of concept %qD", parm
, in_decl
);
8333 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8340 return error_mark_node
;
8343 /* We need to evaluate the template arguments, even though this
8344 template-id may be nested within a "sizeof". */
8345 saved_unevaluated_operand
= cp_unevaluated_operand
;
8346 cp_unevaluated_operand
= 0;
8347 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8348 c_inhibit_evaluation_warnings
= 0;
8349 new_inner_args
= make_tree_vec (nparms
);
8350 new_args
= add_outermost_template_args (args
, new_inner_args
);
8351 int pack_adjust
= 0;
8352 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8357 /* Get the Ith template parameter. */
8358 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8360 if (parm
== error_mark_node
)
8362 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8366 /* Calculate the next argument. */
8367 if (arg_idx
< nargs
)
8368 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8372 if (template_parameter_pack_p (TREE_VALUE (parm
))
8373 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8375 /* Some arguments will be placed in the
8376 template parameter pack PARM. */
8377 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8378 inner_args
, arg_idx
,
8382 if (arg
== NULL_TREE
)
8384 /* We don't know how many args we have yet, just use the
8385 unconverted (and still packed) ones for now. */
8386 new_inner_args
= orig_inner_args
;
8391 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8393 /* Store this argument. */
8394 if (arg
== error_mark_node
)
8397 /* We are done with all of the arguments. */
8403 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8404 arg_idx
+= pack_adjust
;
8405 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8408 fixed_pack_adjust
+= pack_adjust
;
8416 if (PACK_EXPANSION_P (arg
))
8418 /* "If every valid specialization of a variadic template
8419 requires an empty template parameter pack, the template is
8420 ill-formed, no diagnostic required." So check that the
8421 pattern works with this parameter. */
8422 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8423 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8427 if (conv
== error_mark_node
)
8429 if (complain
& tf_error
)
8430 inform (input_location
, "so any instantiation with a "
8431 "non-empty parameter pack would be ill-formed");
8434 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8435 /* Recover from missing typename. */
8436 TREE_VEC_ELT (inner_args
, arg_idx
)
8437 = make_pack_expansion (conv
, complain
);
8439 /* We don't know how many args we have yet, just
8440 use the unconverted ones for now. */
8441 new_inner_args
= inner_args
;
8446 else if (require_all_args
)
8448 /* There must be a default arg in this case. */
8449 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8451 /* The position of the first default template argument,
8452 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8454 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8455 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8456 arg_idx
- pack_adjust
);
8461 if (arg
== error_mark_node
)
8463 if (complain
& tf_error
)
8464 error ("template argument %d is invalid", arg_idx
+ 1);
8468 /* This can occur if there was an error in the template
8469 parameter list itself (which we would already have
8470 reported) that we are trying to recover from, e.g., a class
8471 template with a parameter list such as
8472 template<typename..., typename> (cpp0x/variadic150.C). */
8475 /* This can also happen with a fixed parameter pack (71834). */
8476 if (arg_idx
>= nargs
)
8480 arg
= convert_template_argument (TREE_VALUE (parm
),
8481 arg
, new_args
, complain
,
8484 if (arg
== error_mark_node
)
8486 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8488 cp_unevaluated_operand
= saved_unevaluated_operand
;
8489 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8491 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8493 /* If we had fixed parameter packs, we didn't know how many arguments we
8494 actually needed earlier; now we do. */
8495 nparms
+= fixed_pack_adjust
;
8496 variadic_p
-= fixed_packs
;
8500 if (arg_idx
< nargs
)
8502 /* We had some pack expansion arguments that will only work if the packs
8503 are empty, but wait until instantiation time to complain.
8504 See variadic-ttp3.C. */
8505 int len
= nparms
+ (nargs
- arg_idx
);
8506 tree args
= make_tree_vec (len
);
8508 for (; i
< nparms
; ++i
)
8509 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8510 for (; i
< len
; ++i
, ++arg_idx
)
8511 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8512 arg_idx
- pack_adjust
);
8513 new_inner_args
= args
;
8518 gcc_assert (!(complain
& tf_error
) || seen_error ());
8519 return error_mark_node
;
8522 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8523 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8524 TREE_VEC_LENGTH (new_inner_args
));
8526 return new_inner_args
;
8529 /* Convert all template arguments to their appropriate types, and
8530 return a vector containing the innermost resulting template
8531 arguments. If any error occurs, return error_mark_node. Error and
8532 warning messages are not issued.
8534 Note that no function argument deduction is performed, and default
8535 arguments are used to fill in unspecified arguments. */
8537 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8539 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8542 /* Convert all template arguments to their appropriate type, and
8543 instantiate default arguments as needed. This returns a vector
8544 containing the innermost resulting template arguments, or
8545 error_mark_node if unsuccessful. */
8547 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8548 tsubst_flags_t complain
)
8550 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8553 /* Like coerce_template_parms. If PARMS represents all template
8554 parameters levels, this function returns a vector of vectors
8555 representing all the resulting argument levels. Note that in this
8556 case, only the innermost arguments are coerced because the
8557 outermost ones are supposed to have been coerced already.
8559 Otherwise, if PARMS represents only (the innermost) vector of
8560 parameters, this function returns a vector containing just the
8561 innermost resulting arguments. */
8564 coerce_innermost_template_parms (tree parms
,
8567 tsubst_flags_t complain
,
8568 bool require_all_args
,
8569 bool use_default_args
)
8571 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8572 int args_depth
= TMPL_ARGS_DEPTH (args
);
8575 if (parms_depth
> 1)
8577 coerced_args
= make_tree_vec (parms_depth
);
8581 for (level
= parms
, cur_depth
= parms_depth
;
8582 parms_depth
> 0 && level
!= NULL_TREE
;
8583 level
= TREE_CHAIN (level
), --cur_depth
)
8586 if (cur_depth
== args_depth
)
8587 l
= coerce_template_parms (TREE_VALUE (level
),
8588 args
, in_decl
, complain
,
8592 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8594 if (l
== error_mark_node
)
8595 return error_mark_node
;
8597 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8601 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8602 args
, in_decl
, complain
,
8605 return coerced_args
;
8608 /* Returns 1 if template args OT and NT are equivalent. */
8611 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8615 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8617 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8620 if (TREE_CODE (nt
) == TREE_VEC
)
8621 /* For member templates */
8622 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8623 else if (PACK_EXPANSION_P (ot
))
8624 return (PACK_EXPANSION_P (nt
)
8625 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8626 PACK_EXPANSION_PATTERN (nt
))
8627 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8628 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8629 else if (ARGUMENT_PACK_P (ot
))
8634 if (!ARGUMENT_PACK_P (nt
))
8637 opack
= ARGUMENT_PACK_ARGS (ot
);
8638 npack
= ARGUMENT_PACK_ARGS (nt
);
8639 len
= TREE_VEC_LENGTH (opack
);
8640 if (TREE_VEC_LENGTH (npack
) != len
)
8642 for (i
= 0; i
< len
; ++i
)
8643 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8644 TREE_VEC_ELT (npack
, i
)))
8648 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8650 else if (TYPE_P (nt
))
8654 /* Don't treat an alias template specialization with dependent
8655 arguments as equivalent to its underlying type when used as a
8656 template argument; we need them to be distinct so that we
8657 substitute into the specialization arguments at instantiation
8658 time. And aliases can't be equivalent without being ==, so
8659 we don't need to look any deeper.
8661 During partial ordering, however, we need to treat them normally so
8662 that we can order uses of the same alias with different
8663 cv-qualification (79960). */
8665 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8668 return same_type_p (ot
, nt
);
8670 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8674 /* Try to treat a template non-type argument that has been converted
8675 to the parameter type as equivalent to one that hasn't yet. */
8676 for (enum tree_code code1
= TREE_CODE (ot
);
8677 CONVERT_EXPR_CODE_P (code1
)
8678 || code1
== NON_LVALUE_EXPR
;
8679 code1
= TREE_CODE (ot
))
8680 ot
= TREE_OPERAND (ot
, 0);
8681 for (enum tree_code code2
= TREE_CODE (nt
);
8682 CONVERT_EXPR_CODE_P (code2
)
8683 || code2
== NON_LVALUE_EXPR
;
8684 code2
= TREE_CODE (nt
))
8685 nt
= TREE_OPERAND (nt
, 0);
8687 return cp_tree_equal (ot
, nt
);
8691 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8692 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8693 NEWARG_PTR with the offending arguments if they are non-NULL. */
8696 comp_template_args (tree oldargs
, tree newargs
,
8697 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8702 if (oldargs
== newargs
)
8705 if (!oldargs
|| !newargs
)
8708 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8711 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8713 tree nt
= TREE_VEC_ELT (newargs
, i
);
8714 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8716 if (! template_args_equal (ot
, nt
, partial_order
))
8718 if (oldarg_ptr
!= NULL
)
8720 if (newarg_ptr
!= NULL
)
8729 comp_template_args_porder (tree oargs
, tree nargs
)
8731 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8735 add_pending_template (tree d
)
8737 tree ti
= (TYPE_P (d
)
8738 ? CLASSTYPE_TEMPLATE_INFO (d
)
8739 : DECL_TEMPLATE_INFO (d
));
8740 struct pending_template
*pt
;
8743 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8746 /* We are called both from instantiate_decl, where we've already had a
8747 tinst_level pushed, and instantiate_template, where we haven't.
8749 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8752 push_tinst_level (d
);
8754 pt
= ggc_alloc
<pending_template
> ();
8756 pt
->tinst
= current_tinst_level
;
8757 if (last_pending_template
)
8758 last_pending_template
->next
= pt
;
8760 pending_templates
= pt
;
8762 last_pending_template
= pt
;
8764 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8771 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8772 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8773 documentation for TEMPLATE_ID_EXPR. */
8776 lookup_template_function (tree fns
, tree arglist
)
8780 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8781 return error_mark_node
;
8783 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8785 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8787 error ("%q#D is not a function template", fns
);
8788 return error_mark_node
;
8791 if (BASELINK_P (fns
))
8793 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8795 BASELINK_FUNCTIONS (fns
),
8800 type
= TREE_TYPE (fns
);
8801 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8802 type
= unknown_type_node
;
8804 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8807 /* Within the scope of a template class S<T>, the name S gets bound
8808 (in build_self_reference) to a TYPE_DECL for the class, not a
8809 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8810 or one of its enclosing classes, and that type is a template,
8811 return the associated TEMPLATE_DECL. Otherwise, the original
8814 Also handle the case when DECL is a TREE_LIST of ambiguous
8815 injected-class-names from different bases. */
8818 maybe_get_template_decl_from_type_decl (tree decl
)
8820 if (decl
== NULL_TREE
)
8823 /* DR 176: A lookup that finds an injected-class-name (10.2
8824 [class.member.lookup]) can result in an ambiguity in certain cases
8825 (for example, if it is found in more than one base class). If all of
8826 the injected-class-names that are found refer to specializations of
8827 the same class template, and if the name is followed by a
8828 template-argument-list, the reference refers to the class template
8829 itself and not a specialization thereof, and is not ambiguous. */
8830 if (TREE_CODE (decl
) == TREE_LIST
)
8832 tree t
, tmpl
= NULL_TREE
;
8833 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8835 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8838 else if (tmpl
!= elt
)
8841 if (tmpl
&& t
== NULL_TREE
)
8847 return (decl
!= NULL_TREE
8848 && DECL_SELF_REFERENCE_P (decl
)
8849 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8850 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8853 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8854 parameters, find the desired type.
8856 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8858 IN_DECL, if non-NULL, is the template declaration we are trying to
8861 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8862 the class we are looking up.
8864 Issue error and warning messages under control of COMPLAIN.
8866 If the template class is really a local class in a template
8867 function, then the FUNCTION_CONTEXT is the function in which it is
8870 ??? Note that this function is currently called *twice* for each
8871 template-id: the first time from the parser, while creating the
8872 incomplete type (finish_template_type), and the second type during the
8873 real instantiation (instantiate_template_class). This is surely something
8874 that we want to avoid. It also causes some problems with argument
8875 coercion (see convert_nontype_argument for more information on this). */
8878 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8879 int entering_scope
, tsubst_flags_t complain
)
8881 tree templ
= NULL_TREE
, parmlist
;
8888 if (identifier_p (d1
))
8890 tree value
= innermost_non_namespace_value (d1
);
8891 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8896 push_decl_namespace (context
);
8897 templ
= lookup_name (d1
);
8898 templ
= maybe_get_template_decl_from_type_decl (templ
);
8900 pop_decl_namespace ();
8903 context
= DECL_CONTEXT (templ
);
8905 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8907 tree type
= TREE_TYPE (d1
);
8909 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8910 an implicit typename for the second A. Deal with it. */
8911 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8912 type
= TREE_TYPE (type
);
8914 if (CLASSTYPE_TEMPLATE_INFO (type
))
8916 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8917 d1
= DECL_NAME (templ
);
8920 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8921 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8923 templ
= TYPE_TI_TEMPLATE (d1
);
8924 d1
= DECL_NAME (templ
);
8926 else if (DECL_TYPE_TEMPLATE_P (d1
))
8929 d1
= DECL_NAME (templ
);
8930 context
= DECL_CONTEXT (templ
);
8932 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8935 d1
= DECL_NAME (templ
);
8938 /* Issue an error message if we didn't find a template. */
8941 if (complain
& tf_error
)
8942 error ("%qT is not a template", d1
);
8943 return error_mark_node
;
8946 if (TREE_CODE (templ
) != TEMPLATE_DECL
8947 /* Make sure it's a user visible template, if it was named by
8949 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8950 && !PRIMARY_TEMPLATE_P (templ
)))
8952 if (complain
& tf_error
)
8954 error ("non-template type %qT used as a template", d1
);
8956 error ("for template declaration %q+D", in_decl
);
8958 return error_mark_node
;
8961 complain
&= ~tf_user
;
8963 /* An alias that just changes the name of a template is equivalent to the
8964 other template, so if any of the arguments are pack expansions, strip
8965 the alias to avoid problems with a pack expansion passed to a non-pack
8966 alias template parameter (DR 1430). */
8967 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8968 templ
= get_underlying_template (templ
);
8970 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8973 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8974 if (arglist2
== error_mark_node
8975 || (!uses_template_parms (arglist2
)
8976 && check_instantiated_args (templ
, arglist2
, complain
)))
8977 return error_mark_node
;
8979 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8984 tree template_type
= TREE_TYPE (templ
);
8987 tree found
= NULL_TREE
;
8990 int is_dependent_type
;
8991 int use_partial_inst_tmpl
= false;
8993 if (template_type
== error_mark_node
)
8994 /* An error occurred while building the template TEMPL, and a
8995 diagnostic has most certainly been emitted for that
8996 already. Let's propagate that error. */
8997 return error_mark_node
;
8999 gen_tmpl
= most_general_template (templ
);
9000 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9001 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9002 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9004 if (arg_depth
== 1 && parm_depth
> 1)
9006 /* We've been given an incomplete set of template arguments.
9009 template <class T> struct S1 {
9010 template <class U> struct S2 {};
9011 template <class U> struct S2<U*> {};
9014 we will be called with an ARGLIST of `U*', but the
9015 TEMPLATE will be `template <class T> template
9016 <class U> struct S1<T>::S2'. We must fill in the missing
9018 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9019 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9020 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9023 /* Now we should have enough arguments. */
9024 gcc_assert (parm_depth
== arg_depth
);
9026 /* From here on, we're only interested in the most general
9029 /* Calculate the BOUND_ARGS. These will be the args that are
9030 actually tsubst'd into the definition to create the
9032 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9034 /*require_all_args=*/true,
9035 /*use_default_args=*/true);
9037 if (arglist
== error_mark_node
)
9038 /* We were unable to bind the arguments. */
9039 return error_mark_node
;
9041 /* In the scope of a template class, explicit references to the
9042 template class refer to the type of the template, not any
9043 instantiation of it. For example, in:
9045 template <class T> class C { void f(C<T>); }
9047 the `C<T>' is just the same as `C'. Outside of the
9048 class, however, such a reference is an instantiation. */
9050 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9051 || currently_open_class (template_type
))
9053 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9055 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9056 return template_type
;
9059 /* If we already have this specialization, return it. */
9060 elt
.tmpl
= gen_tmpl
;
9062 elt
.spec
= NULL_TREE
;
9063 hash
= spec_hasher::hash (&elt
);
9064 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9069 /* If the the template's constraints are not satisfied,
9070 then we cannot form a valid type.
9072 Note that the check is deferred until after the hash
9073 lookup. This prevents redundant checks on previously
9074 instantiated specializations. */
9075 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9077 if (complain
& tf_error
)
9079 error ("template constraint failure");
9080 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9082 return error_mark_node
;
9085 is_dependent_type
= uses_template_parms (arglist
);
9087 /* If the deduced arguments are invalid, then the binding
9089 if (!is_dependent_type
9090 && check_instantiated_args (gen_tmpl
,
9091 INNERMOST_TEMPLATE_ARGS (arglist
),
9093 return error_mark_node
;
9095 if (!is_dependent_type
9096 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9097 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9098 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9100 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9101 DECL_NAME (gen_tmpl
),
9102 /*tag_scope=*/ts_global
);
9106 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
9108 if (context
== error_mark_node
)
9109 return error_mark_node
;
9112 context
= global_namespace
;
9114 /* Create the type. */
9115 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9117 /* The user referred to a specialization of an alias
9118 template represented by GEN_TMPL.
9120 [temp.alias]/2 says:
9122 When a template-id refers to the specialization of an
9123 alias template, it is equivalent to the associated
9124 type obtained by substitution of its
9125 template-arguments for the template-parameters in the
9126 type-id of the alias template. */
9128 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9129 /* Note that the call above (by indirectly calling
9130 register_specialization in tsubst_decl) registers the
9131 TYPE_DECL representing the specialization of the alias
9132 template. So next time someone substitutes ARGLIST for
9133 the template parms into the alias template (GEN_TMPL),
9134 she'll get that TYPE_DECL back. */
9136 if (t
== error_mark_node
)
9139 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9141 if (!is_dependent_type
)
9143 set_current_access_from_decl (TYPE_NAME (template_type
));
9144 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9145 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9146 arglist
, complain
, in_decl
),
9147 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9148 arglist
, complain
, in_decl
),
9149 SCOPED_ENUM_P (template_type
), NULL
);
9151 if (t
== error_mark_node
)
9156 /* We don't want to call start_enum for this type, since
9157 the values for the enumeration constants may involve
9158 template parameters. And, no one should be interested
9159 in the enumeration constants for such a type. */
9160 t
= cxx_make_type (ENUMERAL_TYPE
);
9161 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9163 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9164 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9165 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9167 else if (CLASS_TYPE_P (template_type
))
9169 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9170 instantiated here. */
9171 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9173 t
= make_class_type (TREE_CODE (template_type
));
9174 CLASSTYPE_DECLARED_CLASS (t
)
9175 = CLASSTYPE_DECLARED_CLASS (template_type
);
9176 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9178 /* A local class. Make sure the decl gets registered properly. */
9179 if (context
== current_function_decl
)
9180 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
9182 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9183 /* This instantiation is another name for the primary
9184 template type. Set the TYPE_CANONICAL field
9186 TYPE_CANONICAL (t
) = template_type
;
9187 else if (any_template_arguments_need_structural_equality_p (arglist
))
9188 /* Some of the template arguments require structural
9189 equality testing, so this template class requires
9190 structural equality testing. */
9191 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9196 /* If we called start_enum or pushtag above, this information
9197 will already be set up. */
9200 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9202 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9203 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9204 DECL_SOURCE_LOCATION (type_decl
)
9205 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9208 type_decl
= TYPE_NAME (t
);
9210 if (CLASS_TYPE_P (template_type
))
9212 TREE_PRIVATE (type_decl
)
9213 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9214 TREE_PROTECTED (type_decl
)
9215 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9216 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9218 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9219 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9223 if (OVERLOAD_TYPE_P (t
)
9224 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9226 static const char *tags
[] = {"abi_tag", "may_alias"};
9228 for (unsigned ix
= 0; ix
!= 2; ix
++)
9231 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9235 = tree_cons (TREE_PURPOSE (attributes
),
9236 TREE_VALUE (attributes
),
9237 TYPE_ATTRIBUTES (t
));
9241 /* Let's consider the explicit specialization of a member
9242 of a class template specialization that is implicitly instantiated,
9247 template<class U> struct M {}; //#0
9252 struct S<int>::M<char> //#1
9256 [temp.expl.spec]/4 says this is valid.
9258 In this case, when we write:
9261 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9264 When we encounter #1, we want to store the partial instantiation
9265 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9267 For all cases other than this "explicit specialization of member of a
9268 class template", we just want to store the most general template into
9269 the CLASSTYPE_TI_TEMPLATE of M.
9271 This case of "explicit specialization of member of a class template"
9273 1/ the enclosing class is an instantiation of, and therefore not
9274 the same as, the context of the most general template, and
9275 2/ we aren't looking at the partial instantiation itself, i.e.
9276 the innermost arguments are not the same as the innermost parms of
9277 the most general template.
9279 So it's only when 1/ and 2/ happens that we want to use the partial
9280 instantiation of the member template in lieu of its most general
9283 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9284 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9285 /* the enclosing class must be an instantiation... */
9286 && CLASS_TYPE_P (context
)
9287 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9289 TREE_VEC_LENGTH (arglist
)--;
9290 ++processing_template_decl
;
9291 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9292 tree partial_inst_args
=
9293 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9294 arglist
, complain
, NULL_TREE
);
9295 --processing_template_decl
;
9296 TREE_VEC_LENGTH (arglist
)++;
9297 if (partial_inst_args
== error_mark_node
)
9298 return error_mark_node
;
9299 use_partial_inst_tmpl
=
9300 /*...and we must not be looking at the partial instantiation
9302 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9306 if (!use_partial_inst_tmpl
)
9307 /* This case is easy; there are no member templates involved. */
9311 /* This is a full instantiation of a member template. Find
9312 the partial instantiation of which this is an instance. */
9314 /* Temporarily reduce by one the number of levels in the ARGLIST
9315 so as to avoid comparing the last set of arguments. */
9316 TREE_VEC_LENGTH (arglist
)--;
9317 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9318 TREE_VEC_LENGTH (arglist
)++;
9319 /* FOUND is either a proper class type, or an alias
9320 template specialization. In the later case, it's a
9321 TYPE_DECL, resulting from the substituting of arguments
9322 for parameters in the TYPE_DECL of the alias template
9323 done earlier. So be careful while getting the template
9325 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9327 : (TREE_CODE (found
) == TYPE_DECL
9328 ? DECL_TI_TEMPLATE (found
)
9329 : CLASSTYPE_TI_TEMPLATE (found
)));
9332 // Build template info for the new specialization.
9333 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9336 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9337 entry
= ggc_alloc
<spec_entry
> ();
9341 /* Note this use of the partial instantiation so we can check it
9342 later in maybe_process_partial_specialization. */
9343 DECL_TEMPLATE_INSTANTIATIONS (found
)
9344 = tree_cons (arglist
, t
,
9345 DECL_TEMPLATE_INSTANTIATIONS (found
));
9347 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9348 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9349 /* Now that the type has been registered on the instantiations
9350 list, we set up the enumerators. Because the enumeration
9351 constants may involve the enumeration type itself, we make
9352 sure to register the type first, and then create the
9353 constants. That way, doing tsubst_expr for the enumeration
9354 constants won't result in recursive calls here; we'll find
9355 the instantiation and exit above. */
9356 tsubst_enum (template_type
, t
, arglist
);
9358 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9359 /* If the type makes use of template parameters, the
9360 code that generates debugging information will crash. */
9361 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9363 /* Possibly limit visibility based on template args. */
9364 TREE_PUBLIC (type_decl
) = 1;
9365 determine_visibility (type_decl
);
9367 inherit_targ_abi_tags (t
);
9373 /* Wrapper for lookup_template_class_1. */
9376 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9377 int entering_scope
, tsubst_flags_t complain
)
9380 timevar_push (TV_TEMPLATE_INST
);
9381 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9382 entering_scope
, complain
);
9383 timevar_pop (TV_TEMPLATE_INST
);
9387 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9390 lookup_template_variable (tree templ
, tree arglist
)
9392 /* The type of the expression is NULL_TREE since the template-id could refer
9393 to an explicit or partial specialization. */
9394 tree type
= NULL_TREE
;
9395 if (flag_concepts
&& variable_concept_p (templ
))
9396 /* Except that concepts are always bool. */
9397 type
= boolean_type_node
;
9398 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9401 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9404 finish_template_variable (tree var
, tsubst_flags_t complain
)
9406 tree templ
= TREE_OPERAND (var
, 0);
9407 tree arglist
= TREE_OPERAND (var
, 1);
9409 /* We never want to return a VAR_DECL for a variable concept, since they
9410 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9411 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9412 if (concept_p
&& processing_template_decl
)
9415 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9416 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9418 templ
= most_general_template (templ
);
9419 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9420 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9422 /*use_default*/true);
9424 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9426 if (complain
& tf_error
)
9428 error ("use of invalid variable template %qE", var
);
9429 diagnose_constraints (location_of (var
), templ
, arglist
);
9431 return error_mark_node
;
9434 /* If a template-id refers to a specialization of a variable
9435 concept, then the expression is true if and only if the
9436 concept's constraints are satisfied by the given template
9439 NOTE: This is an extension of Concepts Lite TS that
9440 allows constraints to be used in expressions. */
9443 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9444 return evaluate_variable_concept (decl
, arglist
);
9447 return instantiate_template (templ
, arglist
, complain
);
9450 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9451 TARGS template args, and instantiate it if it's not dependent. */
9454 lookup_and_finish_template_variable (tree templ
, tree targs
,
9455 tsubst_flags_t complain
)
9457 templ
= lookup_template_variable (templ
, targs
);
9458 if (!any_dependent_template_arguments_p (targs
))
9460 templ
= finish_template_variable (templ
, complain
);
9464 return convert_from_reference (templ
);
9473 /* True when we should also visit template parameters that occur in
9474 non-deduced contexts. */
9475 bool include_nondeduced_p
;
9476 hash_set
<tree
> *visited
;
9479 /* Called from for_each_template_parm via walk_tree. */
9482 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9485 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9486 tree_fn_t fn
= pfd
->fn
;
9487 void *data
= pfd
->data
;
9488 tree result
= NULL_TREE
;
9490 #define WALK_SUBTREE(NODE) \
9493 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9494 pfd->include_nondeduced_p, \
9496 if (result) goto out; \
9500 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9504 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9505 WALK_SUBTREE (TYPE_CONTEXT (t
));
9507 switch (TREE_CODE (t
))
9510 if (TYPE_PTRMEMFUNC_P (t
))
9516 if (!TYPE_TEMPLATE_INFO (t
))
9519 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9523 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9524 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9528 /* Since we're not going to walk subtrees, we have to do this
9530 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9534 /* Check the return type. */
9535 WALK_SUBTREE (TREE_TYPE (t
));
9537 /* Check the parameter types. Since default arguments are not
9538 instantiated until they are needed, the TYPE_ARG_TYPES may
9539 contain expressions that involve template parameters. But,
9540 no-one should be looking at them yet. And, once they're
9541 instantiated, they don't contain template parameters, so
9542 there's no point in looking at them then, either. */
9546 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9547 WALK_SUBTREE (TREE_VALUE (parm
));
9549 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9550 want walk_tree walking into them itself. */
9554 if (flag_noexcept_type
)
9556 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9558 WALK_SUBTREE (TREE_PURPOSE (spec
));
9563 case UNDERLYING_TYPE
:
9564 if (pfd
->include_nondeduced_p
9565 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9567 pfd
->include_nondeduced_p
,
9569 return error_mark_node
;
9574 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9575 WALK_SUBTREE (DECL_TI_ARGS (t
));
9580 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9581 WALK_SUBTREE (DECL_INITIAL (t
));
9582 if (DECL_CONTEXT (t
)
9583 && pfd
->include_nondeduced_p
)
9584 WALK_SUBTREE (DECL_CONTEXT (t
));
9587 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9588 /* Record template parameters such as `T' inside `TT<T>'. */
9589 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9592 case TEMPLATE_TEMPLATE_PARM
:
9593 case TEMPLATE_TYPE_PARM
:
9594 case TEMPLATE_PARM_INDEX
:
9595 if (fn
&& (*fn
)(t
, data
))
9602 /* A template template parameter is encountered. */
9603 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9604 WALK_SUBTREE (TREE_TYPE (t
));
9606 /* Already substituted template template parameter */
9611 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9612 partial instantiation. */
9613 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9617 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9618 && pfd
->include_nondeduced_p
)
9619 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9624 /* If there's no type, then this thing must be some expression
9625 involving template parameters. */
9626 if (!fn
&& !TREE_TYPE (t
))
9627 return error_mark_node
;
9632 case IMPLICIT_CONV_EXPR
:
9633 case REINTERPRET_CAST_EXPR
:
9634 case CONST_CAST_EXPR
:
9635 case STATIC_CAST_EXPR
:
9636 case DYNAMIC_CAST_EXPR
:
9640 case PSEUDO_DTOR_EXPR
:
9642 return error_mark_node
;
9651 /* We didn't find any template parameters we liked. */
9656 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9657 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9658 call FN with the parameter and the DATA.
9659 If FN returns nonzero, the iteration is terminated, and
9660 for_each_template_parm returns 1. Otherwise, the iteration
9661 continues. If FN never returns a nonzero value, the value
9662 returned by for_each_template_parm is 0. If FN is NULL, it is
9663 considered to be the function which always returns 1.
9665 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9666 parameters that occur in non-deduced contexts. When false, only
9667 visits those template parameters that can be deduced. */
9670 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9671 hash_set
<tree
> *visited
,
9672 bool include_nondeduced_p
,
9675 struct pair_fn_data pfd
;
9680 pfd
.any_fn
= any_fn
;
9682 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9684 /* Walk the tree. (Conceptually, we would like to walk without
9685 duplicates, but for_each_template_parm_r recursively calls
9686 for_each_template_parm, so we would need to reorganize a fair
9687 bit to use walk_tree_without_duplicates, so we keep our own
9690 pfd
.visited
= visited
;
9692 pfd
.visited
= new hash_set
<tree
>;
9693 result
= cp_walk_tree (&t
,
9694 for_each_template_parm_r
,
9708 /* Returns true if T depends on any template parameter. */
9711 uses_template_parms (tree t
)
9717 int saved_processing_template_decl
;
9719 saved_processing_template_decl
= processing_template_decl
;
9720 if (!saved_processing_template_decl
)
9721 processing_template_decl
= 1;
9723 dependent_p
= dependent_type_p (t
);
9724 else if (TREE_CODE (t
) == TREE_VEC
)
9725 dependent_p
= any_dependent_template_arguments_p (t
);
9726 else if (TREE_CODE (t
) == TREE_LIST
)
9727 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9728 || uses_template_parms (TREE_CHAIN (t
)));
9729 else if (TREE_CODE (t
) == TYPE_DECL
)
9730 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9733 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9734 || TREE_CODE (t
) == OVERLOAD
9737 || TREE_CODE (t
) == TRAIT_EXPR
9738 || TREE_CODE (t
) == CONSTRUCTOR
9739 || CONSTANT_CLASS_P (t
))
9740 dependent_p
= (type_dependent_expression_p (t
)
9741 || value_dependent_expression_p (t
));
9744 gcc_assert (t
== error_mark_node
);
9745 dependent_p
= false;
9748 processing_template_decl
= saved_processing_template_decl
;
9753 /* Returns true iff current_function_decl is an incompletely instantiated
9754 template. Useful instead of processing_template_decl because the latter
9755 is set to 0 during instantiate_non_dependent_expr. */
9758 in_template_function (void)
9760 tree fn
= current_function_decl
;
9762 ++processing_template_decl
;
9763 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9764 && DECL_TEMPLATE_INFO (fn
)
9765 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9766 --processing_template_decl
;
9770 /* Returns true if T depends on any template parameter with level LEVEL. */
9773 uses_template_parms_level (tree t
, int level
)
9775 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9776 /*include_nondeduced_p=*/true);
9779 /* Returns true if the signature of DECL depends on any template parameter from
9780 its enclosing class. */
9783 uses_outer_template_parms (tree decl
)
9785 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9788 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9789 &depth
, NULL
, /*include_nondeduced_p=*/true))
9791 if (PRIMARY_TEMPLATE_P (decl
)
9792 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9793 (DECL_TEMPLATE_PARMS (decl
)),
9794 template_parm_outer_level
,
9795 &depth
, NULL
, /*include_nondeduced_p=*/true))
9797 tree ci
= get_constraints (decl
);
9799 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9800 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9801 &depth
, NULL
, /*nondeduced*/true))
9806 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9807 ill-formed translation unit, i.e. a variable or function that isn't
9808 usable in a constant expression. */
9811 neglectable_inst_p (tree d
)
9814 && !undeduced_auto_decl (d
)
9815 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9816 : decl_maybe_constant_var_p (d
)));
9819 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9820 neglectable and instantiated from within an erroneous instantiation. */
9823 limit_bad_template_recursion (tree decl
)
9825 struct tinst_level
*lev
= current_tinst_level
;
9826 int errs
= errorcount
+ sorrycount
;
9827 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9830 for (; lev
; lev
= lev
->next
)
9831 if (neglectable_inst_p (lev
->decl
))
9834 return (lev
&& errs
> lev
->errors
);
9837 static int tinst_depth
;
9838 extern int max_tinst_depth
;
9841 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9843 /* We're starting to instantiate D; record the template instantiation context
9844 for diagnostics and to restore it later. */
9847 push_tinst_level (tree d
)
9849 return push_tinst_level_loc (d
, input_location
);
9852 /* We're starting to instantiate D; record the template instantiation context
9853 at LOC for diagnostics and to restore it later. */
9856 push_tinst_level_loc (tree d
, location_t loc
)
9858 struct tinst_level
*new_level
;
9860 if (tinst_depth
>= max_tinst_depth
)
9862 /* Tell error.c not to try to instantiate any templates. */
9864 fatal_error (input_location
,
9865 "template instantiation depth exceeds maximum of %d"
9866 " (use -ftemplate-depth= to increase the maximum)",
9871 /* If the current instantiation caused problems, don't let it instantiate
9872 anything else. Do allow deduction substitution and decls usable in
9873 constant expressions. */
9874 if (limit_bad_template_recursion (d
))
9877 /* When not -quiet, dump template instantiations other than functions, since
9878 announce_function will take care of those. */
9880 && TREE_CODE (d
) != TREE_LIST
9881 && TREE_CODE (d
) != FUNCTION_DECL
)
9882 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9884 new_level
= ggc_alloc
<tinst_level
> ();
9885 new_level
->decl
= d
;
9886 new_level
->locus
= loc
;
9887 new_level
->errors
= errorcount
+sorrycount
;
9888 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9889 new_level
->next
= current_tinst_level
;
9890 current_tinst_level
= new_level
;
9893 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9894 depth_reached
= tinst_depth
;
9899 /* We're done instantiating this template; return to the instantiation
9903 pop_tinst_level (void)
9905 /* Restore the filename and line number stashed away when we started
9906 this instantiation. */
9907 input_location
= current_tinst_level
->locus
;
9908 current_tinst_level
= current_tinst_level
->next
;
9912 /* We're instantiating a deferred template; restore the template
9913 instantiation context in which the instantiation was requested, which
9914 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9917 reopen_tinst_level (struct tinst_level
*level
)
9919 struct tinst_level
*t
;
9922 for (t
= level
; t
; t
= t
->next
)
9925 current_tinst_level
= level
;
9927 if (current_tinst_level
)
9928 current_tinst_level
->errors
= errorcount
+sorrycount
;
9932 /* Returns the TINST_LEVEL which gives the original instantiation
9935 struct tinst_level
*
9936 outermost_tinst_level (void)
9938 struct tinst_level
*level
= current_tinst_level
;
9941 level
= level
->next
;
9945 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9946 vector of template arguments, as for tsubst.
9948 Returns an appropriate tsubst'd friend declaration. */
9951 tsubst_friend_function (tree decl
, tree args
)
9955 if (TREE_CODE (decl
) == FUNCTION_DECL
9956 && DECL_TEMPLATE_INSTANTIATION (decl
)
9957 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9958 /* This was a friend declared with an explicit template
9959 argument list, e.g.:
9963 to indicate that f was a template instantiation, not a new
9964 function declaration. Now, we have to figure out what
9965 instantiation of what template. */
9967 tree template_id
, arglist
, fns
;
9970 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9972 /* Friend functions are looked up in the containing namespace scope.
9973 We must enter that scope, to avoid finding member functions of the
9974 current class with same name. */
9975 push_nested_namespace (ns
);
9976 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9977 tf_warning_or_error
, NULL_TREE
,
9978 /*integral_constant_expression_p=*/false);
9979 pop_nested_namespace (ns
);
9980 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9981 tf_warning_or_error
, NULL_TREE
);
9982 template_id
= lookup_template_function (fns
, arglist
);
9984 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9985 tmpl
= determine_specialization (template_id
, new_friend
,
9987 /*need_member_template=*/0,
9988 TREE_VEC_LENGTH (args
),
9990 return instantiate_template (tmpl
, new_args
, tf_error
);
9993 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9995 /* The NEW_FRIEND will look like an instantiation, to the
9996 compiler, but is not an instantiation from the point of view of
9997 the language. For example, we might have had:
9999 template <class T> struct S {
10000 template <class U> friend void f(T, U);
10003 Then, in S<int>, template <class U> void f(int, U) is not an
10004 instantiation of anything. */
10005 if (new_friend
== error_mark_node
)
10006 return error_mark_node
;
10008 DECL_USE_TEMPLATE (new_friend
) = 0;
10009 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10011 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10012 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10013 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10016 /* The mangled name for the NEW_FRIEND is incorrect. The function
10017 is not a template instantiation and should not be mangled like
10018 one. Therefore, we forget the mangling here; we'll recompute it
10019 later if we need it. */
10020 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10022 SET_DECL_RTL (new_friend
, NULL
);
10023 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10026 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10029 tree new_friend_template_info
;
10030 tree new_friend_result_template_info
;
10032 int new_friend_is_defn
;
10034 /* We must save some information from NEW_FRIEND before calling
10035 duplicate decls since that function will free NEW_FRIEND if
10037 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10038 new_friend_is_defn
=
10039 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10040 (template_for_substitution (new_friend
)))
10042 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10044 /* This declaration is a `primary' template. */
10045 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10047 new_friend_result_template_info
10048 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10051 new_friend_result_template_info
= NULL_TREE
;
10053 /* Inside pushdecl_namespace_level, we will push into the
10054 current namespace. However, the friend function should go
10055 into the namespace of the template. */
10056 ns
= decl_namespace_context (new_friend
);
10057 push_nested_namespace (ns
);
10058 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10059 pop_nested_namespace (ns
);
10061 if (old_decl
== error_mark_node
)
10062 return error_mark_node
;
10064 if (old_decl
!= new_friend
)
10066 /* This new friend declaration matched an existing
10067 declaration. For example, given:
10069 template <class T> void f(T);
10070 template <class U> class C {
10071 template <class T> friend void f(T) {}
10074 the friend declaration actually provides the definition
10075 of `f', once C has been instantiated for some type. So,
10076 old_decl will be the out-of-class template declaration,
10077 while new_friend is the in-class definition.
10079 But, if `f' was called before this point, the
10080 instantiation of `f' will have DECL_TI_ARGS corresponding
10081 to `T' but not to `U', references to which might appear
10082 in the definition of `f'. Previously, the most general
10083 template for an instantiation of `f' was the out-of-class
10084 version; now it is the in-class version. Therefore, we
10085 run through all specialization of `f', adding to their
10086 DECL_TI_ARGS appropriately. In particular, they need a
10087 new set of outer arguments, corresponding to the
10088 arguments for this class instantiation.
10090 The same situation can arise with something like this:
10092 friend void f(int);
10093 template <class T> class C {
10094 friend void f(T) {}
10097 when `C<int>' is instantiated. Now, `f(int)' is defined
10100 if (!new_friend_is_defn
)
10101 /* On the other hand, if the in-class declaration does
10102 *not* provide a definition, then we don't want to alter
10103 existing definitions. We can just leave everything
10108 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10109 tree new_args
= TI_ARGS (new_friend_template_info
);
10111 /* Overwrite whatever template info was there before, if
10112 any, with the new template information pertaining to
10113 the declaration. */
10114 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10116 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10118 /* We should have called reregister_specialization in
10119 duplicate_decls. */
10120 gcc_assert (retrieve_specialization (new_template
,
10124 /* Instantiate it if the global has already been used. */
10125 if (DECL_ODR_USED (old_decl
))
10126 instantiate_decl (old_decl
, /*defer_ok=*/true,
10127 /*expl_inst_class_mem_p=*/false);
10133 /* Indicate that the old function template is a partial
10135 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10136 = new_friend_result_template_info
;
10138 gcc_assert (new_template
10139 == most_general_template (new_template
));
10140 gcc_assert (new_template
!= old_decl
);
10142 /* Reassign any specializations already in the hash table
10143 to the new more general template, and add the
10144 additional template args. */
10145 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10147 t
= TREE_CHAIN (t
))
10149 tree spec
= TREE_VALUE (t
);
10152 elt
.tmpl
= old_decl
;
10153 elt
.args
= DECL_TI_ARGS (spec
);
10154 elt
.spec
= NULL_TREE
;
10156 decl_specializations
->remove_elt (&elt
);
10158 DECL_TI_ARGS (spec
)
10159 = add_outermost_template_args (new_args
,
10160 DECL_TI_ARGS (spec
));
10162 register_specialization
10163 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10166 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10170 /* The information from NEW_FRIEND has been merged into OLD_DECL
10171 by duplicate_decls. */
10172 new_friend
= old_decl
;
10177 tree context
= DECL_CONTEXT (new_friend
);
10181 template <class T> class C {
10182 template <class U> friend void C1<U>::f (); // case 1
10183 friend void C2<T>::f (); // case 2
10185 we only need to make sure CONTEXT is a complete type for
10186 case 2. To distinguish between the two cases, we note that
10187 CONTEXT of case 1 remains dependent type after tsubst while
10188 this isn't true for case 2. */
10189 ++processing_template_decl
;
10190 dependent_p
= dependent_type_p (context
);
10191 --processing_template_decl
;
10194 && !complete_type_or_else (context
, NULL_TREE
))
10195 return error_mark_node
;
10197 if (COMPLETE_TYPE_P (context
))
10199 tree fn
= new_friend
;
10200 /* do_friend adds the TEMPLATE_DECL for any member friend
10201 template even if it isn't a member template, i.e.
10202 template <class T> friend A<T>::f();
10203 Look through it in that case. */
10204 if (TREE_CODE (fn
) == TEMPLATE_DECL
10205 && !PRIMARY_TEMPLATE_P (fn
))
10206 fn
= DECL_TEMPLATE_RESULT (fn
);
10207 /* Check to see that the declaration is really present, and,
10208 possibly obtain an improved declaration. */
10209 fn
= check_classfn (context
, fn
, NULL_TREE
);
10219 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10220 template arguments, as for tsubst.
10222 Returns an appropriate tsubst'd friend type or error_mark_node on
10226 tsubst_friend_class (tree friend_tmpl
, tree args
)
10230 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10232 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10233 return TREE_TYPE (tmpl
);
10236 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10237 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10238 push_nested_namespace (context
);
10240 push_nested_class (context
);
10242 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10243 /*non_class=*/false, /*block_p=*/false,
10244 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10246 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10248 /* The friend template has already been declared. Just
10249 check to see that the declarations match, and install any new
10250 default parameters. We must tsubst the default parameters,
10251 of course. We only need the innermost template parameters
10252 because that is all that redeclare_class_template will look
10254 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10255 > TMPL_ARGS_DEPTH (args
))
10257 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10258 args
, tf_warning_or_error
);
10259 location_t saved_input_location
= input_location
;
10260 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10261 tree cons
= get_constraints (tmpl
);
10262 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10263 input_location
= saved_input_location
;
10268 /* The friend template has not already been declared. In this
10269 case, the instantiation of the template class will cause the
10270 injection of this template into the namespace scope. */
10271 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10273 if (tmpl
!= error_mark_node
)
10275 /* The new TMPL is not an instantiation of anything, so we
10276 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10277 for the new type because that is supposed to be the
10278 corresponding template decl, i.e., TMPL. */
10279 DECL_USE_TEMPLATE (tmpl
) = 0;
10280 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10281 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10282 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10283 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10285 /* It is hidden. */
10286 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10287 DECL_ANTICIPATED (tmpl
)
10288 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10290 /* Inject this template into the enclosing namspace scope. */
10291 tmpl
= pushdecl_namespace_level (tmpl
, true);
10295 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10296 pop_nested_namespace (context
);
10298 pop_nested_class ();
10300 return TREE_TYPE (tmpl
);
10303 /* Returns zero if TYPE cannot be completed later due to circularity.
10304 Otherwise returns one. */
10307 can_complete_type_without_circularity (tree type
)
10309 if (type
== NULL_TREE
|| type
== error_mark_node
)
10311 else if (COMPLETE_TYPE_P (type
))
10313 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10314 return can_complete_type_without_circularity (TREE_TYPE (type
));
10315 else if (CLASS_TYPE_P (type
)
10316 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10322 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10323 tsubst_flags_t
, tree
);
10325 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10326 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10329 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10330 tsubst_flags_t complain
, tree in_decl
)
10332 gcc_assert (ATTR_IS_DEPENDENT (t
));
10334 tree val
= TREE_VALUE (t
);
10335 if (val
== NULL_TREE
)
10336 /* Nothing to do. */;
10337 else if ((flag_openmp
|| flag_openmp_simd
)
10338 && is_attribute_p ("omp declare simd",
10339 get_attribute_name (t
)))
10341 tree clauses
= TREE_VALUE (val
);
10342 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10343 complain
, in_decl
);
10344 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10345 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10346 tree parms
= DECL_ARGUMENTS (*decl_p
);
10348 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10350 val
= build_tree_list (NULL_TREE
, clauses
);
10354 /* If the first attribute argument is an identifier, don't
10355 pass it through tsubst. Attributes like mode, format,
10356 cleanup and several target specific attributes expect it
10358 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10361 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10362 /*integral_constant_expression_p=*/false);
10363 if (chain
!= TREE_CHAIN (val
))
10364 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10366 else if (PACK_EXPANSION_P (val
))
10368 /* An attribute pack expansion. */
10369 tree purp
= TREE_PURPOSE (t
);
10370 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10371 if (pack
== error_mark_node
)
10372 return error_mark_node
;
10373 int len
= TREE_VEC_LENGTH (pack
);
10374 tree list
= NULL_TREE
;
10376 for (int i
= 0; i
< len
; ++i
)
10378 tree elt
= TREE_VEC_ELT (pack
, i
);
10379 *q
= build_tree_list (purp
, elt
);
10380 q
= &TREE_CHAIN (*q
);
10385 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10386 /*integral_constant_expression_p=*/false);
10388 if (val
!= TREE_VALUE (t
))
10389 return build_tree_list (TREE_PURPOSE (t
), val
);
10393 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10394 unchanged or a new TREE_LIST chain. */
10397 tsubst_attributes (tree attributes
, tree args
,
10398 tsubst_flags_t complain
, tree in_decl
)
10400 tree last_dep
= NULL_TREE
;
10402 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10403 if (ATTR_IS_DEPENDENT (t
))
10406 attributes
= copy_list (attributes
);
10411 for (tree
*p
= &attributes
; *p
; )
10414 if (ATTR_IS_DEPENDENT (t
))
10416 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10421 p
= &TREE_CHAIN (*p
);
10422 *p
= TREE_CHAIN (t
);
10426 p
= &TREE_CHAIN (*p
);
10432 /* Apply any attributes which had to be deferred until instantiation
10433 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10434 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10437 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10438 tree args
, tsubst_flags_t complain
, tree in_decl
)
10440 tree last_dep
= NULL_TREE
;
10444 if (attributes
== NULL_TREE
)
10447 if (DECL_P (*decl_p
))
10449 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10451 p
= &DECL_ATTRIBUTES (*decl_p
);
10452 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10453 to our attributes parameter. */
10454 gcc_assert (*p
== attributes
);
10458 p
= &TYPE_ATTRIBUTES (*decl_p
);
10459 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10460 lookup_template_class_1, and should be preserved. */
10461 gcc_assert (*p
!= attributes
);
10463 p
= &TREE_CHAIN (*p
);
10466 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10467 if (ATTR_IS_DEPENDENT (t
))
10470 attributes
= copy_list (attributes
);
10477 tree late_attrs
= NULL_TREE
;
10478 tree
*q
= &late_attrs
;
10483 if (ATTR_IS_DEPENDENT (t
))
10485 *p
= TREE_CHAIN (t
);
10486 TREE_CHAIN (t
) = NULL_TREE
;
10487 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10489 q
= &TREE_CHAIN (*q
);
10492 p
= &TREE_CHAIN (t
);
10495 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10499 /* Perform (or defer) access check for typedefs that were referenced
10500 from within the template TMPL code.
10501 This is a subroutine of instantiate_decl and instantiate_class_template.
10502 TMPL is the template to consider and TARGS is the list of arguments of
10506 perform_typedefs_access_check (tree tmpl
, tree targs
)
10508 location_t saved_location
;
10510 qualified_typedef_usage_t
*iter
;
10513 || (!CLASS_TYPE_P (tmpl
)
10514 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10517 saved_location
= input_location
;
10518 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10520 tree type_decl
= iter
->typedef_decl
;
10521 tree type_scope
= iter
->context
;
10523 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10526 if (uses_template_parms (type_decl
))
10527 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10528 if (uses_template_parms (type_scope
))
10529 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10531 /* Make access check error messages point to the location
10532 of the use of the typedef. */
10533 input_location
= iter
->locus
;
10534 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10535 type_decl
, type_decl
,
10536 tf_warning_or_error
);
10538 input_location
= saved_location
;
10542 instantiate_class_template_1 (tree type
)
10544 tree templ
, args
, pattern
, t
, member
;
10548 unsigned int saved_maximum_field_alignment
;
10551 if (type
== error_mark_node
)
10552 return error_mark_node
;
10554 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10555 || uses_template_parms (type
))
10558 /* Figure out which template is being instantiated. */
10559 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10560 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10562 /* Mark the type as in the process of being defined. */
10563 TYPE_BEING_DEFINED (type
) = 1;
10565 /* Determine what specialization of the original template to
10567 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10568 if (t
== error_mark_node
)
10569 return error_mark_node
;
10572 /* This TYPE is actually an instantiation of a partial
10573 specialization. We replace the innermost set of ARGS with
10574 the arguments appropriate for substitution. For example,
10577 template <class T> struct S {};
10578 template <class T> struct S<T*> {};
10580 and supposing that we are instantiating S<int*>, ARGS will
10581 presently be {int*} -- but we need {int}. */
10582 pattern
= TREE_TYPE (t
);
10583 args
= TREE_PURPOSE (t
);
10587 pattern
= TREE_TYPE (templ
);
10588 args
= CLASSTYPE_TI_ARGS (type
);
10591 /* If the template we're instantiating is incomplete, then clearly
10592 there's nothing we can do. */
10593 if (!COMPLETE_TYPE_P (pattern
))
10595 /* We can try again later. */
10596 TYPE_BEING_DEFINED (type
) = 0;
10600 /* If we've recursively instantiated too many templates, stop. */
10601 if (! push_tinst_level (type
))
10604 /* We may be in the middle of deferred access check. Disable
10606 push_deferring_access_checks (dk_no_deferred
);
10608 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10609 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10611 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10612 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10613 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10614 fn_context
= error_mark_node
;
10616 push_to_top_level ();
10619 cp_unevaluated_operand
= 0;
10620 c_inhibit_evaluation_warnings
= 0;
10622 /* Use #pragma pack from the template context. */
10623 saved_maximum_field_alignment
= maximum_field_alignment
;
10624 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10626 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10628 /* Set the input location to the most specialized template definition.
10629 This is needed if tsubsting causes an error. */
10630 typedecl
= TYPE_MAIN_DECL (pattern
);
10631 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10632 DECL_SOURCE_LOCATION (typedecl
);
10634 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10635 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10636 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10637 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10638 if (ANON_AGGR_TYPE_P (pattern
))
10639 SET_ANON_AGGR_TYPE_P (type
);
10640 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10642 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10643 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10644 /* Adjust visibility for template arguments. */
10645 determine_visibility (TYPE_MAIN_DECL (type
));
10647 if (CLASS_TYPE_P (type
))
10648 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10650 pbinfo
= TYPE_BINFO (pattern
);
10652 /* We should never instantiate a nested class before its enclosing
10653 class; we need to look up the nested class by name before we can
10654 instantiate it, and that lookup should instantiate the enclosing
10656 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10657 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10659 base_list
= NULL_TREE
;
10660 if (BINFO_N_BASE_BINFOS (pbinfo
))
10666 /* We must enter the scope containing the type, as that is where
10667 the accessibility of types named in dependent bases are
10669 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10671 /* Substitute into each of the bases to determine the actual
10673 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10676 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10677 tree expanded_bases
= NULL_TREE
;
10680 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10683 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10684 args
, tf_error
, NULL_TREE
);
10685 if (expanded_bases
== error_mark_node
)
10688 len
= TREE_VEC_LENGTH (expanded_bases
);
10691 for (idx
= 0; idx
< len
; idx
++)
10693 if (expanded_bases
)
10694 /* Extract the already-expanded base class. */
10695 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10697 /* Substitute to figure out the base class. */
10698 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10701 if (base
== error_mark_node
)
10704 base_list
= tree_cons (access
, base
, base_list
);
10705 if (BINFO_VIRTUAL_P (pbase_binfo
))
10706 TREE_TYPE (base_list
) = integer_type_node
;
10710 /* The list is now in reverse order; correct that. */
10711 base_list
= nreverse (base_list
);
10714 pop_scope (pushed_scope
);
10716 /* Now call xref_basetypes to set up all the base-class
10718 xref_basetypes (type
, base_list
);
10720 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10721 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10722 args
, tf_error
, NULL_TREE
);
10723 fixup_attribute_variants (type
);
10725 /* Now that our base classes are set up, enter the scope of the
10726 class, so that name lookups into base classes, etc. will work
10727 correctly. This is precisely analogous to what we do in
10728 begin_class_definition when defining an ordinary non-template
10729 class, except we also need to push the enclosing classes. */
10730 push_nested_class (type
);
10732 /* Now members are processed in the order of declaration. */
10733 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10734 member
; member
= TREE_CHAIN (member
))
10736 tree t
= TREE_VALUE (member
);
10738 if (TREE_PURPOSE (member
))
10742 if (LAMBDA_TYPE_P (t
))
10743 /* A closure type for a lambda in an NSDMI or default argument.
10744 Ignore it; it will be regenerated when needed. */
10747 /* Build new CLASSTYPE_NESTED_UTDS. */
10750 bool class_template_p
;
10752 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10753 && TYPE_LANG_SPECIFIC (t
)
10754 && CLASSTYPE_IS_TEMPLATE (t
));
10755 /* If the member is a class template, then -- even after
10756 substitution -- there may be dependent types in the
10757 template argument list for the class. We increment
10758 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10759 that function will assume that no types are dependent
10760 when outside of a template. */
10761 if (class_template_p
)
10762 ++processing_template_decl
;
10763 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10764 if (class_template_p
)
10765 --processing_template_decl
;
10766 if (newtag
== error_mark_node
)
10769 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10771 tree name
= TYPE_IDENTIFIER (t
);
10773 if (class_template_p
)
10774 /* Unfortunately, lookup_template_class sets
10775 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10776 instantiation (i.e., for the type of a member
10777 template class nested within a template class.)
10778 This behavior is required for
10779 maybe_process_partial_specialization to work
10780 correctly, but is not accurate in this case;
10781 the TAG is not an instantiation of anything.
10782 (The corresponding TEMPLATE_DECL is an
10783 instantiation, but the TYPE is not.) */
10784 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10786 /* Now, we call pushtag to put this NEWTAG into the scope of
10787 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10788 pushtag calling push_template_decl. We don't have to do
10789 this for enums because it will already have been done in
10792 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10793 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10796 else if (DECL_DECLARES_FUNCTION_P (t
))
10800 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10801 ++processing_template_decl
;
10802 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10803 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10804 --processing_template_decl
;
10805 set_current_access_from_decl (r
);
10806 finish_member_declaration (r
);
10807 /* Instantiate members marked with attribute used. */
10808 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10810 if (TREE_CODE (r
) == FUNCTION_DECL
10811 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10812 cp_check_omp_declare_reduction (r
);
10814 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
10815 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10816 /* A closure type for a lambda in an NSDMI or default argument.
10817 Ignore it; it will be regenerated when needed. */;
10820 /* Build new TYPE_FIELDS. */
10821 if (TREE_CODE (t
) == STATIC_ASSERT
)
10825 ++c_inhibit_evaluation_warnings
;
10827 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10828 tf_warning_or_error
, NULL_TREE
,
10829 /*integral_constant_expression_p=*/true);
10830 --c_inhibit_evaluation_warnings
;
10832 finish_static_assert (condition
,
10833 STATIC_ASSERT_MESSAGE (t
),
10834 STATIC_ASSERT_SOURCE_LOCATION (t
),
10835 /*member_p=*/true);
10837 else if (TREE_CODE (t
) != CONST_DECL
)
10840 tree vec
= NULL_TREE
;
10843 /* The file and line for this declaration, to
10844 assist in error message reporting. Since we
10845 called push_tinst_level above, we don't need to
10847 input_location
= DECL_SOURCE_LOCATION (t
);
10849 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10850 ++processing_template_decl
;
10851 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10852 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10853 --processing_template_decl
;
10855 if (TREE_CODE (r
) == TREE_VEC
)
10857 /* A capture pack became multiple fields. */
10859 len
= TREE_VEC_LENGTH (vec
);
10862 for (int i
= 0; i
< len
; ++i
)
10865 r
= TREE_VEC_ELT (vec
, i
);
10870 [t]he initialization (and any associated
10871 side-effects) of a static data member does
10872 not occur unless the static data member is
10873 itself used in a way that requires the
10874 definition of the static data member to
10877 Therefore, we do not substitute into the
10878 initialized for the static data member here. */
10879 finish_static_data_member_decl
10881 /*init=*/NULL_TREE
,
10882 /*init_const_expr_p=*/false,
10883 /*asmspec_tree=*/NULL_TREE
,
10885 /* Instantiate members marked with attribute used. */
10886 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10889 else if (TREE_CODE (r
) == FIELD_DECL
)
10891 /* Determine whether R has a valid type and can be
10892 completed later. If R is invalid, then its type
10893 is replaced by error_mark_node. */
10894 tree rtype
= TREE_TYPE (r
);
10895 if (can_complete_type_without_circularity (rtype
))
10896 complete_type (rtype
);
10898 if (!complete_or_array_type_p (rtype
))
10900 /* If R's type couldn't be completed and
10901 it isn't a flexible array member (whose
10902 type is incomplete by definition) give
10904 cxx_incomplete_type_error (r
, rtype
);
10905 TREE_TYPE (r
) = error_mark_node
;
10909 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10910 such a thing will already have been added to the field
10911 list by tsubst_enum in finish_member_declaration in the
10912 CLASSTYPE_NESTED_UTDS case above. */
10913 if (!(TREE_CODE (r
) == TYPE_DECL
10914 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10915 && DECL_ARTIFICIAL (r
)))
10917 set_current_access_from_decl (r
);
10918 finish_member_declaration (r
);
10926 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10927 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10929 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10931 tree friend_type
= t
;
10932 bool adjust_processing_template_decl
= false;
10934 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10936 /* template <class T> friend class C; */
10937 friend_type
= tsubst_friend_class (friend_type
, args
);
10938 adjust_processing_template_decl
= true;
10940 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10942 /* template <class T> friend class C::D; */
10943 friend_type
= tsubst (friend_type
, args
,
10944 tf_warning_or_error
, NULL_TREE
);
10945 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10946 friend_type
= TREE_TYPE (friend_type
);
10947 adjust_processing_template_decl
= true;
10949 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10950 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10952 /* This could be either
10956 when dependent_type_p is false or
10958 template <class U> friend class T::C;
10961 /* Bump processing_template_decl in case this is something like
10962 template <class T> friend struct A<T>::B. */
10963 ++processing_template_decl
;
10964 friend_type
= tsubst (friend_type
, args
,
10965 tf_warning_or_error
, NULL_TREE
);
10966 if (dependent_type_p (friend_type
))
10967 adjust_processing_template_decl
= true;
10968 --processing_template_decl
;
10970 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10971 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10972 && TYPE_HIDDEN_P (friend_type
))
10976 where C hasn't been declared yet. Let's lookup name
10977 from namespace scope directly, bypassing any name that
10978 come from dependent base class. */
10979 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10981 /* The call to xref_tag_from_type does injection for friend
10983 push_nested_namespace (ns
);
10985 xref_tag_from_type (friend_type
, NULL_TREE
,
10986 /*tag_scope=*/ts_current
);
10987 pop_nested_namespace (ns
);
10989 else if (uses_template_parms (friend_type
))
10990 /* friend class C<T>; */
10991 friend_type
= tsubst (friend_type
, args
,
10992 tf_warning_or_error
, NULL_TREE
);
10997 where C is already declared or
10999 friend class C<int>;
11001 We don't have to do anything in these cases. */
11003 if (adjust_processing_template_decl
)
11004 /* Trick make_friend_class into realizing that the friend
11005 we're adding is a template, not an ordinary class. It's
11006 important that we use make_friend_class since it will
11007 perform some error-checking and output cross-reference
11009 ++processing_template_decl
;
11011 if (friend_type
!= error_mark_node
)
11012 make_friend_class (type
, friend_type
, /*complain=*/false);
11014 if (adjust_processing_template_decl
)
11015 --processing_template_decl
;
11019 /* Build new DECL_FRIENDLIST. */
11022 /* The file and line for this declaration, to
11023 assist in error message reporting. Since we
11024 called push_tinst_level above, we don't need to
11026 input_location
= DECL_SOURCE_LOCATION (t
);
11028 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11030 ++processing_template_decl
;
11031 push_deferring_access_checks (dk_no_check
);
11034 r
= tsubst_friend_function (t
, args
);
11035 add_friend (type
, r
, /*complain=*/false);
11036 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11038 pop_deferring_access_checks ();
11039 --processing_template_decl
;
11047 /* Restore these before substituting into the lambda capture
11049 cp_unevaluated_operand
= saved_unevaluated_operand
;
11050 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11053 /* Set the file and line number information to whatever is given for
11054 the class itself. This puts error messages involving generated
11055 implicit functions at a predictable point, and the same point
11056 that would be used for non-template classes. */
11057 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11059 unreverse_member_declarations (type
);
11060 finish_struct_1 (type
);
11061 TYPE_BEING_DEFINED (type
) = 0;
11063 /* We don't instantiate default arguments for member functions. 14.7.1:
11065 The implicit instantiation of a class template specialization causes
11066 the implicit instantiation of the declarations, but not of the
11067 definitions or default arguments, of the class member functions,
11068 member classes, static data members and member templates.... */
11070 /* Some typedefs referenced from within the template code need to be access
11071 checked at template instantiation time, i.e now. These types were
11072 added to the template at parsing time. Let's get those and perform
11073 the access checks then. */
11074 perform_typedefs_access_check (pattern
, args
);
11075 perform_deferred_access_checks (tf_warning_or_error
);
11076 pop_nested_class ();
11077 maximum_field_alignment
= saved_maximum_field_alignment
;
11079 pop_from_top_level ();
11080 pop_deferring_access_checks ();
11081 pop_tinst_level ();
11083 /* The vtable for a template class can be emitted in any translation
11084 unit in which the class is instantiated. When there is no key
11085 method, however, finish_struct_1 will already have added TYPE to
11086 the keyed_classes. */
11087 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11088 vec_safe_push (keyed_classes
, type
);
11093 /* Wrapper for instantiate_class_template_1. */
11096 instantiate_class_template (tree type
)
11099 timevar_push (TV_TEMPLATE_INST
);
11100 ret
= instantiate_class_template_1 (type
);
11101 timevar_pop (TV_TEMPLATE_INST
);
11106 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11112 else if (TYPE_P (t
))
11113 r
= tsubst (t
, args
, complain
, in_decl
);
11116 if (!(complain
& tf_warning
))
11117 ++c_inhibit_evaluation_warnings
;
11118 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11119 /*integral_constant_expression_p=*/true);
11120 if (!(complain
& tf_warning
))
11121 --c_inhibit_evaluation_warnings
;
11126 /* Given a function parameter pack TMPL_PARM and some function parameters
11127 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11128 and set *SPEC_P to point at the next point in the list. */
11131 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11133 /* Collect all of the extra "packed" parameters into an
11136 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11137 tree spec_parm
= *spec_p
;
11140 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11142 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11145 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11146 parmvec
= make_tree_vec (len
);
11147 spec_parm
= *spec_p
;
11148 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11150 tree elt
= spec_parm
;
11151 if (DECL_PACK_P (elt
))
11152 elt
= make_pack_expansion (elt
);
11153 TREE_VEC_ELT (parmvec
, i
) = elt
;
11156 /* Build the argument packs. */
11157 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11158 *spec_p
= spec_parm
;
11163 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11164 NONTYPE_ARGUMENT_PACK. */
11167 make_fnparm_pack (tree spec_parm
)
11169 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11172 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11173 pack expansion with no extra args, 2 if it has extra args, or 0
11174 if it is not a pack expansion. */
11177 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11179 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11180 if (i
>= TREE_VEC_LENGTH (vec
))
11182 tree elt
= TREE_VEC_ELT (vec
, i
);
11184 /* A decl pack is itself an expansion. */
11185 elt
= TREE_TYPE (elt
);
11186 if (!PACK_EXPANSION_P (elt
))
11188 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11194 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11197 make_argument_pack_select (tree arg_pack
, unsigned index
)
11199 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11201 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11202 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11207 /* This is a subroutine of tsubst_pack_expansion.
11209 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11210 mechanism to store the (non complete list of) arguments of the
11211 substitution and return a non substituted pack expansion, in order
11212 to wait for when we have enough arguments to really perform the
11216 use_pack_expansion_extra_args_p (tree parm_packs
,
11218 bool has_empty_arg
)
11220 /* If one pack has an expansion and another pack has a normal
11221 argument or if one pack has an empty argument and an another
11222 one hasn't then tsubst_pack_expansion cannot perform the
11223 substitution and need to fall back on the
11224 PACK_EXPANSION_EXTRA mechanism. */
11225 if (parm_packs
== NULL_TREE
)
11227 else if (has_empty_arg
)
11230 bool has_expansion_arg
= false;
11231 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11233 bool has_non_expansion_arg
= false;
11234 for (tree parm_pack
= parm_packs
;
11236 parm_pack
= TREE_CHAIN (parm_pack
))
11238 tree arg
= TREE_VALUE (parm_pack
);
11240 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11242 /* We can't substitute a pack expansion with extra args into
11246 has_expansion_arg
= true;
11248 has_non_expansion_arg
= true;
11251 if (has_expansion_arg
&& has_non_expansion_arg
)
11257 /* [temp.variadic]/6 says that:
11259 The instantiation of a pack expansion [...]
11260 produces a list E1,E2, ..., En, where N is the number of elements
11261 in the pack expansion parameters.
11263 This subroutine of tsubst_pack_expansion produces one of these Ei.
11265 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11266 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11267 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11268 INDEX is the index 'i' of the element Ei to produce. ARGS,
11269 COMPLAIN, and IN_DECL are the same parameters as for the
11270 tsubst_pack_expansion function.
11272 The function returns the resulting Ei upon successful completion,
11273 or error_mark_node.
11275 Note that this function possibly modifies the ARGS parameter, so
11276 it's the responsibility of the caller to restore it. */
11279 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11282 tree args
/* This parm gets
11284 tsubst_flags_t complain
,
11288 bool ith_elem_is_expansion
= false;
11290 /* For each parameter pack, change the substitution of the parameter
11291 pack to the ith argument in its argument pack, then expand the
11293 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11295 tree parm
= TREE_PURPOSE (pack
);
11296 tree arg_pack
= TREE_VALUE (pack
);
11297 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11299 ith_elem_is_expansion
|=
11300 argument_pack_element_is_expansion_p (arg_pack
, index
);
11302 /* Select the Ith argument from the pack. */
11303 if (TREE_CODE (parm
) == PARM_DECL
11305 || TREE_CODE (parm
) == FIELD_DECL
)
11309 aps
= make_argument_pack_select (arg_pack
, index
);
11310 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11311 return error_mark_node
;
11312 register_local_specialization (aps
, parm
);
11315 aps
= retrieve_local_specialization (parm
);
11320 template_parm_level_and_index (parm
, &level
, &idx
);
11324 aps
= make_argument_pack_select (arg_pack
, index
);
11325 /* Update the corresponding argument. */
11326 TMPL_ARG (args
, level
, idx
) = aps
;
11329 /* Re-use the ARGUMENT_PACK_SELECT. */
11330 aps
= TMPL_ARG (args
, level
, idx
);
11332 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11335 /* Substitute into the PATTERN with the (possibly altered)
11337 if (pattern
== in_decl
)
11338 /* Expanding a fixed parameter pack from
11339 coerce_template_parameter_pack. */
11340 t
= tsubst_decl (pattern
, args
, complain
);
11341 else if (pattern
== error_mark_node
)
11342 t
= error_mark_node
;
11343 else if (constraint_p (pattern
))
11345 if (processing_template_decl
)
11346 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11348 t
= (constraints_satisfied_p (pattern
, args
)
11349 ? boolean_true_node
: boolean_false_node
);
11351 else if (!TYPE_P (pattern
))
11352 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11353 /*integral_constant_expression_p=*/false);
11355 t
= tsubst (pattern
, args
, complain
, in_decl
);
11357 /* If the Ith argument pack element is a pack expansion, then
11358 the Ith element resulting from the substituting is going to
11359 be a pack expansion as well. */
11360 if (ith_elem_is_expansion
)
11361 t
= make_pack_expansion (t
, complain
);
11366 /* When the unexpanded parameter pack in a fold expression expands to an empty
11367 sequence, the value of the expression is as follows; the program is
11368 ill-formed if the operator is not listed in this table.
11375 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11377 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11378 if (!FOLD_EXPR_MODIFY_P (t
))
11381 case TRUTH_ANDIF_EXPR
:
11382 return boolean_true_node
;
11383 case TRUTH_ORIF_EXPR
:
11384 return boolean_false_node
;
11385 case COMPOUND_EXPR
:
11391 if (complain
& tf_error
)
11392 error_at (location_of (t
),
11393 "fold of empty expansion over %O", code
);
11394 return error_mark_node
;
11397 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11398 form an expression that combines the two terms using the
11402 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11404 tree op
= FOLD_EXPR_OP (t
);
11405 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11407 // Handle compound assignment operators.
11408 if (FOLD_EXPR_MODIFY_P (t
))
11409 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11413 case COMPOUND_EXPR
:
11414 return build_x_compound_expr (input_location
, left
, right
, complain
);
11416 return build_m_component_ref (left
, right
, complain
);
11418 return build_x_binary_op (input_location
, code
,
11419 left
, TREE_CODE (left
),
11420 right
, TREE_CODE (right
),
11426 /* Substitute ARGS into the pack of a fold expression T. */
11429 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11431 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11434 /* Substitute ARGS into the pack of a fold expression T. */
11437 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11439 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11442 /* Expand a PACK of arguments into a grouped as left fold.
11443 Given a pack containing elements A0, A1, ..., An and an
11444 operator @, this builds the expression:
11446 ((A0 @ A1) @ A2) ... @ An
11448 Note that PACK must not be empty.
11450 The operator is defined by the original fold expression T. */
11453 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11455 tree left
= TREE_VEC_ELT (pack
, 0);
11456 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11458 tree right
= TREE_VEC_ELT (pack
, i
);
11459 left
= fold_expression (t
, left
, right
, complain
);
11464 /* Substitute into a unary left fold expression. */
11467 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11470 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11471 if (pack
== error_mark_node
)
11472 return error_mark_node
;
11473 if (PACK_EXPANSION_P (pack
))
11475 tree r
= copy_node (t
);
11476 FOLD_EXPR_PACK (r
) = pack
;
11479 if (TREE_VEC_LENGTH (pack
) == 0)
11480 return expand_empty_fold (t
, complain
);
11482 return expand_left_fold (t
, pack
, complain
);
11485 /* Substitute into a binary left fold expression.
11487 Do ths by building a single (non-empty) vector of argumnts and
11488 building the expression from those elements. */
11491 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11494 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11495 if (pack
== error_mark_node
)
11496 return error_mark_node
;
11497 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11498 if (init
== error_mark_node
)
11499 return error_mark_node
;
11501 if (PACK_EXPANSION_P (pack
))
11503 tree r
= copy_node (t
);
11504 FOLD_EXPR_PACK (r
) = pack
;
11505 FOLD_EXPR_INIT (r
) = init
;
11509 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11510 TREE_VEC_ELT (vec
, 0) = init
;
11511 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11512 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11514 return expand_left_fold (t
, vec
, complain
);
11517 /* Expand a PACK of arguments into a grouped as right fold.
11518 Given a pack containing elementns A0, A1, ..., and an
11519 operator @, this builds the expression:
11521 A0@ ... (An-2 @ (An-1 @ An))
11523 Note that PACK must not be empty.
11525 The operator is defined by the original fold expression T. */
11528 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11530 // Build the expression.
11531 int n
= TREE_VEC_LENGTH (pack
);
11532 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11533 for (--n
; n
!= 0; --n
)
11535 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11536 right
= fold_expression (t
, left
, right
, complain
);
11541 /* Substitute into a unary right fold expression. */
11544 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11547 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11548 if (pack
== error_mark_node
)
11549 return error_mark_node
;
11550 if (PACK_EXPANSION_P (pack
))
11552 tree r
= copy_node (t
);
11553 FOLD_EXPR_PACK (r
) = pack
;
11556 if (TREE_VEC_LENGTH (pack
) == 0)
11557 return expand_empty_fold (t
, complain
);
11559 return expand_right_fold (t
, pack
, complain
);
11562 /* Substitute into a binary right fold expression.
11564 Do ths by building a single (non-empty) vector of arguments and
11565 building the expression from those elements. */
11568 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11571 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11572 if (pack
== error_mark_node
)
11573 return error_mark_node
;
11574 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11575 if (init
== error_mark_node
)
11576 return error_mark_node
;
11578 if (PACK_EXPANSION_P (pack
))
11580 tree r
= copy_node (t
);
11581 FOLD_EXPR_PACK (r
) = pack
;
11582 FOLD_EXPR_INIT (r
) = init
;
11586 int n
= TREE_VEC_LENGTH (pack
);
11587 tree vec
= make_tree_vec (n
+ 1);
11588 for (int i
= 0; i
< n
; ++i
)
11589 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11590 TREE_VEC_ELT (vec
, n
) = init
;
11592 return expand_right_fold (t
, vec
, complain
);
11595 /* Walk through the pattern of a pack expansion, adding everything in
11596 local_specializations to a list. */
11600 hash_set
<tree
> internal
;
11602 tsubst_flags_t complain
;
11604 el_data (tsubst_flags_t c
)
11605 : extra (NULL_TREE
), complain (c
) {}
11608 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11610 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11611 tree
*extra
= &data
.extra
;
11612 tsubst_flags_t complain
= data
.complain
;
11613 if (TREE_CODE (*tp
) == DECL_EXPR
)
11614 data
.internal
.add (DECL_EXPR_DECL (*tp
));
11615 else if (tree spec
= retrieve_local_specialization (*tp
))
11617 if (data
.internal
.contains (*tp
))
11618 /* Don't mess with variables declared within the pattern. */
11620 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11622 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11623 tree args
= ARGUMENT_PACK_ARGS (spec
);
11624 if (TREE_VEC_LENGTH (args
) == 1)
11626 tree elt
= TREE_VEC_ELT (args
, 0);
11627 if (PACK_EXPANSION_P (elt
))
11628 elt
= PACK_EXPANSION_PATTERN (elt
);
11629 if (DECL_PACK_P (elt
))
11632 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11634 /* Handle lambda capture here, since we aren't doing any
11635 substitution now, and so tsubst_copy won't call
11636 process_outer_var_ref. */
11637 tree args
= ARGUMENT_PACK_ARGS (spec
);
11638 int len
= TREE_VEC_LENGTH (args
);
11639 for (int i
= 0; i
< len
; ++i
)
11641 tree arg
= TREE_VEC_ELT (args
, i
);
11643 if (outer_automatic_var_p (arg
))
11644 carg
= process_outer_var_ref (arg
, complain
);
11647 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11651 spec
= copy_node (spec
);
11652 args
= copy_node (args
);
11653 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11654 register_local_specialization (spec
, *tp
);
11656 TREE_VEC_ELT (args
, i
) = carg
;
11661 if (outer_automatic_var_p (spec
))
11662 spec
= process_outer_var_ref (spec
, complain
);
11663 *extra
= tree_cons (*tp
, spec
, *extra
);
11668 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11670 el_data
data (complain
);
11671 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
11675 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
11676 for use in PACK_EXPANSION_EXTRA_ARGS. */
11679 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
11682 if (local_specializations
)
11683 if (tree locals
= extract_local_specs (pattern
, complain
))
11684 extra
= tree_cons (NULL_TREE
, extra
, locals
);
11688 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
11689 normal template args to ARGS. */
11692 add_extra_args (tree extra
, tree args
)
11694 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
11696 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
11698 /* The partial instantiation involved local declarations collected in
11699 extract_local_specs; map from the general template to our local
11701 tree gen
= TREE_PURPOSE (elt
);
11702 tree inst
= TREE_VALUE (elt
);
11704 if (tree local
= retrieve_local_specialization (inst
))
11706 /* else inst is already a full instantiation of the pack. */
11707 register_local_specialization (inst
, gen
);
11709 gcc_assert (!TREE_PURPOSE (extra
));
11710 extra
= TREE_VALUE (extra
);
11712 return add_to_template_args (extra
, args
);
11715 /* Substitute ARGS into T, which is an pack expansion
11716 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11717 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11718 (if only a partial substitution could be performed) or
11719 ERROR_MARK_NODE if there was an error. */
11721 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11725 tree pack
, packs
= NULL_TREE
;
11726 bool unsubstituted_packs
= false;
11727 bool unsubstituted_fn_pack
= false;
11730 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11731 bool need_local_specializations
= false;
11734 gcc_assert (PACK_EXPANSION_P (t
));
11735 pattern
= PACK_EXPANSION_PATTERN (t
);
11737 /* Add in any args remembered from an earlier partial instantiation. */
11738 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11740 levels
= TMPL_ARGS_DEPTH (args
);
11742 /* Determine the argument packs that will instantiate the parameter
11743 packs used in the expansion expression. While we're at it,
11744 compute the number of arguments to be expanded and make sure it
11746 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11747 pack
= TREE_CHAIN (pack
))
11749 tree parm_pack
= TREE_VALUE (pack
);
11750 tree arg_pack
= NULL_TREE
;
11751 tree orig_arg
= NULL_TREE
;
11754 if (TREE_CODE (parm_pack
) == BASES
)
11756 gcc_assert (parm_pack
== pattern
);
11757 if (BASES_DIRECT (parm_pack
))
11758 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11763 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11764 args
, complain
, in_decl
,
11767 else if (builtin_pack_call_p (parm_pack
))
11769 /* ??? Support use in other patterns. */
11770 gcc_assert (parm_pack
== pattern
);
11771 return expand_builtin_pack_call (parm_pack
, args
,
11772 complain
, in_decl
);
11774 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11776 /* We know we have correct local_specializations if this
11777 expansion is at function scope, or if we're dealing with a
11778 local parameter in a requires expression; for the latter,
11779 tsubst_requires_expr set it up appropriately. */
11780 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11781 arg_pack
= retrieve_local_specialization (parm_pack
);
11783 /* We can't rely on local_specializations for a parameter
11784 name used later in a function declaration (such as in a
11785 late-specified return type). Even if it exists, it might
11786 have the wrong value for a recursive call. */
11787 need_local_specializations
= true;
11791 /* This parameter pack was used in an unevaluated context. Just
11792 make a dummy decl, since it's only used for its type. */
11793 ++cp_unevaluated_operand
;
11794 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11795 --cp_unevaluated_operand
;
11796 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11797 /* Partial instantiation of the parm_pack, we can't build
11798 up an argument pack yet. */
11799 arg_pack
= NULL_TREE
;
11801 arg_pack
= make_fnparm_pack (arg_pack
);
11803 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11804 /* This argument pack isn't fully instantiated yet. We set this
11805 flag rather than clear arg_pack because we do want to do the
11806 optimization below, and we don't want to substitute directly
11807 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11808 where it isn't expected). */
11809 unsubstituted_fn_pack
= true;
11811 else if (is_normal_capture_proxy (parm_pack
))
11813 arg_pack
= retrieve_local_specialization (parm_pack
);
11814 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11815 unsubstituted_fn_pack
= true;
11820 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11822 if (level
<= levels
)
11823 arg_pack
= TMPL_ARG (args
, level
, idx
);
11826 orig_arg
= arg_pack
;
11827 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11828 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11830 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11831 /* This can only happen if we forget to expand an argument
11832 pack somewhere else. Just return an error, silently. */
11834 result
= make_tree_vec (1);
11835 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11842 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11844 /* Don't bother trying to do a partial substitution with
11845 incomplete packs; we'll try again after deduction. */
11846 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11851 else if (len
!= my_len
11852 && !unsubstituted_fn_pack
)
11854 if (!(complain
& tf_error
))
11855 /* Fail quietly. */;
11856 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11857 error ("mismatched argument pack lengths while expanding %qT",
11860 error ("mismatched argument pack lengths while expanding %qE",
11862 return error_mark_node
;
11865 /* Keep track of the parameter packs and their corresponding
11867 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11868 TREE_TYPE (packs
) = orig_arg
;
11872 /* We can't substitute for this parameter pack. We use a flag as
11873 well as the missing_level counter because function parameter
11874 packs don't have a level. */
11875 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
11876 unsubstituted_packs
= true;
11880 /* If the expansion is just T..., return the matching argument pack, unless
11881 we need to call convert_from_reference on all the elements. This is an
11882 important optimization; see c++/68422. */
11883 if (!unsubstituted_packs
11884 && TREE_PURPOSE (packs
) == pattern
)
11886 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11888 /* If the argument pack is a single pack expansion, pull it out. */
11889 if (TREE_VEC_LENGTH (args
) == 1
11890 && pack_expansion_args_count (args
))
11891 return TREE_VEC_ELT (args
, 0);
11893 /* Types need no adjustment, nor does sizeof..., and if we still have
11894 some pack expansion args we won't do anything yet. */
11895 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11896 || PACK_EXPANSION_SIZEOF_P (t
)
11897 || pack_expansion_args_count (args
))
11899 /* Also optimize expression pack expansions if we can tell that the
11900 elements won't have reference type. */
11901 tree type
= TREE_TYPE (pattern
);
11902 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11903 && !PACK_EXPANSION_P (type
)
11904 && !WILDCARD_TYPE_P (type
))
11906 /* Otherwise use the normal path so we get convert_from_reference. */
11909 /* We cannot expand this expansion expression, because we don't have
11910 all of the argument packs we need. */
11911 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11913 /* We got some full packs, but we can't substitute them in until we
11914 have values for all the packs. So remember these until then. */
11916 t
= make_pack_expansion (pattern
, complain
);
11917 PACK_EXPANSION_EXTRA_ARGS (t
)
11918 = build_extra_args (pattern
, args
, complain
);
11921 else if (unsubstituted_packs
)
11923 /* There were no real arguments, we're just replacing a parameter
11924 pack with another version of itself. Substitute into the
11925 pattern and return a PACK_EXPANSION_*. The caller will need to
11927 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11928 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11929 /*integral_constant_expression_p=*/false);
11931 t
= tsubst (pattern
, args
, complain
, in_decl
);
11932 t
= make_pack_expansion (t
, complain
);
11936 gcc_assert (len
>= 0);
11938 if (need_local_specializations
)
11940 /* We're in a late-specified return type, so create our own local
11941 specializations map; the current map is either NULL or (in the
11942 case of recursive unification) might have bindings that we don't
11943 want to use or alter. */
11944 saved_local_specializations
= local_specializations
;
11945 local_specializations
= new hash_map
<tree
, tree
>;
11948 /* For each argument in each argument pack, substitute into the
11950 result
= make_tree_vec (len
);
11951 tree elem_args
= copy_template_args (args
);
11952 for (i
= 0; i
< len
; ++i
)
11954 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11956 elem_args
, complain
,
11958 TREE_VEC_ELT (result
, i
) = t
;
11959 if (t
== error_mark_node
)
11961 result
= error_mark_node
;
11966 /* Update ARGS to restore the substitution from parameter packs to
11967 their argument packs. */
11968 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11970 tree parm
= TREE_PURPOSE (pack
);
11972 if (TREE_CODE (parm
) == PARM_DECL
11974 || TREE_CODE (parm
) == FIELD_DECL
)
11975 register_local_specialization (TREE_TYPE (pack
), parm
);
11980 if (TREE_VALUE (pack
) == NULL_TREE
)
11983 template_parm_level_and_index (parm
, &level
, &idx
);
11985 /* Update the corresponding argument. */
11986 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11987 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11990 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11994 if (need_local_specializations
)
11996 delete local_specializations
;
11997 local_specializations
= saved_local_specializations
;
12000 /* If the dependent pack arguments were such that we end up with only a
12001 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12002 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12003 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12004 return TREE_VEC_ELT (result
, 0);
12009 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12010 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12011 parameter packs; all parms generated from a function parameter pack will
12012 have the same DECL_PARM_INDEX. */
12015 get_pattern_parm (tree parm
, tree tmpl
)
12017 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12020 if (DECL_ARTIFICIAL (parm
))
12022 for (patparm
= DECL_ARGUMENTS (pattern
);
12023 patparm
; patparm
= DECL_CHAIN (patparm
))
12024 if (DECL_ARTIFICIAL (patparm
)
12025 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12030 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12031 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12032 gcc_assert (DECL_PARM_INDEX (patparm
)
12033 == DECL_PARM_INDEX (parm
));
12039 /* Make an argument pack out of the TREE_VEC VEC. */
12042 make_argument_pack (tree vec
)
12045 tree elt
= TREE_VEC_ELT (vec
, 0);
12047 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12050 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12051 TREE_CONSTANT (pack
) = 1;
12053 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12057 /* Return an exact copy of template args T that can be modified
12061 copy_template_args (tree t
)
12063 if (t
== error_mark_node
)
12066 int len
= TREE_VEC_LENGTH (t
);
12067 tree new_vec
= make_tree_vec (len
);
12069 for (int i
= 0; i
< len
; ++i
)
12071 tree elt
= TREE_VEC_ELT (t
, i
);
12072 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12073 elt
= copy_template_args (elt
);
12074 TREE_VEC_ELT (new_vec
, i
) = elt
;
12077 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12078 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12083 /* Substitute ARGS into the vector or list of template arguments T. */
12086 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12089 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12092 if (t
== error_mark_node
)
12093 return error_mark_node
;
12095 len
= TREE_VEC_LENGTH (t
);
12096 elts
= XALLOCAVEC (tree
, len
);
12098 for (i
= 0; i
< len
; i
++)
12100 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12103 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12104 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12105 else if (PACK_EXPANSION_P (orig_arg
))
12107 /* Substitute into an expansion expression. */
12108 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12110 if (TREE_CODE (new_arg
) == TREE_VEC
)
12111 /* Add to the expanded length adjustment the number of
12112 expanded arguments. We subtract one from this
12113 measurement, because the argument pack expression
12114 itself is already counted as 1 in
12115 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12116 the argument pack is empty. */
12117 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12119 else if (ARGUMENT_PACK_P (orig_arg
))
12121 /* Substitute into each of the arguments. */
12122 new_arg
= TYPE_P (orig_arg
)
12123 ? cxx_make_type (TREE_CODE (orig_arg
))
12124 : make_node (TREE_CODE (orig_arg
));
12126 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12127 args
, complain
, in_decl
);
12128 if (pack_args
== error_mark_node
)
12129 new_arg
= error_mark_node
;
12131 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12133 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12134 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12137 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12139 if (new_arg
== error_mark_node
)
12140 return error_mark_node
;
12143 if (new_arg
!= orig_arg
)
12150 /* Make space for the expanded arguments coming from template
12152 t
= make_tree_vec (len
+ expanded_len_adjust
);
12153 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12154 arguments for a member template.
12155 In that case each TREE_VEC in ORIG_T represents a level of template
12156 arguments, and ORIG_T won't carry any non defaulted argument count.
12157 It will rather be the nested TREE_VECs that will carry one.
12158 In other words, ORIG_T carries a non defaulted argument count only
12159 if it doesn't contain any nested TREE_VEC. */
12160 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12162 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12163 count
+= expanded_len_adjust
;
12164 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12166 for (i
= 0, out
= 0; i
< len
; i
++)
12168 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12169 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12170 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12174 /* Now expand the template argument pack "in place". */
12175 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12176 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12180 TREE_VEC_ELT (t
, out
) = elts
[i
];
12188 /* Substitute ARGS into one level PARMS of template parameters. */
12191 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12193 if (parms
== error_mark_node
)
12194 return error_mark_node
;
12196 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12198 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12200 tree tuple
= TREE_VEC_ELT (parms
, i
);
12202 if (tuple
== error_mark_node
)
12205 TREE_VEC_ELT (new_vec
, i
) =
12206 tsubst_template_parm (tuple
, args
, complain
);
12212 /* Return the result of substituting ARGS into the template parameters
12213 given by PARMS. If there are m levels of ARGS and m + n levels of
12214 PARMS, then the result will contain n levels of PARMS. For
12215 example, if PARMS is `template <class T> template <class U>
12216 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12217 result will be `template <int*, double, class V>'. */
12220 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12222 tree r
= NULL_TREE
;
12225 /* When substituting into a template, we must set
12226 PROCESSING_TEMPLATE_DECL as the template parameters may be
12227 dependent if they are based on one-another, and the dependency
12228 predicates are short-circuit outside of templates. */
12229 ++processing_template_decl
;
12231 for (new_parms
= &r
;
12232 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12233 new_parms
= &(TREE_CHAIN (*new_parms
)),
12234 parms
= TREE_CHAIN (parms
))
12236 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12239 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12240 - TMPL_ARGS_DEPTH (args
)),
12241 new_vec
, NULL_TREE
);
12244 --processing_template_decl
;
12249 /* Return the result of substituting ARGS into one template parameter
12250 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12251 parameter and which TREE_PURPOSE is the default argument of the
12252 template parameter. */
12255 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12257 tree default_value
, parm_decl
;
12259 if (args
== NULL_TREE
12261 || t
== error_mark_node
)
12264 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12266 default_value
= TREE_PURPOSE (t
);
12267 parm_decl
= TREE_VALUE (t
);
12269 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12270 if (TREE_CODE (parm_decl
) == PARM_DECL
12271 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12272 parm_decl
= error_mark_node
;
12273 default_value
= tsubst_template_arg (default_value
, args
,
12274 complain
, NULL_TREE
);
12276 return build_tree_list (default_value
, parm_decl
);
12279 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12280 type T. If T is not an aggregate or enumeration type, it is
12281 handled as if by tsubst. IN_DECL is as for tsubst. If
12282 ENTERING_SCOPE is nonzero, T is the context for a template which
12283 we are presently tsubst'ing. Return the substituted value. */
12286 tsubst_aggr_type (tree t
,
12288 tsubst_flags_t complain
,
12290 int entering_scope
)
12292 if (t
== NULL_TREE
)
12295 switch (TREE_CODE (t
))
12298 if (TYPE_PTRMEMFUNC_P (t
))
12299 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12301 /* Fall through. */
12302 case ENUMERAL_TYPE
:
12304 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12309 int saved_unevaluated_operand
;
12310 int saved_inhibit_evaluation_warnings
;
12312 /* In "sizeof(X<I>)" we need to evaluate "I". */
12313 saved_unevaluated_operand
= cp_unevaluated_operand
;
12314 cp_unevaluated_operand
= 0;
12315 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12316 c_inhibit_evaluation_warnings
= 0;
12318 /* First, determine the context for the type we are looking
12320 context
= TYPE_CONTEXT (t
);
12321 if (context
&& TYPE_P (context
))
12323 context
= tsubst_aggr_type (context
, args
, complain
,
12324 in_decl
, /*entering_scope=*/1);
12325 /* If context is a nested class inside a class template,
12326 it may still need to be instantiated (c++/33959). */
12327 context
= complete_type (context
);
12330 /* Then, figure out what arguments are appropriate for the
12331 type we are trying to find. For example, given:
12333 template <class T> struct S;
12334 template <class T, class U> void f(T, U) { S<U> su; }
12336 and supposing that we are instantiating f<int, double>,
12337 then our ARGS will be {int, double}, but, when looking up
12338 S we only want {double}. */
12339 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12340 complain
, in_decl
);
12341 if (argvec
== error_mark_node
)
12342 r
= error_mark_node
;
12345 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12346 entering_scope
, complain
);
12347 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12350 cp_unevaluated_operand
= saved_unevaluated_operand
;
12351 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12356 /* This is not a template type, so there's nothing to do. */
12360 return tsubst (t
, args
, complain
, in_decl
);
12364 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12366 /* Substitute into the default argument ARG (a default argument for
12367 FN), which has the indicated TYPE. */
12370 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12371 tsubst_flags_t complain
)
12373 tree saved_class_ptr
= NULL_TREE
;
12374 tree saved_class_ref
= NULL_TREE
;
12375 int errs
= errorcount
+ sorrycount
;
12377 /* This can happen in invalid code. */
12378 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12381 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12382 parm
= chain_index (parmnum
, parm
);
12383 tree parmtype
= TREE_TYPE (parm
);
12384 if (DECL_BY_REFERENCE (parm
))
12385 parmtype
= TREE_TYPE (parmtype
);
12386 if (parmtype
== error_mark_node
)
12387 return error_mark_node
;
12389 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12392 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12395 /* This default argument came from a template. Instantiate the
12396 default argument here, not in tsubst. In the case of
12405 we must be careful to do name lookup in the scope of S<T>,
12406 rather than in the current class. */
12407 push_access_scope (fn
);
12408 /* The "this" pointer is not valid in a default argument. */
12411 saved_class_ptr
= current_class_ptr
;
12412 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12413 saved_class_ref
= current_class_ref
;
12414 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12417 start_lambda_scope (parm
);
12419 push_deferring_access_checks(dk_no_deferred
);
12420 /* The default argument expression may cause implicitly defined
12421 member functions to be synthesized, which will result in garbage
12422 collection. We must treat this situation as if we were within
12423 the body of function so as to avoid collecting live data on the
12426 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12427 complain
, NULL_TREE
,
12428 /*integral_constant_expression_p=*/false);
12430 pop_deferring_access_checks();
12432 finish_lambda_scope ();
12434 /* Restore the "this" pointer. */
12437 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12438 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12441 if (errorcount
+sorrycount
> errs
12442 && (complain
& tf_warning_or_error
))
12443 inform (input_location
,
12444 " when instantiating default argument for call to %qD", fn
);
12446 /* Make sure the default argument is reasonable. */
12447 arg
= check_default_argument (type
, arg
, complain
);
12449 pop_access_scope (fn
);
12451 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12454 defarg_inst
= tree_cache_map::create_ggc (37);
12455 defarg_inst
->put (parm
, arg
);
12461 /* Substitute into all the default arguments for FN. */
12464 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12469 tmpl_args
= DECL_TI_ARGS (fn
);
12471 /* If this function is not yet instantiated, we certainly don't need
12472 its default arguments. */
12473 if (uses_template_parms (tmpl_args
))
12475 /* Don't do this again for clones. */
12476 if (DECL_CLONED_FUNCTION_P (fn
))
12480 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12482 arg
= TREE_CHAIN (arg
), ++i
)
12483 if (TREE_PURPOSE (arg
))
12484 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12486 TREE_PURPOSE (arg
),
12490 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12493 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12494 tree lambda_fntype
)
12496 tree gen_tmpl
, argvec
;
12497 hashval_t hash
= 0;
12500 /* Nobody should be tsubst'ing into non-template functions. */
12501 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12503 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12505 /* If T is not dependent, just return it. */
12506 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12509 /* Calculate the most general template of which R is a
12511 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12513 /* We're substituting a lambda function under tsubst_lambda_expr but not
12514 directly from it; find the matching function we're already inside.
12515 But don't do this if T is a generic lambda with a single level of
12516 template parms, as in that case we're doing a normal instantiation. */
12517 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12518 && (!generic_lambda_fn_p (t
)
12519 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12520 return enclosing_instantiation_of (t
);
12522 /* Calculate the complete set of arguments used to
12524 argvec
= tsubst_template_args (DECL_TI_ARGS
12525 (DECL_TEMPLATE_RESULT
12526 (DECL_TI_TEMPLATE (t
))),
12527 args
, complain
, in_decl
);
12528 if (argvec
== error_mark_node
)
12529 return error_mark_node
;
12531 /* Check to see if we already have this specialization. */
12532 if (!lambda_fntype
)
12534 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12535 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12539 /* We can see more levels of arguments than parameters if
12540 there was a specialization of a member template, like
12543 template <class T> struct S { template <class U> void f(); }
12544 template <> template <class U> void S<int>::f(U);
12546 Here, we'll be substituting into the specialization,
12547 because that's where we can find the code we actually
12548 want to generate, but we'll have enough arguments for
12549 the most general template.
12551 We also deal with the peculiar case:
12553 template <class T> struct S {
12554 template <class U> friend void f();
12556 template <class U> void f() {}
12558 template void f<double>();
12560 Here, the ARGS for the instantiation of will be {int,
12561 double}. But, we only need as many ARGS as there are
12562 levels of template parameters in CODE_PATTERN. We are
12563 careful not to get fooled into reducing the ARGS in
12566 template <class T> struct S { template <class U> void f(U); }
12567 template <class T> template <> void S<T>::f(int) {}
12569 which we can spot because the pattern will be a
12570 specialization in this case. */
12571 int args_depth
= TMPL_ARGS_DEPTH (args
);
12573 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12575 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12576 args
= get_innermost_template_args (args
, parms_depth
);
12580 /* This special case arises when we have something like this:
12582 template <class T> struct S {
12583 friend void f<int>(int, double);
12586 Here, the DECL_TI_TEMPLATE for the friend declaration
12587 will be an IDENTIFIER_NODE. We are being called from
12588 tsubst_friend_function, and we want only to create a
12589 new decl (R) with appropriate types so that we can call
12590 determine_specialization. */
12591 gen_tmpl
= NULL_TREE
;
12592 argvec
= NULL_TREE
;
12595 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12597 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12598 bool member
= ctx
&& TYPE_P (ctx
);
12600 if (member
&& !closure
)
12601 ctx
= tsubst_aggr_type (ctx
, args
,
12602 complain
, t
, /*entering_scope=*/1);
12604 tree type
= (lambda_fntype
? lambda_fntype
12605 : tsubst (TREE_TYPE (t
), args
,
12606 complain
| tf_fndecl_type
, in_decl
));
12607 if (type
== error_mark_node
)
12608 return error_mark_node
;
12610 /* If we hit excessive deduction depth, the type is bogus even if
12611 it isn't error_mark_node, so don't build a decl. */
12612 if (excessive_deduction_depth
)
12613 return error_mark_node
;
12615 /* We do NOT check for matching decls pushed separately at this
12616 point, as they may not represent instantiations of this
12617 template, and in any case are considered separate under the
12619 tree r
= copy_decl (t
);
12620 DECL_USE_TEMPLATE (r
) = 0;
12621 TREE_TYPE (r
) = type
;
12622 /* Clear out the mangled name and RTL for the instantiation. */
12623 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12624 SET_DECL_RTL (r
, NULL
);
12625 /* Leave DECL_INITIAL set on deleted instantiations. */
12626 if (!DECL_DELETED_FN (r
))
12627 DECL_INITIAL (r
) = NULL_TREE
;
12628 DECL_CONTEXT (r
) = ctx
;
12630 /* OpenMP UDRs have the only argument a reference to the declared
12631 type. We want to diagnose if the declared type is a reference,
12632 which is invalid, but as references to references are usually
12633 quietly merged, diagnose it here. */
12634 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12637 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12638 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12639 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12640 error_at (DECL_SOURCE_LOCATION (t
),
12641 "reference type %qT in "
12642 "%<#pragma omp declare reduction%>", argtype
);
12643 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12644 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12648 if (member
&& DECL_CONV_FN_P (r
))
12649 /* Type-conversion operator. Reconstruct the name, in
12650 case it's the name of one of the template's parameters. */
12651 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12653 tree parms
= DECL_ARGUMENTS (t
);
12655 parms
= DECL_CHAIN (parms
);
12656 parms
= tsubst (parms
, args
, complain
, t
);
12657 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12658 DECL_CONTEXT (parm
) = r
;
12661 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12662 DECL_CHAIN (tparm
) = parms
;
12665 DECL_ARGUMENTS (r
) = parms
;
12666 DECL_RESULT (r
) = NULL_TREE
;
12668 TREE_STATIC (r
) = 0;
12669 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12670 DECL_EXTERNAL (r
) = 1;
12671 /* If this is an instantiation of a function with internal
12672 linkage, we already know what object file linkage will be
12673 assigned to the instantiation. */
12674 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12675 DECL_DEFER_OUTPUT (r
) = 0;
12676 DECL_CHAIN (r
) = NULL_TREE
;
12677 DECL_PENDING_INLINE_INFO (r
) = 0;
12678 DECL_PENDING_INLINE_P (r
) = 0;
12679 DECL_SAVED_TREE (r
) = NULL_TREE
;
12680 DECL_STRUCT_FUNCTION (r
) = NULL
;
12682 /* We'll re-clone as appropriate in instantiate_template. */
12683 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12685 /* If we aren't complaining now, return on error before we register
12686 the specialization so that we'll complain eventually. */
12687 if ((complain
& tf_error
) == 0
12688 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12689 && !grok_op_properties (r
, /*complain=*/false))
12690 return error_mark_node
;
12692 /* When instantiating a constrained member, substitute
12693 into the constraints to create a new constraint. */
12694 if (tree ci
= get_constraints (t
))
12697 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12698 set_constraints (r
, ci
);
12701 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12702 this in the special friend case mentioned above where
12703 GEN_TMPL is NULL. */
12704 if (gen_tmpl
&& !closure
)
12706 DECL_TEMPLATE_INFO (r
)
12707 = build_template_info (gen_tmpl
, argvec
);
12708 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12711 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12713 /* We instantiated this while substituting into
12714 the type earlier (template/friend54.C). */
12717 /* We're not supposed to instantiate default arguments
12718 until they are called, for a template. But, for a
12721 template <class T> void f ()
12722 { extern void g(int i = T()); }
12724 we should do the substitution when the template is
12725 instantiated. We handle the member function case in
12726 instantiate_class_template since the default arguments
12727 might refer to other members of the class. */
12729 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12730 && !uses_template_parms (argvec
))
12731 tsubst_default_arguments (r
, complain
);
12734 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12736 /* Copy the list of befriending classes. */
12737 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12739 friends
= &TREE_CHAIN (*friends
))
12741 *friends
= copy_node (*friends
);
12742 TREE_VALUE (*friends
)
12743 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12746 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12748 maybe_retrofit_in_chrg (r
);
12749 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12750 return error_mark_node
;
12751 /* If this is an instantiation of a member template, clone it.
12752 If it isn't, that'll be handled by
12753 clone_constructors_and_destructors. */
12754 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12755 clone_function_decl (r
, /*update_methods=*/false);
12757 else if ((complain
& tf_error
) != 0
12758 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12759 && !grok_op_properties (r
, /*complain=*/true))
12760 return error_mark_node
;
12762 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12763 SET_DECL_FRIEND_CONTEXT (r
,
12764 tsubst (DECL_FRIEND_CONTEXT (t
),
12765 args
, complain
, in_decl
));
12767 /* Possibly limit visibility based on template args. */
12768 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12769 if (DECL_VISIBILITY_SPECIFIED (t
))
12771 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12772 DECL_ATTRIBUTES (r
)
12773 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12775 determine_visibility (r
);
12776 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12777 && !processing_template_decl
)
12778 defaulted_late_check (r
);
12780 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12781 args
, complain
, in_decl
);
12785 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12788 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12789 tree lambda_fntype
)
12791 /* We can get here when processing a member function template,
12792 member class template, or template template parameter. */
12793 tree decl
= DECL_TEMPLATE_RESULT (t
);
12799 hashval_t hash
= 0;
12801 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12803 /* Template template parameter is treated here. */
12804 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12805 if (new_type
== error_mark_node
)
12806 r
= error_mark_node
;
12807 /* If we get a real template back, return it. This can happen in
12808 the context of most_specialized_partial_spec. */
12809 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12812 /* The new TEMPLATE_DECL was built in
12813 reduce_template_parm_level. */
12814 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12818 if (!lambda_fntype
)
12820 /* We might already have an instance of this template.
12821 The ARGS are for the surrounding class type, so the
12822 full args contain the tsubst'd args for the context,
12823 plus the innermost args from the template decl. */
12824 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12825 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12826 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12827 /* Because this is a template, the arguments will still be
12828 dependent, even after substitution. If
12829 PROCESSING_TEMPLATE_DECL is not set, the dependency
12830 predicates will short-circuit. */
12831 ++processing_template_decl
;
12832 full_args
= tsubst_template_args (tmpl_args
, args
,
12833 complain
, in_decl
);
12834 --processing_template_decl
;
12835 if (full_args
== error_mark_node
)
12836 return error_mark_node
;
12838 /* If this is a default template template argument,
12839 tsubst might not have changed anything. */
12840 if (full_args
== tmpl_args
)
12843 hash
= hash_tmpl_and_args (t
, full_args
);
12844 spec
= retrieve_specialization (t
, full_args
, hash
);
12845 if (spec
!= NULL_TREE
)
12849 /* Make a new template decl. It will be similar to the
12850 original, but will record the current template arguments.
12851 We also create a new function declaration, which is just
12852 like the old one, but points to this new template, rather
12853 than the old one. */
12855 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12856 DECL_CHAIN (r
) = NULL_TREE
;
12858 // Build new template info linking to the original template decl.
12859 if (!lambda_fntype
)
12861 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12862 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12865 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12867 /* The template parameters for this new template are all the
12868 template parameters for the old template, except the
12869 outermost level of parameters. */
12870 DECL_TEMPLATE_PARMS (r
)
12871 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12874 if (TREE_CODE (decl
) == TYPE_DECL
12875 && !TYPE_DECL_ALIAS_P (decl
))
12878 ++processing_template_decl
;
12879 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12880 --processing_template_decl
;
12881 if (new_type
== error_mark_node
)
12882 return error_mark_node
;
12884 TREE_TYPE (r
) = new_type
;
12885 /* For a partial specialization, we need to keep pointing to
12886 the primary template. */
12887 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12888 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12889 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12890 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12891 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12896 ++processing_template_decl
;
12897 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12898 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12900 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12901 --processing_template_decl
;
12902 if (new_decl
== error_mark_node
)
12903 return error_mark_node
;
12905 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12906 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12907 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12910 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12911 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12915 DECL_TI_TEMPLATE (new_decl
) = r
;
12916 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12920 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12921 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12923 if (PRIMARY_TEMPLATE_P (t
))
12924 DECL_PRIMARY_TEMPLATE (r
) = r
;
12926 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12928 /* Record this non-type partial instantiation. */
12929 register_specialization (r
, t
,
12930 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12936 /* True if FN is the op() for a lambda in an uninstantiated template. */
12939 lambda_fn_in_template_p (tree fn
)
12941 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12943 tree closure
= DECL_CONTEXT (fn
);
12944 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12947 /* We're instantiating a variable from template function TCTX. Return the
12948 corresponding current enclosing scope. This gets complicated because lambda
12949 functions in templates are regenerated rather than instantiated, but generic
12950 lambda functions are subsequently instantiated. */
12953 enclosing_instantiation_of (tree otctx
)
12956 tree fn
= current_function_decl
;
12957 int lambda_count
= 0;
12959 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12960 tctx
= decl_function_context (tctx
))
12962 for (; fn
; fn
= decl_function_context (fn
))
12965 int flambda_count
= 0;
12966 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12967 fn
= decl_function_context (fn
))
12969 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
12970 ? most_general_template (fn
) != most_general_template (tctx
)
12973 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12974 || DECL_CONV_FN_P (ofn
));
12977 gcc_unreachable ();
12980 /* Substitute the ARGS into the T, which is a _DECL. Return the
12981 result of the substitution. Issue error and warning messages under
12982 control of COMPLAIN. */
12985 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12987 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12988 location_t saved_loc
;
12989 tree r
= NULL_TREE
;
12991 hashval_t hash
= 0;
12993 /* Set the filename and linenumber to improve error-reporting. */
12994 saved_loc
= input_location
;
12995 input_location
= DECL_SOURCE_LOCATION (t
);
12997 switch (TREE_CODE (t
))
12999 case TEMPLATE_DECL
:
13000 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13003 case FUNCTION_DECL
:
13004 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13009 tree type
= NULL_TREE
;
13011 tree expanded_types
= NULL_TREE
;
13012 tree prev_r
= NULL_TREE
;
13013 tree first_r
= NULL_TREE
;
13015 if (DECL_PACK_P (t
))
13017 /* If there is a local specialization that isn't a
13018 parameter pack, it means that we're doing a "simple"
13019 substitution from inside tsubst_pack_expansion. Just
13020 return the local specialization (which will be a single
13022 tree spec
= retrieve_local_specialization (t
);
13024 && TREE_CODE (spec
) == PARM_DECL
13025 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13028 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13029 the parameters in this function parameter pack. */
13030 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13031 complain
, in_decl
);
13032 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13034 len
= TREE_VEC_LENGTH (expanded_types
);
13036 /* Zero-length parameter packs are boring. Just substitute
13039 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13044 /* All we did was update the type. Make a note of that. */
13045 type
= expanded_types
;
13046 expanded_types
= NULL_TREE
;
13050 /* Loop through all of the parameters we'll build. When T is
13051 a function parameter pack, LEN is the number of expanded
13052 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13054 for (i
= 0; i
< len
; ++i
)
13058 if (DECL_TEMPLATE_PARM_P (t
))
13059 SET_DECL_TEMPLATE_PARM_P (r
);
13061 if (expanded_types
)
13062 /* We're on the Ith parameter of the function parameter
13065 /* Get the Ith type. */
13066 type
= TREE_VEC_ELT (expanded_types
, i
);
13068 /* Rename the parameter to include the index. */
13070 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13073 /* We're dealing with a normal parameter. */
13074 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13076 type
= type_decays_to (type
);
13077 TREE_TYPE (r
) = type
;
13078 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13080 if (DECL_INITIAL (r
))
13082 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13083 DECL_INITIAL (r
) = TREE_TYPE (r
);
13085 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13086 complain
, in_decl
);
13089 DECL_CONTEXT (r
) = NULL_TREE
;
13091 if (!DECL_TEMPLATE_PARM_P (r
))
13092 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13094 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13095 args
, complain
, in_decl
);
13097 /* Keep track of the first new parameter we
13098 generate. That's what will be returned to the
13103 /* Build a proper chain of parameters when substituting
13104 into a function parameter pack. */
13106 DECL_CHAIN (prev_r
) = r
;
13109 /* If cp_unevaluated_operand is set, we're just looking for a
13110 single dummy parameter, so don't keep going. */
13111 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13112 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13113 complain
, DECL_CHAIN (t
));
13115 /* FIRST_R contains the start of the chain we've built. */
13122 tree type
= NULL_TREE
;
13123 tree vec
= NULL_TREE
;
13124 tree expanded_types
= NULL_TREE
;
13127 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13129 /* This field is a lambda capture pack. Return a TREE_VEC of
13130 the expanded fields to instantiate_class_template_1. */
13131 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13132 complain
, in_decl
);
13133 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13135 len
= TREE_VEC_LENGTH (expanded_types
);
13136 vec
= make_tree_vec (len
);
13140 /* All we did was update the type. Make a note of that. */
13141 type
= expanded_types
;
13142 expanded_types
= NULL_TREE
;
13146 for (int i
= 0; i
< len
; ++i
)
13149 if (expanded_types
)
13151 type
= TREE_VEC_ELT (expanded_types
, i
);
13153 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13156 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13158 if (type
== error_mark_node
)
13159 RETURN (error_mark_node
);
13160 TREE_TYPE (r
) = type
;
13161 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13163 if (DECL_C_BIT_FIELD (r
))
13164 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13166 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13167 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13169 /*integral_constant_expression_p=*/true);
13170 if (DECL_INITIAL (t
))
13172 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13173 NSDMI in perform_member_init. Still set DECL_INITIAL
13174 so that we know there is one. */
13175 DECL_INITIAL (r
) = void_node
;
13176 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13177 retrofit_lang_decl (r
);
13178 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13180 /* We don't have to set DECL_CONTEXT here; it is set by
13181 finish_member_declaration. */
13182 DECL_CHAIN (r
) = NULL_TREE
;
13184 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13185 args
, complain
, in_decl
);
13188 TREE_VEC_ELT (vec
, i
) = r
;
13197 /* We reach here only for member using decls. We also need to check
13198 uses_template_parms because DECL_DEPENDENT_P is not set for a
13199 using-declaration that designates a member of the current
13200 instantiation (c++/53549). */
13201 if (DECL_DEPENDENT_P (t
)
13202 || uses_template_parms (USING_DECL_SCOPE (t
)))
13204 tree scope
= USING_DECL_SCOPE (t
);
13205 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13206 if (PACK_EXPANSION_P (scope
))
13208 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13209 int len
= TREE_VEC_LENGTH (vec
);
13210 r
= make_tree_vec (len
);
13211 for (int i
= 0; i
< len
; ++i
)
13213 tree escope
= TREE_VEC_ELT (vec
, i
);
13214 tree elt
= do_class_using_decl (escope
, name
);
13217 r
= error_mark_node
;
13222 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13223 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13225 TREE_VEC_ELT (r
, i
) = elt
;
13230 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13231 complain
, in_decl
);
13232 r
= do_class_using_decl (inst_scope
, name
);
13234 r
= error_mark_node
;
13237 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13238 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13245 DECL_CHAIN (r
) = NULL_TREE
;
13252 tree argvec
= NULL_TREE
;
13253 tree gen_tmpl
= NULL_TREE
;
13255 tree tmpl
= NULL_TREE
;
13257 tree type
= NULL_TREE
;
13260 if (TREE_TYPE (t
) == error_mark_node
)
13261 RETURN (error_mark_node
);
13263 if (TREE_CODE (t
) == TYPE_DECL
13264 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13266 /* If this is the canonical decl, we don't have to
13267 mess with instantiations, and often we can't (for
13268 typename, template type parms and such). Note that
13269 TYPE_NAME is not correct for the above test if
13270 we've copied the type for a typedef. */
13271 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13272 if (type
== error_mark_node
)
13273 RETURN (error_mark_node
);
13274 r
= TYPE_NAME (type
);
13278 /* Check to see if we already have the specialization we
13281 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13283 /* T is a static data member or namespace-scope entity.
13284 We have to substitute into namespace-scope variables
13285 (not just variable templates) because of cases like:
13287 template <class T> void f() { extern T t; }
13289 where the entity referenced is not known until
13290 instantiation time. */
13292 ctx
= DECL_CONTEXT (t
);
13293 if (DECL_CLASS_SCOPE_P (t
))
13295 ctx
= tsubst_aggr_type (ctx
, args
,
13297 in_decl
, /*entering_scope=*/1);
13298 /* If CTX is unchanged, then T is in fact the
13299 specialization we want. That situation occurs when
13300 referencing a static data member within in its own
13301 class. We can use pointer equality, rather than
13302 same_type_p, because DECL_CONTEXT is always
13304 if (ctx
== DECL_CONTEXT (t
)
13305 /* ... unless T is a member template; in which
13306 case our caller can be willing to create a
13307 specialization of that template represented
13309 && !(DECL_TI_TEMPLATE (t
)
13310 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13316 tmpl
= DECL_TI_TEMPLATE (t
);
13317 gen_tmpl
= most_general_template (tmpl
);
13318 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13319 if (argvec
!= error_mark_node
)
13320 argvec
= (coerce_innermost_template_parms
13321 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13322 argvec
, t
, complain
,
13323 /*all*/true, /*defarg*/true));
13324 if (argvec
== error_mark_node
)
13325 RETURN (error_mark_node
);
13326 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13327 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13332 /* A local variable. */
13334 /* Subsequent calls to pushdecl will fill this in. */
13336 /* Unless this is a reference to a static variable from an
13337 enclosing function, in which case we need to fill it in now. */
13338 if (TREE_STATIC (t
))
13340 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13341 if (fn
!= current_function_decl
)
13344 spec
= retrieve_local_specialization (t
);
13346 /* If we already have the specialization we need, there is
13347 nothing more to do. */
13354 /* Create a new node for the specialization we need. */
13356 if (type
== NULL_TREE
)
13358 if (is_typedef_decl (t
))
13359 type
= DECL_ORIGINAL_TYPE (t
);
13361 type
= TREE_TYPE (t
);
13363 && VAR_HAD_UNKNOWN_BOUND (t
)
13364 && type
!= error_mark_node
)
13365 type
= strip_array_domain (type
);
13366 tree sub_args
= args
;
13367 if (tree auto_node
= type_uses_auto (type
))
13369 /* Mask off any template args past the variable's context so we
13370 don't replace the auto with an unrelated argument. */
13371 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13372 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13374 /* This should never happen with the new lambda instantiation
13375 model, but keep the handling just in case. */
13376 gcc_assert (!CHECKING_P
),
13377 sub_args
= strip_innermost_template_args (args
, extra
);
13379 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13383 /* Even if the original location is out of scope, the
13384 newly substituted one is not. */
13385 DECL_DEAD_FOR_LOCAL (r
) = 0;
13386 DECL_INITIALIZED_P (r
) = 0;
13387 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13388 if (type
== error_mark_node
)
13389 RETURN (error_mark_node
);
13390 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13392 /* It may seem that this case cannot occur, since:
13397 declares a function, not a variable. However:
13400 template <typename T> void g() { T t; }
13401 template void g<f>();
13403 is an attempt to declare a variable with function
13405 error ("variable %qD has function type",
13406 /* R is not yet sufficiently initialized, so we
13407 just use its name. */
13409 RETURN (error_mark_node
);
13411 type
= complete_type (type
);
13412 /* Wait until cp_finish_decl to set this again, to handle
13413 circular dependency (template/instantiate6.C). */
13414 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13415 type
= check_var_type (DECL_NAME (r
), type
);
13417 if (DECL_HAS_VALUE_EXPR_P (t
))
13419 tree ve
= DECL_VALUE_EXPR (t
);
13420 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13421 /*constant_expression_p=*/false);
13422 if (REFERENCE_REF_P (ve
))
13424 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13425 ve
= TREE_OPERAND (ve
, 0);
13427 SET_DECL_VALUE_EXPR (r
, ve
);
13429 if (CP_DECL_THREAD_LOCAL_P (r
)
13430 && !processing_template_decl
)
13431 set_decl_tls_model (r
, decl_default_tls_model (r
));
13433 else if (DECL_SELF_REFERENCE_P (t
))
13434 SET_DECL_SELF_REFERENCE_P (r
);
13435 TREE_TYPE (r
) = type
;
13436 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13437 DECL_CONTEXT (r
) = ctx
;
13438 /* Clear out the mangled name and RTL for the instantiation. */
13439 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13440 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13441 SET_DECL_RTL (r
, NULL
);
13442 /* The initializer must not be expanded until it is required;
13443 see [temp.inst]. */
13444 DECL_INITIAL (r
) = NULL_TREE
;
13445 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13448 if (DECL_LANG_SPECIFIC (r
))
13449 SET_DECL_DEPENDENT_INIT_P (r
, false);
13451 SET_DECL_MODE (r
, VOIDmode
);
13453 /* Possibly limit visibility based on template args. */
13454 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13455 if (DECL_VISIBILITY_SPECIFIED (t
))
13457 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13458 DECL_ATTRIBUTES (r
)
13459 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13461 determine_visibility (r
);
13466 /* A static data member declaration is always marked
13467 external when it is declared in-class, even if an
13468 initializer is present. We mimic the non-template
13469 processing here. */
13470 DECL_EXTERNAL (r
) = 1;
13471 if (DECL_NAMESPACE_SCOPE_P (t
))
13472 DECL_NOT_REALLY_EXTERN (r
) = 1;
13474 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13475 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13476 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13480 if (DECL_LANG_SPECIFIC (r
))
13481 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13482 if (!cp_unevaluated_operand
)
13483 register_local_specialization (r
, t
);
13486 DECL_CHAIN (r
) = NULL_TREE
;
13488 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13490 args
, complain
, in_decl
);
13492 /* Preserve a typedef that names a type. */
13493 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13495 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13496 set_underlying_type (r
);
13497 if (TYPE_DECL_ALIAS_P (r
))
13498 /* An alias template specialization can be dependent
13499 even if its underlying type is not. */
13500 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13503 layout_decl (r
, 0);
13508 gcc_unreachable ();
13513 /* Restore the file and line information. */
13514 input_location
= saved_loc
;
13519 /* Substitute into the ARG_TYPES of a function type.
13520 If END is a TREE_CHAIN, leave it and any following types
13524 tsubst_arg_types (tree arg_types
,
13527 tsubst_flags_t complain
,
13530 tree remaining_arg_types
;
13531 tree type
= NULL_TREE
;
13533 tree expanded_args
= NULL_TREE
;
13536 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13539 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13540 args
, end
, complain
, in_decl
);
13541 if (remaining_arg_types
== error_mark_node
)
13542 return error_mark_node
;
13544 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13546 /* For a pack expansion, perform substitution on the
13547 entire expression. Later on, we'll handle the arguments
13549 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13550 args
, complain
, in_decl
);
13552 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13553 /* So that we'll spin through the parameters, one by one. */
13554 i
= TREE_VEC_LENGTH (expanded_args
);
13557 /* We only partially substituted into the parameter
13558 pack. Our type is TYPE_PACK_EXPANSION. */
13559 type
= expanded_args
;
13560 expanded_args
= NULL_TREE
;
13568 type
= TREE_VEC_ELT (expanded_args
, i
);
13570 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13572 if (type
== error_mark_node
)
13573 return error_mark_node
;
13574 if (VOID_TYPE_P (type
))
13576 if (complain
& tf_error
)
13578 error ("invalid parameter type %qT", type
);
13580 error ("in declaration %q+D", in_decl
);
13582 return error_mark_node
;
13585 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13586 return error_mark_node
;
13588 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13589 top-level qualifiers as required. */
13590 type
= cv_unqualified (type_decays_to (type
));
13592 /* We do not substitute into default arguments here. The standard
13593 mandates that they be instantiated only when needed, which is
13594 done in build_over_call. */
13595 default_arg
= TREE_PURPOSE (arg_types
);
13597 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13598 since the new op() won't have any associated template arguments for us
13599 to refer to later. */
13600 if (lambda_fn_in_template_p (in_decl
))
13601 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13602 false/*fn*/, false/*constexpr*/);
13604 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13606 /* We've instantiated a template before its default arguments
13607 have been parsed. This can happen for a nested template
13608 class, and is not an error unless we require the default
13609 argument in a call of this function. */
13610 remaining_arg_types
=
13611 tree_cons (default_arg
, type
, remaining_arg_types
);
13612 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13615 remaining_arg_types
=
13616 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13619 return remaining_arg_types
;
13622 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13623 *not* handle the exception-specification for FNTYPE, because the
13624 initial substitution of explicitly provided template parameters
13625 during argument deduction forbids substitution into the
13626 exception-specification:
13630 All references in the function type of the function template to the
13631 corresponding template parameters are replaced by the specified tem-
13632 plate argument values. If a substitution in a template parameter or
13633 in the function type of the function template results in an invalid
13634 type, type deduction fails. [Note: The equivalent substitution in
13635 exception specifications is done only when the function is instanti-
13636 ated, at which point a program is ill-formed if the substitution
13637 results in an invalid type.] */
13640 tsubst_function_type (tree t
,
13642 tsubst_flags_t complain
,
13646 tree arg_types
= NULL_TREE
;
13649 /* The TYPE_CONTEXT is not used for function/method types. */
13650 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13652 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13654 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13656 if (late_return_type_p
)
13658 /* Substitute the argument types. */
13659 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13660 complain
, in_decl
);
13661 if (arg_types
== error_mark_node
)
13662 return error_mark_node
;
13664 tree save_ccp
= current_class_ptr
;
13665 tree save_ccr
= current_class_ref
;
13666 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13667 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13668 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13671 /* DR 1207: 'this' is in scope in the trailing return type. */
13672 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13675 /* Substitute the return type. */
13676 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13680 current_class_ptr
= save_ccp
;
13681 current_class_ref
= save_ccr
;
13685 /* Substitute the return type. */
13686 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13688 if (return_type
== error_mark_node
)
13689 return error_mark_node
;
13690 /* DR 486 clarifies that creation of a function type with an
13691 invalid return type is a deduction failure. */
13692 if (TREE_CODE (return_type
) == ARRAY_TYPE
13693 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13695 if (complain
& tf_error
)
13697 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13698 error ("function returning an array");
13700 error ("function returning a function");
13702 return error_mark_node
;
13705 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13706 return error_mark_node
;
13708 if (!late_return_type_p
)
13710 /* Substitute the argument types. */
13711 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13712 complain
, in_decl
);
13713 if (arg_types
== error_mark_node
)
13714 return error_mark_node
;
13717 /* Construct a new type node and return it. */
13718 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13720 fntype
= build_function_type (return_type
, arg_types
);
13721 fntype
= apply_memfn_quals (fntype
,
13722 type_memfn_quals (t
),
13723 type_memfn_rqual (t
));
13727 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13728 /* Don't pick up extra function qualifiers from the basetype. */
13729 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13730 if (! MAYBE_CLASS_TYPE_P (r
))
13734 Type deduction may fail for any of the following
13737 -- Attempting to create "pointer to member of T" when T
13738 is not a class type. */
13739 if (complain
& tf_error
)
13740 error ("creating pointer to member function of non-class type %qT",
13742 return error_mark_node
;
13745 fntype
= build_method_type_directly (r
, return_type
,
13746 TREE_CHAIN (arg_types
));
13747 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13749 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13751 if (late_return_type_p
)
13752 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13757 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13758 ARGS into that specification, and return the substituted
13759 specification. If there is no specification, return NULL_TREE. */
13762 tsubst_exception_specification (tree fntype
,
13764 tsubst_flags_t complain
,
13771 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13772 new_specs
= NULL_TREE
;
13773 if (specs
&& TREE_PURPOSE (specs
))
13775 /* A noexcept-specifier. */
13776 tree expr
= TREE_PURPOSE (specs
);
13777 if (TREE_CODE (expr
) == INTEGER_CST
)
13781 /* Defer instantiation of noexcept-specifiers to avoid
13782 excessive instantiations (c++/49107). */
13783 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13784 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13786 /* We already partially instantiated this member template,
13787 so combine the new args with the old. */
13788 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13789 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13790 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13791 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13795 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13796 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13800 new_specs
= tsubst_copy_and_build
13801 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13802 /*integral_constant_expression_p=*/true);
13803 new_specs
= build_noexcept_spec (new_specs
, complain
);
13807 if (! TREE_VALUE (specs
))
13814 tree expanded_specs
= NULL_TREE
;
13816 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13818 /* Expand the pack expansion type. */
13819 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13823 if (expanded_specs
== error_mark_node
)
13824 return error_mark_node
;
13825 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13826 len
= TREE_VEC_LENGTH (expanded_specs
);
13829 /* We're substituting into a member template, so
13830 we got a TYPE_PACK_EXPANSION back. Add that
13831 expansion and move on. */
13832 gcc_assert (TREE_CODE (expanded_specs
)
13833 == TYPE_PACK_EXPANSION
);
13834 new_specs
= add_exception_specifier (new_specs
,
13837 specs
= TREE_CHAIN (specs
);
13842 for (i
= 0; i
< len
; ++i
)
13844 if (expanded_specs
)
13845 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13847 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13848 if (spec
== error_mark_node
)
13850 new_specs
= add_exception_specifier (new_specs
, spec
,
13854 specs
= TREE_CHAIN (specs
);
13860 /* Take the tree structure T and replace template parameters used
13861 therein with the argument vector ARGS. IN_DECL is an associated
13862 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13863 Issue error and warning messages under control of COMPLAIN. Note
13864 that we must be relatively non-tolerant of extensions here, in
13865 order to preserve conformance; if we allow substitutions that
13866 should not be allowed, we may allow argument deductions that should
13867 not succeed, and therefore report ambiguous overload situations
13868 where there are none. In theory, we could allow the substitution,
13869 but indicate that it should have failed, and allow our caller to
13870 make sure that the right thing happens, but we don't try to do this
13873 This function is used for dealing with types, decls and the like;
13874 for expressions, use tsubst_expr or tsubst_copy. */
13877 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13879 enum tree_code code
;
13880 tree type
, r
= NULL_TREE
;
13882 if (t
== NULL_TREE
|| t
== error_mark_node
13883 || t
== integer_type_node
13884 || t
== void_type_node
13885 || t
== char_type_node
13886 || t
== unknown_type_node
13887 || TREE_CODE (t
) == NAMESPACE_DECL
13888 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13892 return tsubst_decl (t
, args
, complain
);
13894 if (args
== NULL_TREE
)
13897 code
= TREE_CODE (t
);
13899 if (code
== IDENTIFIER_NODE
)
13900 type
= IDENTIFIER_TYPE_VALUE (t
);
13902 type
= TREE_TYPE (t
);
13904 gcc_assert (type
!= unknown_type_node
);
13906 /* Reuse typedefs. We need to do this to handle dependent attributes,
13907 such as attribute aligned. */
13909 && typedef_variant_p (t
))
13911 tree decl
= TYPE_NAME (t
);
13913 if (alias_template_specialization_p (t
))
13915 /* DECL represents an alias template and we want to
13917 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13918 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13919 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13921 else if (DECL_CLASS_SCOPE_P (decl
)
13922 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13923 && uses_template_parms (DECL_CONTEXT (decl
)))
13925 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13926 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13927 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13929 else if (DECL_FUNCTION_SCOPE_P (decl
)
13930 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13931 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13932 r
= retrieve_local_specialization (decl
);
13934 /* The typedef is from a non-template context. */
13940 r
= cp_build_qualified_type_real
13941 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13942 complain
| tf_ignore_bad_quals
);
13947 /* We don't have an instantiation yet, so drop the typedef. */
13948 int quals
= cp_type_quals (t
);
13949 t
= DECL_ORIGINAL_TYPE (decl
);
13950 t
= cp_build_qualified_type_real (t
, quals
,
13951 complain
| tf_ignore_bad_quals
);
13955 bool fndecl_type
= (complain
& tf_fndecl_type
);
13956 complain
&= ~tf_fndecl_type
;
13959 && code
!= TYPENAME_TYPE
13960 && code
!= TEMPLATE_TYPE_PARM
13961 && code
!= TEMPLATE_PARM_INDEX
13962 && code
!= IDENTIFIER_NODE
13963 && code
!= FUNCTION_TYPE
13964 && code
!= METHOD_TYPE
)
13965 type
= tsubst (type
, args
, complain
, in_decl
);
13966 if (type
== error_mark_node
)
13967 return error_mark_node
;
13973 case ENUMERAL_TYPE
:
13974 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13975 /*entering_scope=*/0);
13978 case IDENTIFIER_NODE
:
13989 if (t
== integer_type_node
)
13992 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13993 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13997 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13999 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14000 /*integral_constant_expression_p=*/false);
14002 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14004 if (TREE_CODE (max
) == NOP_EXPR
14005 && TREE_SIDE_EFFECTS (omax
)
14006 && !TREE_TYPE (max
))
14007 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14009 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14010 with TREE_SIDE_EFFECTS that indicates this is not an integral
14011 constant expression. */
14012 if (processing_template_decl
14013 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14015 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14016 TREE_SIDE_EFFECTS (max
) = 1;
14019 return compute_array_index_type (NULL_TREE
, max
, complain
);
14022 case TEMPLATE_TYPE_PARM
:
14023 case TEMPLATE_TEMPLATE_PARM
:
14024 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14025 case TEMPLATE_PARM_INDEX
:
14030 tree arg
= NULL_TREE
;
14032 /* Early in template argument deduction substitution, we don't
14033 want to reduce the level of 'auto', or it will be confused
14034 with a normal template parm in subsequent deduction. */
14035 if (is_auto (t
) && (complain
& tf_partial
))
14040 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14041 template_parm_level_and_index (t
, &level
, &idx
);
14043 levels
= TMPL_ARGS_DEPTH (args
);
14044 if (level
<= levels
14045 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14047 arg
= TMPL_ARG (args
, level
, idx
);
14049 /* See through ARGUMENT_PACK_SELECT arguments. */
14050 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14051 arg
= argument_pack_select_arg (arg
);
14054 if (arg
== error_mark_node
)
14055 return error_mark_node
;
14056 else if (arg
!= NULL_TREE
)
14058 if (ARGUMENT_PACK_P (arg
))
14059 /* If ARG is an argument pack, we don't actually want to
14060 perform a substitution here, because substitutions
14061 for argument packs are only done
14062 element-by-element. We can get to this point when
14063 substituting the type of a non-type template
14064 parameter pack, when that type actually contains
14065 template parameter packs from an outer template, e.g.,
14067 template<typename... Types> struct A {
14068 template<Types... Values> struct B { };
14072 if (code
== TEMPLATE_TYPE_PARM
)
14075 gcc_assert (TYPE_P (arg
));
14077 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14079 return cp_build_qualified_type_real
14080 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14082 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14084 /* We are processing a type constructed from a
14085 template template parameter. */
14086 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14087 args
, complain
, in_decl
);
14088 if (argvec
== error_mark_node
)
14089 return error_mark_node
;
14091 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14092 || TREE_CODE (arg
) == TEMPLATE_DECL
14093 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14095 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14096 /* Consider this code:
14098 template <template <class> class Template>
14100 template <class Arg> using Bind = Template<Arg>;
14103 template <template <class> class Template, class Arg>
14104 using Instantiate = Template<Arg>; //#0
14106 template <template <class> class Template,
14109 Instantiate<Internal<Template>::template Bind,
14112 When #1 is parsed, the
14113 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14114 parameter `Template' in #0 matches the
14115 UNBOUND_CLASS_TEMPLATE representing the argument
14116 `Internal<Template>::template Bind'; We then want
14117 to assemble the type `Bind<Argument>' that can't
14118 be fully created right now, because
14119 `Internal<Template>' not being complete, the Bind
14120 template cannot be looked up in that context. So
14121 we need to "store" `Bind<Argument>' for later
14122 when the context of Bind becomes complete. Let's
14123 store that in a TYPENAME_TYPE. */
14124 return make_typename_type (TYPE_CONTEXT (arg
),
14125 build_nt (TEMPLATE_ID_EXPR
,
14126 TYPE_IDENTIFIER (arg
),
14131 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14132 are resolving nested-types in the signature of a
14133 member function templates. Otherwise ARG is a
14134 TEMPLATE_DECL and is the real template to be
14136 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14137 arg
= TYPE_NAME (arg
);
14139 r
= lookup_template_class (arg
,
14141 DECL_CONTEXT (arg
),
14142 /*entering_scope=*/0,
14144 return cp_build_qualified_type_real
14145 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14147 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14150 /* TEMPLATE_PARM_INDEX. */
14151 return convert_from_reference (unshare_expr (arg
));
14155 /* This can happen during the attempted tsubst'ing in
14156 unify. This means that we don't yet have any information
14157 about the template parameter in question. */
14160 /* If we get here, we must have been looking at a parm for a
14161 more deeply nested template. Make a new version of this
14162 template parameter, but with a lower level. */
14165 case TEMPLATE_TYPE_PARM
:
14166 case TEMPLATE_TEMPLATE_PARM
:
14167 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14168 if (cp_type_quals (t
))
14170 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14171 r
= cp_build_qualified_type_real
14172 (r
, cp_type_quals (t
),
14173 complain
| (code
== TEMPLATE_TYPE_PARM
14174 ? tf_ignore_bad_quals
: 0));
14176 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14177 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14178 && (r
= (TEMPLATE_PARM_DESCENDANTS
14179 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14180 && (r
= TREE_TYPE (r
))
14181 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14182 /* Break infinite recursion when substituting the constraints
14183 of a constrained placeholder. */;
14187 TEMPLATE_TYPE_PARM_INDEX (r
)
14188 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14189 r
, levels
, args
, complain
);
14190 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14191 TYPE_MAIN_VARIANT (r
) = r
;
14192 TYPE_POINTER_TO (r
) = NULL_TREE
;
14193 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14195 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14197 /* Propagate constraints on placeholders. */
14198 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14199 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14200 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14201 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14203 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14204 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14208 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14209 /* We have reduced the level of the template
14210 template parameter, but not the levels of its
14211 template parameters, so canonical_type_parameter
14212 will not be able to find the canonical template
14213 template parameter for this level. Thus, we
14214 require structural equality checking to compare
14215 TEMPLATE_TEMPLATE_PARMs. */
14216 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14217 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14218 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14220 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14222 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14224 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14225 /* We might need to substitute into the types of non-type
14226 template parameters. */
14227 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14228 complain
, in_decl
);
14229 if (tmpl
== error_mark_node
)
14230 return error_mark_node
;
14231 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14232 complain
, in_decl
);
14233 if (argvec
== error_mark_node
)
14234 return error_mark_node
;
14236 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14237 = build_template_info (tmpl
, argvec
);
14242 case TEMPLATE_PARM_INDEX
:
14243 /* OK, now substitute the type of the non-type parameter. We
14244 couldn't do it earlier because it might be an auto parameter,
14245 and we wouldn't need to if we had an argument. */
14246 type
= tsubst (type
, args
, complain
, in_decl
);
14247 if (type
== error_mark_node
)
14248 return error_mark_node
;
14249 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14253 gcc_unreachable ();
14261 tree purpose
, value
, chain
;
14263 if (t
== void_list_node
)
14266 purpose
= TREE_PURPOSE (t
);
14269 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14270 if (purpose
== error_mark_node
)
14271 return error_mark_node
;
14273 value
= TREE_VALUE (t
);
14276 value
= tsubst (value
, args
, complain
, in_decl
);
14277 if (value
== error_mark_node
)
14278 return error_mark_node
;
14280 chain
= TREE_CHAIN (t
);
14281 if (chain
&& chain
!= void_type_node
)
14283 chain
= tsubst (chain
, args
, complain
, in_decl
);
14284 if (chain
== error_mark_node
)
14285 return error_mark_node
;
14287 if (purpose
== TREE_PURPOSE (t
)
14288 && value
== TREE_VALUE (t
)
14289 && chain
== TREE_CHAIN (t
))
14291 return hash_tree_cons (purpose
, value
, chain
);
14295 /* We should never be tsubsting a binfo. */
14296 gcc_unreachable ();
14299 /* A vector of template arguments. */
14300 gcc_assert (!type
);
14301 return tsubst_template_args (t
, args
, complain
, in_decl
);
14304 case REFERENCE_TYPE
:
14306 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14311 Type deduction may fail for any of the following
14314 -- Attempting to create a pointer to reference type.
14315 -- Attempting to create a reference to a reference type or
14316 a reference to void.
14318 Core issue 106 says that creating a reference to a reference
14319 during instantiation is no longer a cause for failure. We
14320 only enforce this check in strict C++98 mode. */
14321 if ((TREE_CODE (type
) == REFERENCE_TYPE
14322 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14323 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14325 static location_t last_loc
;
14327 /* We keep track of the last time we issued this error
14328 message to avoid spewing a ton of messages during a
14329 single bad template instantiation. */
14330 if (complain
& tf_error
14331 && last_loc
!= input_location
)
14333 if (VOID_TYPE_P (type
))
14334 error ("forming reference to void");
14335 else if (code
== POINTER_TYPE
)
14336 error ("forming pointer to reference type %qT", type
);
14338 error ("forming reference to reference type %qT", type
);
14339 last_loc
= input_location
;
14342 return error_mark_node
;
14344 else if (TREE_CODE (type
) == FUNCTION_TYPE
14345 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14346 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14348 if (complain
& tf_error
)
14350 if (code
== POINTER_TYPE
)
14351 error ("forming pointer to qualified function type %qT",
14354 error ("forming reference to qualified function type %qT",
14357 return error_mark_node
;
14359 else if (code
== POINTER_TYPE
)
14361 r
= build_pointer_type (type
);
14362 if (TREE_CODE (type
) == METHOD_TYPE
)
14363 r
= build_ptrmemfunc_type (r
);
14365 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14366 /* In C++0x, during template argument substitution, when there is an
14367 attempt to create a reference to a reference type, reference
14368 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14370 "If a template-argument for a template-parameter T names a type
14371 that is a reference to a type A, an attempt to create the type
14372 'lvalue reference to cv T' creates the type 'lvalue reference to
14373 A,' while an attempt to create the type type rvalue reference to
14374 cv T' creates the type T"
14376 r
= cp_build_reference_type
14378 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14380 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14381 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14383 if (r
!= error_mark_node
)
14384 /* Will this ever be needed for TYPE_..._TO values? */
14391 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14392 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14396 Type deduction may fail for any of the following
14399 -- Attempting to create "pointer to member of T" when T
14400 is not a class type. */
14401 if (complain
& tf_error
)
14402 error ("creating pointer to member of non-class type %qT", r
);
14403 return error_mark_node
;
14405 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14407 if (complain
& tf_error
)
14408 error ("creating pointer to member reference type %qT", type
);
14409 return error_mark_node
;
14411 if (VOID_TYPE_P (type
))
14413 if (complain
& tf_error
)
14414 error ("creating pointer to member of type void");
14415 return error_mark_node
;
14417 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14418 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14420 /* The type of the implicit object parameter gets its
14421 cv-qualifiers from the FUNCTION_TYPE. */
14424 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14425 type_memfn_rqual (type
));
14426 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14427 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14431 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14435 case FUNCTION_TYPE
:
14440 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14441 if (fntype
== error_mark_node
)
14442 return error_mark_node
;
14444 /* Substitute the exception specification. */
14445 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14446 /*defer_ok*/fndecl_type
);
14447 if (specs
== error_mark_node
)
14448 return error_mark_node
;
14450 fntype
= build_exception_variant (fntype
, specs
);
14455 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14456 if (domain
== error_mark_node
)
14457 return error_mark_node
;
14459 /* As an optimization, we avoid regenerating the array type if
14460 it will obviously be the same as T. */
14461 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14464 /* These checks should match the ones in create_array_type_for_decl.
14468 The deduction may fail for any of the following reasons:
14470 -- Attempting to create an array with an element type that
14471 is void, a function type, or a reference type, or [DR337]
14472 an abstract class type. */
14473 if (VOID_TYPE_P (type
)
14474 || TREE_CODE (type
) == FUNCTION_TYPE
14475 || (TREE_CODE (type
) == ARRAY_TYPE
14476 && TYPE_DOMAIN (type
) == NULL_TREE
)
14477 || TREE_CODE (type
) == REFERENCE_TYPE
)
14479 if (complain
& tf_error
)
14480 error ("creating array of %qT", type
);
14481 return error_mark_node
;
14484 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14485 return error_mark_node
;
14487 r
= build_cplus_array_type (type
, domain
);
14489 if (TYPE_USER_ALIGN (t
))
14491 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14492 TYPE_USER_ALIGN (r
) = 1;
14498 case TYPENAME_TYPE
:
14500 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14501 in_decl
, /*entering_scope=*/1);
14502 if (ctx
== error_mark_node
)
14503 return error_mark_node
;
14505 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14506 complain
, in_decl
);
14507 if (f
== error_mark_node
)
14508 return error_mark_node
;
14510 if (!MAYBE_CLASS_TYPE_P (ctx
))
14512 if (complain
& tf_error
)
14513 error ("%qT is not a class, struct, or union type", ctx
);
14514 return error_mark_node
;
14516 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14518 /* Normally, make_typename_type does not require that the CTX
14519 have complete type in order to allow things like:
14521 template <class T> struct S { typename S<T>::X Y; };
14523 But, such constructs have already been resolved by this
14524 point, so here CTX really should have complete type, unless
14525 it's a partial instantiation. */
14526 ctx
= complete_type (ctx
);
14527 if (!COMPLETE_TYPE_P (ctx
))
14529 if (complain
& tf_error
)
14530 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14531 return error_mark_node
;
14535 f
= make_typename_type (ctx
, f
, typename_type
,
14536 complain
| tf_keep_type_decl
);
14537 if (f
== error_mark_node
)
14539 if (TREE_CODE (f
) == TYPE_DECL
)
14541 complain
|= tf_ignore_bad_quals
;
14545 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14547 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14549 if (complain
& tf_error
)
14550 error ("%qT resolves to %qT, which is not an enumeration type",
14553 return error_mark_node
;
14555 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14557 if (complain
& tf_error
)
14558 error ("%qT resolves to %qT, which is is not a class type",
14561 return error_mark_node
;
14565 return cp_build_qualified_type_real
14566 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14569 case UNBOUND_CLASS_TEMPLATE
:
14571 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14572 in_decl
, /*entering_scope=*/1);
14573 tree name
= TYPE_IDENTIFIER (t
);
14574 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14576 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14577 return error_mark_node
;
14580 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14581 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14588 ++cp_unevaluated_operand
;
14589 ++c_inhibit_evaluation_warnings
;
14591 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14593 /*integral_constant_expression_p=*/false);
14595 --cp_unevaluated_operand
;
14596 --c_inhibit_evaluation_warnings
;
14598 type
= finish_typeof (type
);
14599 return cp_build_qualified_type_real (type
,
14601 | cp_type_quals (type
),
14605 case DECLTYPE_TYPE
:
14609 ++cp_unevaluated_operand
;
14610 ++c_inhibit_evaluation_warnings
;
14612 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14613 complain
|tf_decltype
, in_decl
,
14614 /*function_p*/false,
14615 /*integral_constant_expression*/false);
14617 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14619 if (type
== NULL_TREE
)
14621 if (complain
& tf_error
)
14622 error ("empty initializer in lambda init-capture");
14623 type
= error_mark_node
;
14625 else if (TREE_CODE (type
) == TREE_LIST
)
14626 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14629 --cp_unevaluated_operand
;
14630 --c_inhibit_evaluation_warnings
;
14632 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14633 type
= lambda_capture_field_type (type
,
14634 DECLTYPE_FOR_INIT_CAPTURE (t
),
14635 DECLTYPE_FOR_REF_CAPTURE (t
));
14636 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14637 type
= lambda_proxy_type (type
);
14640 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14641 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14643 /* In a template ~id could be either a complement expression
14644 or an unqualified-id naming a destructor; if instantiating
14645 it produces an expression, it's not an id-expression or
14648 type
= finish_decltype_type (type
, id
, complain
);
14650 return cp_build_qualified_type_real (type
,
14652 | cp_type_quals (type
),
14653 complain
| tf_ignore_bad_quals
);
14656 case UNDERLYING_TYPE
:
14658 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14659 complain
, in_decl
);
14660 return finish_underlying_type (type
);
14663 case TYPE_ARGUMENT_PACK
:
14664 case NONTYPE_ARGUMENT_PACK
:
14668 if (code
== NONTYPE_ARGUMENT_PACK
)
14669 r
= make_node (code
);
14671 r
= cxx_make_type (code
);
14673 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14674 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14675 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14693 /* We should use one of the expression tsubsts for these codes. */
14694 gcc_unreachable ();
14697 sorry ("use of %qs in template", get_tree_code_name (code
));
14698 return error_mark_node
;
14702 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14703 expression on the left-hand side of the "." or "->" operator. We
14704 only do the lookup if we had a dependent BASELINK. Otherwise we
14705 adjust it onto the instantiated heirarchy. */
14708 tsubst_baselink (tree baselink
, tree object_type
,
14709 tree args
, tsubst_flags_t complain
, tree in_decl
)
14711 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14712 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14713 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14715 tree optype
= BASELINK_OPTYPE (baselink
);
14716 optype
= tsubst (optype
, args
, complain
, in_decl
);
14718 tree template_args
= NULL_TREE
;
14719 bool template_id_p
= false;
14720 tree fns
= BASELINK_FUNCTIONS (baselink
);
14721 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14723 template_id_p
= true;
14724 template_args
= TREE_OPERAND (fns
, 1);
14725 fns
= TREE_OPERAND (fns
, 0);
14727 template_args
= tsubst_template_args (template_args
, args
,
14728 complain
, in_decl
);
14731 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14732 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14733 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14737 tree name
= OVL_NAME (fns
);
14738 if (IDENTIFIER_CONV_OP_P (name
))
14739 name
= make_conv_op_name (optype
);
14741 if (name
== complete_dtor_identifier
)
14742 /* Treat as-if non-dependent below. */
14743 dependent_p
= false;
14745 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14748 if ((complain
& tf_error
)
14749 && constructor_name_p (name
, qualifying_scope
))
14750 error ("cannot call constructor %<%T::%D%> directly",
14751 qualifying_scope
, name
);
14752 return error_mark_node
;
14755 if (BASELINK_P (baselink
))
14756 fns
= BASELINK_FUNCTIONS (baselink
);
14759 /* We're going to overwrite pieces below, make a duplicate. */
14760 baselink
= copy_node (baselink
);
14762 /* If lookup found a single function, mark it as used at this point.
14763 (If lookup found multiple functions the one selected later by
14764 overload resolution will be marked as used at that point.) */
14765 if (!template_id_p
&& !really_overloaded_fn (fns
))
14767 tree fn
= OVL_FIRST (fns
);
14768 bool ok
= mark_used (fn
, complain
);
14769 if (!ok
&& !(complain
& tf_error
))
14770 return error_mark_node
;
14771 if (ok
&& BASELINK_P (baselink
))
14772 /* We might have instantiated an auto function. */
14773 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
14776 if (BASELINK_P (baselink
))
14778 /* Add back the template arguments, if present. */
14780 BASELINK_FUNCTIONS (baselink
)
14781 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14783 /* Update the conversion operator type. */
14784 BASELINK_OPTYPE (baselink
) = optype
;
14788 object_type
= current_class_type
;
14790 if (qualified_p
|| !dependent_p
)
14792 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14796 /* We need to call adjust_result_of_qualified_name_lookup in case the
14797 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14798 so that we still get virtual function binding. */
14799 BASELINK_QUALIFIED_P (baselink
) = false;
14805 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14806 true if the qualified-id will be a postfix-expression in-and-of
14807 itself; false if more of the postfix-expression follows the
14808 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14812 tsubst_qualified_id (tree qualified_id
, tree args
,
14813 tsubst_flags_t complain
, tree in_decl
,
14814 bool done
, bool address_p
)
14820 tree template_args
;
14821 location_t loc
= UNKNOWN_LOCATION
;
14823 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14825 /* Figure out what name to look up. */
14826 name
= TREE_OPERAND (qualified_id
, 1);
14827 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14829 is_template
= true;
14830 loc
= EXPR_LOCATION (name
);
14831 template_args
= TREE_OPERAND (name
, 1);
14833 template_args
= tsubst_template_args (template_args
, args
,
14834 complain
, in_decl
);
14835 if (template_args
== error_mark_node
)
14836 return error_mark_node
;
14837 name
= TREE_OPERAND (name
, 0);
14841 is_template
= false;
14842 template_args
= NULL_TREE
;
14845 /* Substitute into the qualifying scope. When there are no ARGS, we
14846 are just trying to simplify a non-dependent expression. In that
14847 case the qualifying scope may be dependent, and, in any case,
14848 substituting will not help. */
14849 scope
= TREE_OPERAND (qualified_id
, 0);
14852 scope
= tsubst (scope
, args
, complain
, in_decl
);
14853 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14858 if (dependent_scope_p (scope
))
14861 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14862 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14863 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14864 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14868 if (!BASELINK_P (name
) && !DECL_P (expr
))
14870 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14872 /* A BIT_NOT_EXPR is used to represent a destructor. */
14873 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14875 error ("qualifying type %qT does not match destructor name ~%qT",
14876 scope
, TREE_OPERAND (expr
, 0));
14877 expr
= error_mark_node
;
14880 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14881 /*is_type_p=*/0, false);
14884 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14885 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14886 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14888 if (complain
& tf_error
)
14890 error ("dependent-name %qE is parsed as a non-type, but "
14891 "instantiation yields a type", qualified_id
);
14892 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14894 return error_mark_node
;
14900 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14902 /* Remember that there was a reference to this entity. */
14903 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14904 return error_mark_node
;
14907 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14909 if (complain
& tf_error
)
14910 qualified_name_lookup_error (scope
,
14911 TREE_OPERAND (qualified_id
, 1),
14912 expr
, input_location
);
14913 return error_mark_node
;
14918 /* We may be repeating a check already done during parsing, but
14919 if it was well-formed and passed then, it will pass again
14920 now, and if it didn't, we wouldn't have got here. The case
14921 we want to catch is when we couldn't tell then, and can now,
14922 namely when templ prior to substitution was an
14924 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
14925 return error_mark_node
;
14927 if (variable_template_p (expr
))
14928 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14931 expr
= lookup_template_function (expr
, template_args
);
14934 if (expr
== error_mark_node
&& complain
& tf_error
)
14935 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14936 expr
, input_location
);
14937 else if (TYPE_P (scope
))
14939 expr
= (adjust_result_of_qualified_name_lookup
14940 (expr
, scope
, current_nonlambda_class_type ()));
14941 expr
= (finish_qualified_id_expr
14942 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14943 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14944 /*template_arg_p=*/false, complain
));
14947 /* Expressions do not generally have reference type. */
14948 if (TREE_CODE (expr
) != SCOPE_REF
14949 /* However, if we're about to form a pointer-to-member, we just
14950 want the referenced member referenced. */
14951 && TREE_CODE (expr
) != OFFSET_REF
)
14952 expr
= convert_from_reference (expr
);
14954 if (REF_PARENTHESIZED_P (qualified_id
))
14955 expr
= force_paren_expr (expr
);
14960 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14961 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14965 tsubst_init (tree init
, tree decl
, tree args
,
14966 tsubst_flags_t complain
, tree in_decl
)
14971 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14973 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14975 /* If we had an initializer but it
14976 instantiated to nothing,
14977 value-initialize the object. This will
14978 only occur when the initializer was a
14979 pack expansion where the parameter packs
14980 used in that expansion were of length
14982 init
= build_value_init (TREE_TYPE (decl
),
14984 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14985 init
= get_target_expr_sfinae (init
, complain
);
14986 if (TREE_CODE (init
) == TARGET_EXPR
)
14987 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14993 /* Like tsubst, but deals with expressions. This function just replaces
14994 template parms; to finish processing the resultant expression, use
14995 tsubst_copy_and_build or tsubst_expr. */
14998 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15000 enum tree_code code
;
15003 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15006 code
= TREE_CODE (t
);
15011 r
= retrieve_local_specialization (t
);
15013 if (r
== NULL_TREE
)
15015 /* We get here for a use of 'this' in an NSDMI. */
15016 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15017 return current_class_ptr
;
15019 /* This can happen for a parameter name used later in a function
15020 declaration (such as in a late-specified return type). Just
15021 make a dummy decl, since it's only used for its type. */
15022 gcc_assert (cp_unevaluated_operand
!= 0);
15023 r
= tsubst_decl (t
, args
, complain
);
15024 /* Give it the template pattern as its context; its true context
15025 hasn't been instantiated yet and this is good enough for
15027 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15030 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15031 r
= argument_pack_select_arg (r
);
15032 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15033 return error_mark_node
;
15041 if (DECL_TEMPLATE_PARM_P (t
))
15042 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15043 /* There is no need to substitute into namespace-scope
15045 if (DECL_NAMESPACE_SCOPE_P (t
))
15047 /* If ARGS is NULL, then T is known to be non-dependent. */
15048 if (args
== NULL_TREE
)
15049 return scalar_constant_value (t
);
15051 /* Unfortunately, we cannot just call lookup_name here.
15054 template <int I> int f() {
15056 struct S { void g() { E e = a; } };
15059 When we instantiate f<7>::S::g(), say, lookup_name is not
15060 clever enough to find f<7>::a. */
15062 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15063 /*entering_scope=*/0);
15065 for (v
= TYPE_VALUES (enum_type
);
15067 v
= TREE_CHAIN (v
))
15068 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15069 return TREE_VALUE (v
);
15071 /* We didn't find the name. That should never happen; if
15072 name-lookup found it during preliminary parsing, we
15073 should find it again here during instantiation. */
15074 gcc_unreachable ();
15079 if (DECL_CONTEXT (t
))
15083 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15084 /*entering_scope=*/1);
15085 if (ctx
!= DECL_CONTEXT (t
))
15087 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15090 if (complain
& tf_error
)
15091 error ("using invalid field %qD", t
);
15092 return error_mark_node
;
15101 case FUNCTION_DECL
:
15102 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15103 r
= tsubst (t
, args
, complain
, in_decl
);
15104 else if (local_variable_p (t
)
15105 && uses_template_parms (DECL_CONTEXT (t
)))
15107 r
= retrieve_local_specialization (t
);
15108 if (r
== NULL_TREE
)
15110 /* First try name lookup to find the instantiation. */
15111 r
= lookup_name (DECL_NAME (t
));
15112 if (r
&& !is_capture_proxy (r
))
15114 /* Make sure that the one we found is the one we want. */
15115 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15116 if (ctx
!= DECL_CONTEXT (r
))
15124 /* This can happen for a variable used in a
15125 late-specified return type of a local lambda, or for a
15126 local static or constant. Building a new VAR_DECL
15127 should be OK in all those cases. */
15128 r
= tsubst_decl (t
, args
, complain
);
15129 if (local_specializations
)
15130 /* Avoid infinite recursion (79640). */
15131 register_local_specialization (r
, t
);
15132 if (decl_maybe_constant_var_p (r
))
15134 /* We can't call cp_finish_decl, so handle the
15135 initializer by hand. */
15136 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15137 complain
, in_decl
);
15138 if (!processing_template_decl
)
15139 init
= maybe_constant_init (init
);
15140 if (processing_template_decl
15141 ? potential_constant_expression (init
)
15142 : reduced_constant_expression_p (init
))
15143 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15144 = TREE_CONSTANT (r
) = true;
15145 DECL_INITIAL (r
) = init
;
15146 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15148 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15149 complain
, adc_variable_type
);
15151 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15152 || decl_constant_var_p (r
)
15153 || errorcount
|| sorrycount
);
15154 if (!processing_template_decl
15155 && !TREE_STATIC (r
))
15156 r
= process_outer_var_ref (r
, complain
);
15158 /* Remember this for subsequent uses. */
15159 if (local_specializations
)
15160 register_local_specialization (r
, t
);
15162 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15163 r
= argument_pack_select_arg (r
);
15167 if (!mark_used (r
, complain
))
15168 return error_mark_node
;
15171 case NAMESPACE_DECL
:
15175 /* An OVERLOAD will always be a non-dependent overload set; an
15176 overload set from function scope will just be represented with an
15177 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15178 gcc_assert (!uses_template_parms (t
));
15179 /* We must have marked any lookups as persistent. */
15180 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
15184 return tsubst_baselink (t
, current_nonlambda_class_type (),
15185 args
, complain
, in_decl
);
15187 case TEMPLATE_DECL
:
15188 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15189 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15190 args
, complain
, in_decl
);
15191 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15192 return tsubst (t
, args
, complain
, in_decl
);
15193 else if (DECL_CLASS_SCOPE_P (t
)
15194 && uses_template_parms (DECL_CONTEXT (t
)))
15196 /* Template template argument like the following example need
15199 template <template <class> class TT> struct C {};
15200 template <class T> struct D {
15201 template <class U> struct E {};
15206 We are processing the template argument `E' in #1 for
15207 the template instantiation #2. Originally, `E' is a
15208 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15209 have to substitute this with one having context `D<int>'. */
15211 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15212 if (dependent_scope_p (context
))
15214 /* When rewriting a constructor into a deduction guide, a
15215 non-dependent name can become dependent, so memtmpl<args>
15216 becomes context::template memtmpl<args>. */
15217 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15218 return build_qualified_name (type
, context
, DECL_NAME (t
),
15221 return lookup_field (context
, DECL_NAME(t
), 0, false);
15224 /* Ordinary template template argument. */
15227 case NON_LVALUE_EXPR
:
15228 case VIEW_CONVERT_EXPR
:
15230 /* Handle location wrappers by substituting the wrapped node
15231 first, *then* reusing the resulting type. Doing the type
15232 first ensures that we handle template parameters and
15233 parameter pack expansions. */
15234 gcc_assert (location_wrapper_p (t
));
15235 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15236 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15240 case REINTERPRET_CAST_EXPR
:
15241 case CONST_CAST_EXPR
:
15242 case STATIC_CAST_EXPR
:
15243 case DYNAMIC_CAST_EXPR
:
15244 case IMPLICIT_CONV_EXPR
:
15248 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15249 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15250 return build1 (code
, type
, op0
);
15254 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15255 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15257 tree expanded
, op
= TREE_OPERAND (t
, 0);
15260 if (SIZEOF_EXPR_TYPE_P (t
))
15261 op
= TREE_TYPE (op
);
15263 ++cp_unevaluated_operand
;
15264 ++c_inhibit_evaluation_warnings
;
15265 /* We only want to compute the number of arguments. */
15266 if (PACK_EXPANSION_P (op
))
15267 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15269 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15270 args
, complain
, in_decl
);
15271 --cp_unevaluated_operand
;
15272 --c_inhibit_evaluation_warnings
;
15274 if (TREE_CODE (expanded
) == TREE_VEC
)
15276 len
= TREE_VEC_LENGTH (expanded
);
15277 /* Set TREE_USED for the benefit of -Wunused. */
15278 for (int i
= 0; i
< len
; i
++)
15279 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15280 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15283 if (expanded
== error_mark_node
)
15284 return error_mark_node
;
15285 else if (PACK_EXPANSION_P (expanded
)
15286 || (TREE_CODE (expanded
) == TREE_VEC
15287 && pack_expansion_args_count (expanded
)))
15290 if (PACK_EXPANSION_P (expanded
))
15292 else if (TREE_VEC_LENGTH (expanded
) == 1)
15293 expanded
= TREE_VEC_ELT (expanded
, 0);
15295 expanded
= make_argument_pack (expanded
);
15297 if (TYPE_P (expanded
))
15298 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15299 complain
& tf_error
);
15301 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15302 complain
& tf_error
);
15305 return build_int_cst (size_type_node
, len
);
15307 if (SIZEOF_EXPR_TYPE_P (t
))
15309 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15310 args
, complain
, in_decl
);
15311 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15312 r
= build1 (SIZEOF_EXPR
,
15313 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15314 SIZEOF_EXPR_TYPE_P (r
) = 1;
15321 case TRUTH_NOT_EXPR
:
15324 case UNARY_PLUS_EXPR
: /* Unary + */
15326 case AT_ENCODE_EXPR
:
15330 case REALPART_EXPR
:
15331 case IMAGPART_EXPR
:
15334 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15335 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15336 return build1 (code
, type
, op0
);
15339 case COMPONENT_REF
:
15344 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15345 name
= TREE_OPERAND (t
, 1);
15346 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15348 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15349 complain
, in_decl
);
15350 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15352 else if (TREE_CODE (name
) == SCOPE_REF
15353 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15355 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15356 complain
, in_decl
);
15357 name
= TREE_OPERAND (name
, 1);
15358 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15359 complain
, in_decl
);
15360 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15361 name
= build_qualified_name (/*type=*/NULL_TREE
,
15363 /*template_p=*/false);
15365 else if (BASELINK_P (name
))
15366 name
= tsubst_baselink (name
,
15367 non_reference (TREE_TYPE (object
)),
15371 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15372 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15378 case TRUNC_DIV_EXPR
:
15379 case CEIL_DIV_EXPR
:
15380 case FLOOR_DIV_EXPR
:
15381 case ROUND_DIV_EXPR
:
15382 case EXACT_DIV_EXPR
:
15386 case TRUNC_MOD_EXPR
:
15387 case FLOOR_MOD_EXPR
:
15388 case TRUTH_ANDIF_EXPR
:
15389 case TRUTH_ORIF_EXPR
:
15390 case TRUTH_AND_EXPR
:
15391 case TRUTH_OR_EXPR
:
15404 case COMPOUND_EXPR
:
15407 case PREDECREMENT_EXPR
:
15408 case PREINCREMENT_EXPR
:
15409 case POSTDECREMENT_EXPR
:
15410 case POSTINCREMENT_EXPR
:
15412 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15413 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15414 return build_nt (code
, op0
, op1
);
15419 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15420 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15421 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15422 QUALIFIED_NAME_IS_TEMPLATE (t
));
15427 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15428 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15429 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15434 int n
= VL_EXP_OPERAND_LENGTH (t
);
15435 tree result
= build_vl_exp (CALL_EXPR
, n
);
15437 for (i
= 0; i
< n
; i
++)
15438 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15439 complain
, in_decl
);
15445 case PSEUDO_DTOR_EXPR
:
15446 case VEC_PERM_EXPR
:
15448 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15449 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15450 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15451 r
= build_nt (code
, op0
, op1
, op2
);
15452 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15458 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15459 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15460 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15461 r
= build_nt (code
, op0
, op1
, op2
);
15462 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15468 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15469 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15470 r
= build_nt (code
, op0
, op1
);
15471 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15472 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15476 case TEMPLATE_ID_EXPR
:
15478 /* Substituted template arguments */
15479 tree fn
= TREE_OPERAND (t
, 0);
15480 tree targs
= TREE_OPERAND (t
, 1);
15482 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15484 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15486 return lookup_template_function (fn
, targs
);
15491 tree purpose
, value
, chain
;
15493 if (t
== void_list_node
)
15496 purpose
= TREE_PURPOSE (t
);
15498 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15499 value
= TREE_VALUE (t
);
15501 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15502 chain
= TREE_CHAIN (t
);
15503 if (chain
&& chain
!= void_type_node
)
15504 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15505 if (purpose
== TREE_PURPOSE (t
)
15506 && value
== TREE_VALUE (t
)
15507 && chain
== TREE_CHAIN (t
))
15509 return tree_cons (purpose
, value
, chain
);
15514 case ENUMERAL_TYPE
:
15516 case TEMPLATE_TYPE_PARM
:
15517 case TEMPLATE_TEMPLATE_PARM
:
15518 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15519 case TEMPLATE_PARM_INDEX
:
15521 case REFERENCE_TYPE
:
15523 case FUNCTION_TYPE
:
15526 case TYPENAME_TYPE
:
15527 case UNBOUND_CLASS_TEMPLATE
:
15529 case DECLTYPE_TYPE
:
15531 return tsubst (t
, args
, complain
, in_decl
);
15535 /* Fall through. */
15536 case IDENTIFIER_NODE
:
15537 if (IDENTIFIER_CONV_OP_P (t
))
15539 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15540 return make_conv_op_name (new_type
);
15546 /* This is handled by tsubst_copy_and_build. */
15547 gcc_unreachable ();
15551 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15552 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15553 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15556 case CLEANUP_POINT_EXPR
:
15557 /* We shouldn't have built any of these during initial template
15558 generation. Instead, they should be built during instantiation
15559 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15560 gcc_unreachable ();
15564 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15565 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15566 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15567 r
= build2 (code
, type
, op0
, op1
);
15568 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15569 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15570 && !(complain
& tf_error
))
15571 return error_mark_node
;
15575 case EXPR_PACK_EXPANSION
:
15576 error ("invalid use of pack expansion expression");
15577 return error_mark_node
;
15579 case NONTYPE_ARGUMENT_PACK
:
15580 error ("use %<...%> to expand argument pack");
15581 return error_mark_node
;
15584 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15592 /* Instantiate any typedefs in the type. */
15593 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15594 r
= fold_convert (type
, t
);
15595 gcc_assert (TREE_CODE (r
) == code
);
15600 /* These can sometimes show up in a partial instantiation, but never
15601 involve template parms. */
15602 gcc_assert (!uses_template_parms (t
));
15605 case UNARY_LEFT_FOLD_EXPR
:
15606 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15607 case UNARY_RIGHT_FOLD_EXPR
:
15608 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15609 case BINARY_LEFT_FOLD_EXPR
:
15610 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15611 case BINARY_RIGHT_FOLD_EXPR
:
15612 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15616 case DEBUG_BEGIN_STMT
:
15617 /* ??? There's no point in copying it for now, but maybe some
15618 day it will contain more information, such as a pointer back
15619 to the containing function, inlined copy or so. */
15623 /* We shouldn't get here, but keep going if !flag_checking. */
15625 gcc_unreachable ();
15630 /* Helper function for tsubst_omp_clauses, used for instantiation of
15631 OMP_CLAUSE_DECL of clauses. */
15634 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15637 if (decl
== NULL_TREE
)
15640 /* Handle an OpenMP array section represented as a TREE_LIST (or
15641 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15642 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15643 TREE_LIST. We can handle it exactly the same as an array section
15644 (purpose, value, and a chain), even though the nomenclature
15645 (low_bound, length, etc) is different. */
15646 if (TREE_CODE (decl
) == TREE_LIST
)
15649 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15650 /*integral_constant_expression_p=*/false);
15651 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15652 /*integral_constant_expression_p=*/false);
15653 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15655 if (TREE_PURPOSE (decl
) == low_bound
15656 && TREE_VALUE (decl
) == length
15657 && TREE_CHAIN (decl
) == chain
)
15659 tree ret
= tree_cons (low_bound
, length
, chain
);
15660 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15661 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15664 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15665 /*integral_constant_expression_p=*/false);
15666 /* Undo convert_from_reference tsubst_expr could have called. */
15668 && REFERENCE_REF_P (ret
)
15669 && !REFERENCE_REF_P (decl
))
15670 ret
= TREE_OPERAND (ret
, 0);
15674 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15677 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15678 tree args
, tsubst_flags_t complain
, tree in_decl
)
15680 tree new_clauses
= NULL_TREE
, nc
, oc
;
15681 tree linear_no_step
= NULL_TREE
;
15683 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15685 nc
= copy_node (oc
);
15686 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15689 switch (OMP_CLAUSE_CODE (nc
))
15691 case OMP_CLAUSE_LASTPRIVATE
:
15692 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15694 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15695 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15696 in_decl
, /*integral_constant_expression_p=*/false);
15697 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15698 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15701 case OMP_CLAUSE_PRIVATE
:
15702 case OMP_CLAUSE_SHARED
:
15703 case OMP_CLAUSE_FIRSTPRIVATE
:
15704 case OMP_CLAUSE_COPYIN
:
15705 case OMP_CLAUSE_COPYPRIVATE
:
15706 case OMP_CLAUSE_UNIFORM
:
15707 case OMP_CLAUSE_DEPEND
:
15708 case OMP_CLAUSE_FROM
:
15709 case OMP_CLAUSE_TO
:
15710 case OMP_CLAUSE_MAP
:
15711 case OMP_CLAUSE_USE_DEVICE_PTR
:
15712 case OMP_CLAUSE_IS_DEVICE_PTR
:
15713 OMP_CLAUSE_DECL (nc
)
15714 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15717 case OMP_CLAUSE_TILE
:
15718 case OMP_CLAUSE_IF
:
15719 case OMP_CLAUSE_NUM_THREADS
:
15720 case OMP_CLAUSE_SCHEDULE
:
15721 case OMP_CLAUSE_COLLAPSE
:
15722 case OMP_CLAUSE_FINAL
:
15723 case OMP_CLAUSE_DEVICE
:
15724 case OMP_CLAUSE_DIST_SCHEDULE
:
15725 case OMP_CLAUSE_NUM_TEAMS
:
15726 case OMP_CLAUSE_THREAD_LIMIT
:
15727 case OMP_CLAUSE_SAFELEN
:
15728 case OMP_CLAUSE_SIMDLEN
:
15729 case OMP_CLAUSE_NUM_TASKS
:
15730 case OMP_CLAUSE_GRAINSIZE
:
15731 case OMP_CLAUSE_PRIORITY
:
15732 case OMP_CLAUSE_ORDERED
:
15733 case OMP_CLAUSE_HINT
:
15734 case OMP_CLAUSE_NUM_GANGS
:
15735 case OMP_CLAUSE_NUM_WORKERS
:
15736 case OMP_CLAUSE_VECTOR_LENGTH
:
15737 case OMP_CLAUSE_WORKER
:
15738 case OMP_CLAUSE_VECTOR
:
15739 case OMP_CLAUSE_ASYNC
:
15740 case OMP_CLAUSE_WAIT
:
15741 OMP_CLAUSE_OPERAND (nc
, 0)
15742 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15743 in_decl
, /*integral_constant_expression_p=*/false);
15745 case OMP_CLAUSE_REDUCTION
:
15746 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15748 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15749 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15751 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15752 complain
, in_decl
);
15753 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15754 = build_qualified_name (NULL_TREE
, scope
,
15755 TREE_OPERAND (placeholder
, 1),
15759 gcc_assert (identifier_p (placeholder
));
15761 OMP_CLAUSE_DECL (nc
)
15762 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15765 case OMP_CLAUSE_GANG
:
15766 case OMP_CLAUSE_ALIGNED
:
15767 OMP_CLAUSE_DECL (nc
)
15768 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15770 OMP_CLAUSE_OPERAND (nc
, 1)
15771 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15772 in_decl
, /*integral_constant_expression_p=*/false);
15774 case OMP_CLAUSE_LINEAR
:
15775 OMP_CLAUSE_DECL (nc
)
15776 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15778 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15780 gcc_assert (!linear_no_step
);
15781 linear_no_step
= nc
;
15783 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15784 OMP_CLAUSE_LINEAR_STEP (nc
)
15785 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15786 complain
, in_decl
);
15788 OMP_CLAUSE_LINEAR_STEP (nc
)
15789 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15791 /*integral_constant_expression_p=*/false);
15793 case OMP_CLAUSE_NOWAIT
:
15794 case OMP_CLAUSE_DEFAULT
:
15795 case OMP_CLAUSE_UNTIED
:
15796 case OMP_CLAUSE_MERGEABLE
:
15797 case OMP_CLAUSE_INBRANCH
:
15798 case OMP_CLAUSE_NOTINBRANCH
:
15799 case OMP_CLAUSE_PROC_BIND
:
15800 case OMP_CLAUSE_FOR
:
15801 case OMP_CLAUSE_PARALLEL
:
15802 case OMP_CLAUSE_SECTIONS
:
15803 case OMP_CLAUSE_TASKGROUP
:
15804 case OMP_CLAUSE_NOGROUP
:
15805 case OMP_CLAUSE_THREADS
:
15806 case OMP_CLAUSE_SIMD
:
15807 case OMP_CLAUSE_DEFAULTMAP
:
15808 case OMP_CLAUSE_INDEPENDENT
:
15809 case OMP_CLAUSE_AUTO
:
15810 case OMP_CLAUSE_SEQ
:
15813 gcc_unreachable ();
15815 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15816 switch (OMP_CLAUSE_CODE (nc
))
15818 case OMP_CLAUSE_SHARED
:
15819 case OMP_CLAUSE_PRIVATE
:
15820 case OMP_CLAUSE_FIRSTPRIVATE
:
15821 case OMP_CLAUSE_LASTPRIVATE
:
15822 case OMP_CLAUSE_COPYPRIVATE
:
15823 case OMP_CLAUSE_LINEAR
:
15824 case OMP_CLAUSE_REDUCTION
:
15825 case OMP_CLAUSE_USE_DEVICE_PTR
:
15826 case OMP_CLAUSE_IS_DEVICE_PTR
:
15827 /* tsubst_expr on SCOPE_REF results in returning
15828 finish_non_static_data_member result. Undo that here. */
15829 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15830 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15831 == IDENTIFIER_NODE
))
15833 tree t
= OMP_CLAUSE_DECL (nc
);
15836 switch (TREE_CODE (v
))
15838 case COMPONENT_REF
:
15842 case POINTER_PLUS_EXPR
:
15843 v
= TREE_OPERAND (v
, 0);
15846 if (DECL_CONTEXT (v
) == current_function_decl
15847 && DECL_ARTIFICIAL (v
)
15848 && DECL_NAME (v
) == this_identifier
)
15849 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15856 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15857 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15858 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15859 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15860 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15862 tree decl
= OMP_CLAUSE_DECL (nc
);
15865 retrofit_lang_decl (decl
);
15866 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15875 new_clauses
= nreverse (new_clauses
);
15876 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15878 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15879 if (linear_no_step
)
15880 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15881 if (nc
== linear_no_step
)
15883 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15887 return new_clauses
;
15890 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15893 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15896 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15898 tree purpose
, value
, chain
;
15903 if (TREE_CODE (t
) != TREE_LIST
)
15904 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15905 /*function_p=*/false,
15906 /*integral_constant_expression_p=*/false);
15908 if (t
== void_list_node
)
15911 purpose
= TREE_PURPOSE (t
);
15913 purpose
= RECUR (purpose
);
15914 value
= TREE_VALUE (t
);
15917 if (TREE_CODE (value
) != LABEL_DECL
)
15918 value
= RECUR (value
);
15921 value
= lookup_label (DECL_NAME (value
));
15922 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15923 TREE_USED (value
) = 1;
15926 chain
= TREE_CHAIN (t
);
15927 if (chain
&& chain
!= void_type_node
)
15928 chain
= RECUR (chain
);
15929 return tree_cons (purpose
, value
, chain
);
15933 /* Used to temporarily communicate the list of #pragma omp parallel
15934 clauses to #pragma omp for instantiation if they are combined
15937 static tree
*omp_parallel_combined_clauses
;
15939 /* Substitute one OMP_FOR iterator. */
15942 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15943 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15944 tree args
, tsubst_flags_t complain
, tree in_decl
,
15945 bool integral_constant_expression_p
)
15947 #define RECUR(NODE) \
15948 tsubst_expr ((NODE), args, complain, in_decl, \
15949 integral_constant_expression_p)
15950 tree decl
, init
, cond
, incr
;
15952 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15953 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15955 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15957 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15958 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15961 decl
= TREE_OPERAND (init
, 0);
15962 init
= TREE_OPERAND (init
, 1);
15963 tree decl_expr
= NULL_TREE
;
15964 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15966 /* We need to jump through some hoops to handle declarations in the
15967 init-statement, since we might need to handle auto deduction,
15968 but we need to keep control of initialization. */
15970 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15971 decl
= tsubst_decl (decl
, args
, complain
);
15975 if (TREE_CODE (decl
) == SCOPE_REF
)
15977 decl
= RECUR (decl
);
15978 if (TREE_CODE (decl
) == COMPONENT_REF
)
15982 switch (TREE_CODE (v
))
15984 case COMPONENT_REF
:
15988 case POINTER_PLUS_EXPR
:
15989 v
= TREE_OPERAND (v
, 0);
15992 if (DECL_CONTEXT (v
) == current_function_decl
15993 && DECL_ARTIFICIAL (v
)
15994 && DECL_NAME (v
) == this_identifier
)
15996 decl
= TREE_OPERAND (decl
, 1);
15997 decl
= omp_privatize_field (decl
, false);
16007 decl
= RECUR (decl
);
16009 init
= RECUR (init
);
16011 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16012 if (auto_node
&& init
)
16014 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16016 gcc_assert (!type_dependent_expression_p (decl
));
16018 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16022 /* Declare the variable, but don't let that initialize it. */
16023 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16024 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16026 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16029 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16030 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16031 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16033 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16034 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16035 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16036 NOP_EXPR
, rhs
, complain
);
16039 incr
= RECUR (incr
);
16040 TREE_VEC_ELT (declv
, i
) = decl
;
16041 TREE_VEC_ELT (initv
, i
) = init
;
16042 TREE_VEC_ELT (condv
, i
) = cond
;
16043 TREE_VEC_ELT (incrv
, i
) = incr
;
16049 /* Declare and initialize the variable. */
16057 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
16059 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16061 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16062 && OMP_CLAUSE_DECL (*pc
) == decl
)
16064 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16065 && OMP_CLAUSE_DECL (*pc
) == decl
)
16069 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16071 *pc
= OMP_CLAUSE_CHAIN (c
);
16072 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16075 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16076 && OMP_CLAUSE_DECL (*pc
) == decl
)
16078 error ("iteration variable %qD should not be firstprivate",
16080 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16082 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16083 && OMP_CLAUSE_DECL (*pc
) == decl
)
16085 error ("iteration variable %qD should not be reduction",
16087 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16090 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16095 if (*pc
== NULL_TREE
)
16097 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16098 OMP_CLAUSE_DECL (c
) = decl
;
16099 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16102 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16107 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16108 if (COMPARISON_CLASS_P (cond
))
16110 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16111 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16112 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16115 cond
= RECUR (cond
);
16116 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16117 switch (TREE_CODE (incr
))
16119 case PREINCREMENT_EXPR
:
16120 case PREDECREMENT_EXPR
:
16121 case POSTINCREMENT_EXPR
:
16122 case POSTDECREMENT_EXPR
:
16123 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16124 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16127 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16128 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16130 tree rhs
= TREE_OPERAND (incr
, 1);
16131 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16132 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16133 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16134 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16135 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16139 incr
= RECUR (incr
);
16142 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16143 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16145 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16146 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16147 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16148 TREE_TYPE (decl
), lhs
,
16149 RECUR (TREE_OPERAND (incr
, 2))));
16151 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16152 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16153 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16155 tree rhs
= TREE_OPERAND (incr
, 2);
16156 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16157 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16158 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16159 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16160 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16164 incr
= RECUR (incr
);
16167 incr
= RECUR (incr
);
16171 TREE_VEC_ELT (declv
, i
) = decl
;
16172 TREE_VEC_ELT (initv
, i
) = init
;
16173 TREE_VEC_ELT (condv
, i
) = cond
;
16174 TREE_VEC_ELT (incrv
, i
) = incr
;
16178 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16179 of OMP_TARGET's body. */
16182 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16184 *walk_subtrees
= 0;
16185 switch (TREE_CODE (*tp
))
16190 case STATEMENT_LIST
:
16191 *walk_subtrees
= 1;
16199 /* Helper function for tsubst_expr. For decomposition declaration
16200 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16201 also the corresponding decls representing the identifiers
16202 of the decomposition declaration. Return DECL if successful
16203 or error_mark_node otherwise, set *FIRST to the first decl
16204 in the list chained through DECL_CHAIN and *CNT to the number
16208 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16209 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16212 tree decl2
, decl3
, prev
= decl
;
16214 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16215 for (decl2
= DECL_CHAIN (pattern_decl
);
16218 && DECL_DECOMPOSITION_P (decl2
)
16219 && DECL_NAME (decl2
);
16220 decl2
= DECL_CHAIN (decl2
))
16222 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16224 gcc_assert (errorcount
);
16225 return error_mark_node
;
16228 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16229 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16230 tree v
= DECL_VALUE_EXPR (decl2
);
16231 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16232 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16233 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16234 SET_DECL_VALUE_EXPR (decl2
, v
);
16235 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16237 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16238 maybe_push_decl (decl3
);
16239 if (error_operand_p (decl3
))
16240 decl
= error_mark_node
;
16241 else if (decl
!= error_mark_node
16242 && DECL_CHAIN (decl3
) != prev
)
16244 gcc_assert (errorcount
);
16245 decl
= error_mark_node
;
16254 /* Like tsubst_copy for expressions, etc. but also does semantic
16258 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16259 bool integral_constant_expression_p
)
16261 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16262 #define RECUR(NODE) \
16263 tsubst_expr ((NODE), args, complain, in_decl, \
16264 integral_constant_expression_p)
16270 if (t
== NULL_TREE
|| t
== error_mark_node
)
16273 loc
= input_location
;
16274 if (EXPR_HAS_LOCATION (t
))
16275 input_location
= EXPR_LOCATION (t
);
16276 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16277 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16279 switch (TREE_CODE (t
))
16281 case STATEMENT_LIST
:
16283 tree_stmt_iterator i
;
16284 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16285 RECUR (tsi_stmt (i
));
16289 case CTOR_INITIALIZER
:
16290 finish_mem_initializers (tsubst_initializer_list
16291 (TREE_OPERAND (t
, 0), args
));
16295 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16299 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16300 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16301 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16303 finish_expr_stmt (tmp
);
16307 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16308 /*attribs=*/NULL_TREE
);
16313 tree decl
, pattern_decl
;
16316 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16317 if (TREE_CODE (decl
) == LABEL_DECL
)
16318 finish_label_decl (DECL_NAME (decl
));
16319 else if (TREE_CODE (decl
) == USING_DECL
)
16321 tree scope
= USING_DECL_SCOPE (decl
);
16322 tree name
= DECL_NAME (decl
);
16324 scope
= tsubst (scope
, args
, complain
, in_decl
);
16325 decl
= lookup_qualified_name (scope
, name
,
16326 /*is_type_p=*/false,
16327 /*complain=*/false);
16328 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16329 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16331 finish_local_using_decl (decl
, scope
, name
);
16333 else if (is_capture_proxy (decl
)
16334 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16336 /* We're in tsubst_lambda_expr, we've already inserted a new
16337 capture proxy, so look it up and register it. */
16339 if (DECL_PACK_P (decl
))
16341 inst
= (retrieve_local_specialization
16342 (DECL_CAPTURED_VARIABLE (decl
)));
16343 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16347 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16348 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16349 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16351 register_local_specialization (inst
, decl
);
16354 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16355 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16356 /* Don't copy the old closure; we'll create a new one in
16357 tsubst_lambda_expr. */
16361 init
= DECL_INITIAL (decl
);
16362 decl
= tsubst (decl
, args
, complain
, in_decl
);
16363 if (decl
!= error_mark_node
)
16365 /* By marking the declaration as instantiated, we avoid
16366 trying to instantiate it. Since instantiate_decl can't
16367 handle local variables, and since we've already done
16368 all that needs to be done, that's the right thing to
16371 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16373 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16374 /* Anonymous aggregates are a special case. */
16375 finish_anon_union (decl
);
16376 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16378 DECL_CONTEXT (decl
) = current_function_decl
;
16379 if (DECL_NAME (decl
) == this_identifier
)
16381 tree lam
= DECL_CONTEXT (current_function_decl
);
16382 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16383 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16385 insert_capture_proxy (decl
);
16387 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16388 /* We already did a pushtag. */;
16389 else if (TREE_CODE (decl
) == FUNCTION_DECL
16390 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16391 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16393 DECL_CONTEXT (decl
) = NULL_TREE
;
16395 DECL_CONTEXT (decl
) = current_function_decl
;
16396 cp_check_omp_declare_reduction (decl
);
16400 int const_init
= false;
16401 maybe_push_decl (decl
);
16403 && DECL_PRETTY_FUNCTION_P (decl
))
16405 /* For __PRETTY_FUNCTION__ we have to adjust the
16407 const char *const name
16408 = cxx_printable_name (current_function_decl
, 2);
16409 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16412 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16415 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16418 && DECL_DECOMPOSITION_P (decl
)
16419 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16424 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16425 complain
, in_decl
, &first
, &cnt
);
16426 if (ndecl
!= error_mark_node
)
16427 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16428 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16429 if (ndecl
!= error_mark_node
)
16430 cp_finish_decomp (ndecl
, first
, cnt
);
16433 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16442 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16443 RECUR (FOR_INIT_STMT (t
));
16444 finish_init_stmt (stmt
);
16445 tmp
= RECUR (FOR_COND (t
));
16446 finish_for_cond (tmp
, stmt
, false, 0);
16447 tmp
= RECUR (FOR_EXPR (t
));
16448 finish_for_expr (tmp
, stmt
);
16450 bool prev
= note_iteration_stmt_body_start ();
16451 RECUR (FOR_BODY (t
));
16452 note_iteration_stmt_body_end (prev
);
16454 finish_for_stmt (stmt
);
16457 case RANGE_FOR_STMT
:
16459 /* Construct another range_for, if this is not a final
16460 substitution (for inside inside a generic lambda of a
16461 template). Otherwise convert to a regular for. */
16463 stmt
= (processing_template_decl
16464 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16465 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16466 decl
= RANGE_FOR_DECL (t
);
16467 decl
= tsubst (decl
, args
, complain
, in_decl
);
16468 maybe_push_decl (decl
);
16469 expr
= RECUR (RANGE_FOR_EXPR (t
));
16471 tree decomp_first
= NULL_TREE
;
16472 unsigned decomp_cnt
= 0;
16473 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16474 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16476 &decomp_first
, &decomp_cnt
);
16478 if (processing_template_decl
)
16480 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16481 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16482 finish_range_for_decl (stmt
, decl
, expr
);
16486 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16487 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16488 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16489 decomp_first
, decomp_cnt
,
16490 RANGE_FOR_IVDEP (t
), unroll
);
16493 bool prev
= note_iteration_stmt_body_start ();
16494 RECUR (RANGE_FOR_BODY (t
));
16495 note_iteration_stmt_body_end (prev
);
16496 finish_for_stmt (stmt
);
16501 stmt
= begin_while_stmt ();
16502 tmp
= RECUR (WHILE_COND (t
));
16503 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16505 bool prev
= note_iteration_stmt_body_start ();
16506 RECUR (WHILE_BODY (t
));
16507 note_iteration_stmt_body_end (prev
);
16509 finish_while_stmt (stmt
);
16513 stmt
= begin_do_stmt ();
16515 bool prev
= note_iteration_stmt_body_start ();
16516 RECUR (DO_BODY (t
));
16517 note_iteration_stmt_body_end (prev
);
16519 finish_do_body (stmt
);
16520 tmp
= RECUR (DO_COND (t
));
16521 finish_do_stmt (tmp
, stmt
, false, 0);
16525 stmt
= begin_if_stmt ();
16526 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16527 if (IF_STMT_CONSTEXPR_P (t
))
16528 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
16529 tmp
= RECUR (IF_COND (t
));
16530 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16531 if (IF_STMT_CONSTEXPR_P (t
)
16532 && instantiation_dependent_expression_p (tmp
))
16534 /* We're partially instantiating a generic lambda, but the condition
16535 of the constexpr if is still dependent. Don't substitute into the
16536 branches now, just remember the template arguments. */
16537 do_poplevel (IF_SCOPE (stmt
));
16538 IF_COND (stmt
) = IF_COND (t
);
16539 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
16540 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
16541 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
16545 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16546 /* Don't instantiate the THEN_CLAUSE. */;
16549 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16551 ++c_inhibit_evaluation_warnings
;
16552 RECUR (THEN_CLAUSE (t
));
16554 --c_inhibit_evaluation_warnings
;
16556 finish_then_clause (stmt
);
16558 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16559 /* Don't instantiate the ELSE_CLAUSE. */;
16560 else if (ELSE_CLAUSE (t
))
16562 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16563 begin_else_clause (stmt
);
16565 ++c_inhibit_evaluation_warnings
;
16566 RECUR (ELSE_CLAUSE (t
));
16568 --c_inhibit_evaluation_warnings
;
16569 finish_else_clause (stmt
);
16572 finish_if_stmt (stmt
);
16576 if (BIND_EXPR_BODY_BLOCK (t
))
16577 stmt
= begin_function_body ();
16579 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16580 ? BCS_TRY_BLOCK
: 0);
16582 RECUR (BIND_EXPR_BODY (t
));
16584 if (BIND_EXPR_BODY_BLOCK (t
))
16585 finish_function_body (stmt
);
16587 finish_compound_stmt (stmt
);
16591 finish_break_stmt ();
16594 case CONTINUE_STMT
:
16595 finish_continue_stmt ();
16599 stmt
= begin_switch_stmt ();
16600 tmp
= RECUR (SWITCH_STMT_COND (t
));
16601 finish_switch_cond (tmp
, stmt
);
16602 RECUR (SWITCH_STMT_BODY (t
));
16603 finish_switch_stmt (stmt
);
16606 case CASE_LABEL_EXPR
:
16608 tree low
= RECUR (CASE_LOW (t
));
16609 tree high
= RECUR (CASE_HIGH (t
));
16610 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16611 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16612 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16613 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16619 tree decl
= LABEL_EXPR_LABEL (t
);
16622 label
= finish_label_stmt (DECL_NAME (decl
));
16623 if (TREE_CODE (label
) == LABEL_DECL
)
16624 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16625 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16626 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16631 tmp
= GOTO_DESTINATION (t
);
16632 if (TREE_CODE (tmp
) != LABEL_DECL
)
16633 /* Computed goto's must be tsubst'd into. On the other hand,
16634 non-computed gotos must not be; the identifier in question
16635 will have no binding. */
16638 tmp
= DECL_NAME (tmp
);
16639 finish_goto_stmt (tmp
);
16644 tree string
= RECUR (ASM_STRING (t
));
16645 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16646 complain
, in_decl
);
16647 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16648 complain
, in_decl
);
16649 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16650 complain
, in_decl
);
16651 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16652 complain
, in_decl
);
16653 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16655 tree asm_expr
= tmp
;
16656 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16657 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16658 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16665 stmt
= begin_try_block ();
16666 RECUR (TRY_STMTS (t
));
16667 finish_cleanup_try_block (stmt
);
16668 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16672 tree compound_stmt
= NULL_TREE
;
16674 if (FN_TRY_BLOCK_P (t
))
16675 stmt
= begin_function_try_block (&compound_stmt
);
16677 stmt
= begin_try_block ();
16679 RECUR (TRY_STMTS (t
));
16681 if (FN_TRY_BLOCK_P (t
))
16682 finish_function_try_block (stmt
);
16684 finish_try_block (stmt
);
16686 RECUR (TRY_HANDLERS (t
));
16687 if (FN_TRY_BLOCK_P (t
))
16688 finish_function_handler_sequence (stmt
, compound_stmt
);
16690 finish_handler_sequence (stmt
);
16696 tree decl
= HANDLER_PARMS (t
);
16700 decl
= tsubst (decl
, args
, complain
, in_decl
);
16701 /* Prevent instantiate_decl from trying to instantiate
16702 this variable. We've already done all that needs to be
16704 if (decl
!= error_mark_node
)
16705 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16707 stmt
= begin_handler ();
16708 finish_handler_parms (decl
, stmt
);
16709 RECUR (HANDLER_BODY (t
));
16710 finish_handler (stmt
);
16715 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16716 if (CLASS_TYPE_P (tmp
))
16718 /* Local classes are not independent templates; they are
16719 instantiated along with their containing function. And this
16720 way we don't have to deal with pushing out of one local class
16721 to instantiate a member of another local class. */
16722 /* Closures are handled by the LAMBDA_EXPR. */
16723 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16724 complete_type (tmp
);
16725 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16727 || (TREE_CODE (fld
) == FUNCTION_DECL
16728 && !DECL_ARTIFICIAL (fld
)))
16729 && DECL_TEMPLATE_INSTANTIATION (fld
))
16730 instantiate_decl (fld
, /*defer_ok=*/false,
16731 /*expl_inst_class=*/false);
16735 case STATIC_ASSERT
:
16739 ++c_inhibit_evaluation_warnings
;
16741 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16744 /*integral_constant_expression_p=*/true);
16745 --c_inhibit_evaluation_warnings
;
16747 finish_static_assert (condition
,
16748 STATIC_ASSERT_MESSAGE (t
),
16749 STATIC_ASSERT_SOURCE_LOCATION (t
),
16750 /*member_p=*/false);
16755 case OACC_PARALLEL
:
16756 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16758 stmt
= begin_omp_parallel ();
16759 RECUR (OMP_BODY (t
));
16760 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16764 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16765 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16766 complain
, in_decl
);
16767 if (OMP_PARALLEL_COMBINED (t
))
16768 omp_parallel_combined_clauses
= &tmp
;
16769 stmt
= begin_omp_parallel ();
16770 RECUR (OMP_PARALLEL_BODY (t
));
16771 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16772 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16773 = OMP_PARALLEL_COMBINED (t
);
16774 pop_omp_privatization_clauses (r
);
16778 r
= push_omp_privatization_clauses (false);
16779 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16780 complain
, in_decl
);
16781 stmt
= begin_omp_task ();
16782 RECUR (OMP_TASK_BODY (t
));
16783 finish_omp_task (tmp
, stmt
);
16784 pop_omp_privatization_clauses (r
);
16789 case OMP_DISTRIBUTE
:
16793 tree clauses
, body
, pre_body
;
16794 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16795 tree orig_declv
= NULL_TREE
;
16796 tree incrv
= NULL_TREE
;
16797 enum c_omp_region_type ort
= C_ORT_OMP
;
16800 if (TREE_CODE (t
) == OACC_LOOP
)
16803 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16804 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16806 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16808 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16809 if (OMP_FOR_ORIG_DECLS (t
))
16810 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16811 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16812 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16813 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16816 stmt
= begin_omp_structured_block ();
16818 pre_body
= push_stmt_list ();
16819 RECUR (OMP_FOR_PRE_BODY (t
));
16820 pre_body
= pop_stmt_list (pre_body
);
16822 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16823 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16824 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16825 incrv
, &clauses
, args
, complain
, in_decl
,
16826 integral_constant_expression_p
);
16827 omp_parallel_combined_clauses
= NULL
;
16829 body
= push_stmt_list ();
16830 RECUR (OMP_FOR_BODY (t
));
16831 body
= pop_stmt_list (body
);
16833 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16834 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16835 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16839 t
= make_node (TREE_CODE (t
));
16840 TREE_TYPE (t
) = void_type_node
;
16841 OMP_FOR_BODY (t
) = body
;
16842 OMP_FOR_PRE_BODY (t
) = pre_body
;
16843 OMP_FOR_CLAUSES (t
) = clauses
;
16844 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16848 add_stmt (finish_omp_structured_block (stmt
));
16849 pop_omp_privatization_clauses (r
);
16854 omp_parallel_combined_clauses
= NULL
;
16859 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16860 && OMP_TEAMS_COMBINED (t
));
16861 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16863 stmt
= push_stmt_list ();
16864 RECUR (OMP_BODY (t
));
16865 stmt
= pop_stmt_list (stmt
);
16868 OMP_BODY (t
) = stmt
;
16869 OMP_CLAUSES (t
) = tmp
;
16871 pop_omp_privatization_clauses (r
);
16875 case OMP_TARGET_DATA
:
16877 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16878 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16880 keep_next_level (true);
16881 stmt
= begin_omp_structured_block ();
16883 RECUR (OMP_BODY (t
));
16884 stmt
= finish_omp_structured_block (stmt
);
16887 OMP_BODY (t
) = stmt
;
16888 OMP_CLAUSES (t
) = tmp
;
16889 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16891 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16894 /* For combined target teams, ensure the num_teams and
16895 thread_limit clause expressions are evaluated on the host,
16896 before entering the target construct. */
16898 for (c
= OMP_TEAMS_CLAUSES (teams
);
16899 c
; c
= OMP_CLAUSE_CHAIN (c
))
16900 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16901 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16902 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16904 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16905 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16906 if (expr
== error_mark_node
)
16908 tmp
= TARGET_EXPR_SLOT (expr
);
16910 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16911 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16912 OMP_CLAUSE_FIRSTPRIVATE
);
16913 OMP_CLAUSE_DECL (tc
) = tmp
;
16914 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16915 OMP_TARGET_CLAUSES (t
) = tc
;
16924 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16925 complain
, in_decl
);
16926 OACC_DECLARE_CLAUSES (t
) = tmp
;
16930 case OMP_TARGET_UPDATE
:
16931 case OMP_TARGET_ENTER_DATA
:
16932 case OMP_TARGET_EXIT_DATA
:
16933 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16934 complain
, in_decl
);
16936 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16940 case OACC_ENTER_DATA
:
16941 case OACC_EXIT_DATA
:
16943 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16944 complain
, in_decl
);
16946 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16951 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16952 complain
, in_decl
);
16953 stmt
= push_stmt_list ();
16954 RECUR (OMP_BODY (t
));
16955 stmt
= pop_stmt_list (stmt
);
16958 OMP_BODY (t
) = stmt
;
16959 OMP_ORDERED_CLAUSES (t
) = tmp
;
16965 case OMP_TASKGROUP
:
16966 stmt
= push_stmt_list ();
16967 RECUR (OMP_BODY (t
));
16968 stmt
= pop_stmt_list (stmt
);
16971 OMP_BODY (t
) = stmt
;
16976 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16977 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16979 tree op1
= TREE_OPERAND (t
, 1);
16980 tree rhs1
= NULL_TREE
;
16982 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16984 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16985 op1
= TREE_OPERAND (op1
, 1);
16987 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16988 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16989 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16990 NULL_TREE
, NULL_TREE
, rhs1
,
16991 OMP_ATOMIC_SEQ_CST (t
));
16995 tree op1
= TREE_OPERAND (t
, 1);
16996 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16997 tree rhs1
= NULL_TREE
;
16998 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16999 enum tree_code opcode
= NOP_EXPR
;
17000 if (code
== OMP_ATOMIC_READ
)
17002 v
= RECUR (TREE_OPERAND (op1
, 0));
17003 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17005 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17006 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17008 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17009 v
= RECUR (TREE_OPERAND (op1
, 0));
17010 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17011 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17013 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17014 op11
= TREE_OPERAND (op11
, 1);
17016 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17017 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17018 opcode
= TREE_CODE (op11
);
17019 if (opcode
== MODIFY_EXPR
)
17025 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17026 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17028 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
17029 OMP_ATOMIC_SEQ_CST (t
));
17033 case TRANSACTION_EXPR
:
17036 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17037 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17039 if (TRANSACTION_EXPR_IS_STMT (t
))
17041 tree body
= TRANSACTION_EXPR_BODY (t
);
17042 tree noex
= NULL_TREE
;
17043 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17045 noex
= MUST_NOT_THROW_COND (body
);
17046 if (noex
== NULL_TREE
)
17047 noex
= boolean_true_node
;
17048 body
= TREE_OPERAND (body
, 0);
17050 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17052 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17056 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17057 RECUR (TRANSACTION_EXPR_BODY (t
)),
17064 case MUST_NOT_THROW_EXPR
:
17066 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17067 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17068 RETURN (build_must_not_throw_expr (op0
, cond
));
17071 case EXPR_PACK_EXPANSION
:
17072 error ("invalid use of pack expansion expression");
17073 RETURN (error_mark_node
);
17075 case NONTYPE_ARGUMENT_PACK
:
17076 error ("use %<...%> to expand argument pack");
17077 RETURN (error_mark_node
);
17079 case COMPOUND_EXPR
:
17080 tmp
= RECUR (TREE_OPERAND (t
, 0));
17081 if (tmp
== NULL_TREE
)
17082 /* If the first operand was a statement, we're done with it. */
17083 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17084 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17085 RECUR (TREE_OPERAND (t
, 1)),
17088 case ANNOTATE_EXPR
:
17089 tmp
= RECUR (TREE_OPERAND (t
, 0));
17090 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17091 TREE_TYPE (tmp
), tmp
,
17092 RECUR (TREE_OPERAND (t
, 1)),
17093 RECUR (TREE_OPERAND (t
, 2))));
17096 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17098 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17099 /*function_p=*/false,
17100 integral_constant_expression_p
));
17103 RETURN (NULL_TREE
);
17105 input_location
= loc
;
17111 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17112 function. For description of the body see comment above
17113 cp_parser_omp_declare_reduction_exprs. */
17116 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17118 if (t
== NULL_TREE
|| t
== error_mark_node
)
17121 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17123 tree_stmt_iterator tsi
;
17126 memset (stmts
, 0, sizeof stmts
);
17127 for (i
= 0, tsi
= tsi_start (t
);
17128 i
< 7 && !tsi_end_p (tsi
);
17129 i
++, tsi_next (&tsi
))
17130 stmts
[i
] = tsi_stmt (tsi
);
17131 gcc_assert (tsi_end_p (tsi
));
17135 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17136 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17137 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17138 args
, complain
, in_decl
);
17139 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17140 args
, complain
, in_decl
);
17141 DECL_CONTEXT (omp_out
) = current_function_decl
;
17142 DECL_CONTEXT (omp_in
) = current_function_decl
;
17143 keep_next_level (true);
17144 tree block
= begin_omp_structured_block ();
17145 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17146 block
= finish_omp_structured_block (block
);
17147 block
= maybe_cleanup_point_expr_void (block
);
17148 add_decl_expr (omp_out
);
17149 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17150 TREE_NO_WARNING (omp_out
) = 1;
17151 add_decl_expr (omp_in
);
17152 finish_expr_stmt (block
);
17156 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17157 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17158 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17159 args
, complain
, in_decl
);
17160 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17161 args
, complain
, in_decl
);
17162 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17163 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17164 keep_next_level (true);
17165 tree block
= begin_omp_structured_block ();
17166 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17167 block
= finish_omp_structured_block (block
);
17168 block
= maybe_cleanup_point_expr_void (block
);
17169 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17170 add_decl_expr (omp_priv
);
17171 add_decl_expr (omp_orig
);
17172 finish_expr_stmt (block
);
17174 add_decl_expr (omp_orig
);
17178 /* T is a postfix-expression that is not being used in a function
17179 call. Return the substituted version of T. */
17182 tsubst_non_call_postfix_expression (tree t
, tree args
,
17183 tsubst_flags_t complain
,
17186 if (TREE_CODE (t
) == SCOPE_REF
)
17187 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17188 /*done=*/false, /*address_p=*/false);
17190 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17191 /*function_p=*/false,
17192 /*integral_constant_expression_p=*/false);
17197 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17198 instantiation context. Instantiating a pack expansion containing a lambda
17199 might result in multiple lambdas all based on the same lambda in the
17203 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17205 tree oldfn
= lambda_function (t
);
17208 tree r
= build_lambda_expr ();
17210 LAMBDA_EXPR_LOCATION (r
)
17211 = LAMBDA_EXPR_LOCATION (t
);
17212 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17213 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17214 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17216 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17217 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
17219 record_lambda_scope (r
);
17221 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17222 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17224 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17225 cap
= TREE_CHAIN (cap
))
17227 tree field
= TREE_PURPOSE (cap
);
17228 if (PACK_EXPANSION_P (field
))
17229 field
= PACK_EXPANSION_PATTERN (field
);
17230 field
= tsubst_decl (field
, args
, complain
);
17232 if (field
== error_mark_node
)
17233 return error_mark_node
;
17235 tree init
= TREE_VALUE (cap
);
17236 if (PACK_EXPANSION_P (init
))
17237 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17239 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17240 /*fn*/false, /*constexpr*/false);
17242 if (TREE_CODE (field
) == TREE_VEC
)
17244 int len
= TREE_VEC_LENGTH (field
);
17245 gcc_assert (TREE_CODE (init
) == TREE_VEC
17246 && TREE_VEC_LENGTH (init
) == len
);
17247 for (int i
= 0; i
< len
; ++i
)
17248 LAMBDA_EXPR_CAPTURE_LIST (r
)
17249 = tree_cons (TREE_VEC_ELT (field
, i
),
17250 TREE_VEC_ELT (init
, i
),
17251 LAMBDA_EXPR_CAPTURE_LIST (r
));
17255 LAMBDA_EXPR_CAPTURE_LIST (r
)
17256 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17258 if (id_equal (DECL_NAME (field
), "__this"))
17259 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17263 tree type
= begin_lambda_type (r
);
17265 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17266 determine_visibility (TYPE_NAME (type
));
17268 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17270 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17271 ? DECL_TI_TEMPLATE (oldfn
)
17274 tree fntype
= static_fn_type (oldfn
);
17276 ++processing_template_decl
;
17277 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17279 --processing_template_decl
;
17281 if (fntype
== error_mark_node
)
17282 r
= error_mark_node
;
17285 /* Fix the type of 'this'. */
17286 fntype
= build_memfn_type (fntype
, type
,
17287 type_memfn_quals (fntype
),
17288 type_memfn_rqual (fntype
));
17292 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17293 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17294 finish_member_declaration (tmpl
);
17299 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17300 finish_member_declaration (fn
);
17303 /* Let finish_function set this. */
17304 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17306 bool nested
= cfun
;
17308 push_function_context ();
17310 /* Still increment function_depth so that we don't GC in the
17311 middle of an expression. */
17314 local_specialization_stack
s (lss_copy
);
17316 tree body
= start_lambda_function (fn
, r
);
17318 register_parameter_specializations (oldfn
, fn
);
17320 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17321 /*constexpr*/false);
17323 finish_lambda_function (body
);
17326 pop_function_context ();
17330 /* The capture list was built up in reverse order; fix that now. */
17331 LAMBDA_EXPR_CAPTURE_LIST (r
)
17332 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17334 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17336 maybe_add_lambda_conv_op (type
);
17339 finish_struct (type
, /*attr*/NULL_TREE
);
17341 insert_pending_capture_proxies ();
17346 /* Like tsubst but deals with expressions and performs semantic
17347 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17350 tsubst_copy_and_build (tree t
,
17352 tsubst_flags_t complain
,
17355 bool integral_constant_expression_p
)
17357 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17358 #define RECUR(NODE) \
17359 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17360 /*function_p=*/false, \
17361 integral_constant_expression_p)
17366 if (t
== NULL_TREE
|| t
== error_mark_node
)
17369 loc
= input_location
;
17370 if (EXPR_HAS_LOCATION (t
))
17371 input_location
= EXPR_LOCATION (t
);
17373 /* N3276 decltype magic only applies to calls at the top level or on the
17374 right side of a comma. */
17375 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17376 complain
&= ~tf_decltype
;
17378 switch (TREE_CODE (t
))
17382 /* Fall through. */
17383 case IDENTIFIER_NODE
:
17387 bool non_integral_constant_expression_p
;
17388 const char *error_msg
;
17390 if (IDENTIFIER_CONV_OP_P (t
))
17392 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17393 t
= make_conv_op_name (new_type
);
17396 /* Look up the name. */
17397 decl
= lookup_name (t
);
17399 /* By convention, expressions use ERROR_MARK_NODE to indicate
17400 failure, not NULL_TREE. */
17401 if (decl
== NULL_TREE
)
17402 decl
= error_mark_node
;
17404 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17406 integral_constant_expression_p
,
17407 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17408 &non_integral_constant_expression_p
,
17409 /*template_p=*/false,
17411 /*address_p=*/false,
17412 /*template_arg_p=*/false,
17417 if (!function_p
&& identifier_p (decl
))
17419 if (complain
& tf_error
)
17420 unqualified_name_lookup_error (decl
);
17421 decl
= error_mark_node
;
17426 case TEMPLATE_ID_EXPR
:
17429 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17430 tree targs
= TREE_OPERAND (t
, 1);
17433 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17434 if (targs
== error_mark_node
)
17435 RETURN (error_mark_node
);
17437 if (TREE_CODE (templ
) == SCOPE_REF
)
17439 tree name
= TREE_OPERAND (templ
, 1);
17440 tree tid
= lookup_template_function (name
, targs
);
17441 TREE_OPERAND (templ
, 1) = tid
;
17445 if (variable_template_p (templ
))
17446 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17448 if (TREE_CODE (templ
) == COMPONENT_REF
)
17450 object
= TREE_OPERAND (templ
, 0);
17451 templ
= TREE_OPERAND (templ
, 1);
17454 object
= NULL_TREE
;
17455 templ
= lookup_template_function (templ
, targs
);
17458 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17459 object
, templ
, NULL_TREE
));
17461 RETURN (baselink_for_fns (templ
));
17466 tree r
= RECUR (TREE_OPERAND (t
, 0));
17468 if (REFERENCE_REF_P (t
))
17470 /* A type conversion to reference type will be enclosed in
17471 such an indirect ref, but the substitution of the cast
17472 will have also added such an indirect ref. */
17473 r
= convert_from_reference (r
);
17476 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17477 complain
|decltype_flag
);
17479 if (REF_PARENTHESIZED_P (t
))
17480 r
= force_paren_expr (r
);
17487 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17488 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17489 RETURN (build_nop (type
, op0
));
17492 case IMPLICIT_CONV_EXPR
:
17494 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17495 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17496 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17498 retval
= copy_node (t
);
17499 TREE_TYPE (retval
) = type
;
17500 TREE_OPERAND (retval
, 0) = expr
;
17503 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17504 /* We'll pass this to convert_nontype_argument again, we don't need
17505 to actually perform any conversion here. */
17507 int flags
= LOOKUP_IMPLICIT
;
17508 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17509 flags
= LOOKUP_NORMAL
;
17510 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17516 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17517 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17518 if (op0
== error_mark_node
)
17519 RETURN (error_mark_node
);
17520 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17524 case REINTERPRET_CAST_EXPR
:
17525 case CONST_CAST_EXPR
:
17526 case DYNAMIC_CAST_EXPR
:
17527 case STATIC_CAST_EXPR
:
17530 tree op
, r
= NULL_TREE
;
17532 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17533 if (integral_constant_expression_p
17534 && !cast_valid_in_integral_constant_expression_p (type
))
17536 if (complain
& tf_error
)
17537 error ("a cast to a type other than an integral or "
17538 "enumeration type cannot appear in a constant-expression");
17539 RETURN (error_mark_node
);
17542 op
= RECUR (TREE_OPERAND (t
, 0));
17544 warning_sentinel
s(warn_useless_cast
);
17545 warning_sentinel
s2(warn_ignored_qualifiers
);
17546 switch (TREE_CODE (t
))
17549 r
= build_functional_cast (type
, op
, complain
);
17551 case REINTERPRET_CAST_EXPR
:
17552 r
= build_reinterpret_cast (type
, op
, complain
);
17554 case CONST_CAST_EXPR
:
17555 r
= build_const_cast (type
, op
, complain
);
17557 case DYNAMIC_CAST_EXPR
:
17558 r
= build_dynamic_cast (type
, op
, complain
);
17560 case STATIC_CAST_EXPR
:
17561 r
= build_static_cast (type
, op
, complain
);
17564 gcc_unreachable ();
17570 case POSTDECREMENT_EXPR
:
17571 case POSTINCREMENT_EXPR
:
17572 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17573 args
, complain
, in_decl
);
17574 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17575 complain
|decltype_flag
));
17577 case PREDECREMENT_EXPR
:
17578 case PREINCREMENT_EXPR
:
17582 case TRUTH_NOT_EXPR
:
17583 case UNARY_PLUS_EXPR
: /* Unary + */
17584 case REALPART_EXPR
:
17585 case IMAGPART_EXPR
:
17586 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17587 RECUR (TREE_OPERAND (t
, 0)),
17588 complain
|decltype_flag
));
17590 case FIX_TRUNC_EXPR
:
17591 gcc_unreachable ();
17594 op1
= TREE_OPERAND (t
, 0);
17595 if (TREE_CODE (op1
) == LABEL_DECL
)
17596 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17597 EXPR_LOCATION (op1
)));
17598 if (TREE_CODE (op1
) == SCOPE_REF
)
17599 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17600 /*done=*/true, /*address_p=*/true);
17602 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17604 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17605 complain
|decltype_flag
));
17610 case TRUNC_DIV_EXPR
:
17611 case CEIL_DIV_EXPR
:
17612 case FLOOR_DIV_EXPR
:
17613 case ROUND_DIV_EXPR
:
17614 case EXACT_DIV_EXPR
:
17618 case TRUNC_MOD_EXPR
:
17619 case FLOOR_MOD_EXPR
:
17620 case TRUTH_ANDIF_EXPR
:
17621 case TRUTH_ORIF_EXPR
:
17622 case TRUTH_AND_EXPR
:
17623 case TRUTH_OR_EXPR
:
17639 warning_sentinel
s1(warn_type_limits
);
17640 warning_sentinel
s2(warn_div_by_zero
);
17641 warning_sentinel
s3(warn_logical_op
);
17642 warning_sentinel
s4(warn_tautological_compare
);
17643 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17644 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17645 tree r
= build_x_binary_op
17646 (input_location
, TREE_CODE (t
),
17648 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17650 : TREE_CODE (TREE_OPERAND (t
, 0))),
17652 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17654 : TREE_CODE (TREE_OPERAND (t
, 1))),
17656 complain
|decltype_flag
);
17657 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17658 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17663 case POINTER_PLUS_EXPR
:
17665 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17666 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17667 RETURN (fold_build_pointer_plus (op0
, op1
));
17671 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17672 /*address_p=*/false));
17674 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17675 args
, complain
, in_decl
);
17676 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17677 RECUR (TREE_OPERAND (t
, 1)),
17678 complain
|decltype_flag
));
17681 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17682 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17683 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17690 op1
= TREE_OPERAND (t
, 0);
17691 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17692 op1
= TREE_TYPE (op1
);
17695 /* When there are no ARGS, we are trying to evaluate a
17696 non-dependent expression from the parser. Trying to do
17697 the substitutions may not work. */
17699 op1
= TREE_TYPE (op1
);
17703 ++cp_unevaluated_operand
;
17704 ++c_inhibit_evaluation_warnings
;
17706 op1
= tsubst (op1
, args
, complain
, in_decl
);
17708 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17709 /*function_p=*/false,
17710 /*integral_constant_expression_p=*/
17712 --cp_unevaluated_operand
;
17713 --c_inhibit_evaluation_warnings
;
17716 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17717 complain
& tf_error
);
17719 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17720 complain
& tf_error
);
17721 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17723 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17725 if (!processing_template_decl
&& TYPE_P (op1
))
17727 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17728 build1 (NOP_EXPR
, op1
, error_mark_node
));
17729 SIZEOF_EXPR_TYPE_P (r
) = 1;
17732 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17733 TREE_SIDE_EFFECTS (r
) = 0;
17734 TREE_READONLY (r
) = 1;
17736 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17741 case AT_ENCODE_EXPR
:
17743 op1
= TREE_OPERAND (t
, 0);
17744 ++cp_unevaluated_operand
;
17745 ++c_inhibit_evaluation_warnings
;
17746 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17747 /*function_p=*/false,
17748 /*integral_constant_expression_p=*/false);
17749 --cp_unevaluated_operand
;
17750 --c_inhibit_evaluation_warnings
;
17751 RETURN (objc_build_encode_expr (op1
));
17754 case NOEXCEPT_EXPR
:
17755 op1
= TREE_OPERAND (t
, 0);
17756 ++cp_unevaluated_operand
;
17757 ++c_inhibit_evaluation_warnings
;
17758 ++cp_noexcept_operand
;
17759 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17760 /*function_p=*/false,
17761 /*integral_constant_expression_p=*/false);
17762 --cp_unevaluated_operand
;
17763 --c_inhibit_evaluation_warnings
;
17764 --cp_noexcept_operand
;
17765 RETURN (finish_noexcept_expr (op1
, complain
));
17769 warning_sentinel
s(warn_div_by_zero
);
17770 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17771 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17772 tree r
= build_x_modify_expr
17773 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17774 complain
|decltype_flag
);
17775 /* TREE_NO_WARNING must be set if either the expression was
17776 parenthesized or it uses an operator such as >>= rather
17777 than plain assignment. In the former case, it was already
17778 set and must be copied. In the latter case,
17779 build_x_modify_expr sets it and it must not be reset
17781 if (TREE_NO_WARNING (t
))
17782 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17788 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17789 args
, complain
, in_decl
);
17790 /* Remember that there was a reference to this entity. */
17792 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17793 RETURN (error_mark_node
);
17794 RETURN (build_x_arrow (input_location
, op1
, complain
));
17798 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17799 tree init
= RECUR (TREE_OPERAND (t
, 3));
17800 vec
<tree
, va_gc
> *placement_vec
;
17801 vec
<tree
, va_gc
> *init_vec
;
17804 if (placement
== NULL_TREE
)
17805 placement_vec
= NULL
;
17808 placement_vec
= make_tree_vector ();
17809 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17810 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17813 /* If there was an initializer in the original tree, but it
17814 instantiated to an empty list, then we should pass a
17815 non-NULL empty vector to tell build_new that it was an
17816 empty initializer() rather than no initializer. This can
17817 only happen when the initializer is a pack expansion whose
17818 parameter packs are of length zero. */
17819 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17823 init_vec
= make_tree_vector ();
17824 if (init
== void_node
)
17825 gcc_assert (init_vec
!= NULL
);
17828 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17829 vec_safe_push (init_vec
, TREE_VALUE (init
));
17833 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17834 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17835 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17836 NEW_EXPR_USE_GLOBAL (t
),
17839 if (placement_vec
!= NULL
)
17840 release_tree_vector (placement_vec
);
17841 if (init_vec
!= NULL
)
17842 release_tree_vector (init_vec
);
17849 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17850 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17851 RETURN (delete_sanity (op0
, op1
,
17852 DELETE_EXPR_USE_VEC (t
),
17853 DELETE_EXPR_USE_GLOBAL (t
),
17857 case COMPOUND_EXPR
:
17859 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17860 complain
& ~tf_decltype
, in_decl
,
17861 /*function_p=*/false,
17862 integral_constant_expression_p
);
17863 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17865 RECUR (TREE_OPERAND (t
, 1)),
17866 complain
|decltype_flag
));
17872 vec
<tree
, va_gc
> *call_args
;
17873 unsigned int nargs
, i
;
17878 function
= CALL_EXPR_FN (t
);
17879 /* Internal function with no arguments. */
17880 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17883 /* When we parsed the expression, we determined whether or
17884 not Koenig lookup should be performed. */
17885 koenig_p
= KOENIG_LOOKUP_P (t
);
17886 if (function
== NULL_TREE
)
17889 qualified_p
= false;
17891 else if (TREE_CODE (function
) == SCOPE_REF
)
17893 qualified_p
= true;
17894 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17896 /*address_p=*/false);
17898 else if (koenig_p
&& identifier_p (function
))
17900 /* Do nothing; calling tsubst_copy_and_build on an identifier
17901 would incorrectly perform unqualified lookup again.
17903 Note that we can also have an IDENTIFIER_NODE if the earlier
17904 unqualified lookup found a member function; in that case
17905 koenig_p will be false and we do want to do the lookup
17906 again to find the instantiated member function.
17908 FIXME but doing that causes c++/15272, so we need to stop
17909 using IDENTIFIER_NODE in that situation. */
17910 qualified_p
= false;
17914 if (TREE_CODE (function
) == COMPONENT_REF
)
17916 tree op
= TREE_OPERAND (function
, 1);
17918 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17919 || (BASELINK_P (op
)
17920 && BASELINK_QUALIFIED_P (op
)));
17923 qualified_p
= false;
17925 if (TREE_CODE (function
) == ADDR_EXPR
17926 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17927 /* Avoid error about taking the address of a constructor. */
17928 function
= TREE_OPERAND (function
, 0);
17930 function
= tsubst_copy_and_build (function
, args
, complain
,
17933 integral_constant_expression_p
);
17935 if (BASELINK_P (function
))
17936 qualified_p
= true;
17939 nargs
= call_expr_nargs (t
);
17940 call_args
= make_tree_vector ();
17941 for (i
= 0; i
< nargs
; ++i
)
17943 tree arg
= CALL_EXPR_ARG (t
, i
);
17945 if (!PACK_EXPANSION_P (arg
))
17946 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17949 /* Expand the pack expansion and push each entry onto
17951 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17952 if (TREE_CODE (arg
) == TREE_VEC
)
17954 unsigned int len
, j
;
17956 len
= TREE_VEC_LENGTH (arg
);
17957 for (j
= 0; j
< len
; ++j
)
17959 tree value
= TREE_VEC_ELT (arg
, j
);
17960 if (value
!= NULL_TREE
)
17961 value
= convert_from_reference (value
);
17962 vec_safe_push (call_args
, value
);
17967 /* A partial substitution. Add one entry. */
17968 vec_safe_push (call_args
, arg
);
17973 /* We do not perform argument-dependent lookup if normal
17974 lookup finds a non-function, in accordance with the
17975 expected resolution of DR 218. */
17977 && ((is_overloaded_fn (function
)
17978 /* If lookup found a member function, the Koenig lookup is
17979 not appropriate, even if an unqualified-name was used
17980 to denote the function. */
17981 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17982 || identifier_p (function
))
17983 /* Only do this when substitution turns a dependent call
17984 into a non-dependent call. */
17985 && type_dependent_expression_p_push (t
)
17986 && !any_type_dependent_arguments_p (call_args
))
17987 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17989 if (function
!= NULL_TREE
17990 && identifier_p (function
)
17991 && !any_type_dependent_arguments_p (call_args
))
17993 if (koenig_p
&& (complain
& tf_warning_or_error
))
17995 /* For backwards compatibility and good diagnostics, try
17996 the unqualified lookup again if we aren't in SFINAE
17998 tree unq
= (tsubst_copy_and_build
17999 (function
, args
, complain
, in_decl
, true,
18000 integral_constant_expression_p
));
18001 if (unq
== error_mark_node
)
18003 release_tree_vector (call_args
);
18004 RETURN (error_mark_node
);
18007 if (unq
!= function
)
18009 /* In a lambda fn, we have to be careful to not
18010 introduce new this captures. Legacy code can't
18011 be using lambdas anyway, so it's ok to be
18013 bool in_lambda
= (current_class_type
18014 && LAMBDA_TYPE_P (current_class_type
));
18015 char const *const msg
18016 = G_("%qD was not declared in this scope, "
18017 "and no declarations were found by "
18018 "argument-dependent lookup at the point "
18019 "of instantiation");
18023 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
18026 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
18032 if (INDIRECT_REF_P (fn
))
18033 fn
= TREE_OPERAND (fn
, 0);
18034 if (is_overloaded_fn (fn
))
18035 fn
= get_first_fn (fn
);
18038 /* Can't say anything more. */;
18039 else if (DECL_CLASS_SCOPE_P (fn
))
18041 location_t loc
= EXPR_LOC_OR_LOC (t
,
18044 "declarations in dependent base %qT are "
18045 "not found by unqualified lookup",
18046 DECL_CLASS_CONTEXT (fn
));
18047 if (current_class_ptr
)
18049 "use %<this->%D%> instead", function
);
18052 "use %<%T::%D%> instead",
18053 current_class_name
, function
);
18056 inform (DECL_SOURCE_LOCATION (fn
),
18057 "%qD declared here, later in the "
18058 "translation unit", fn
);
18061 release_tree_vector (call_args
);
18062 RETURN (error_mark_node
);
18069 if (identifier_p (function
))
18071 if (complain
& tf_error
)
18072 unqualified_name_lookup_error (function
);
18073 release_tree_vector (call_args
);
18074 RETURN (error_mark_node
);
18078 /* Remember that there was a reference to this entity. */
18079 if (function
!= NULL_TREE
18080 && DECL_P (function
)
18081 && !mark_used (function
, complain
) && !(complain
& tf_error
))
18083 release_tree_vector (call_args
);
18084 RETURN (error_mark_node
);
18087 /* Put back tf_decltype for the actual call. */
18088 complain
|= decltype_flag
;
18090 if (function
== NULL_TREE
)
18091 switch (CALL_EXPR_IFN (t
))
18094 gcc_assert (nargs
== 1);
18095 if (vec_safe_length (call_args
) != 1)
18097 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
18098 "wrong number of arguments to "
18099 "%<__builtin_launder%>");
18100 ret
= error_mark_node
;
18103 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
18105 (*call_args
)[0], complain
);
18109 /* Unsupported internal function with arguments. */
18110 gcc_unreachable ();
18112 else if (TREE_CODE (function
) == OFFSET_REF
)
18113 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
18115 else if (TREE_CODE (function
) == COMPONENT_REF
)
18117 tree instance
= TREE_OPERAND (function
, 0);
18118 tree fn
= TREE_OPERAND (function
, 1);
18120 if (processing_template_decl
18121 && (type_dependent_expression_p (instance
)
18122 || (!BASELINK_P (fn
)
18123 && TREE_CODE (fn
) != FIELD_DECL
)
18124 || type_dependent_expression_p (fn
)
18125 || any_type_dependent_arguments_p (call_args
)))
18126 ret
= build_min_nt_call_vec (function
, call_args
);
18127 else if (!BASELINK_P (fn
))
18128 ret
= finish_call_expr (function
, &call_args
,
18129 /*disallow_virtual=*/false,
18130 /*koenig_p=*/false,
18133 ret
= (build_new_method_call
18135 &call_args
, NULL_TREE
,
18136 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18141 ret
= finish_call_expr (function
, &call_args
,
18142 /*disallow_virtual=*/qualified_p
,
18146 release_tree_vector (call_args
);
18148 if (ret
!= error_mark_node
)
18150 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18151 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18152 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18153 bool thk
= CALL_FROM_THUNK_P (t
);
18154 if (op
|| ord
|| rev
|| thk
)
18156 function
= extract_call_expr (ret
);
18157 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18158 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18159 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18162 if (TREE_CODE (function
) == CALL_EXPR
)
18163 CALL_FROM_THUNK_P (function
) = true;
18165 AGGR_INIT_FROM_THUNK_P (function
) = true;
18166 /* The thunk location is not interesting. */
18167 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
18177 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18178 tree folded_cond
= fold_non_dependent_expr (cond
);
18181 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
18183 if (integer_zerop (folded_cond
))
18185 ++c_inhibit_evaluation_warnings
;
18186 exp1
= RECUR (TREE_OPERAND (t
, 1));
18187 --c_inhibit_evaluation_warnings
;
18188 exp2
= RECUR (TREE_OPERAND (t
, 2));
18192 exp1
= RECUR (TREE_OPERAND (t
, 1));
18193 ++c_inhibit_evaluation_warnings
;
18194 exp2
= RECUR (TREE_OPERAND (t
, 2));
18195 --c_inhibit_evaluation_warnings
;
18197 cond
= folded_cond
;
18201 exp1
= RECUR (TREE_OPERAND (t
, 1));
18202 exp2
= RECUR (TREE_OPERAND (t
, 2));
18205 warning_sentinel
s(warn_duplicated_branches
);
18206 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
18207 cond
, exp1
, exp2
, complain
));
18210 case PSEUDO_DTOR_EXPR
:
18212 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18213 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18214 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
18215 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
18221 tree purpose
, value
, chain
;
18223 if (t
== void_list_node
)
18226 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
18227 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
18229 /* We have pack expansions, so expand those and
18230 create a new list out of it. */
18231 tree purposevec
= NULL_TREE
;
18232 tree valuevec
= NULL_TREE
;
18236 /* Expand the argument expressions. */
18237 if (TREE_PURPOSE (t
))
18238 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18239 complain
, in_decl
);
18240 if (TREE_VALUE (t
))
18241 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18242 complain
, in_decl
);
18244 /* Build the rest of the list. */
18245 chain
= TREE_CHAIN (t
);
18246 if (chain
&& chain
!= void_type_node
)
18247 chain
= RECUR (chain
);
18249 /* Determine the number of arguments. */
18250 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18252 len
= TREE_VEC_LENGTH (purposevec
);
18253 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18255 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18256 len
= TREE_VEC_LENGTH (valuevec
);
18259 /* Since we only performed a partial substitution into
18260 the argument pack, we only RETURN (a single list
18262 if (purposevec
== TREE_PURPOSE (t
)
18263 && valuevec
== TREE_VALUE (t
)
18264 && chain
== TREE_CHAIN (t
))
18267 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18270 /* Convert the argument vectors into a TREE_LIST */
18274 /* Grab the Ith values. */
18276 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18279 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18282 /* Build the list (backwards). */
18283 chain
= tree_cons (purpose
, value
, chain
);
18289 purpose
= TREE_PURPOSE (t
);
18291 purpose
= RECUR (purpose
);
18292 value
= TREE_VALUE (t
);
18294 value
= RECUR (value
);
18295 chain
= TREE_CHAIN (t
);
18296 if (chain
&& chain
!= void_type_node
)
18297 chain
= RECUR (chain
);
18298 if (purpose
== TREE_PURPOSE (t
)
18299 && value
== TREE_VALUE (t
)
18300 && chain
== TREE_CHAIN (t
))
18302 RETURN (tree_cons (purpose
, value
, chain
));
18305 case COMPONENT_REF
:
18312 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18313 args
, complain
, in_decl
);
18314 /* Remember that there was a reference to this entity. */
18315 if (DECL_P (object
)
18316 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18317 RETURN (error_mark_node
);
18318 object_type
= TREE_TYPE (object
);
18320 member
= TREE_OPERAND (t
, 1);
18321 if (BASELINK_P (member
))
18322 member
= tsubst_baselink (member
,
18323 non_reference (TREE_TYPE (object
)),
18324 args
, complain
, in_decl
);
18326 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18327 if (member
== error_mark_node
)
18328 RETURN (error_mark_node
);
18330 if (TREE_CODE (member
) == FIELD_DECL
)
18332 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18333 if (TREE_CODE (r
) == COMPONENT_REF
)
18334 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18337 else if (type_dependent_expression_p (object
))
18338 /* We can't do much here. */;
18339 else if (!CLASS_TYPE_P (object_type
))
18341 if (scalarish_type_p (object_type
))
18343 tree s
= NULL_TREE
;
18344 tree dtor
= member
;
18346 if (TREE_CODE (dtor
) == SCOPE_REF
)
18348 s
= TREE_OPERAND (dtor
, 0);
18349 dtor
= TREE_OPERAND (dtor
, 1);
18351 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18353 dtor
= TREE_OPERAND (dtor
, 0);
18355 RETURN (finish_pseudo_destructor_expr
18356 (object
, s
, dtor
, input_location
));
18360 else if (TREE_CODE (member
) == SCOPE_REF
18361 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18363 /* Lookup the template functions now that we know what the
18365 tree scope
= TREE_OPERAND (member
, 0);
18366 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18367 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18368 member
= lookup_qualified_name (scope
, tmpl
,
18369 /*is_type_p=*/false,
18370 /*complain=*/false);
18371 if (BASELINK_P (member
))
18373 BASELINK_FUNCTIONS (member
)
18374 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18376 member
= (adjust_result_of_qualified_name_lookup
18377 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18382 qualified_name_lookup_error (scope
, tmpl
, member
,
18384 RETURN (error_mark_node
);
18387 else if (TREE_CODE (member
) == SCOPE_REF
18388 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18389 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18391 if (complain
& tf_error
)
18393 if (TYPE_P (TREE_OPERAND (member
, 0)))
18394 error ("%qT is not a class or namespace",
18395 TREE_OPERAND (member
, 0));
18397 error ("%qD is not a class or namespace",
18398 TREE_OPERAND (member
, 0));
18400 RETURN (error_mark_node
);
18403 r
= finish_class_member_access_expr (object
, member
,
18404 /*template_p=*/false,
18406 if (TREE_CODE (r
) == COMPONENT_REF
)
18407 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18412 RETURN (build_throw
18413 (RECUR (TREE_OPERAND (t
, 0))));
18417 vec
<constructor_elt
, va_gc
> *n
;
18418 constructor_elt
*ce
;
18419 unsigned HOST_WIDE_INT idx
;
18420 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18421 bool process_index_p
;
18423 bool need_copy_p
= false;
18426 if (type
== error_mark_node
)
18427 RETURN (error_mark_node
);
18429 /* We do not want to process the index of aggregate
18430 initializers as they are identifier nodes which will be
18431 looked up by digest_init. */
18432 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18434 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18435 newlen
= vec_safe_length (n
);
18436 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18438 if (ce
->index
&& process_index_p
18439 /* An identifier index is looked up in the type
18440 being initialized, not the current scope. */
18441 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18442 ce
->index
= RECUR (ce
->index
);
18444 if (PACK_EXPANSION_P (ce
->value
))
18446 /* Substitute into the pack expansion. */
18447 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18450 if (ce
->value
== error_mark_node
18451 || PACK_EXPANSION_P (ce
->value
))
18453 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18454 /* Just move the argument into place. */
18455 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18458 /* Update the length of the final CONSTRUCTOR
18459 arguments vector, and note that we will need to
18461 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18462 need_copy_p
= true;
18466 ce
->value
= RECUR (ce
->value
);
18471 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18473 vec_alloc (n
, newlen
);
18474 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18476 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18478 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18479 for (i
= 0; i
< len
; ++i
)
18480 CONSTRUCTOR_APPEND_ELT (n
, 0,
18481 TREE_VEC_ELT (ce
->value
, i
));
18484 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18488 r
= build_constructor (init_list_type_node
, n
);
18489 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18491 if (TREE_HAS_CONSTRUCTOR (t
))
18493 fcl_t cl
= fcl_functional
;
18494 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18496 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18499 TREE_TYPE (r
) = type
;
18505 tree operand_0
= TREE_OPERAND (t
, 0);
18506 if (TYPE_P (operand_0
))
18508 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18509 RETURN (get_typeid (operand_0
, complain
));
18513 operand_0
= RECUR (operand_0
);
18514 RETURN (build_typeid (operand_0
, complain
));
18525 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18526 /* ??? We're doing a subset of finish_id_expression here. */
18528 && !processing_template_decl
18529 && !cp_unevaluated_operand
18530 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18531 && CP_DECL_THREAD_LOCAL_P (r
))
18533 if (tree wrap
= get_tls_wrapper_fn (r
))
18534 /* Replace an evaluated use of the thread_local variable with
18535 a call to its wrapper. */
18536 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18538 else if (outer_automatic_var_p (r
))
18539 r
= process_outer_var_ref (r
, complain
);
18541 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18542 /* If the original type was a reference, we'll be wrapped in
18543 the appropriate INDIRECT_REF. */
18544 r
= convert_from_reference (r
);
18550 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18551 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18552 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18555 case OFFSETOF_EXPR
:
18558 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18559 in_decl
, /*function_p=*/false,
18560 /*integral_constant_expression_p=*/false);
18561 RETURN (finish_offsetof (object_ptr
,
18562 RECUR (TREE_OPERAND (t
, 0)),
18563 EXPR_LOCATION (t
)));
18566 case ADDRESSOF_EXPR
:
18567 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18568 RECUR (TREE_OPERAND (t
, 0)), complain
));
18572 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18573 complain
, in_decl
);
18575 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18576 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18577 type2
= RECUR (type2
);
18579 type2
= tsubst (type2
, args
, complain
, in_decl
);
18581 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18586 tree old_stmt_expr
= cur_stmt_expr
;
18587 tree stmt_expr
= begin_stmt_expr ();
18589 cur_stmt_expr
= stmt_expr
;
18590 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18591 integral_constant_expression_p
);
18592 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18593 cur_stmt_expr
= old_stmt_expr
;
18595 /* If the resulting list of expression statement is empty,
18596 fold it further into void_node. */
18597 if (empty_expr_stmt_p (stmt_expr
))
18598 stmt_expr
= void_node
;
18600 RETURN (stmt_expr
);
18605 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18607 RETURN (build_lambda_object (r
));
18611 /* We can get here for a constant initializer of non-dependent type.
18612 FIXME stop folding in cp_parser_initializer_clause. */
18614 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18619 case TRANSACTION_EXPR
:
18620 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18621 integral_constant_expression_p
));
18624 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18626 case VEC_PERM_EXPR
:
18628 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18629 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18630 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18631 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18635 case REQUIRES_EXPR
:
18636 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18638 case NON_LVALUE_EXPR
:
18639 case VIEW_CONVERT_EXPR
:
18640 /* We should only see these for location wrapper nodes, or within
18641 instantiate_non_dependent_expr (when args is NULL_TREE). */
18642 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18643 if (location_wrapper_p (t
))
18644 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18645 EXPR_LOCATION (t
)));
18649 /* Handle Objective-C++ constructs, if appropriate. */
18652 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18653 in_decl
, /*function_p=*/false);
18657 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18663 input_location
= loc
;
18667 /* Verify that the instantiated ARGS are valid. For type arguments,
18668 make sure that the type's linkage is ok. For non-type arguments,
18669 make sure they are constants if they are integral or enumerations.
18670 Emit an error under control of COMPLAIN, and return TRUE on error. */
18673 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18675 if (dependent_template_arg_p (t
))
18677 if (ARGUMENT_PACK_P (t
))
18679 tree vec
= ARGUMENT_PACK_ARGS (t
);
18680 int len
= TREE_VEC_LENGTH (vec
);
18681 bool result
= false;
18684 for (i
= 0; i
< len
; ++i
)
18685 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18689 else if (TYPE_P (t
))
18691 /* [basic.link]: A name with no linkage (notably, the name
18692 of a class or enumeration declared in a local scope)
18693 shall not be used to declare an entity with linkage.
18694 This implies that names with no linkage cannot be used as
18697 DR 757 relaxes this restriction for C++0x. */
18698 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18699 : no_linkage_check (t
, /*relaxed_p=*/false));
18703 /* DR 488 makes use of a type with no linkage cause
18704 type deduction to fail. */
18705 if (complain
& tf_error
)
18707 if (TYPE_UNNAMED_P (nt
))
18708 error ("%qT is/uses unnamed type", t
);
18710 error ("template argument for %qD uses local type %qT",
18715 /* In order to avoid all sorts of complications, we do not
18716 allow variably-modified types as template arguments. */
18717 else if (variably_modified_type_p (t
, NULL_TREE
))
18719 if (complain
& tf_error
)
18720 error ("%qT is a variably modified type", t
);
18724 /* Class template and alias template arguments should be OK. */
18725 else if (DECL_TYPE_TEMPLATE_P (t
))
18727 /* A non-type argument of integral or enumerated type must be a
18729 else if (TREE_TYPE (t
)
18730 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18731 && !REFERENCE_REF_P (t
)
18732 && !TREE_CONSTANT (t
))
18734 if (complain
& tf_error
)
18735 error ("integral expression %qE is not constant", t
);
18742 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18744 int ix
, len
= DECL_NTPARMS (tmpl
);
18745 bool result
= false;
18747 for (ix
= 0; ix
!= len
; ix
++)
18749 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18752 if (result
&& (complain
& tf_error
))
18753 error (" trying to instantiate %qD", tmpl
);
18757 /* We're out of SFINAE context now, so generate diagnostics for the access
18758 errors we saw earlier when instantiating D from TMPL and ARGS. */
18761 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18763 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18764 tree type
= TREE_TYPE (pattern
);
18765 location_t loc
= input_location
;
18767 push_access_scope (d
);
18768 push_deferring_access_checks (dk_no_deferred
);
18769 input_location
= DECL_SOURCE_LOCATION (pattern
);
18770 tsubst (type
, args
, tf_warning_or_error
, d
);
18771 input_location
= loc
;
18772 pop_deferring_access_checks ();
18773 pop_access_scope (d
);
18776 /* Instantiate the indicated variable, function, or alias template TMPL with
18777 the template arguments in TARG_PTR. */
18780 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18782 tree targ_ptr
= orig_args
;
18786 bool access_ok
= true;
18788 if (tmpl
== error_mark_node
)
18789 return error_mark_node
;
18791 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18793 /* If this function is a clone, handle it specially. */
18794 if (DECL_CLONED_FUNCTION_P (tmpl
))
18799 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18800 DECL_CLONED_FUNCTION. */
18801 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18802 targ_ptr
, complain
);
18803 if (spec
== error_mark_node
)
18804 return error_mark_node
;
18806 /* Look for the clone. */
18807 FOR_EACH_CLONE (clone
, spec
)
18808 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18810 /* We should always have found the clone by now. */
18811 gcc_unreachable ();
18815 if (targ_ptr
== error_mark_node
)
18816 return error_mark_node
;
18818 /* Check to see if we already have this specialization. */
18819 gen_tmpl
= most_general_template (tmpl
);
18820 if (TMPL_ARGS_DEPTH (targ_ptr
)
18821 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18822 /* targ_ptr only has the innermost template args, so add the outer ones
18823 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18824 the case of a non-dependent call within a template definition). */
18825 targ_ptr
= (add_outermost_template_args
18826 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18829 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18830 but it doesn't seem to be on the hot path. */
18831 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18833 gcc_assert (tmpl
== gen_tmpl
18834 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18836 || fndecl
== NULL_TREE
);
18838 if (spec
!= NULL_TREE
)
18840 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18842 if (complain
& tf_error
)
18843 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18844 return error_mark_node
;
18849 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18851 return error_mark_node
;
18853 /* We are building a FUNCTION_DECL, during which the access of its
18854 parameters and return types have to be checked. However this
18855 FUNCTION_DECL which is the desired context for access checking
18856 is not built yet. We solve this chicken-and-egg problem by
18857 deferring all checks until we have the FUNCTION_DECL. */
18858 push_deferring_access_checks (dk_deferred
);
18860 /* Instantiation of the function happens in the context of the function
18861 template, not the context of the overload resolution we're doing. */
18862 push_to_top_level ();
18863 /* If there are dependent arguments, e.g. because we're doing partial
18864 ordering, make sure processing_template_decl stays set. */
18865 if (uses_template_parms (targ_ptr
))
18866 ++processing_template_decl
;
18867 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18869 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18870 complain
, gen_tmpl
, true);
18871 push_nested_class (ctx
);
18874 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18876 fndecl
= NULL_TREE
;
18877 if (VAR_P (pattern
))
18879 /* We need to determine if we're using a partial or explicit
18880 specialization now, because the type of the variable could be
18882 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18883 tree elt
= most_specialized_partial_spec (tid
, complain
);
18884 if (elt
== error_mark_node
)
18885 pattern
= error_mark_node
;
18888 tree partial_tmpl
= TREE_VALUE (elt
);
18889 tree partial_args
= TREE_PURPOSE (elt
);
18890 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18891 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18895 /* Substitute template parameters to obtain the specialization. */
18896 if (fndecl
== NULL_TREE
)
18897 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18898 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18899 pop_nested_class ();
18900 pop_from_top_level ();
18902 if (fndecl
== error_mark_node
)
18904 pop_deferring_access_checks ();
18905 return error_mark_node
;
18908 /* The DECL_TI_TEMPLATE should always be the immediate parent
18909 template, not the most general template. */
18910 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18911 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18913 /* Now we know the specialization, compute access previously
18914 deferred. Do no access control for inheriting constructors,
18915 as we already checked access for the inherited constructor. */
18916 if (!(flag_new_inheriting_ctors
18917 && DECL_INHERITED_CTOR (fndecl
)))
18919 push_access_scope (fndecl
);
18920 if (!perform_deferred_access_checks (complain
))
18922 pop_access_scope (fndecl
);
18924 pop_deferring_access_checks ();
18926 /* If we've just instantiated the main entry point for a function,
18927 instantiate all the alternate entry points as well. We do this
18928 by cloning the instantiation of the main entry point, not by
18929 instantiating the template clones. */
18930 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18931 clone_function_decl (fndecl
, /*update_methods=*/false);
18935 if (!(complain
& tf_error
))
18937 /* Remember to reinstantiate when we're out of SFINAE so the user
18938 can see the errors. */
18939 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18941 return error_mark_node
;
18946 /* Wrapper for instantiate_template_1. */
18949 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18952 timevar_push (TV_TEMPLATE_INST
);
18953 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18954 timevar_pop (TV_TEMPLATE_INST
);
18958 /* Instantiate the alias template TMPL with ARGS. Also push a template
18959 instantiation level, which instantiate_template doesn't do because
18960 functions and variables have sufficient context established by the
18964 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18966 struct pending_template
*old_last_pend
= last_pending_template
;
18967 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18968 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18969 return error_mark_node
;
18970 tree tinst
= build_tree_list (tmpl
, args
);
18971 if (!push_tinst_level (tinst
))
18974 return error_mark_node
;
18978 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18979 args
, tmpl
, complain
,
18980 /*require_all_args=*/true,
18981 /*use_default_args=*/true);
18983 tree r
= instantiate_template (tmpl
, args
, complain
);
18984 pop_tinst_level ();
18985 /* We can't free this if a pending_template entry or last_error_tinst_level
18986 is pointing at it. */
18987 if (last_pending_template
== old_last_pend
18988 && last_error_tinst_level
== old_error_tinst
)
18994 /* PARM is a template parameter pack for FN. Returns true iff
18995 PARM is used in a deducible way in the argument list of FN. */
18998 pack_deducible_p (tree parm
, tree fn
)
19000 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19001 for (; t
; t
= TREE_CHAIN (t
))
19003 tree type
= TREE_VALUE (t
);
19005 if (!PACK_EXPANSION_P (type
))
19007 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19008 packs
; packs
= TREE_CHAIN (packs
))
19009 if (template_args_equal (TREE_VALUE (packs
), parm
))
19011 /* The template parameter pack is used in a function parameter
19012 pack. If this is the end of the parameter list, the
19013 template parameter pack is deducible. */
19014 if (TREE_CHAIN (t
) == void_list_node
)
19017 /* Otherwise, not. Well, it could be deduced from
19018 a non-pack parameter, but doing so would end up with
19019 a deduction mismatch, so don't bother. */
19023 /* The template parameter pack isn't used in any function parameter
19024 packs, but it might be used deeper, e.g. tuple<Args...>. */
19028 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19029 NARGS elements of the arguments that are being used when calling
19030 it. TARGS is a vector into which the deduced template arguments
19033 Returns either a FUNCTION_DECL for the matching specialization of FN or
19034 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19035 true, diagnostics will be printed to explain why it failed.
19037 If FN is a conversion operator, or we are trying to produce a specific
19038 specialization, RETURN_TYPE is the return type desired.
19040 The EXPLICIT_TARGS are explicit template arguments provided via a
19043 The parameter STRICT is one of:
19046 We are deducing arguments for a function call, as in
19047 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19048 deducing arguments for a call to the result of a conversion
19049 function template, as in [over.call.object].
19052 We are deducing arguments for a conversion function, as in
19053 [temp.deduct.conv].
19056 We are deducing arguments when doing an explicit instantiation
19057 as in [temp.explicit], when determining an explicit specialization
19058 as in [temp.expl.spec], or when taking the address of a function
19059 template, as in [temp.deduct.funcaddr]. */
19062 fn_type_unification (tree fn
,
19063 tree explicit_targs
,
19066 unsigned int nargs
,
19068 unification_kind_t strict
,
19075 tree decl
= NULL_TREE
;
19076 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19078 static int deduction_depth
;
19079 struct pending_template
*old_last_pend
= last_pending_template
;
19080 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
19083 if (flag_new_inheriting_ctors
)
19084 fn
= strip_inheriting_ctors (fn
);
19086 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
19088 tree r
= error_mark_node
;
19090 tree full_targs
= targs
;
19091 if (TMPL_ARGS_DEPTH (targs
)
19092 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
19093 full_targs
= (add_outermost_template_args
19094 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
19098 complain
|= tf_decltype
;
19100 /* In C++0x, it's possible to have a function template whose type depends
19101 on itself recursively. This is most obvious with decltype, but can also
19102 occur with enumeration scope (c++/48969). So we need to catch infinite
19103 recursion and reject the substitution at deduction time; this function
19104 will return error_mark_node for any repeated substitution.
19106 This also catches excessive recursion such as when f<N> depends on
19107 f<N-1> across all integers, and returns error_mark_node for all the
19108 substitutions back up to the initial one.
19110 This is, of course, not reentrant. */
19111 if (excessive_deduction_depth
)
19112 return error_mark_node
;
19113 tinst
= build_tree_list (fn
, NULL_TREE
);
19116 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
19118 fntype
= TREE_TYPE (fn
);
19119 if (explicit_targs
)
19123 The specified template arguments must match the template
19124 parameters in kind (i.e., type, nontype, template), and there
19125 must not be more arguments than there are parameters;
19126 otherwise type deduction fails.
19128 Nontype arguments must match the types of the corresponding
19129 nontype template parameters, or must be convertible to the
19130 types of the corresponding nontype parameters as specified in
19131 _temp.arg.nontype_, otherwise type deduction fails.
19133 All references in the function type of the function template
19134 to the corresponding template parameters are replaced by the
19135 specified template argument values. If a substitution in a
19136 template parameter or in the function type of the function
19137 template results in an invalid type, type deduction fails. */
19138 int i
, len
= TREE_VEC_LENGTH (tparms
);
19139 location_t loc
= input_location
;
19140 bool incomplete
= false;
19142 if (explicit_targs
== error_mark_node
)
19145 if (TMPL_ARGS_DEPTH (explicit_targs
)
19146 < TMPL_ARGS_DEPTH (full_targs
))
19147 explicit_targs
= add_outermost_template_args (full_targs
,
19150 /* Adjust any explicit template arguments before entering the
19151 substitution context. */
19153 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
19155 /*require_all_args=*/false,
19156 /*use_default_args=*/false));
19157 if (explicit_targs
== error_mark_node
)
19160 /* Substitute the explicit args into the function type. This is
19161 necessary so that, for instance, explicitly declared function
19162 arguments can match null pointed constants. If we were given
19163 an incomplete set of explicit args, we must not do semantic
19164 processing during substitution as we could create partial
19166 for (i
= 0; i
< len
; i
++)
19168 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
19169 bool parameter_pack
= false;
19170 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
19172 /* Dig out the actual parm. */
19173 if (TREE_CODE (parm
) == TYPE_DECL
19174 || TREE_CODE (parm
) == TEMPLATE_DECL
)
19176 parm
= TREE_TYPE (parm
);
19177 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
19179 else if (TREE_CODE (parm
) == PARM_DECL
)
19181 parm
= DECL_INITIAL (parm
);
19182 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
19185 if (!parameter_pack
&& targ
== NULL_TREE
)
19186 /* No explicit argument for this template parameter. */
19189 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
19191 /* Mark the argument pack as "incomplete". We could
19192 still deduce more arguments during unification.
19193 We remove this mark in type_unification_real. */
19196 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
19197 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
19198 = ARGUMENT_PACK_ARGS (targ
);
19201 /* We have some incomplete argument packs. */
19206 TREE_VALUE (tinst
) = explicit_targs
;
19207 if (!push_tinst_level (tinst
))
19209 excessive_deduction_depth
= true;
19212 processing_template_decl
+= incomplete
;
19213 input_location
= DECL_SOURCE_LOCATION (fn
);
19214 /* Ignore any access checks; we'll see them again in
19215 instantiate_template and they might have the wrong
19216 access path at this point. */
19217 push_deferring_access_checks (dk_deferred
);
19218 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
19219 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
19220 pop_deferring_access_checks ();
19221 input_location
= loc
;
19222 processing_template_decl
-= incomplete
;
19223 pop_tinst_level ();
19225 if (fntype
== error_mark_node
)
19228 /* Place the explicitly specified arguments in TARGS. */
19229 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
19230 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
19231 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
19234 /* Never do unification on the 'this' parameter. */
19235 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
19237 if (return_type
&& strict
== DEDUCE_CALL
)
19239 /* We're deducing for a call to the result of a template conversion
19240 function. The parms we really want are in return_type. */
19241 if (POINTER_TYPE_P (return_type
))
19242 return_type
= TREE_TYPE (return_type
);
19243 parms
= TYPE_ARG_TYPES (return_type
);
19245 else if (return_type
)
19249 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19250 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19251 new_args
[0] = return_type
;
19252 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19257 /* We allow incomplete unification without an error message here
19258 because the standard doesn't seem to explicitly prohibit it. Our
19259 callers must be ready to deal with unification failures in any
19262 TREE_VALUE (tinst
) = targs
;
19263 /* If we aren't explaining yet, push tinst context so we can see where
19264 any errors (e.g. from class instantiations triggered by instantiation
19265 of default template arguments) come from. If we are explaining, this
19266 context is redundant. */
19267 if (!explain_p
&& !push_tinst_level (tinst
))
19269 excessive_deduction_depth
= true;
19273 /* type_unification_real will pass back any access checks from default
19274 template argument substitution. */
19275 vec
<deferred_access_check
, va_gc
> *checks
;
19278 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19279 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19280 strict
, flags
, &checks
, explain_p
);
19282 pop_tinst_level ();
19286 /* Now that we have bindings for all of the template arguments,
19287 ensure that the arguments deduced for the template template
19288 parameters have compatible template parameter lists. We cannot
19289 check this property before we have deduced all template
19290 arguments, because the template parameter types of a template
19291 template parameter might depend on prior template parameters
19292 deduced after the template template parameter. The following
19293 ill-formed example illustrates this issue:
19295 template<typename T, template<T> class C> void f(C<5>, T);
19297 template<int N> struct X {};
19300 f(X<5>(), 5l); // error: template argument deduction fails
19303 The template parameter list of 'C' depends on the template type
19304 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19305 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19306 time that we deduce 'C'. */
19307 if (!template_template_parm_bindings_ok_p
19308 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19310 unify_inconsistent_template_template_parameters (explain_p
);
19314 /* All is well so far. Now, check:
19318 When all template arguments have been deduced, all uses of
19319 template parameters in nondeduced contexts are replaced with
19320 the corresponding deduced argument values. If the
19321 substitution results in an invalid type, as described above,
19322 type deduction fails. */
19323 TREE_VALUE (tinst
) = targs
;
19324 if (!push_tinst_level (tinst
))
19326 excessive_deduction_depth
= true;
19330 /* Also collect access checks from the instantiation. */
19331 reopen_deferring_access_checks (checks
);
19333 decl
= instantiate_template (fn
, targs
, complain
);
19335 checks
= get_deferred_access_checks ();
19336 pop_deferring_access_checks ();
19338 pop_tinst_level ();
19340 if (decl
== error_mark_node
)
19343 /* Now perform any access checks encountered during substitution. */
19344 push_access_scope (decl
);
19345 ok
= perform_access_checks (checks
, complain
);
19346 pop_access_scope (decl
);
19350 /* If we're looking for an exact match, check that what we got
19351 is indeed an exact match. It might not be if some template
19352 parameters are used in non-deduced contexts. But don't check
19353 for an exact match if we have dependent template arguments;
19354 in that case we're doing partial ordering, and we already know
19355 that we have two candidates that will provide the actual type. */
19356 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19358 tree substed
= TREE_TYPE (decl
);
19362 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19364 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19365 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19366 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19368 unify_type_mismatch (explain_p
, args
[i
],
19369 TREE_VALUE (sarg
));
19374 /* After doing deduction with the inherited constructor, actually return an
19375 instantiation of the inheriting constructor. */
19377 decl
= instantiate_template (orig_fn
, targs
, complain
);
19383 if (excessive_deduction_depth
)
19385 if (deduction_depth
== 0)
19386 /* Reset once we're all the way out. */
19387 excessive_deduction_depth
= false;
19390 /* We can't free this if a pending_template entry or last_error_tinst_level
19391 is pointing at it. */
19392 if (last_pending_template
== old_last_pend
19393 && last_error_tinst_level
== old_error_tinst
)
19399 /* Adjust types before performing type deduction, as described in
19400 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19401 sections are symmetric. PARM is the type of a function parameter
19402 or the return type of the conversion function. ARG is the type of
19403 the argument passed to the call, or the type of the value
19404 initialized with the result of the conversion function.
19405 ARG_EXPR is the original argument expression, which may be null. */
19408 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19421 /* Swap PARM and ARG throughout the remainder of this
19422 function; the handling is precisely symmetric since PARM
19423 will initialize ARG rather than vice versa. */
19424 std::swap (parm
, arg
);
19428 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19429 too, but here handle it by stripping the reference from PARM
19430 rather than by adding it to ARG. */
19431 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19432 && TYPE_REF_IS_RVALUE (*parm
)
19433 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19434 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19435 && TREE_CODE (*arg
) == REFERENCE_TYPE
19436 && !TYPE_REF_IS_RVALUE (*arg
))
19437 *parm
= TREE_TYPE (*parm
);
19438 /* Nothing else to do in this case. */
19442 gcc_unreachable ();
19445 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19447 /* [temp.deduct.call]
19449 If P is not a reference type:
19451 --If A is an array type, the pointer type produced by the
19452 array-to-pointer standard conversion (_conv.array_) is
19453 used in place of A for type deduction; otherwise,
19455 --If A is a function type, the pointer type produced by
19456 the function-to-pointer standard conversion
19457 (_conv.func_) is used in place of A for type deduction;
19460 --If A is a cv-qualified type, the top level
19461 cv-qualifiers of A's type are ignored for type
19463 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19464 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19465 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19466 *arg
= build_pointer_type (*arg
);
19468 *arg
= TYPE_MAIN_VARIANT (*arg
);
19471 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19472 reference to a cv-unqualified template parameter that does not represent a
19473 template parameter of a class template (during class template argument
19474 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19475 an lvalue, the type "lvalue reference to A" is used in place of A for type
19477 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19478 && TYPE_REF_IS_RVALUE (*parm
)
19479 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19480 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19481 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19482 && (arg_expr
? lvalue_p (arg_expr
)
19483 /* try_one_overload doesn't provide an arg_expr, but
19484 functions are always lvalues. */
19485 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19486 *arg
= build_reference_type (*arg
);
19488 /* [temp.deduct.call]
19490 If P is a cv-qualified type, the top level cv-qualifiers
19491 of P's type are ignored for type deduction. If P is a
19492 reference type, the type referred to by P is used for
19494 *parm
= TYPE_MAIN_VARIANT (*parm
);
19495 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19497 *parm
= TREE_TYPE (*parm
);
19498 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19501 /* DR 322. For conversion deduction, remove a reference type on parm
19502 too (which has been swapped into ARG). */
19503 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19504 *arg
= TREE_TYPE (*arg
);
19509 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19510 template which does contain any deducible template parameters; check if
19511 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19512 unify_one_argument. */
19515 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19516 int flags
, bool explain_p
)
19521 type
= TREE_TYPE (arg
);
19525 if (same_type_p (parm
, type
))
19526 return unify_success (explain_p
);
19528 if (strict
== DEDUCE_CONV
)
19530 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19531 explain_p
? tf_warning_or_error
: tf_none
))
19532 return unify_success (explain_p
);
19534 else if (strict
!= DEDUCE_EXACT
)
19536 if (can_convert_arg (parm
, type
,
19537 TYPE_P (arg
) ? NULL_TREE
: arg
,
19538 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19539 return unify_success (explain_p
);
19542 if (strict
== DEDUCE_EXACT
)
19543 return unify_type_mismatch (explain_p
, parm
, arg
);
19545 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19548 static bool uses_deducible_template_parms (tree type
);
19550 /* Returns true iff the expression EXPR is one from which a template
19551 argument can be deduced. In other words, if it's an undecorated
19552 use of a template non-type parameter. */
19555 deducible_expression (tree expr
)
19557 /* Strip implicit conversions. */
19558 while (CONVERT_EXPR_P (expr
))
19559 expr
= TREE_OPERAND (expr
, 0);
19560 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19563 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19564 deducible way; that is, if it has a max value of <PARM> - 1. */
19567 deducible_array_bound (tree domain
)
19569 if (domain
== NULL_TREE
)
19572 tree max
= TYPE_MAX_VALUE (domain
);
19573 if (TREE_CODE (max
) != MINUS_EXPR
)
19576 return deducible_expression (TREE_OPERAND (max
, 0));
19579 /* Returns true iff the template arguments ARGS use a template parameter
19580 in a deducible way. */
19583 deducible_template_args (tree args
)
19585 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19588 tree elt
= TREE_VEC_ELT (args
, i
);
19589 if (ARGUMENT_PACK_P (elt
))
19590 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19593 if (PACK_EXPANSION_P (elt
))
19594 elt
= PACK_EXPANSION_PATTERN (elt
);
19595 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19597 else if (TYPE_P (elt
))
19598 deducible
= uses_deducible_template_parms (elt
);
19600 deducible
= deducible_expression (elt
);
19608 /* Returns true iff TYPE contains any deducible references to template
19609 parameters, as per 14.8.2.5. */
19612 uses_deducible_template_parms (tree type
)
19614 if (PACK_EXPANSION_P (type
))
19615 type
= PACK_EXPANSION_PATTERN (type
);
19622 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19623 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19629 if (POINTER_TYPE_P (type
))
19630 return uses_deducible_template_parms (TREE_TYPE (type
));
19632 /* T[integer-constant ]
19634 if (TREE_CODE (type
) == ARRAY_TYPE
)
19635 return (uses_deducible_template_parms (TREE_TYPE (type
))
19636 || deducible_array_bound (TYPE_DOMAIN (type
)));
19648 if (TYPE_PTRMEM_P (type
))
19649 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19650 || (uses_deducible_template_parms
19651 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19653 /* template-name <T> (where template-name refers to a class template)
19654 template-name <i> (where template-name refers to a class template) */
19655 if (CLASS_TYPE_P (type
)
19656 && CLASSTYPE_TEMPLATE_INFO (type
)
19657 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19658 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19659 (CLASSTYPE_TI_ARGS (type
)));
19664 if (TREE_CODE (type
) == FUNCTION_TYPE
19665 || TREE_CODE (type
) == METHOD_TYPE
)
19667 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19669 tree parm
= TYPE_ARG_TYPES (type
);
19670 if (TREE_CODE (type
) == METHOD_TYPE
)
19671 parm
= TREE_CHAIN (parm
);
19672 for (; parm
; parm
= TREE_CHAIN (parm
))
19673 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19680 /* Subroutine of type_unification_real and unify_pack_expansion to
19681 handle unification of a single P/A pair. Parameters are as
19682 for those functions. */
19685 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19686 int subr
, unification_kind_t strict
,
19689 tree arg_expr
= NULL_TREE
;
19692 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19693 return unify_invalid (explain_p
);
19694 if (arg
== unknown_type_node
)
19695 /* We can't deduce anything from this, but we might get all the
19696 template args from other function args. */
19697 return unify_success (explain_p
);
19699 /* Implicit conversions (Clause 4) will be performed on a function
19700 argument to convert it to the type of the corresponding function
19701 parameter if the parameter type contains no template-parameters that
19702 participate in template argument deduction. */
19703 if (strict
!= DEDUCE_EXACT
19704 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19705 /* For function parameters with no deducible template parameters,
19706 just return. We'll check non-dependent conversions later. */
19707 return unify_success (explain_p
);
19712 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19713 | UNIFY_ALLOW_MORE_CV_QUAL
19714 | UNIFY_ALLOW_DERIVED
);
19718 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19722 arg_strict
= UNIFY_ALLOW_NONE
;
19726 gcc_unreachable ();
19729 /* We only do these transformations if this is the top-level
19730 parameter_type_list in a call or declaration matching; in other
19731 situations (nested function declarators, template argument lists) we
19732 won't be comparing a type to an expression, and we don't do any type
19738 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19739 if (type_unknown_p (arg
))
19741 /* [temp.deduct.type] A template-argument can be
19742 deduced from a pointer to function or pointer
19743 to member function argument if the set of
19744 overloaded functions does not contain function
19745 templates and at most one of a set of
19746 overloaded functions provides a unique
19748 resolve_overloaded_unification (tparms
, targs
, parm
,
19750 arg_strict
, explain_p
);
19751 /* If a unique match was not found, this is a
19752 non-deduced context, so we still succeed. */
19753 return unify_success (explain_p
);
19757 arg
= unlowered_expr_type (arg
);
19758 if (arg
== error_mark_node
)
19759 return unify_invalid (explain_p
);
19763 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19766 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19767 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19768 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19770 /* For deduction from an init-list we need the actual list. */
19771 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19773 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19776 /* for_each_template_parm callback that always returns 0. */
19779 zero_r (tree
, void *)
19784 /* for_each_template_parm any_fn callback to handle deduction of a template
19785 type argument from the type of an array bound. */
19788 array_deduction_r (tree t
, void *data
)
19790 tree_pair_p d
= (tree_pair_p
)data
;
19791 tree
&tparms
= d
->purpose
;
19792 tree
&targs
= d
->value
;
19794 if (TREE_CODE (t
) == ARRAY_TYPE
)
19795 if (tree dom
= TYPE_DOMAIN (t
))
19796 if (tree max
= TYPE_MAX_VALUE (dom
))
19798 if (TREE_CODE (max
) == MINUS_EXPR
)
19799 max
= TREE_OPERAND (max
, 0);
19800 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19801 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19802 UNIFY_ALLOW_NONE
, /*explain*/false);
19805 /* Keep walking. */
19809 /* Try to deduce any not-yet-deduced template type arguments from the type of
19810 an array bound. This is handled separately from unify because 14.8.2.5 says
19811 "The type of a type parameter is only deduced from an array bound if it is
19812 not otherwise deduced." */
19815 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19817 tree_pair_s data
= { tparms
, targs
};
19818 hash_set
<tree
> visited
;
19819 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19820 /*nondeduced*/false, array_deduction_r
);
19823 /* Most parms like fn_type_unification.
19825 If SUBR is 1, we're being called recursively (to unify the
19826 arguments of a function or method parameter of a function
19829 CHECKS is a pointer to a vector of access checks encountered while
19830 substituting default template arguments. */
19833 type_unification_real (tree tparms
,
19837 unsigned int xnargs
,
19839 unification_kind_t strict
,
19841 vec
<deferred_access_check
, va_gc
> **checks
,
19846 int ntparms
= TREE_VEC_LENGTH (tparms
);
19847 int saw_undeduced
= 0;
19850 unsigned int nargs
;
19853 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19854 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19855 gcc_assert (ntparms
> 0);
19857 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19859 /* Reset the number of non-defaulted template arguments contained
19861 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19869 while (parms
&& parms
!= void_list_node
19872 parm
= TREE_VALUE (parms
);
19874 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19875 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19876 /* For a function parameter pack that occurs at the end of the
19877 parameter-declaration-list, the type A of each remaining
19878 argument of the call is compared with the type P of the
19879 declarator-id of the function parameter pack. */
19882 parms
= TREE_CHAIN (parms
);
19884 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19885 /* For a function parameter pack that does not occur at the
19886 end of the parameter-declaration-list, the type of the
19887 parameter pack is a non-deduced context. */
19893 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19899 && parms
!= void_list_node
19900 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19902 /* Unify the remaining arguments with the pack expansion type. */
19904 tree parmvec
= make_tree_vec (1);
19906 /* Allocate a TREE_VEC and copy in all of the arguments */
19907 argvec
= make_tree_vec (nargs
- ia
);
19908 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19909 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19911 /* Copy the parameter into parmvec. */
19912 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19913 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19914 /*subr=*/subr
, explain_p
))
19917 /* Advance to the end of the list of parameters. */
19918 parms
= TREE_CHAIN (parms
);
19921 /* Fail if we've reached the end of the parm list, and more args
19922 are present, and the parm list isn't variadic. */
19923 if (ia
< nargs
&& parms
== void_list_node
)
19924 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19925 /* Fail if parms are left and they don't have default values and
19926 they aren't all deduced as empty packs (c++/57397). This is
19927 consistent with sufficient_parms_p. */
19928 if (parms
&& parms
!= void_list_node
19929 && TREE_PURPOSE (parms
) == NULL_TREE
)
19931 unsigned int count
= nargs
;
19936 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19939 p
= TREE_CHAIN (p
);
19941 while (p
&& p
!= void_list_node
);
19942 if (count
!= nargs
)
19943 return unify_too_few_arguments (explain_p
, ia
, count
,
19949 tsubst_flags_t complain
= (explain_p
19950 ? tf_warning_or_error
19952 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19954 for (i
= 0; i
< ntparms
; i
++)
19956 tree targ
= TREE_VEC_ELT (targs
, i
);
19957 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19959 /* Clear the "incomplete" flags on all argument packs now so that
19960 substituting them into later default arguments works. */
19961 if (targ
&& ARGUMENT_PACK_P (targ
))
19963 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19964 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19967 if (targ
|| tparm
== error_mark_node
)
19969 tparm
= TREE_VALUE (tparm
);
19971 if (TREE_CODE (tparm
) == TYPE_DECL
19972 && !tried_array_deduction
)
19974 try_array_deduction (tparms
, targs
, xparms
);
19975 tried_array_deduction
= true;
19976 if (TREE_VEC_ELT (targs
, i
))
19980 /* If this is an undeduced nontype parameter that depends on
19981 a type parameter, try another pass; its type may have been
19982 deduced from a later argument than the one from which
19983 this parameter can be deduced. */
19984 if (TREE_CODE (tparm
) == PARM_DECL
19985 && uses_template_parms (TREE_TYPE (tparm
))
19986 && saw_undeduced
< 2)
19992 /* Core issue #226 (C++0x) [temp.deduct]:
19994 If a template argument has not been deduced, its
19995 default template argument, if any, is used.
19997 When we are in C++98 mode, TREE_PURPOSE will either
19998 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19999 to explicitly check cxx_dialect here. */
20000 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
20001 /* OK, there is a default argument. Wait until after the
20002 conversion check to do substitution. */
20005 /* If the type parameter is a parameter pack, then it will
20006 be deduced to an empty parameter pack. */
20007 if (template_parameter_pack_p (tparm
))
20011 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
20013 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
20014 TREE_CONSTANT (arg
) = 1;
20017 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20019 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
20021 TREE_VEC_ELT (targs
, i
) = arg
;
20025 return unify_parameter_deduction_failure (explain_p
, tparm
);
20028 /* DR 1391: All parameters have args, now check non-dependent parms for
20030 if (saw_undeduced
< 2)
20031 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
20032 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
20034 parm
= TREE_VALUE (parms
);
20036 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20037 && (!TREE_CHAIN (parms
)
20038 || TREE_CHAIN (parms
) == void_list_node
))
20039 /* For a function parameter pack that occurs at the end of the
20040 parameter-declaration-list, the type A of each remaining
20041 argument of the call is compared with the type P of the
20042 declarator-id of the function parameter pack. */
20045 parms
= TREE_CHAIN (parms
);
20047 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20048 /* For a function parameter pack that does not occur at the
20049 end of the parameter-declaration-list, the type of the
20050 parameter pack is a non-deduced context. */
20056 if (uses_template_parms (parm
))
20058 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
20063 /* Now substitute into the default template arguments. */
20064 for (i
= 0; i
< ntparms
; i
++)
20066 tree targ
= TREE_VEC_ELT (targs
, i
);
20067 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20069 if (targ
|| tparm
== error_mark_node
)
20071 tree parm
= TREE_VALUE (tparm
);
20072 tree arg
= TREE_PURPOSE (tparm
);
20073 reopen_deferring_access_checks (*checks
);
20074 location_t save_loc
= input_location
;
20076 input_location
= DECL_SOURCE_LOCATION (parm
);
20077 if (saw_undeduced
== 1)
20078 ++processing_template_decl
;
20080 if (saw_undeduced
== 1
20081 && TREE_CODE (parm
) == PARM_DECL
20082 && uses_template_parms (TREE_TYPE (parm
)))
20084 /* The type of this non-type parameter depends on undeduced
20085 parameters. Don't try to use its default argument yet,
20086 but do check whether the arguments we already have cause
20087 substitution failure, so that that happens before we try
20088 later default arguments (78489). */
20089 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
20091 if (type
== error_mark_node
)
20092 arg
= error_mark_node
;
20098 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
20100 if (!uses_template_parms (arg
))
20101 arg
= convert_template_argument (parm
, arg
, full_targs
,
20102 complain
, i
, NULL_TREE
);
20103 else if (saw_undeduced
== 1)
20106 arg
= error_mark_node
;
20109 if (saw_undeduced
== 1)
20110 --processing_template_decl
;
20111 input_location
= save_loc
;
20112 *checks
= get_deferred_access_checks ();
20113 pop_deferring_access_checks ();
20115 if (arg
== error_mark_node
)
20119 TREE_VEC_ELT (targs
, i
) = arg
;
20120 /* The position of the first default template argument,
20121 is also the number of non-defaulted arguments in TARGS.
20123 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20124 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
20128 if (saw_undeduced
++ == 1)
20132 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20133 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
20135 return unify_success (explain_p
);
20138 /* Subroutine of type_unification_real. Args are like the variables
20139 at the call site. ARG is an overloaded function (or template-id);
20140 we try deducing template args from each of the overloads, and if
20141 only one succeeds, we go with that. Modifies TARGS and returns
20142 true on success. */
20145 resolve_overloaded_unification (tree tparms
,
20149 unification_kind_t strict
,
20153 tree tempargs
= copy_node (targs
);
20155 tree goodfn
= NULL_TREE
;
20158 if (TREE_CODE (arg
) == ADDR_EXPR
)
20160 arg
= TREE_OPERAND (arg
, 0);
20166 if (TREE_CODE (arg
) == COMPONENT_REF
)
20167 /* Handle `&x' where `x' is some static or non-static member
20169 arg
= TREE_OPERAND (arg
, 1);
20171 if (TREE_CODE (arg
) == OFFSET_REF
)
20172 arg
= TREE_OPERAND (arg
, 1);
20174 /* Strip baselink information. */
20175 if (BASELINK_P (arg
))
20176 arg
= BASELINK_FUNCTIONS (arg
);
20178 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
20180 /* If we got some explicit template args, we need to plug them into
20181 the affected templates before we try to unify, in case the
20182 explicit args will completely resolve the templates in question. */
20185 tree expl_subargs
= TREE_OPERAND (arg
, 1);
20186 arg
= TREE_OPERAND (arg
, 0);
20188 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20191 tree subargs
, elem
;
20193 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20196 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20197 expl_subargs
, NULL_TREE
, tf_none
,
20198 /*require_all_args=*/true,
20199 /*use_default_args=*/true);
20200 if (subargs
!= error_mark_node
20201 && !any_dependent_template_arguments_p (subargs
))
20203 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
20204 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
20205 elem
, strict
, sub_strict
, addr_p
, explain_p
)
20206 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
20215 /* If no templates (or more than one) are fully resolved by the
20216 explicit arguments, this template-id is a non-deduced context; it
20217 could still be OK if we deduce all template arguments for the
20218 enclosing call through other arguments. */
20222 else if (TREE_CODE (arg
) != OVERLOAD
20223 && TREE_CODE (arg
) != FUNCTION_DECL
)
20224 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20225 -- but the deduction does not succeed because the expression is
20226 not just the function on its own. */
20229 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20232 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
20233 strict
, sub_strict
, addr_p
, explain_p
)
20234 && (!goodfn
|| !decls_match (goodfn
, fn
)))
20241 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20242 to function or pointer to member function argument if the set of
20243 overloaded functions does not contain function templates and at most
20244 one of a set of overloaded functions provides a unique match.
20246 So if we found multiple possibilities, we return success but don't
20247 deduce anything. */
20251 int i
= TREE_VEC_LENGTH (targs
);
20253 if (TREE_VEC_ELT (tempargs
, i
))
20255 tree old
= TREE_VEC_ELT (targs
, i
);
20256 tree new_
= TREE_VEC_ELT (tempargs
, i
);
20257 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
20258 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
20259 /* Don't forget explicit template arguments in a pack. */
20260 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
20261 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
20262 TREE_VEC_ELT (targs
, i
) = new_
;
20271 /* Core DR 115: In contexts where deduction is done and fails, or in
20272 contexts where deduction is not done, if a template argument list is
20273 specified and it, along with any default template arguments, identifies
20274 a single function template specialization, then the template-id is an
20275 lvalue for the function template specialization. */
20278 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20280 tree expr
, offset
, baselink
;
20283 if (!type_unknown_p (orig_expr
))
20288 offset
= NULL_TREE
;
20289 baselink
= NULL_TREE
;
20291 if (TREE_CODE (expr
) == ADDR_EXPR
)
20293 expr
= TREE_OPERAND (expr
, 0);
20296 if (TREE_CODE (expr
) == OFFSET_REF
)
20299 expr
= TREE_OPERAND (expr
, 1);
20301 if (BASELINK_P (expr
))
20304 expr
= BASELINK_FUNCTIONS (expr
);
20307 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20310 tree goodfn
= NULL_TREE
;
20312 /* If we got some explicit template args, we need to plug them into
20313 the affected templates before we try to unify, in case the
20314 explicit args will completely resolve the templates in question. */
20316 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20317 tree arg
= TREE_OPERAND (expr
, 0);
20318 tree badfn
= NULL_TREE
;
20319 tree badargs
= NULL_TREE
;
20321 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20324 tree subargs
, elem
;
20326 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20329 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20330 expl_subargs
, NULL_TREE
, tf_none
,
20331 /*require_all_args=*/true,
20332 /*use_default_args=*/true);
20333 if (subargs
!= error_mark_node
20334 && !any_dependent_template_arguments_p (subargs
))
20336 elem
= instantiate_template (fn
, subargs
, tf_none
);
20337 if (elem
== error_mark_node
)
20342 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20351 mark_used (goodfn
);
20354 expr
= build_baselink (BASELINK_BINFO (baselink
),
20355 BASELINK_ACCESS_BINFO (baselink
),
20356 expr
, BASELINK_OPTYPE (baselink
));
20360 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20361 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20364 expr
= cp_build_addr_expr (expr
, complain
);
20367 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20368 /* There were no good options and at least one bad one, so let the
20369 user know what the problem is. */
20370 instantiate_template (badfn
, badargs
, complain
);
20375 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20376 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20377 different overloads deduce different arguments for a given parm.
20378 ADDR_P is true if the expression for which deduction is being
20379 performed was of the form "& fn" rather than simply "fn".
20381 Returns 1 on success. */
20384 try_one_overload (tree tparms
,
20389 unification_kind_t strict
,
20398 if (arg
== error_mark_node
)
20401 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20402 to function or pointer to member function argument if the set of
20403 overloaded functions does not contain function templates and at most
20404 one of a set of overloaded functions provides a unique match.
20406 So if this is a template, just return success. */
20408 if (uses_template_parms (arg
))
20411 if (TREE_CODE (arg
) == METHOD_TYPE
)
20412 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20414 arg
= build_pointer_type (arg
);
20416 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20418 /* We don't copy orig_targs for this because if we have already deduced
20419 some template args from previous args, unify would complain when we
20420 try to deduce a template parameter for the same argument, even though
20421 there isn't really a conflict. */
20422 nargs
= TREE_VEC_LENGTH (targs
);
20423 tempargs
= make_tree_vec (nargs
);
20425 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20428 /* First make sure we didn't deduce anything that conflicts with
20429 explicitly specified args. */
20430 for (i
= nargs
; i
--; )
20432 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20433 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20437 else if (uses_template_parms (elt
))
20438 /* Since we're unifying against ourselves, we will fill in
20439 template args used in the function parm list with our own
20440 template parms. Discard them. */
20441 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20442 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20444 /* Check that the argument at each index of the deduced argument pack
20445 is equivalent to the corresponding explicitly specified argument.
20446 We may have deduced more arguments than were explicitly specified,
20449 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20450 that's wrong if we deduce the same argument pack from multiple
20451 function arguments: it's only incomplete the first time. */
20453 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20454 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20456 if (TREE_VEC_LENGTH (deduced_pack
)
20457 < TREE_VEC_LENGTH (explicit_pack
))
20460 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20461 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20462 TREE_VEC_ELT (deduced_pack
, j
)))
20465 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20469 for (i
= nargs
; i
--; )
20471 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20474 TREE_VEC_ELT (targs
, i
) = elt
;
20480 /* PARM is a template class (perhaps with unbound template
20481 parameters). ARG is a fully instantiated type. If ARG can be
20482 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20483 TARGS are as for unify. */
20486 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20489 tree copy_of_targs
;
20491 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20493 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20494 /* Matches anything. */;
20495 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20496 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20499 /* We need to make a new template argument vector for the call to
20500 unify. If we used TARGS, we'd clutter it up with the result of
20501 the attempted unification, even if this class didn't work out.
20502 We also don't want to commit ourselves to all the unifications
20503 we've already done, since unification is supposed to be done on
20504 an argument-by-argument basis. In other words, consider the
20505 following pathological case:
20507 template <int I, int J, int K>
20510 template <int I, int J>
20511 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20513 template <int I, int J, int K>
20514 void f(S<I, J, K>, S<I, I, I>);
20523 Now, by the time we consider the unification involving `s2', we
20524 already know that we must have `f<0, 0, 0>'. But, even though
20525 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20526 because there are two ways to unify base classes of S<0, 1, 2>
20527 with S<I, I, I>. If we kept the already deduced knowledge, we
20528 would reject the possibility I=1. */
20529 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20531 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20533 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20538 /* If unification failed, we're done. */
20539 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20540 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20546 /* Given a template type PARM and a class type ARG, find the unique
20547 base type in ARG that is an instance of PARM. We do not examine
20548 ARG itself; only its base-classes. If there is not exactly one
20549 appropriate base class, return NULL_TREE. PARM may be the type of
20550 a partial specialization, as well as a plain template type. Used
20553 static enum template_base_result
20554 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20555 bool explain_p
, tree
*result
)
20557 tree rval
= NULL_TREE
;
20560 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20562 binfo
= TYPE_BINFO (complete_type (arg
));
20565 /* The type could not be completed. */
20566 *result
= NULL_TREE
;
20567 return tbr_incomplete_type
;
20570 /* Walk in inheritance graph order. The search order is not
20571 important, and this avoids multiple walks of virtual bases. */
20572 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20574 tree r
= try_class_unification (tparms
, targs
, parm
,
20575 BINFO_TYPE (binfo
), explain_p
);
20579 /* If there is more than one satisfactory baseclass, then:
20583 If they yield more than one possible deduced A, the type
20587 if (rval
&& !same_type_p (r
, rval
))
20589 *result
= NULL_TREE
;
20590 return tbr_ambiguous_baseclass
;
20598 return tbr_success
;
20601 /* Returns the level of DECL, which declares a template parameter. */
20604 template_decl_level (tree decl
)
20606 switch (TREE_CODE (decl
))
20609 case TEMPLATE_DECL
:
20610 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20613 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20616 gcc_unreachable ();
20621 /* Decide whether ARG can be unified with PARM, considering only the
20622 cv-qualifiers of each type, given STRICT as documented for unify.
20623 Returns nonzero iff the unification is OK on that basis. */
20626 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20628 int arg_quals
= cp_type_quals (arg
);
20629 int parm_quals
= cp_type_quals (parm
);
20631 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20632 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20634 /* Although a CVR qualifier is ignored when being applied to a
20635 substituted template parameter ([8.3.2]/1 for example), that
20636 does not allow us to unify "const T" with "int&" because both
20637 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20638 It is ok when we're allowing additional CV qualifiers
20639 at the outer level [14.8.2.1]/3,1st bullet. */
20640 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20641 || TREE_CODE (arg
) == FUNCTION_TYPE
20642 || TREE_CODE (arg
) == METHOD_TYPE
)
20643 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20646 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20647 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20651 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20652 && (arg_quals
& parm_quals
) != parm_quals
)
20655 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20656 && (parm_quals
& arg_quals
) != arg_quals
)
20662 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20664 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20666 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20667 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20668 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20670 *index
= TEMPLATE_TYPE_IDX (parm
);
20671 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20675 *index
= TEMPLATE_PARM_IDX (parm
);
20676 *level
= TEMPLATE_PARM_LEVEL (parm
);
20680 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20682 if (unify (TP, TA, P, A, S, EP)) \
20686 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20687 expansion at the end of PACKED_PARMS. Returns 0 if the type
20688 deduction succeeds, 1 otherwise. STRICT is the same as in
20689 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20690 function call argument list. We'll need to adjust the arguments to make them
20691 types. SUBR tells us if this is from a recursive call to
20692 type_unification_real, or for comparing two template argument
20696 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20697 tree packed_args
, unification_kind_t strict
,
20698 bool subr
, bool explain_p
)
20701 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20702 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20703 tree pack
, packs
= NULL_TREE
;
20704 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20706 /* Add in any args remembered from an earlier partial instantiation. */
20707 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20708 int levels
= TMPL_ARGS_DEPTH (targs
);
20710 packed_args
= expand_template_argument_pack (packed_args
);
20712 int len
= TREE_VEC_LENGTH (packed_args
);
20714 /* Determine the parameter packs we will be deducing from the
20715 pattern, and record their current deductions. */
20716 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20717 pack
; pack
= TREE_CHAIN (pack
))
20719 tree parm_pack
= TREE_VALUE (pack
);
20722 /* Only template parameter packs can be deduced, not e.g. function
20723 parameter packs or __bases or __integer_pack. */
20724 if (!TEMPLATE_PARM_P (parm_pack
))
20727 /* Determine the index and level of this parameter pack. */
20728 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20729 if (level
< levels
)
20732 /* Keep track of the parameter packs and their corresponding
20734 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20735 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20738 /* Loop through all of the arguments that have not yet been
20739 unified and unify each with the pattern. */
20740 for (i
= start
; i
< len
; i
++)
20743 bool any_explicit
= false;
20744 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20746 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20747 or the element of its argument pack at the current index if
20748 this argument was explicitly specified. */
20749 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20753 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20756 if (TREE_VALUE (pack
)
20757 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20758 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20760 any_explicit
= true;
20761 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20763 TMPL_ARG (targs
, level
, idx
) = arg
;
20766 /* If we had explicit template arguments, substitute them into the
20767 pattern before deduction. */
20770 /* Some arguments might still be unspecified or dependent. */
20772 ++processing_template_decl
;
20773 dependent
= any_dependent_template_arguments_p (targs
);
20775 --processing_template_decl
;
20776 parm
= tsubst (pattern
, targs
,
20777 explain_p
? tf_warning_or_error
: tf_none
,
20780 --processing_template_decl
;
20781 if (parm
== error_mark_node
)
20787 /* Unify the pattern with the current argument. */
20788 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20792 /* For each parameter pack, collect the deduced value. */
20793 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20796 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20798 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20799 TMPL_ARG (targs
, level
, idx
);
20803 /* Verify that the results of unification with the parameter packs
20804 produce results consistent with what we've seen before, and make
20805 the deduced argument packs available. */
20806 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20808 tree old_pack
= TREE_VALUE (pack
);
20809 tree new_args
= TREE_TYPE (pack
);
20810 int i
, len
= TREE_VEC_LENGTH (new_args
);
20812 bool nondeduced_p
= false;
20814 /* By default keep the original deduced argument pack.
20815 If necessary, more specific code is going to update the
20816 resulting deduced argument later down in this function. */
20817 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20818 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20820 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20821 actually deduce anything. */
20822 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20823 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20824 nondeduced_p
= true;
20828 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20830 /* If we had fewer function args than explicit template args,
20831 just use the explicits. */
20832 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20833 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20834 if (len
< explicit_len
)
20835 new_args
= explicit_args
;
20841 /* Build the deduced *_ARGUMENT_PACK. */
20842 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20844 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20845 TREE_CONSTANT (result
) = 1;
20848 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20850 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20852 /* Note the deduced argument packs for this parameter
20854 TMPL_ARG (targs
, level
, idx
) = result
;
20856 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20857 && (ARGUMENT_PACK_ARGS (old_pack
)
20858 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20860 /* We only had the explicitly-provided arguments before, but
20861 now we have a complete set of arguments. */
20862 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20864 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20865 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20866 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20870 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20871 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20873 if (!comp_template_args (old_args
, new_args
,
20874 &bad_old_arg
, &bad_new_arg
))
20875 /* Inconsistent unification of this parameter pack. */
20876 return unify_parameter_pack_inconsistent (explain_p
,
20882 return unify_success (explain_p
);
20885 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20886 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20887 parameters and return value are as for unify. */
20890 unify_array_domain (tree tparms
, tree targs
,
20891 tree parm_dom
, tree arg_dom
,
20899 /* Our representation of array types uses "N - 1" as the
20900 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20901 not an integer constant. We cannot unify arbitrarily
20902 complex expressions, so we eliminate the MINUS_EXPRs
20904 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20905 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20908 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20909 parm_max
= TREE_OPERAND (parm_max
, 0);
20911 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20912 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20915 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20916 trying to unify the type of a variable with the type
20917 of a template parameter. For example:
20919 template <unsigned int N>
20920 void f (char (&) [N]);
20927 Here, the type of the ARG will be "int [g(i)]", and
20928 may be a SAVE_EXPR, etc. */
20929 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20930 return unify_vla_arg (explain_p
, arg_dom
);
20931 arg_max
= TREE_OPERAND (arg_max
, 0);
20934 /* If only one of the bounds used a MINUS_EXPR, compensate
20935 by adding one to the other bound. */
20936 if (parm_cst
&& !arg_cst
)
20937 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20941 else if (arg_cst
&& !parm_cst
)
20942 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20947 return unify (tparms
, targs
, parm_max
, arg_max
,
20948 UNIFY_ALLOW_INTEGER
, explain_p
);
20951 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20953 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20958 if (PACK_EXPANSION_P (t
))
20959 t
= PACK_EXPANSION_PATTERN (t
);
20960 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20961 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20962 || DECL_TYPE_TEMPLATE_P (t
))
20964 else if (TYPE_P (t
))
20970 /* Deduce the value of template parameters. TPARMS is the (innermost)
20971 set of template parameters to a template. TARGS is the bindings
20972 for those template parameters, as determined thus far; TARGS may
20973 include template arguments for outer levels of template parameters
20974 as well. PARM is a parameter to a template function, or a
20975 subcomponent of that parameter; ARG is the corresponding argument.
20976 This function attempts to match PARM with ARG in a manner
20977 consistent with the existing assignments in TARGS. If more values
20978 are deduced, then TARGS is updated.
20980 Returns 0 if the type deduction succeeds, 1 otherwise. The
20981 parameter STRICT is a bitwise or of the following flags:
20984 Require an exact match between PARM and ARG.
20985 UNIFY_ALLOW_MORE_CV_QUAL:
20986 Allow the deduced ARG to be more cv-qualified (by qualification
20987 conversion) than ARG.
20988 UNIFY_ALLOW_LESS_CV_QUAL:
20989 Allow the deduced ARG to be less cv-qualified than ARG.
20990 UNIFY_ALLOW_DERIVED:
20991 Allow the deduced ARG to be a template base class of ARG,
20992 or a pointer to a template base class of the type pointed to by
20994 UNIFY_ALLOW_INTEGER:
20995 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20996 case for more information.
20997 UNIFY_ALLOW_OUTER_LEVEL:
20998 This is the outermost level of a deduction. Used to determine validity
20999 of qualification conversions. A valid qualification conversion must
21000 have const qualified pointers leading up to the inner type which
21001 requires additional CV quals, except at the outer level, where const
21002 is not required [conv.qual]. It would be normal to set this flag in
21003 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21004 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21005 This is the outermost level of a deduction, and PARM can be more CV
21006 qualified at this point.
21007 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21008 This is the outermost level of a deduction, and PARM can be less CV
21009 qualified at this point. */
21012 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
21018 int strict_in
= strict
;
21019 tsubst_flags_t complain
= (explain_p
21020 ? tf_warning_or_error
21023 /* I don't think this will do the right thing with respect to types.
21024 But the only case I've seen it in so far has been array bounds, where
21025 signedness is the only information lost, and I think that will be
21027 while (CONVERT_EXPR_P (parm
))
21028 parm
= TREE_OPERAND (parm
, 0);
21030 if (arg
== error_mark_node
)
21031 return unify_invalid (explain_p
);
21032 if (arg
== unknown_type_node
21033 || arg
== init_list_type_node
)
21034 /* We can't deduce anything from this, but we might get all the
21035 template args from other function args. */
21036 return unify_success (explain_p
);
21038 if (parm
== any_targ_node
|| arg
== any_targ_node
)
21039 return unify_success (explain_p
);
21041 /* If PARM uses template parameters, then we can't bail out here,
21042 even if ARG == PARM, since we won't record unifications for the
21043 template parameters. We might need them if we're trying to
21044 figure out which of two things is more specialized. */
21045 if (arg
== parm
&& !uses_template_parms (parm
))
21046 return unify_success (explain_p
);
21048 /* Handle init lists early, so the rest of the function can assume
21049 we're dealing with a type. */
21050 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
21054 tree orig_parm
= parm
;
21056 /* Replace T with std::initializer_list<T> for deduction. */
21057 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21058 && flag_deduce_init_list
)
21059 parm
= listify (parm
);
21061 if (!is_std_init_list (parm
)
21062 && TREE_CODE (parm
) != ARRAY_TYPE
)
21063 /* We can only deduce from an initializer list argument if the
21064 parameter is std::initializer_list or an array; otherwise this
21065 is a non-deduced context. */
21066 return unify_success (explain_p
);
21068 if (TREE_CODE (parm
) == ARRAY_TYPE
)
21069 elttype
= TREE_TYPE (parm
);
21072 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
21073 /* Deduction is defined in terms of a single type, so just punt
21074 on the (bizarre) std::initializer_list<T...>. */
21075 if (PACK_EXPANSION_P (elttype
))
21076 return unify_success (explain_p
);
21079 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
21081 int elt_strict
= strict
;
21083 if (elt
== error_mark_node
)
21084 return unify_invalid (explain_p
);
21086 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
21088 tree type
= TREE_TYPE (elt
);
21089 if (type
== error_mark_node
)
21090 return unify_invalid (explain_p
);
21091 /* It should only be possible to get here for a call. */
21092 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
21093 elt_strict
|= maybe_adjust_types_for_deduction
21094 (DEDUCE_CALL
, &elttype
, &type
, elt
);
21098 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
21102 if (TREE_CODE (parm
) == ARRAY_TYPE
21103 && deducible_array_bound (TYPE_DOMAIN (parm
)))
21105 /* Also deduce from the length of the initializer list. */
21106 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
21107 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
21108 if (idx
== error_mark_node
)
21109 return unify_invalid (explain_p
);
21110 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21114 /* If the std::initializer_list<T> deduction worked, replace the
21115 deduced A with std::initializer_list<A>. */
21116 if (orig_parm
!= parm
)
21118 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
21119 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21120 targ
= listify (targ
);
21121 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
21123 return unify_success (explain_p
);
21126 /* If parm and arg aren't the same kind of thing (template, type, or
21127 expression), fail early. */
21128 if (pa_kind (parm
) != pa_kind (arg
))
21129 return unify_invalid (explain_p
);
21131 /* Immediately reject some pairs that won't unify because of
21132 cv-qualification mismatches. */
21133 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21135 /* It is the elements of the array which hold the cv quals of an array
21136 type, and the elements might be template type parms. We'll check
21137 when we recurse. */
21138 && TREE_CODE (arg
) != ARRAY_TYPE
21139 /* We check the cv-qualifiers when unifying with template type
21140 parameters below. We want to allow ARG `const T' to unify with
21141 PARM `T' for example, when computing which of two templates
21142 is more specialized, for example. */
21143 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
21144 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
21145 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21147 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
21148 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
21149 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
21150 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
21151 strict
&= ~UNIFY_ALLOW_DERIVED
;
21152 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21153 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
21155 switch (TREE_CODE (parm
))
21157 case TYPENAME_TYPE
:
21159 case UNBOUND_CLASS_TEMPLATE
:
21160 /* In a type which contains a nested-name-specifier, template
21161 argument values cannot be deduced for template parameters used
21162 within the nested-name-specifier. */
21163 return unify_success (explain_p
);
21165 case TEMPLATE_TYPE_PARM
:
21166 case TEMPLATE_TEMPLATE_PARM
:
21167 case BOUND_TEMPLATE_TEMPLATE_PARM
:
21168 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21169 if (error_operand_p (tparm
))
21170 return unify_invalid (explain_p
);
21172 if (TEMPLATE_TYPE_LEVEL (parm
)
21173 != template_decl_level (tparm
))
21174 /* The PARM is not one we're trying to unify. Just check
21175 to see if it matches ARG. */
21177 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21178 && (is_auto (parm
) ? is_auto (arg
)
21179 : same_type_p (parm
, arg
)))
21180 return unify_success (explain_p
);
21182 return unify_type_mismatch (explain_p
, parm
, arg
);
21184 idx
= TEMPLATE_TYPE_IDX (parm
);
21185 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21186 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
21187 if (error_operand_p (tparm
))
21188 return unify_invalid (explain_p
);
21190 /* Check for mixed types and values. */
21191 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21192 && TREE_CODE (tparm
) != TYPE_DECL
)
21193 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21194 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
21195 gcc_unreachable ();
21197 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21199 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
21200 && CLASS_TYPE_P (arg
))
21202 /* First try to match ARG directly. */
21203 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
21207 /* Otherwise, look for a suitable base of ARG, as below. */
21208 enum template_base_result r
;
21209 r
= get_template_base (tparms
, targs
, parm
, arg
,
21212 return unify_no_common_base (explain_p
, r
, parm
, arg
);
21216 /* ARG must be constructed from a template class or a template
21217 template parameter. */
21218 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
21219 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21220 return unify_template_deduction_failure (explain_p
, parm
, arg
);
21222 /* Deduce arguments T, i from TT<T> or TT<i>. */
21223 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
21226 arg
= TYPE_TI_TEMPLATE (arg
);
21228 /* Fall through to deduce template name. */
21231 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21232 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21234 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21236 /* Simple cases: Value already set, does match or doesn't. */
21237 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
21238 return unify_success (explain_p
);
21240 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21244 /* If PARM is `const T' and ARG is only `int', we don't have
21245 a match unless we are allowing additional qualification.
21246 If ARG is `const int' and PARM is just `T' that's OK;
21247 that binds `const int' to `T'. */
21248 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
21250 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21252 /* Consider the case where ARG is `const volatile int' and
21253 PARM is `const T'. Then, T should be `volatile int'. */
21254 arg
= cp_build_qualified_type_real
21255 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
21256 if (arg
== error_mark_node
)
21257 return unify_invalid (explain_p
);
21259 /* Simple cases: Value already set, does match or doesn't. */
21260 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
21261 return unify_success (explain_p
);
21263 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21265 /* Make sure that ARG is not a variable-sized array. (Note
21266 that were talking about variable-sized arrays (like
21267 `int[n]'), rather than arrays of unknown size (like
21268 `int[]').) We'll get very confused by such a type since
21269 the bound of the array is not constant, and therefore
21270 not mangleable. Besides, such types are not allowed in
21271 ISO C++, so we can do as we please here. We do allow
21272 them for 'auto' deduction, since that isn't ABI-exposed. */
21273 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21274 return unify_vla_arg (explain_p
, arg
);
21276 /* Strip typedefs as in convert_template_argument. */
21277 arg
= canonicalize_type_argument (arg
, tf_none
);
21280 /* If ARG is a parameter pack or an expansion, we cannot unify
21281 against it unless PARM is also a parameter pack. */
21282 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21283 && !template_parameter_pack_p (parm
))
21284 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21286 /* If the argument deduction results is a METHOD_TYPE,
21287 then there is a problem.
21288 METHOD_TYPE doesn't map to any real C++ type the result of
21289 the deduction can not be of that type. */
21290 if (TREE_CODE (arg
) == METHOD_TYPE
)
21291 return unify_method_type_error (explain_p
, arg
);
21293 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21294 return unify_success (explain_p
);
21296 case TEMPLATE_PARM_INDEX
:
21297 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21298 if (error_operand_p (tparm
))
21299 return unify_invalid (explain_p
);
21301 if (TEMPLATE_PARM_LEVEL (parm
)
21302 != template_decl_level (tparm
))
21304 /* The PARM is not one we're trying to unify. Just check
21305 to see if it matches ARG. */
21306 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21307 && cp_tree_equal (parm
, arg
));
21309 unify_expression_unequal (explain_p
, parm
, arg
);
21313 idx
= TEMPLATE_PARM_IDX (parm
);
21314 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21318 if ((strict
& UNIFY_ALLOW_INTEGER
)
21319 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21320 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21321 /* We're deducing from an array bound, the type doesn't matter. */
21322 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21323 int x
= !cp_tree_equal (targ
, arg
);
21325 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21329 /* [temp.deduct.type] If, in the declaration of a function template
21330 with a non-type template-parameter, the non-type
21331 template-parameter is used in an expression in the function
21332 parameter-list and, if the corresponding template-argument is
21333 deduced, the template-argument type shall match the type of the
21334 template-parameter exactly, except that a template-argument
21335 deduced from an array bound may be of any integral type.
21336 The non-type parameter might use already deduced type parameters. */
21337 tparm
= TREE_TYPE (parm
);
21338 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
21339 /* We don't have enough levels of args to do any substitution. This
21340 can happen in the context of -fnew-ttp-matching. */;
21343 ++processing_template_decl
;
21344 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
21345 --processing_template_decl
;
21347 if (tree a
= type_uses_auto (tparm
))
21349 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21350 if (tparm
== error_mark_node
)
21355 if (!TREE_TYPE (arg
))
21356 /* Template-parameter dependent expression. Just accept it for now.
21357 It will later be processed in convert_template_argument. */
21359 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21360 non_reference (tparm
)))
21362 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21363 && CP_INTEGRAL_TYPE_P (tparm
))
21364 /* Convert the ARG to the type of PARM; the deduced non-type
21365 template argument must exactly match the types of the
21366 corresponding parameter. */
21367 arg
= fold (build_nop (tparm
, arg
));
21368 else if (uses_template_parms (tparm
))
21370 /* We haven't deduced the type of this parameter yet. */
21371 if (cxx_dialect
>= cxx17
21372 /* We deduce from array bounds in try_array_deduction. */
21373 && !(strict
& UNIFY_ALLOW_INTEGER
))
21375 /* Deduce it from the non-type argument. */
21376 tree atype
= TREE_TYPE (arg
);
21377 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21379 UNIFY_ALLOW_NONE
, explain_p
);
21382 /* Try again later. */
21383 return unify_success (explain_p
);
21386 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21388 /* If ARG is a parameter pack or an expansion, we cannot unify
21389 against it unless PARM is also a parameter pack. */
21390 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21391 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21392 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21395 bool removed_attr
= false;
21396 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21398 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21399 return unify_success (explain_p
);
21403 /* A pointer-to-member constant can be unified only with
21404 another constant. */
21405 if (TREE_CODE (arg
) != PTRMEM_CST
)
21406 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21408 /* Just unify the class member. It would be useless (and possibly
21409 wrong, depending on the strict flags) to unify also
21410 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21411 arg refer to the same variable, even if through different
21412 classes. For instance:
21414 struct A { int x; };
21417 Unification of &A::x and &B::x must succeed. */
21418 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21419 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21424 if (!TYPE_PTR_P (arg
))
21425 return unify_type_mismatch (explain_p
, parm
, arg
);
21427 /* [temp.deduct.call]
21429 A can be another pointer or pointer to member type that can
21430 be converted to the deduced A via a qualification
21431 conversion (_conv.qual_).
21433 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21434 This will allow for additional cv-qualification of the
21435 pointed-to types if appropriate. */
21437 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21438 /* The derived-to-base conversion only persists through one
21439 level of pointers. */
21440 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21442 return unify (tparms
, targs
, TREE_TYPE (parm
),
21443 TREE_TYPE (arg
), strict
, explain_p
);
21446 case REFERENCE_TYPE
:
21447 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21448 return unify_type_mismatch (explain_p
, parm
, arg
);
21449 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21450 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21453 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21454 return unify_type_mismatch (explain_p
, parm
, arg
);
21455 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21456 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21457 return unify_type_mismatch (explain_p
, parm
, arg
);
21458 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21459 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21460 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21461 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21462 TYPE_DOMAIN (arg
), explain_p
);
21463 return unify_success (explain_p
);
21470 case ENUMERAL_TYPE
:
21473 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21474 return unify_type_mismatch (explain_p
, parm
, arg
);
21476 /* We have already checked cv-qualification at the top of the
21478 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21479 return unify_type_mismatch (explain_p
, parm
, arg
);
21481 /* As far as unification is concerned, this wins. Later checks
21482 will invalidate it if necessary. */
21483 return unify_success (explain_p
);
21485 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21486 /* Type INTEGER_CST can come from ordinary constant template args. */
21488 while (CONVERT_EXPR_P (arg
))
21489 arg
= TREE_OPERAND (arg
, 0);
21491 if (TREE_CODE (arg
) != INTEGER_CST
)
21492 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21493 return (tree_int_cst_equal (parm
, arg
)
21494 ? unify_success (explain_p
)
21495 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21499 int i
, len
, argslen
;
21500 int parm_variadic_p
= 0;
21502 if (TREE_CODE (arg
) != TREE_VEC
)
21503 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21505 len
= TREE_VEC_LENGTH (parm
);
21506 argslen
= TREE_VEC_LENGTH (arg
);
21508 /* Check for pack expansions in the parameters. */
21509 for (i
= 0; i
< len
; ++i
)
21511 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21514 /* We can unify against something with a trailing
21516 parm_variadic_p
= 1;
21518 /* [temp.deduct.type]/9: If the template argument list of
21519 P contains a pack expansion that is not the last
21520 template argument, the entire template argument list
21521 is a non-deduced context. */
21522 return unify_success (explain_p
);
21526 /* If we don't have enough arguments to satisfy the parameters
21527 (not counting the pack expression at the end), or we have
21528 too many arguments for a parameter list that doesn't end in
21529 a pack expression, we can't unify. */
21530 if (parm_variadic_p
21531 ? argslen
< len
- parm_variadic_p
21533 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21535 /* Unify all of the parameters that precede the (optional)
21536 pack expression. */
21537 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21539 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21540 TREE_VEC_ELT (parm
, i
),
21541 TREE_VEC_ELT (arg
, i
),
21542 UNIFY_ALLOW_NONE
, explain_p
);
21544 if (parm_variadic_p
)
21545 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21547 /*subr=*/true, explain_p
);
21548 return unify_success (explain_p
);
21553 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21554 return unify_type_mismatch (explain_p
, parm
, arg
);
21556 if (TYPE_PTRMEMFUNC_P (parm
))
21558 if (!TYPE_PTRMEMFUNC_P (arg
))
21559 return unify_type_mismatch (explain_p
, parm
, arg
);
21561 return unify (tparms
, targs
,
21562 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21563 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21564 strict
, explain_p
);
21566 else if (TYPE_PTRMEMFUNC_P (arg
))
21567 return unify_type_mismatch (explain_p
, parm
, arg
);
21569 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21571 tree t
= NULL_TREE
;
21573 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21575 /* First, we try to unify the PARM and ARG directly. */
21576 t
= try_class_unification (tparms
, targs
,
21577 parm
, arg
, explain_p
);
21581 /* Fallback to the special case allowed in
21582 [temp.deduct.call]:
21584 If P is a class, and P has the form
21585 template-id, then A can be a derived class of
21586 the deduced A. Likewise, if P is a pointer to
21587 a class of the form template-id, A can be a
21588 pointer to a derived class pointed to by the
21590 enum template_base_result r
;
21591 r
= get_template_base (tparms
, targs
, parm
, arg
,
21596 /* Don't give the derived diagnostic if we're
21597 already dealing with the same template. */
21599 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21600 && (CLASSTYPE_TI_TEMPLATE (parm
)
21601 == CLASSTYPE_TI_TEMPLATE (arg
)));
21602 return unify_no_common_base (explain_p
&& !same_template
,
21607 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21608 && (CLASSTYPE_TI_TEMPLATE (parm
)
21609 == CLASSTYPE_TI_TEMPLATE (arg
)))
21610 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21611 Then, we should unify `int' and `U'. */
21614 /* There's no chance of unification succeeding. */
21615 return unify_type_mismatch (explain_p
, parm
, arg
);
21617 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21618 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21620 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21621 return unify_type_mismatch (explain_p
, parm
, arg
);
21622 return unify_success (explain_p
);
21625 case FUNCTION_TYPE
:
21627 unsigned int nargs
;
21632 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21633 return unify_type_mismatch (explain_p
, parm
, arg
);
21635 /* CV qualifications for methods can never be deduced, they must
21636 match exactly. We need to check them explicitly here,
21637 because type_unification_real treats them as any other
21638 cv-qualified parameter. */
21639 if (TREE_CODE (parm
) == METHOD_TYPE
21640 && (!check_cv_quals_for_unify
21642 class_of_this_parm (arg
),
21643 class_of_this_parm (parm
))))
21644 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21645 if (TREE_CODE (arg
) == FUNCTION_TYPE
21646 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21647 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21648 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21649 return unify_type_mismatch (explain_p
, parm
, arg
);
21651 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21652 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21654 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21655 args
= XALLOCAVEC (tree
, nargs
);
21656 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21657 a
!= NULL_TREE
&& a
!= void_list_node
;
21658 a
= TREE_CHAIN (a
), ++i
)
21659 args
[i
] = TREE_VALUE (a
);
21662 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21663 args
, nargs
, 1, DEDUCE_EXACT
,
21664 LOOKUP_NORMAL
, NULL
, explain_p
))
21667 if (flag_noexcept_type
)
21669 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21670 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21671 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21672 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21673 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21674 && uses_template_parms (TREE_PURPOSE (pspec
)))
21675 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21676 TREE_PURPOSE (aspec
),
21677 UNIFY_ALLOW_NONE
, explain_p
);
21678 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21679 return unify_type_mismatch (explain_p
, parm
, arg
);
21686 /* Unify a pointer to member with a pointer to member function, which
21687 deduces the type of the member as a function type. */
21688 if (TYPE_PTRMEMFUNC_P (arg
))
21690 /* Check top-level cv qualifiers */
21691 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21692 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21694 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21695 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21696 UNIFY_ALLOW_NONE
, explain_p
);
21698 /* Determine the type of the function we are unifying against. */
21699 tree fntype
= static_fn_type (arg
);
21701 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21704 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21705 return unify_type_mismatch (explain_p
, parm
, arg
);
21706 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21707 TYPE_OFFSET_BASETYPE (arg
),
21708 UNIFY_ALLOW_NONE
, explain_p
);
21709 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21710 strict
, explain_p
);
21713 if (DECL_TEMPLATE_PARM_P (parm
))
21714 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21715 if (arg
!= scalar_constant_value (parm
))
21716 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21717 return unify_success (explain_p
);
21720 case TEMPLATE_DECL
:
21721 /* Matched cases are handled by the ARG == PARM test above. */
21722 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21725 /* We might get a variable as a non-type template argument in parm if the
21726 corresponding parameter is type-dependent. Make any necessary
21727 adjustments based on whether arg is a reference. */
21728 if (CONSTANT_CLASS_P (arg
))
21729 parm
= fold_non_dependent_expr (parm
);
21730 else if (REFERENCE_REF_P (arg
))
21732 tree sub
= TREE_OPERAND (arg
, 0);
21734 if (TREE_CODE (sub
) == ADDR_EXPR
)
21735 arg
= TREE_OPERAND (sub
, 0);
21737 /* Now use the normal expression code to check whether they match. */
21740 case TYPE_ARGUMENT_PACK
:
21741 case NONTYPE_ARGUMENT_PACK
:
21742 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21743 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21746 case DECLTYPE_TYPE
:
21747 case UNDERLYING_TYPE
:
21748 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21749 or UNDERLYING_TYPE nodes. */
21750 return unify_success (explain_p
);
21753 /* Unification fails if we hit an error node. */
21754 return unify_invalid (explain_p
);
21757 if (REFERENCE_REF_P (parm
))
21759 bool pexp
= PACK_EXPANSION_P (arg
);
21761 arg
= PACK_EXPANSION_PATTERN (arg
);
21762 if (REFERENCE_REF_P (arg
))
21763 arg
= TREE_OPERAND (arg
, 0);
21765 arg
= make_pack_expansion (arg
, complain
);
21766 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21767 strict
, explain_p
);
21772 /* An unresolved overload is a nondeduced context. */
21773 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21774 return unify_success (explain_p
);
21775 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21777 /* We must be looking at an expression. This can happen with
21781 void foo(S<I>, S<I + 2>);
21783 This is a "nondeduced context":
21787 The nondeduced contexts are:
21789 --A type that is a template-id in which one or more of
21790 the template-arguments is an expression that references
21791 a template-parameter.
21793 In these cases, we assume deduction succeeded, but don't
21794 actually infer any unifications. */
21796 if (!uses_template_parms (parm
)
21797 && !template_args_equal (parm
, arg
))
21798 return unify_expression_unequal (explain_p
, parm
, arg
);
21800 return unify_success (explain_p
);
21803 #undef RECUR_AND_CHECK_FAILURE
21805 /* Note that DECL can be defined in this translation unit, if
21809 mark_definable (tree decl
)
21812 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21813 FOR_EACH_CLONE (clone
, decl
)
21814 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21817 /* Called if RESULT is explicitly instantiated, or is a member of an
21818 explicitly instantiated class. */
21821 mark_decl_instantiated (tree result
, int extern_p
)
21823 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21825 /* If this entity has already been written out, it's too late to
21826 make any modifications. */
21827 if (TREE_ASM_WRITTEN (result
))
21830 /* For anonymous namespace we don't need to do anything. */
21831 if (decl_anon_ns_mem_p (result
))
21833 gcc_assert (!TREE_PUBLIC (result
));
21837 if (TREE_CODE (result
) != FUNCTION_DECL
)
21838 /* The TREE_PUBLIC flag for function declarations will have been
21839 set correctly by tsubst. */
21840 TREE_PUBLIC (result
) = 1;
21842 /* This might have been set by an earlier implicit instantiation. */
21843 DECL_COMDAT (result
) = 0;
21846 DECL_NOT_REALLY_EXTERN (result
) = 0;
21849 mark_definable (result
);
21850 mark_needed (result
);
21851 /* Always make artificials weak. */
21852 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21853 comdat_linkage (result
);
21854 /* For WIN32 we also want to put explicit instantiations in
21855 linkonce sections. */
21856 else if (TREE_PUBLIC (result
))
21857 maybe_make_one_only (result
);
21860 /* If EXTERN_P, then this function will not be emitted -- unless
21861 followed by an explicit instantiation, at which point its linkage
21862 will be adjusted. If !EXTERN_P, then this function will be
21863 emitted here. In neither circumstance do we want
21864 import_export_decl to adjust the linkage. */
21865 DECL_INTERFACE_KNOWN (result
) = 1;
21868 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21869 important template arguments. If any are missing, we check whether
21870 they're important by using error_mark_node for substituting into any
21871 args that were used for partial ordering (the ones between ARGS and END)
21872 and seeing if it bubbles up. */
21875 check_undeduced_parms (tree targs
, tree args
, tree end
)
21877 bool found
= false;
21879 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21880 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21883 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21887 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21888 if (substed
== error_mark_node
)
21894 /* Given two function templates PAT1 and PAT2, return:
21896 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21897 -1 if PAT2 is more specialized than PAT1.
21898 0 if neither is more specialized.
21900 LEN indicates the number of parameters we should consider
21901 (defaulted parameters should not be considered).
21903 The 1998 std underspecified function template partial ordering, and
21904 DR214 addresses the issue. We take pairs of arguments, one from
21905 each of the templates, and deduce them against each other. One of
21906 the templates will be more specialized if all the *other*
21907 template's arguments deduce against its arguments and at least one
21908 of its arguments *does* *not* deduce against the other template's
21909 corresponding argument. Deduction is done as for class templates.
21910 The arguments used in deduction have reference and top level cv
21911 qualifiers removed. Iff both arguments were originally reference
21912 types *and* deduction succeeds in both directions, an lvalue reference
21913 wins against an rvalue reference and otherwise the template
21914 with the more cv-qualified argument wins for that pairing (if
21915 neither is more cv-qualified, they both are equal). Unlike regular
21916 deduction, after all the arguments have been deduced in this way,
21917 we do *not* verify the deduced template argument values can be
21918 substituted into non-deduced contexts.
21920 The logic can be a bit confusing here, because we look at deduce1 and
21921 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21922 can find template arguments for pat1 to make arg1 look like arg2, that
21923 means that arg2 is at least as specialized as arg1. */
21926 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21928 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21929 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21930 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21931 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21932 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21933 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21934 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21935 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21936 tree origs1
, origs2
;
21937 bool lose1
= false;
21938 bool lose2
= false;
21940 /* Remove the this parameter from non-static member functions. If
21941 one is a non-static member function and the other is not a static
21942 member function, remove the first parameter from that function
21943 also. This situation occurs for operator functions where we
21944 locate both a member function (with this pointer) and non-member
21945 operator (with explicit first operand). */
21946 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21948 len
--; /* LEN is the number of significant arguments for DECL1 */
21949 args1
= TREE_CHAIN (args1
);
21950 if (!DECL_STATIC_FUNCTION_P (decl2
))
21951 args2
= TREE_CHAIN (args2
);
21953 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21955 args2
= TREE_CHAIN (args2
);
21956 if (!DECL_STATIC_FUNCTION_P (decl1
))
21959 args1
= TREE_CHAIN (args1
);
21963 /* If only one is a conversion operator, they are unordered. */
21964 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21967 /* Consider the return type for a conversion function */
21968 if (DECL_CONV_FN_P (decl1
))
21970 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21971 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21975 processing_template_decl
++;
21981 /* Stop when an ellipsis is seen. */
21982 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21984 tree arg1
= TREE_VALUE (args1
);
21985 tree arg2
= TREE_VALUE (args2
);
21986 int deduce1
, deduce2
;
21992 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21993 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21995 /* When both arguments are pack expansions, we need only
21996 unify the patterns themselves. */
21997 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21998 arg2
= PACK_EXPANSION_PATTERN (arg2
);
22000 /* This is the last comparison we need to do. */
22004 /* DR 1847: If a particular P contains no template-parameters that
22005 participate in template argument deduction, that P is not used to
22006 determine the ordering. */
22007 if (!uses_deducible_template_parms (arg1
)
22008 && !uses_deducible_template_parms (arg2
))
22011 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
22013 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
22014 arg1
= TREE_TYPE (arg1
);
22015 quals1
= cp_type_quals (arg1
);
22018 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
22020 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
22021 arg2
= TREE_TYPE (arg2
);
22022 quals2
= cp_type_quals (arg2
);
22025 arg1
= TYPE_MAIN_VARIANT (arg1
);
22026 arg2
= TYPE_MAIN_VARIANT (arg2
);
22028 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
22030 int i
, len2
= remaining_arguments (args2
);
22031 tree parmvec
= make_tree_vec (1);
22032 tree argvec
= make_tree_vec (len2
);
22035 /* Setup the parameter vector, which contains only ARG1. */
22036 TREE_VEC_ELT (parmvec
, 0) = arg1
;
22038 /* Setup the argument vector, which contains the remaining
22040 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
22041 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22043 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
22044 argvec
, DEDUCE_EXACT
,
22045 /*subr=*/true, /*explain_p=*/false)
22048 /* We cannot deduce in the other direction, because ARG1 is
22049 a pack expansion but ARG2 is not. */
22052 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22054 int i
, len1
= remaining_arguments (args1
);
22055 tree parmvec
= make_tree_vec (1);
22056 tree argvec
= make_tree_vec (len1
);
22059 /* Setup the parameter vector, which contains only ARG1. */
22060 TREE_VEC_ELT (parmvec
, 0) = arg2
;
22062 /* Setup the argument vector, which contains the remaining
22064 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
22065 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22067 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
22068 argvec
, DEDUCE_EXACT
,
22069 /*subr=*/true, /*explain_p=*/false)
22072 /* We cannot deduce in the other direction, because ARG2 is
22073 a pack expansion but ARG1 is not.*/
22079 /* The normal case, where neither argument is a pack
22081 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
22082 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22084 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
22085 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22089 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22090 arg2, then arg2 is not as specialized as arg1. */
22096 /* "If, for a given type, deduction succeeds in both directions
22097 (i.e., the types are identical after the transformations above)
22098 and both P and A were reference types (before being replaced with
22099 the type referred to above):
22100 - if the type from the argument template was an lvalue reference and
22101 the type from the parameter template was not, the argument type is
22102 considered to be more specialized than the other; otherwise,
22103 - if the type from the argument template is more cv-qualified
22104 than the type from the parameter template (as described above),
22105 the argument type is considered to be more specialized than the other;
22107 - neither type is more specialized than the other." */
22109 if (deduce1
&& deduce2
)
22111 if (ref1
&& ref2
&& ref1
!= ref2
)
22118 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
22120 if ((quals1
& quals2
) == quals2
)
22122 if ((quals1
& quals2
) == quals1
)
22127 if (lose1
&& lose2
)
22128 /* We've failed to deduce something in either direction.
22129 These must be unordered. */
22134 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22135 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22136 /* We have already processed all of the arguments in our
22137 handing of the pack expansion type. */
22140 args1
= TREE_CHAIN (args1
);
22141 args2
= TREE_CHAIN (args2
);
22144 /* "In most cases, all template parameters must have values in order for
22145 deduction to succeed, but for partial ordering purposes a template
22146 parameter may remain without a value provided it is not used in the
22147 types being used for partial ordering."
22149 Thus, if we are missing any of the targs1 we need to substitute into
22150 origs1, then pat2 is not as specialized as pat1. This can happen when
22151 there is a nondeduced context. */
22152 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
22154 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
22157 processing_template_decl
--;
22159 /* If both deductions succeed, the partial ordering selects the more
22160 constrained template. */
22161 if (!lose1
&& !lose2
)
22163 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
22164 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
22165 lose1
= !subsumes_constraints (c1
, c2
);
22166 lose2
= !subsumes_constraints (c2
, c1
);
22169 /* All things being equal, if the next argument is a pack expansion
22170 for one function but not for the other, prefer the
22171 non-variadic function. FIXME this is bogus; see c++/41958. */
22173 && args1
&& TREE_VALUE (args1
)
22174 && args2
&& TREE_VALUE (args2
))
22176 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
22177 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
22180 if (lose1
== lose2
)
22188 /* Determine which of two partial specializations of TMPL is more
22191 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22192 to the first partial specialization. The TREE_PURPOSE is the
22193 innermost set of template parameters for the partial
22194 specialization. PAT2 is similar, but for the second template.
22196 Return 1 if the first partial specialization is more specialized;
22197 -1 if the second is more specialized; 0 if neither is more
22200 See [temp.class.order] for information about determining which of
22201 two templates is more specialized. */
22204 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
22208 bool any_deductions
= false;
22210 tree tmpl1
= TREE_VALUE (pat1
);
22211 tree tmpl2
= TREE_VALUE (pat2
);
22212 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
22213 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
22215 /* Just like what happens for functions, if we are ordering between
22216 different template specializations, we may encounter dependent
22217 types in the arguments, and we need our dependency check functions
22218 to behave correctly. */
22219 ++processing_template_decl
;
22220 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
22224 any_deductions
= true;
22227 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
22231 any_deductions
= true;
22233 --processing_template_decl
;
22235 /* If both deductions succeed, the partial ordering selects the more
22236 constrained template. */
22237 if (!winner
&& any_deductions
)
22238 return more_constrained (tmpl1
, tmpl2
);
22240 /* In the case of a tie where at least one of the templates
22241 has a parameter pack at the end, the template with the most
22242 non-packed parameters wins. */
22245 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
22246 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
22248 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
22249 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
22250 int len1
= TREE_VEC_LENGTH (args1
);
22251 int len2
= TREE_VEC_LENGTH (args2
);
22253 /* We don't count the pack expansion at the end. */
22254 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
22256 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
22261 else if (len1
< len2
)
22268 /* Return the template arguments that will produce the function signature
22269 DECL from the function template FN, with the explicit template
22270 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22271 also match. Return NULL_TREE if no satisfactory arguments could be
22275 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
22277 int ntparms
= DECL_NTPARMS (fn
);
22278 tree targs
= make_tree_vec (ntparms
);
22279 tree decl_type
= TREE_TYPE (decl
);
22280 tree decl_arg_types
;
22282 unsigned int nargs
, ix
;
22285 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22287 /* Never do unification on the 'this' parameter. */
22288 decl_arg_types
= skip_artificial_parms_for (decl
,
22289 TYPE_ARG_TYPES (decl_type
));
22291 nargs
= list_length (decl_arg_types
);
22292 args
= XALLOCAVEC (tree
, nargs
);
22293 for (arg
= decl_arg_types
, ix
= 0;
22294 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22295 arg
= TREE_CHAIN (arg
), ++ix
)
22296 args
[ix
] = TREE_VALUE (arg
);
22298 if (fn_type_unification (fn
, explicit_args
, targs
,
22300 (check_rettype
|| DECL_CONV_FN_P (fn
)
22301 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22302 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
22304 == error_mark_node
)
22310 /* Return the innermost template arguments that, when applied to a partial
22311 specialization SPEC_TMPL of TMPL, yield the ARGS.
22313 For example, suppose we have:
22315 template <class T, class U> struct S {};
22316 template <class T> struct S<T*, int> {};
22318 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22319 partial specialization and the ARGS will be {double*, int}. The resulting
22320 vector will be {double}, indicating that `T' is bound to `double'. */
22323 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22325 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22327 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22328 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22330 tree innermost_deduced_args
;
22332 innermost_deduced_args
= make_tree_vec (ntparms
);
22333 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22335 deduced_args
= copy_node (args
);
22336 SET_TMPL_ARGS_LEVEL (deduced_args
,
22337 TMPL_ARGS_DEPTH (deduced_args
),
22338 innermost_deduced_args
);
22341 deduced_args
= innermost_deduced_args
;
22343 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22345 if (unify (tparms
, deduced_args
,
22346 INNERMOST_TEMPLATE_ARGS (spec_args
),
22347 INNERMOST_TEMPLATE_ARGS (args
),
22348 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22351 for (i
= 0; i
< ntparms
; ++i
)
22352 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22354 if (!tried_array_deduction
)
22356 try_array_deduction (tparms
, innermost_deduced_args
,
22357 INNERMOST_TEMPLATE_ARGS (spec_args
));
22358 tried_array_deduction
= true;
22359 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22365 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22366 if (!push_tinst_level (tinst
))
22368 excessive_deduction_depth
= true;
22372 /* Verify that nondeduced template arguments agree with the type
22373 obtained from argument deduction.
22377 struct A { typedef int X; };
22378 template <class T, class U> struct C {};
22379 template <class T> struct C<T, typename T::X> {};
22381 Then with the instantiation `C<A, int>', we can deduce that
22382 `T' is `A' but unify () does not check whether `typename T::X'
22384 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22386 if (spec_args
!= error_mark_node
)
22387 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22388 INNERMOST_TEMPLATE_ARGS (spec_args
),
22389 tmpl
, tf_none
, false, false);
22391 pop_tinst_level ();
22393 if (spec_args
== error_mark_node
22394 /* We only need to check the innermost arguments; the other
22395 arguments will always agree. */
22396 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22397 INNERMOST_TEMPLATE_ARGS (args
)))
22400 /* Now that we have bindings for all of the template arguments,
22401 ensure that the arguments deduced for the template template
22402 parameters have compatible template parameter lists. See the use
22403 of template_template_parm_bindings_ok_p in fn_type_unification
22404 for more information. */
22405 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22408 return deduced_args
;
22411 // Compare two function templates T1 and T2 by deducing bindings
22412 // from one against the other. If both deductions succeed, compare
22413 // constraints to see which is more constrained.
22415 more_specialized_inst (tree t1
, tree t2
)
22420 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22426 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22432 // If both deductions succeed, then one may be more constrained.
22433 if (count
== 2 && fate
== 0)
22434 fate
= more_constrained (t1
, t2
);
22439 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22440 Return the TREE_LIST node with the most specialized template, if
22441 any. If there is no most specialized template, the error_mark_node
22444 Note that this function does not look at, or modify, the
22445 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22446 returned is one of the elements of INSTANTIATIONS, callers may
22447 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22448 and retrieve it from the value returned. */
22451 most_specialized_instantiation (tree templates
)
22455 ++processing_template_decl
;
22458 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22460 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22461 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22466 /* Equally specialized, move to next function. If there
22467 is no next function, nothing's most specialized. */
22468 fn
= TREE_CHAIN (fn
);
22476 /* Now verify that champ is better than everything earlier in the
22477 instantiation list. */
22478 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22479 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22486 processing_template_decl
--;
22489 return error_mark_node
;
22494 /* If DECL is a specialization of some template, return the most
22495 general such template. Otherwise, returns NULL_TREE.
22497 For example, given:
22499 template <class T> struct S { template <class U> void f(U); };
22501 if TMPL is `template <class U> void S<int>::f(U)' this will return
22502 the full template. This function will not trace past partial
22503 specializations, however. For example, given in addition:
22505 template <class T> struct S<T*> { template <class U> void f(U); };
22507 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22508 `template <class T> template <class U> S<T*>::f(U)'. */
22511 most_general_template (tree decl
)
22513 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22515 if (tree tinfo
= get_template_info (decl
))
22516 decl
= TI_TEMPLATE (tinfo
);
22517 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22518 template friend, or a FIELD_DECL for a capture pack. */
22519 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22523 /* Look for more and more general templates. */
22524 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22526 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22527 (See cp-tree.h for details.) */
22528 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22531 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22532 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22533 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22536 /* Stop if we run into an explicitly specialized class template. */
22537 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22538 && DECL_CONTEXT (decl
)
22539 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22542 decl
= DECL_TI_TEMPLATE (decl
);
22548 /* Return the most specialized of the template partial specializations
22549 which can produce TARGET, a specialization of some class or variable
22550 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22551 a TEMPLATE_DECL node corresponding to the partial specialization, while
22552 the TREE_PURPOSE is the set of template arguments that must be
22553 substituted into the template pattern in order to generate TARGET.
22555 If the choice of partial specialization is ambiguous, a diagnostic
22556 is issued, and the error_mark_node is returned. If there are no
22557 partial specializations matching TARGET, then NULL_TREE is
22558 returned, indicating that the primary template should be used. */
22561 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22563 tree list
= NULL_TREE
;
22568 tree outer_args
= NULL_TREE
;
22571 if (TYPE_P (target
))
22573 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22574 tmpl
= TI_TEMPLATE (tinfo
);
22575 args
= TI_ARGS (tinfo
);
22577 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22579 tmpl
= TREE_OPERAND (target
, 0);
22580 args
= TREE_OPERAND (target
, 1);
22582 else if (VAR_P (target
))
22584 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22585 tmpl
= TI_TEMPLATE (tinfo
);
22586 args
= TI_ARGS (tinfo
);
22589 gcc_unreachable ();
22591 tree main_tmpl
= most_general_template (tmpl
);
22593 /* For determining which partial specialization to use, only the
22594 innermost args are interesting. */
22595 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22597 outer_args
= strip_innermost_template_args (args
, 1);
22598 args
= INNERMOST_TEMPLATE_ARGS (args
);
22601 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22604 tree spec_tmpl
= TREE_VALUE (t
);
22608 /* Substitute in the template args from the enclosing class. */
22609 ++processing_template_decl
;
22610 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22611 --processing_template_decl
;
22614 if (spec_tmpl
== error_mark_node
)
22615 return error_mark_node
;
22617 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22621 spec_args
= add_to_template_args (outer_args
, spec_args
);
22623 /* Keep the candidate only if the constraints are satisfied,
22624 or if we're not compiling with concepts. */
22626 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22628 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22629 TREE_TYPE (list
) = TREE_TYPE (t
);
22637 ambiguous_p
= false;
22640 t
= TREE_CHAIN (t
);
22641 for (; t
; t
= TREE_CHAIN (t
))
22643 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22650 t
= TREE_CHAIN (t
);
22653 ambiguous_p
= true;
22662 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22664 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22667 ambiguous_p
= true;
22675 char *spaces
= NULL
;
22676 if (!(complain
& tf_error
))
22677 return error_mark_node
;
22678 if (TYPE_P (target
))
22679 error ("ambiguous template instantiation for %q#T", target
);
22681 error ("ambiguous template instantiation for %q#D", target
);
22682 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22683 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22685 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22686 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22687 "%s %#qS", spaces
? spaces
: str
, subst
);
22688 spaces
= spaces
? spaces
: get_spaces (str
);
22691 return error_mark_node
;
22697 /* Explicitly instantiate DECL. */
22700 do_decl_instantiation (tree decl
, tree storage
)
22702 tree result
= NULL_TREE
;
22705 if (!decl
|| decl
== error_mark_node
)
22706 /* An error occurred, for which grokdeclarator has already issued
22707 an appropriate message. */
22709 else if (! DECL_LANG_SPECIFIC (decl
))
22711 error ("explicit instantiation of non-template %q#D", decl
);
22715 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22716 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22718 if (VAR_P (decl
) && !var_templ
)
22720 /* There is an asymmetry here in the way VAR_DECLs and
22721 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22722 the latter, the DECL we get back will be marked as a
22723 template instantiation, and the appropriate
22724 DECL_TEMPLATE_INFO will be set up. This does not happen for
22725 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22726 should handle VAR_DECLs as it currently handles
22728 if (!DECL_CLASS_SCOPE_P (decl
))
22730 error ("%qD is not a static data member of a class template", decl
);
22733 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22734 if (!result
|| !VAR_P (result
))
22736 error ("no matching template for %qD found", decl
);
22739 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22741 error ("type %qT for explicit instantiation %qD does not match "
22742 "declared type %qT", TREE_TYPE (result
), decl
,
22747 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22749 error ("explicit instantiation of %q#D", decl
);
22755 /* Check for various error cases. Note that if the explicit
22756 instantiation is valid the RESULT will currently be marked as an
22757 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22758 until we get here. */
22760 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22762 /* DR 259 [temp.spec].
22764 Both an explicit instantiation and a declaration of an explicit
22765 specialization shall not appear in a program unless the explicit
22766 instantiation follows a declaration of the explicit specialization.
22768 For a given set of template parameters, if an explicit
22769 instantiation of a template appears after a declaration of an
22770 explicit specialization for that template, the explicit
22771 instantiation has no effect. */
22774 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22778 No program shall explicitly instantiate any template more
22781 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22782 the first instantiation was `extern' and the second is not,
22783 and EXTERN_P for the opposite case. */
22784 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22785 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22786 /* If an "extern" explicit instantiation follows an ordinary
22787 explicit instantiation, the template is instantiated. */
22791 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22793 error ("no matching template for %qD found", result
);
22796 else if (!DECL_TEMPLATE_INFO (result
))
22798 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22802 if (storage
== NULL_TREE
)
22804 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22806 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22807 pedwarn (input_location
, OPT_Wpedantic
,
22808 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22813 error ("storage class %qD applied to template instantiation", storage
);
22815 check_explicit_instantiation_namespace (result
);
22816 mark_decl_instantiated (result
, extern_p
);
22818 instantiate_decl (result
, /*defer_ok=*/true,
22819 /*expl_inst_class_mem_p=*/false);
22823 mark_class_instantiated (tree t
, int extern_p
)
22825 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22826 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22827 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22828 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22831 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22832 rest_of_type_compilation (t
, 1);
22836 /* Called from do_type_instantiation through binding_table_foreach to
22837 do recursive instantiation for the type bound in ENTRY. */
22839 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22841 tree storage
= *(tree
*) data
;
22843 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22844 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
22845 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22846 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22849 /* Perform an explicit instantiation of template class T. STORAGE, if
22850 non-null, is the RID for extern, inline or static. COMPLAIN is
22851 nonzero if this is called from the parser, zero if called recursively,
22852 since the standard is unclear (as detailed below). */
22855 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22860 int previous_instantiation_extern_p
= 0;
22862 if (TREE_CODE (t
) == TYPE_DECL
)
22865 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22868 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22870 error ("explicit instantiation of non-class template %qD", tmpl
);
22872 error ("explicit instantiation of non-template type %qT", t
);
22878 if (!COMPLETE_TYPE_P (t
))
22880 if (complain
& tf_error
)
22881 error ("explicit instantiation of %q#T before definition of template",
22886 if (storage
!= NULL_TREE
)
22888 if (!in_system_header_at (input_location
))
22890 if (storage
== ridpointers
[(int) RID_EXTERN
])
22892 if (cxx_dialect
== cxx98
)
22893 pedwarn (input_location
, OPT_Wpedantic
,
22894 "ISO C++ 1998 forbids the use of %<extern%> on "
22895 "explicit instantiations");
22898 pedwarn (input_location
, OPT_Wpedantic
,
22899 "ISO C++ forbids the use of %qE"
22900 " on explicit instantiations", storage
);
22903 if (storage
== ridpointers
[(int) RID_INLINE
])
22905 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22907 else if (storage
== ridpointers
[(int) RID_STATIC
])
22911 error ("storage class %qD applied to template instantiation",
22917 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22919 /* DR 259 [temp.spec].
22921 Both an explicit instantiation and a declaration of an explicit
22922 specialization shall not appear in a program unless the explicit
22923 instantiation follows a declaration of the explicit specialization.
22925 For a given set of template parameters, if an explicit
22926 instantiation of a template appears after a declaration of an
22927 explicit specialization for that template, the explicit
22928 instantiation has no effect. */
22931 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22935 No program shall explicitly instantiate any template more
22938 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22939 instantiation was `extern'. If EXTERN_P then the second is.
22940 These cases are OK. */
22941 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22943 if (!previous_instantiation_extern_p
&& !extern_p
22944 && (complain
& tf_error
))
22945 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22947 /* If we've already instantiated the template, just return now. */
22948 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22952 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22953 mark_class_instantiated (t
, extern_p
);
22958 /* In contrast to implicit instantiation, where only the
22959 declarations, and not the definitions, of members are
22960 instantiated, we have here:
22964 The explicit instantiation of a class template specialization
22965 implies the instantiation of all of its members not
22966 previously explicitly specialized in the translation unit
22967 containing the explicit instantiation.
22969 Of course, we can't instantiate member template classes, since we
22970 don't have any arguments for them. Note that the standard is
22971 unclear on whether the instantiation of the members are
22972 *explicit* instantiations or not. However, the most natural
22973 interpretation is that it should be an explicit
22975 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22977 || (TREE_CODE (fld
) == FUNCTION_DECL
22979 && user_provided_p (fld
)))
22980 && DECL_TEMPLATE_INSTANTIATION (fld
))
22982 mark_decl_instantiated (fld
, extern_p
);
22984 instantiate_decl (fld
, /*defer_ok=*/true,
22985 /*expl_inst_class_mem_p=*/true);
22988 if (CLASSTYPE_NESTED_UTDS (t
))
22989 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22990 bt_instantiate_type_proc
, &storage
);
22993 /* Given a function DECL, which is a specialization of TMPL, modify
22994 DECL to be a re-instantiation of TMPL with the same template
22995 arguments. TMPL should be the template into which tsubst'ing
22996 should occur for DECL, not the most general template.
22998 One reason for doing this is a scenario like this:
23001 void f(const T&, int i);
23003 void g() { f(3, 7); }
23006 void f(const T& t, const int i) { }
23008 Note that when the template is first instantiated, with
23009 instantiate_template, the resulting DECL will have no name for the
23010 first parameter, and the wrong type for the second. So, when we go
23011 to instantiate the DECL, we regenerate it. */
23014 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
23016 /* The arguments used to instantiate DECL, from the most general
23020 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
23022 /* Make sure that we can see identifiers, and compute access
23024 push_access_scope (decl
);
23026 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23034 args_depth
= TMPL_ARGS_DEPTH (args
);
23035 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
23036 if (args_depth
> parms_depth
)
23037 args
= get_innermost_template_args (args
, parms_depth
);
23039 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
23040 args
, tf_error
, NULL_TREE
,
23041 /*defer_ok*/false);
23042 if (specs
&& specs
!= error_mark_node
)
23043 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
23046 /* Merge parameter declarations. */
23047 decl_parm
= skip_artificial_parms_for (decl
,
23048 DECL_ARGUMENTS (decl
));
23050 = skip_artificial_parms_for (code_pattern
,
23051 DECL_ARGUMENTS (code_pattern
));
23052 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
23057 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23058 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
23059 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
23061 parm_type
= type_decays_to (parm_type
);
23062 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23063 TREE_TYPE (decl_parm
) = parm_type
;
23064 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23065 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23067 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23068 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23070 decl_parm
= DECL_CHAIN (decl_parm
);
23071 pattern_parm
= DECL_CHAIN (pattern_parm
);
23073 /* Merge any parameters that match with the function parameter
23075 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
23078 tree expanded_types
;
23079 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23080 the parameters in this function parameter pack. */
23081 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
23082 args
, tf_error
, NULL_TREE
);
23083 len
= TREE_VEC_LENGTH (expanded_types
);
23084 for (i
= 0; i
< len
; i
++)
23089 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23090 /* Rename the parameter to include the index. */
23091 DECL_NAME (decl_parm
) =
23092 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
23093 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
23094 parm_type
= type_decays_to (parm_type
);
23095 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23096 TREE_TYPE (decl_parm
) = parm_type
;
23097 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23098 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23100 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23101 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23103 decl_parm
= DECL_CHAIN (decl_parm
);
23106 /* Merge additional specifiers from the CODE_PATTERN. */
23107 if (DECL_DECLARED_INLINE_P (code_pattern
)
23108 && !DECL_DECLARED_INLINE_P (decl
))
23109 DECL_DECLARED_INLINE_P (decl
) = 1;
23111 else if (VAR_P (decl
))
23113 start_lambda_scope (decl
);
23114 DECL_INITIAL (decl
) =
23115 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
23116 tf_error
, DECL_TI_TEMPLATE (decl
),
23117 /*integral_constant_expression_p=*/false);
23118 finish_lambda_scope ();
23119 if (VAR_HAD_UNKNOWN_BOUND (decl
))
23120 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
23121 tf_error
, DECL_TI_TEMPLATE (decl
));
23124 gcc_unreachable ();
23126 pop_access_scope (decl
);
23129 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23130 substituted to get DECL. */
23133 template_for_substitution (tree decl
)
23135 tree tmpl
= DECL_TI_TEMPLATE (decl
);
23137 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23138 for the instantiation. This is not always the most general
23139 template. Consider, for example:
23142 struct S { template <class U> void f();
23143 template <> void f<int>(); };
23145 and an instantiation of S<double>::f<int>. We want TD to be the
23146 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23147 while (/* An instantiation cannot have a definition, so we need a
23148 more general template. */
23149 DECL_TEMPLATE_INSTANTIATION (tmpl
)
23150 /* We must also deal with friend templates. Given:
23152 template <class T> struct S {
23153 template <class U> friend void f() {};
23156 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23157 so far as the language is concerned, but that's still
23158 where we get the pattern for the instantiation from. On
23159 other hand, if the definition comes outside the class, say:
23161 template <class T> struct S {
23162 template <class U> friend void f();
23164 template <class U> friend void f() {}
23166 we don't need to look any further. That's what the check for
23167 DECL_INITIAL is for. */
23168 || (TREE_CODE (decl
) == FUNCTION_DECL
23169 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
23170 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
23172 /* The present template, TD, should not be a definition. If it
23173 were a definition, we should be using it! Note that we
23174 cannot restructure the loop to just keep going until we find
23175 a template with a definition, since that might go too far if
23176 a specialization was declared, but not defined. */
23178 /* Fetch the more general template. */
23179 tmpl
= DECL_TI_TEMPLATE (tmpl
);
23185 /* Returns true if we need to instantiate this template instance even if we
23186 know we aren't going to emit it. */
23189 always_instantiate_p (tree decl
)
23191 /* We always instantiate inline functions so that we can inline them. An
23192 explicit instantiation declaration prohibits implicit instantiation of
23193 non-inline functions. With high levels of optimization, we would
23194 normally inline non-inline functions -- but we're not allowed to do
23195 that for "extern template" functions. Therefore, we check
23196 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23197 return ((TREE_CODE (decl
) == FUNCTION_DECL
23198 && (DECL_DECLARED_INLINE_P (decl
)
23199 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
23200 /* And we need to instantiate static data members so that
23201 their initializers are available in integral constant
23204 && decl_maybe_constant_var_p (decl
)));
23207 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23208 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23209 error, true otherwise. */
23212 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
23214 tree fntype
, spec
, noex
, clone
;
23216 /* Don't instantiate a noexcept-specification from template context. */
23217 if (processing_template_decl
)
23220 if (DECL_CLONED_FUNCTION_P (fn
))
23221 fn
= DECL_CLONED_FUNCTION (fn
);
23222 fntype
= TREE_TYPE (fn
);
23223 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
23225 if (!spec
|| !TREE_PURPOSE (spec
))
23228 noex
= TREE_PURPOSE (spec
);
23230 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
23232 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
23233 bool added
= false;
23234 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
23235 spec
= get_defaulted_eh_spec (fn
, complain
);
23236 else if (!(added
= !fns
->add (fn
)))
23238 /* If hash_set::add returns true, the element was already there. */
23239 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
23240 DECL_SOURCE_LOCATION (fn
));
23242 "exception specification of %qD depends on itself",
23244 spec
= noexcept_false_spec
;
23246 else if (push_tinst_level (fn
))
23248 push_access_scope (fn
);
23249 push_deferring_access_checks (dk_no_deferred
);
23250 input_location
= DECL_SOURCE_LOCATION (fn
);
23251 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
23252 DEFERRED_NOEXCEPT_ARGS (noex
),
23253 tf_warning_or_error
, fn
,
23254 /*function_p=*/false,
23255 /*integral_constant_expression_p=*/true);
23256 pop_deferring_access_checks ();
23257 pop_access_scope (fn
);
23258 pop_tinst_level ();
23259 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
23260 if (spec
== error_mark_node
)
23261 spec
= noexcept_false_spec
;
23264 spec
= noexcept_false_spec
;
23269 if (spec
== error_mark_node
)
23272 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
23275 FOR_EACH_CLONE (clone
, fn
)
23277 if (TREE_TYPE (clone
) == fntype
)
23278 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23280 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23286 /* We're starting to process the function INST, an instantiation of PATTERN;
23287 add their parameters to local_specializations. */
23290 register_parameter_specializations (tree pattern
, tree inst
)
23292 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23293 tree spec_parm
= DECL_ARGUMENTS (inst
);
23294 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23296 register_local_specialization (spec_parm
, tmpl_parm
);
23297 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23298 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23300 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23302 if (!DECL_PACK_P (tmpl_parm
))
23304 register_local_specialization (spec_parm
, tmpl_parm
);
23305 spec_parm
= DECL_CHAIN (spec_parm
);
23309 /* Register the (value) argument pack as a specialization of
23310 TMPL_PARM, then move on. */
23311 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23312 register_local_specialization (argpack
, tmpl_parm
);
23315 gcc_assert (!spec_parm
);
23318 /* Produce the definition of D, a _DECL generated from a template. If
23319 DEFER_OK is true, then we don't have to actually do the
23320 instantiation now; we just have to do it sometime. Normally it is
23321 an error if this is an explicit instantiation but D is undefined.
23322 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23323 instantiated class template. */
23326 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23328 tree tmpl
= DECL_TI_TEMPLATE (d
);
23335 bool pattern_defined
;
23336 location_t saved_loc
= input_location
;
23337 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23338 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23342 /* This function should only be used to instantiate templates for
23343 functions and static member variables. */
23344 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23346 /* A concept is never instantiated. */
23347 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23349 /* Variables are never deferred; if instantiation is required, they
23350 are instantiated right away. That allows for better code in the
23351 case that an expression refers to the value of the variable --
23352 if the variable has a constant value the referring expression can
23353 take advantage of that fact. */
23357 /* Don't instantiate cloned functions. Instead, instantiate the
23358 functions they cloned. */
23359 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23360 d
= DECL_CLONED_FUNCTION (d
);
23362 if (DECL_TEMPLATE_INSTANTIATED (d
)
23363 || (TREE_CODE (d
) == FUNCTION_DECL
23364 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23365 || DECL_TEMPLATE_SPECIALIZATION (d
))
23366 /* D has already been instantiated or explicitly specialized, so
23367 there's nothing for us to do here.
23369 It might seem reasonable to check whether or not D is an explicit
23370 instantiation, and, if so, stop here. But when an explicit
23371 instantiation is deferred until the end of the compilation,
23372 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23373 the instantiation. */
23376 /* Check to see whether we know that this template will be
23377 instantiated in some other file, as with "extern template"
23379 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23381 /* In general, we do not instantiate such templates. */
23382 if (external_p
&& !always_instantiate_p (d
))
23385 gen_tmpl
= most_general_template (tmpl
);
23386 gen_args
= DECL_TI_ARGS (d
);
23388 if (tmpl
!= gen_tmpl
)
23389 /* We should already have the extra args. */
23390 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23391 == TMPL_ARGS_DEPTH (gen_args
));
23392 /* And what's in the hash table should match D. */
23393 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23394 || spec
== NULL_TREE
);
23396 /* This needs to happen before any tsubsting. */
23397 if (! push_tinst_level (d
))
23400 timevar_push (TV_TEMPLATE_INST
);
23402 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23403 for the instantiation. */
23404 td
= template_for_substitution (d
);
23409 /* Look up an explicit specialization, if any. */
23410 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23411 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23412 if (elt
&& elt
!= error_mark_node
)
23414 td
= TREE_VALUE (elt
);
23415 args
= TREE_PURPOSE (elt
);
23419 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23421 /* We should never be trying to instantiate a member of a class
23422 template or partial specialization. */
23423 gcc_assert (d
!= code_pattern
);
23425 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23426 || DECL_TEMPLATE_SPECIALIZATION (td
))
23427 /* In the case of a friend template whose definition is provided
23428 outside the class, we may have too many arguments. Drop the
23429 ones we don't need. The same is true for specializations. */
23430 args
= get_innermost_template_args
23431 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23433 if (TREE_CODE (d
) == FUNCTION_DECL
)
23435 deleted_p
= DECL_DELETED_FN (code_pattern
);
23436 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23437 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23438 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23444 if (DECL_CLASS_SCOPE_P (code_pattern
))
23445 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23446 || DECL_INLINE_VAR_P (code_pattern
));
23448 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23451 /* We may be in the middle of deferred access check. Disable it now. */
23452 push_deferring_access_checks (dk_no_deferred
);
23454 /* Unless an explicit instantiation directive has already determined
23455 the linkage of D, remember that a definition is available for
23457 if (pattern_defined
23458 && !DECL_INTERFACE_KNOWN (d
)
23459 && !DECL_NOT_REALLY_EXTERN (d
))
23460 mark_definable (d
);
23462 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23463 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23464 input_location
= DECL_SOURCE_LOCATION (d
);
23466 /* If D is a member of an explicitly instantiated class template,
23467 and no definition is available, treat it like an implicit
23469 if (!pattern_defined
&& expl_inst_class_mem_p
23470 && DECL_EXPLICIT_INSTANTIATION (d
))
23472 /* Leave linkage flags alone on instantiations with anonymous
23474 if (TREE_PUBLIC (d
))
23476 DECL_NOT_REALLY_EXTERN (d
) = 0;
23477 DECL_INTERFACE_KNOWN (d
) = 0;
23479 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23482 /* Defer all other templates, unless we have been explicitly
23483 forbidden from doing so. */
23484 if (/* If there is no definition, we cannot instantiate the
23487 /* If it's OK to postpone instantiation, do so. */
23489 /* If this is a static data member that will be defined
23490 elsewhere, we don't want to instantiate the entire data
23491 member, but we do want to instantiate the initializer so that
23492 we can substitute that elsewhere. */
23493 || (external_p
&& VAR_P (d
))
23494 /* Handle here a deleted function too, avoid generating
23495 its body (c++/61080). */
23498 /* The definition of the static data member is now required so
23499 we must substitute the initializer. */
23501 && !DECL_INITIAL (d
)
23502 && DECL_INITIAL (code_pattern
))
23506 bool const_init
= false;
23507 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23509 ns
= decl_namespace_context (d
);
23510 push_nested_namespace (ns
);
23512 push_nested_class (DECL_CONTEXT (d
));
23513 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23515 tf_warning_or_error
, NULL_TREE
,
23516 /*integral_constant_expression_p=*/false);
23517 /* If instantiating the initializer involved instantiating this
23518 again, don't call cp_finish_decl twice. */
23519 if (!DECL_INITIAL (d
))
23521 /* Make sure the initializer is still constant, in case of
23522 circular dependency (template/instantiate6.C). */
23524 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23525 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23526 /*asmspec_tree=*/NULL_TREE
,
23527 LOOKUP_ONLYCONVERTING
);
23530 pop_nested_class ();
23531 pop_nested_namespace (ns
);
23534 /* We restore the source position here because it's used by
23535 add_pending_template. */
23536 input_location
= saved_loc
;
23538 if (at_eof
&& !pattern_defined
23539 && DECL_EXPLICIT_INSTANTIATION (d
)
23540 && DECL_NOT_REALLY_EXTERN (d
))
23543 The definition of a non-exported function template, a
23544 non-exported member function template, or a non-exported
23545 member function or static data member of a class template
23546 shall be present in every translation unit in which it is
23547 explicitly instantiated. */
23548 permerror (input_location
, "explicit instantiation of %qD "
23549 "but no definition available", d
);
23551 /* If we're in unevaluated context, we just wanted to get the
23552 constant value; this isn't an odr use, so don't queue
23553 a full instantiation. */
23554 if (cp_unevaluated_operand
!= 0)
23556 /* ??? Historically, we have instantiated inline functions, even
23557 when marked as "extern template". */
23558 if (!(external_p
&& VAR_P (d
)))
23559 add_pending_template (d
);
23562 /* Tell the repository that D is available in this translation unit
23563 -- and see if it is supposed to be instantiated here. */
23564 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23566 /* In a PCH file, despite the fact that the repository hasn't
23567 requested instantiation in the PCH it is still possible that
23568 an instantiation will be required in a file that includes the
23571 add_pending_template (d
);
23572 /* Instantiate inline functions so that the inliner can do its
23573 job, even though we'll not be emitting a copy of this
23575 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23579 bool push_to_top
, nested
;
23581 fn_context
= decl_function_context (d
);
23582 if (LAMBDA_FUNCTION_P (d
))
23583 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23584 fn_context
= NULL_TREE
;
23585 nested
= current_function_decl
!= NULL_TREE
;
23586 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23588 vec
<tree
> omp_privatization_save
;
23590 save_omp_privatization_clauses (omp_privatization_save
);
23593 push_to_top_level ();
23596 push_function_context ();
23597 cp_unevaluated_operand
= 0;
23598 c_inhibit_evaluation_warnings
= 0;
23601 /* Mark D as instantiated so that recursive calls to
23602 instantiate_decl do not try to instantiate it again. */
23603 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23605 /* Regenerate the declaration in case the template has been modified
23606 by a subsequent redeclaration. */
23607 regenerate_decl_from_template (d
, td
, args
);
23609 /* We already set the file and line above. Reset them now in case
23610 they changed as a result of calling regenerate_decl_from_template. */
23611 input_location
= DECL_SOURCE_LOCATION (d
);
23616 bool const_init
= false;
23618 /* Clear out DECL_RTL; whatever was there before may not be right
23619 since we've reset the type of the declaration. */
23620 SET_DECL_RTL (d
, NULL
);
23621 DECL_IN_AGGR_P (d
) = 0;
23623 /* The initializer is placed in DECL_INITIAL by
23624 regenerate_decl_from_template so we don't need to
23625 push/pop_access_scope again here. Pull it out so that
23626 cp_finish_decl can process it. */
23627 init
= DECL_INITIAL (d
);
23628 DECL_INITIAL (d
) = NULL_TREE
;
23629 DECL_INITIALIZED_P (d
) = 0;
23631 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23632 initializer. That function will defer actual emission until
23633 we have a chance to determine linkage. */
23634 DECL_EXTERNAL (d
) = 0;
23636 /* Enter the scope of D so that access-checking works correctly. */
23637 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23639 push_nested_class (DECL_CONTEXT (d
));
23641 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23642 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23645 pop_nested_class ();
23647 if (variable_template_p (gen_tmpl
))
23648 note_variable_template_instantiation (d
);
23650 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23651 synthesize_method (d
);
23652 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23654 /* Set up the list of local specializations. */
23655 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23656 tree block
= NULL_TREE
;
23658 /* Set up context. */
23659 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23660 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23661 block
= push_stmt_list ();
23663 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23665 /* Some typedefs referenced from within the template code need to be
23666 access checked at template instantiation time, i.e now. These
23667 types were added to the template at parsing time. Let's get those
23668 and perform the access checks then. */
23669 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23672 /* Create substitution entries for the parameters. */
23673 register_parameter_specializations (code_pattern
, d
);
23675 /* Substitute into the body of the function. */
23676 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23677 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23678 tf_warning_or_error
, tmpl
);
23681 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23682 tf_warning_or_error
, tmpl
,
23683 /*integral_constant_expression_p=*/false);
23685 /* Set the current input_location to the end of the function
23686 so that finish_function knows where we are. */
23688 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23690 /* Remember if we saw an infinite loop in the template. */
23691 current_function_infinite_loop
23692 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23695 /* Finish the function. */
23696 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23697 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23698 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23701 d
= finish_function (/*inline_p=*/false);
23702 expand_or_defer_fn (d
);
23705 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23706 cp_check_omp_declare_reduction (d
);
23709 /* We're not deferring instantiation any more. */
23710 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23713 pop_from_top_level ();
23715 pop_function_context ();
23718 restore_omp_privatization_clauses (omp_privatization_save
);
23721 pop_deferring_access_checks ();
23722 timevar_pop (TV_TEMPLATE_INST
);
23723 pop_tinst_level ();
23724 input_location
= saved_loc
;
23725 cp_unevaluated_operand
= saved_unevaluated_operand
;
23726 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23731 /* Run through the list of templates that we wish we could
23732 instantiate, and instantiate any we can. RETRIES is the
23733 number of times we retry pending template instantiation. */
23736 instantiate_pending_templates (int retries
)
23739 location_t saved_loc
= input_location
;
23741 /* Instantiating templates may trigger vtable generation. This in turn
23742 may require further template instantiations. We place a limit here
23743 to avoid infinite loop. */
23744 if (pending_templates
&& retries
>= max_tinst_depth
)
23746 tree decl
= pending_templates
->tinst
->decl
;
23748 fatal_error (input_location
,
23749 "template instantiation depth exceeds maximum of %d"
23750 " instantiating %q+D, possibly from virtual table generation"
23751 " (use -ftemplate-depth= to increase the maximum)",
23752 max_tinst_depth
, decl
);
23753 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23754 /* Pretend that we defined it. */
23755 DECL_INITIAL (decl
) = error_mark_node
;
23761 struct pending_template
**t
= &pending_templates
;
23762 struct pending_template
*last
= NULL
;
23766 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23767 bool complete
= false;
23769 if (TYPE_P (instantiation
))
23771 if (!COMPLETE_TYPE_P (instantiation
))
23773 instantiate_class_template (instantiation
);
23774 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23775 for (tree fld
= TYPE_FIELDS (instantiation
);
23776 fld
; fld
= TREE_CHAIN (fld
))
23778 || (TREE_CODE (fld
) == FUNCTION_DECL
23779 && !DECL_ARTIFICIAL (fld
)))
23780 && DECL_TEMPLATE_INSTANTIATION (fld
))
23781 instantiate_decl (fld
,
23782 /*defer_ok=*/false,
23783 /*expl_inst_class_mem_p=*/false);
23785 if (COMPLETE_TYPE_P (instantiation
))
23789 complete
= COMPLETE_TYPE_P (instantiation
);
23793 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23794 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23797 = instantiate_decl (instantiation
,
23798 /*defer_ok=*/false,
23799 /*expl_inst_class_mem_p=*/false);
23800 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23804 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23805 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23809 /* If INSTANTIATION has been instantiated, then we don't
23810 need to consider it again in the future. */
23818 current_tinst_level
= NULL
;
23820 last_pending_template
= last
;
23822 while (reconsider
);
23824 input_location
= saved_loc
;
23827 /* Substitute ARGVEC into T, which is a list of initializers for
23828 either base class or a non-static data member. The TREE_PURPOSEs
23829 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23830 instantiate_decl. */
23833 tsubst_initializer_list (tree t
, tree argvec
)
23835 tree inits
= NULL_TREE
;
23836 tree target_ctor
= error_mark_node
;
23838 for (; t
; t
= TREE_CHAIN (t
))
23842 tree expanded_bases
= NULL_TREE
;
23843 tree expanded_arguments
= NULL_TREE
;
23846 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23851 /* Expand the base class expansion type into separate base
23853 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23854 tf_warning_or_error
,
23856 if (expanded_bases
== error_mark_node
)
23859 /* We'll be building separate TREE_LISTs of arguments for
23861 len
= TREE_VEC_LENGTH (expanded_bases
);
23862 expanded_arguments
= make_tree_vec (len
);
23863 for (i
= 0; i
< len
; i
++)
23864 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23866 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23867 expand each argument in the TREE_VALUE of t. */
23868 expr
= make_node (EXPR_PACK_EXPANSION
);
23869 PACK_EXPANSION_LOCAL_P (expr
) = true;
23870 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23871 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23873 if (TREE_VALUE (t
) == void_type_node
)
23874 /* VOID_TYPE_NODE is used to indicate
23875 value-initialization. */
23877 for (i
= 0; i
< len
; i
++)
23878 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23882 /* Substitute parameter packs into each argument in the
23884 in_base_initializer
= 1;
23885 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23887 tree expanded_exprs
;
23889 /* Expand the argument. */
23890 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23892 = tsubst_pack_expansion (expr
, argvec
,
23893 tf_warning_or_error
,
23895 if (expanded_exprs
== error_mark_node
)
23898 /* Prepend each of the expanded expressions to the
23899 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23900 for (i
= 0; i
< len
; i
++)
23902 TREE_VEC_ELT (expanded_arguments
, i
) =
23903 tree_cons (NULL_TREE
,
23904 TREE_VEC_ELT (expanded_exprs
, i
),
23905 TREE_VEC_ELT (expanded_arguments
, i
));
23908 in_base_initializer
= 0;
23910 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23911 since we built them backwards. */
23912 for (i
= 0; i
< len
; i
++)
23914 TREE_VEC_ELT (expanded_arguments
, i
) =
23915 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23920 for (i
= 0; i
< len
; ++i
)
23922 if (expanded_bases
)
23924 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23925 decl
= expand_member_init (decl
);
23926 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23931 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23932 tf_warning_or_error
, NULL_TREE
);
23934 decl
= expand_member_init (decl
);
23935 if (decl
&& !DECL_P (decl
))
23936 in_base_initializer
= 1;
23938 init
= TREE_VALUE (t
);
23940 if (init
!= void_type_node
)
23941 init
= tsubst_expr (init
, argvec
,
23942 tf_warning_or_error
, NULL_TREE
,
23943 /*integral_constant_expression_p=*/false);
23944 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23945 /* If we had an initializer but it instantiated to nothing,
23946 value-initialize the object. This will only occur when
23947 the initializer was a pack expansion where the parameter
23948 packs used in that expansion were of length zero. */
23949 init
= void_type_node
;
23950 in_base_initializer
= 0;
23953 if (target_ctor
!= error_mark_node
23954 && init
!= error_mark_node
)
23956 error ("mem-initializer for %qD follows constructor delegation",
23960 /* Look for a target constructor. */
23961 if (init
!= error_mark_node
23962 && decl
&& CLASS_TYPE_P (decl
)
23963 && same_type_p (decl
, current_class_type
))
23965 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
23968 error ("constructor delegation follows mem-initializer for %qD",
23969 TREE_PURPOSE (inits
));
23972 target_ctor
= init
;
23977 init
= build_tree_list (decl
, init
);
23978 TREE_CHAIN (init
) = inits
;
23986 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23989 set_current_access_from_decl (tree decl
)
23991 if (TREE_PRIVATE (decl
))
23992 current_access_specifier
= access_private_node
;
23993 else if (TREE_PROTECTED (decl
))
23994 current_access_specifier
= access_protected_node
;
23996 current_access_specifier
= access_public_node
;
23999 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24000 is the instantiation (which should have been created with
24001 start_enum) and ARGS are the template arguments to use. */
24004 tsubst_enum (tree tag
, tree newtag
, tree args
)
24008 if (SCOPED_ENUM_P (newtag
))
24009 begin_scope (sk_scoped_enum
, newtag
);
24011 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
24016 decl
= TREE_VALUE (e
);
24017 /* Note that in a template enum, the TREE_VALUE is the
24018 CONST_DECL, not the corresponding INTEGER_CST. */
24019 value
= tsubst_expr (DECL_INITIAL (decl
),
24020 args
, tf_warning_or_error
, NULL_TREE
,
24021 /*integral_constant_expression_p=*/true);
24023 /* Give this enumeration constant the correct access. */
24024 set_current_access_from_decl (decl
);
24026 /* Actually build the enumerator itself. Here we're assuming that
24027 enumerators can't have dependent attributes. */
24028 build_enumerator (DECL_NAME (decl
), value
, newtag
,
24029 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
24032 if (SCOPED_ENUM_P (newtag
))
24035 finish_enum_value_list (newtag
);
24036 finish_enum (newtag
);
24038 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
24039 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
24042 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24043 its type -- but without substituting the innermost set of template
24044 arguments. So, innermost set of template parameters will appear in
24048 get_mostly_instantiated_function_type (tree decl
)
24050 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24051 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
24054 /* Return truthvalue if we're processing a template different from
24055 the last one involved in diagnostics. */
24057 problematic_instantiation_changed (void)
24059 return current_tinst_level
!= last_error_tinst_level
;
24062 /* Remember current template involved in diagnostics. */
24064 record_last_problematic_instantiation (void)
24066 last_error_tinst_level
= current_tinst_level
;
24069 struct tinst_level
*
24070 current_instantiation (void)
24072 return current_tinst_level
;
24075 /* Return TRUE if current_function_decl is being instantiated, false
24079 instantiating_current_function_p (void)
24081 return (current_instantiation ()
24082 && current_instantiation ()->decl
== current_function_decl
);
24085 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24086 type. Return false for ok, true for disallowed. Issue error and
24087 inform messages under control of COMPLAIN. */
24090 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
24092 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
24094 else if (TYPE_PTR_P (type
))
24096 else if (TREE_CODE (type
) == REFERENCE_TYPE
24097 && !TYPE_REF_IS_RVALUE (type
))
24099 else if (TYPE_PTRMEM_P (type
))
24101 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
24103 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
24105 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
24107 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
24109 /* A bound template template parm could later be instantiated to have a valid
24110 nontype parm type via an alias template. */
24111 else if (cxx_dialect
>= cxx11
24112 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24115 if (complain
& tf_error
)
24117 if (type
== error_mark_node
)
24118 inform (input_location
, "invalid template non-type parameter");
24120 error ("%q#T is not a valid type for a template non-type parameter",
24126 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24127 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24130 dependent_type_p_r (tree type
)
24136 A type is dependent if it is:
24138 -- a template parameter. Template template parameters are types
24139 for us (since TYPE_P holds true for them) so we handle
24141 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
24142 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
24144 /* -- a qualified-id with a nested-name-specifier which contains a
24145 class-name that names a dependent type or whose unqualified-id
24146 names a dependent type. */
24147 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24150 /* An alias template specialization can be dependent even if the
24151 resulting type is not. */
24152 if (dependent_alias_template_spec_p (type
))
24155 /* -- a cv-qualified type where the cv-unqualified type is
24157 No code is necessary for this bullet; the code below handles
24158 cv-qualified types, and we don't want to strip aliases with
24159 TYPE_MAIN_VARIANT because of DR 1558. */
24160 /* -- a compound type constructed from any dependent type. */
24161 if (TYPE_PTRMEM_P (type
))
24162 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
24163 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24165 else if (TYPE_PTR_P (type
)
24166 || TREE_CODE (type
) == REFERENCE_TYPE
)
24167 return dependent_type_p (TREE_TYPE (type
));
24168 else if (TREE_CODE (type
) == FUNCTION_TYPE
24169 || TREE_CODE (type
) == METHOD_TYPE
)
24173 if (dependent_type_p (TREE_TYPE (type
)))
24175 for (arg_type
= TYPE_ARG_TYPES (type
);
24177 arg_type
= TREE_CHAIN (arg_type
))
24178 if (dependent_type_p (TREE_VALUE (arg_type
)))
24180 if (cxx_dialect
>= cxx17
)
24181 /* A value-dependent noexcept-specifier makes the type dependent. */
24182 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
24183 if (tree noex
= TREE_PURPOSE (spec
))
24184 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24185 affect overload resolution and treating it as dependent breaks
24187 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
24188 && value_dependent_expression_p (noex
))
24192 /* -- an array type constructed from any dependent type or whose
24193 size is specified by a constant expression that is
24196 We checked for type- and value-dependence of the bounds in
24197 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24198 if (TREE_CODE (type
) == ARRAY_TYPE
)
24200 if (TYPE_DOMAIN (type
)
24201 && dependent_type_p (TYPE_DOMAIN (type
)))
24203 return dependent_type_p (TREE_TYPE (type
));
24206 /* -- a template-id in which either the template name is a template
24208 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24210 /* ... or any of the template arguments is a dependent type or
24211 an expression that is type-dependent or value-dependent. */
24212 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
24213 && (any_dependent_template_arguments_p
24214 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
24217 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24218 dependent; if the argument of the `typeof' expression is not
24219 type-dependent, then it should already been have resolved. */
24220 if (TREE_CODE (type
) == TYPEOF_TYPE
24221 || TREE_CODE (type
) == DECLTYPE_TYPE
24222 || TREE_CODE (type
) == UNDERLYING_TYPE
)
24225 /* A template argument pack is dependent if any of its packed
24227 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
24229 tree args
= ARGUMENT_PACK_ARGS (type
);
24230 int i
, len
= TREE_VEC_LENGTH (args
);
24231 for (i
= 0; i
< len
; ++i
)
24232 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24236 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24237 be template parameters. */
24238 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
24241 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
24244 /* The standard does not specifically mention types that are local
24245 to template functions or local classes, but they should be
24246 considered dependent too. For example:
24248 template <int I> void f() {
24253 The size of `E' cannot be known until the value of `I' has been
24254 determined. Therefore, `E' must be considered dependent. */
24255 scope
= TYPE_CONTEXT (type
);
24256 if (scope
&& TYPE_P (scope
))
24257 return dependent_type_p (scope
);
24258 /* Don't use type_dependent_expression_p here, as it can lead
24259 to infinite recursion trying to determine whether a lambda
24260 nested in a lambda is dependent (c++/47687). */
24261 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
24262 && DECL_LANG_SPECIFIC (scope
)
24263 && DECL_TEMPLATE_INFO (scope
)
24264 && (any_dependent_template_arguments_p
24265 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
24268 /* Other types are non-dependent. */
24272 /* Returns TRUE if TYPE is dependent, in the sense of
24273 [temp.dep.type]. Note that a NULL type is considered dependent. */
24276 dependent_type_p (tree type
)
24278 /* If there are no template parameters in scope, then there can't be
24279 any dependent types. */
24280 if (!processing_template_decl
)
24282 /* If we are not processing a template, then nobody should be
24283 providing us with a dependent type. */
24285 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
24289 /* If the type is NULL, we have not computed a type for the entity
24290 in question; in that case, the type is dependent. */
24294 /* Erroneous types can be considered non-dependent. */
24295 if (type
== error_mark_node
)
24298 /* Getting here with global_type_node means we improperly called this
24299 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24300 gcc_checking_assert (type
!= global_type_node
);
24302 /* If we have not already computed the appropriate value for TYPE,
24304 if (!TYPE_DEPENDENT_P_VALID (type
))
24306 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
24307 TYPE_DEPENDENT_P_VALID (type
) = 1;
24310 return TYPE_DEPENDENT_P (type
);
24313 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24314 lookup. In other words, a dependent type that is not the current
24318 dependent_scope_p (tree scope
)
24320 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24321 && !currently_open_class (scope
));
24324 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24325 an unknown base of 'this' (and is therefore instantiation-dependent). */
24328 unknown_base_ref_p (tree t
)
24330 if (!current_class_ptr
)
24333 tree mem
= TREE_OPERAND (t
, 1);
24334 if (shared_member_p (mem
))
24337 tree cur
= current_nonlambda_class_type ();
24338 if (!any_dependent_bases_p (cur
))
24341 tree ctx
= TREE_OPERAND (t
, 0);
24342 if (DERIVED_FROM_P (ctx
, cur
))
24348 /* T is a SCOPE_REF; return whether we need to consider it
24349 instantiation-dependent so that we can check access at instantiation
24350 time even though we know which member it resolves to. */
24353 instantiation_dependent_scope_ref_p (tree t
)
24355 if (DECL_P (TREE_OPERAND (t
, 1))
24356 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24357 && !unknown_base_ref_p (t
)
24358 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24359 TREE_OPERAND (t
, 1)))
24365 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24366 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24369 /* Note that this predicate is not appropriate for general expressions;
24370 only constant expressions (that satisfy potential_constant_expression)
24371 can be tested for value dependence. */
24374 value_dependent_expression_p (tree expression
)
24376 if (!processing_template_decl
|| expression
== NULL_TREE
)
24379 /* A type-dependent expression is also value-dependent. */
24380 if (type_dependent_expression_p (expression
))
24383 switch (TREE_CODE (expression
))
24386 /* A dependent member function of the current instantiation. */
24387 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24389 case FUNCTION_DECL
:
24390 /* A dependent member function of the current instantiation. */
24391 if (DECL_CLASS_SCOPE_P (expression
)
24392 && dependent_type_p (DECL_CONTEXT (expression
)))
24396 case IDENTIFIER_NODE
:
24397 /* A name that has not been looked up -- must be dependent. */
24400 case TEMPLATE_PARM_INDEX
:
24401 /* A non-type template parm. */
24405 /* A non-type template parm. */
24406 if (DECL_TEMPLATE_PARM_P (expression
))
24408 return value_dependent_expression_p (DECL_INITIAL (expression
));
24411 /* A constant with literal type and is initialized
24412 with an expression that is value-dependent. */
24413 if (DECL_DEPENDENT_INIT_P (expression
)
24414 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24415 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24417 if (DECL_HAS_VALUE_EXPR_P (expression
))
24419 tree value_expr
= DECL_VALUE_EXPR (expression
);
24420 if (value_dependent_expression_p (value_expr
))
24425 case DYNAMIC_CAST_EXPR
:
24426 case STATIC_CAST_EXPR
:
24427 case CONST_CAST_EXPR
:
24428 case REINTERPRET_CAST_EXPR
:
24430 case IMPLICIT_CONV_EXPR
:
24431 /* These expressions are value-dependent if the type to which
24432 the cast occurs is dependent or the expression being casted
24433 is value-dependent. */
24435 tree type
= TREE_TYPE (expression
);
24437 if (dependent_type_p (type
))
24440 /* A functional cast has a list of operands. */
24441 expression
= TREE_OPERAND (expression
, 0);
24444 /* If there are no operands, it must be an expression such
24445 as "int()". This should not happen for aggregate types
24446 because it would form non-constant expressions. */
24447 gcc_assert (cxx_dialect
>= cxx11
24448 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24453 if (TREE_CODE (expression
) == TREE_LIST
)
24454 return any_value_dependent_elements_p (expression
);
24456 return value_dependent_expression_p (expression
);
24460 if (SIZEOF_EXPR_TYPE_P (expression
))
24461 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24465 /* A `sizeof' expression is value-dependent if the operand is
24466 type-dependent or is a pack expansion. */
24467 expression
= TREE_OPERAND (expression
, 0);
24468 if (PACK_EXPANSION_P (expression
))
24470 else if (TYPE_P (expression
))
24471 return dependent_type_p (expression
);
24472 return instantiation_dependent_uneval_expression_p (expression
);
24474 case AT_ENCODE_EXPR
:
24475 /* An 'encode' expression is value-dependent if the operand is
24477 expression
= TREE_OPERAND (expression
, 0);
24478 return dependent_type_p (expression
);
24480 case NOEXCEPT_EXPR
:
24481 expression
= TREE_OPERAND (expression
, 0);
24482 return instantiation_dependent_uneval_expression_p (expression
);
24485 /* All instantiation-dependent expressions should also be considered
24486 value-dependent. */
24487 return instantiation_dependent_scope_ref_p (expression
);
24489 case COMPONENT_REF
:
24490 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24491 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24493 case NONTYPE_ARGUMENT_PACK
:
24494 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24495 is value-dependent. */
24497 tree values
= ARGUMENT_PACK_ARGS (expression
);
24498 int i
, len
= TREE_VEC_LENGTH (values
);
24500 for (i
= 0; i
< len
; ++i
)
24501 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24509 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24511 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24517 if (TREE_CODE (type2
) != TREE_LIST
)
24518 return dependent_type_p (type2
);
24520 for (; type2
; type2
= TREE_CHAIN (type2
))
24521 if (dependent_type_p (TREE_VALUE (type2
)))
24528 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24529 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24532 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24533 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24537 tree op
= TREE_OPERAND (expression
, 0);
24538 return (value_dependent_expression_p (op
)
24539 || has_value_dependent_address (op
));
24542 case REQUIRES_EXPR
:
24543 /* Treat all requires-expressions as value-dependent so
24544 we don't try to fold them. */
24548 return dependent_type_p (TREE_OPERAND (expression
, 0));
24552 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24554 tree fn
= get_callee_fndecl (expression
);
24556 nargs
= call_expr_nargs (expression
);
24557 for (i
= 0; i
< nargs
; ++i
)
24559 tree op
= CALL_EXPR_ARG (expression
, i
);
24560 /* In a call to a constexpr member function, look through the
24561 implicit ADDR_EXPR on the object argument so that it doesn't
24562 cause the call to be considered value-dependent. We also
24563 look through it in potential_constant_expression. */
24564 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24565 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24566 && TREE_CODE (op
) == ADDR_EXPR
)
24567 op
= TREE_OPERAND (op
, 0);
24568 if (value_dependent_expression_p (op
))
24574 case TEMPLATE_ID_EXPR
:
24575 return variable_concept_p (TREE_OPERAND (expression
, 0));
24581 if (dependent_type_p (TREE_TYPE (expression
)))
24583 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24584 if (value_dependent_expression_p (val
))
24590 /* Treat a GNU statement expression as dependent to avoid crashing
24591 under instantiate_non_dependent_expr; it can't be constant. */
24595 /* A constant expression is value-dependent if any subexpression is
24596 value-dependent. */
24597 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24599 case tcc_reference
:
24601 case tcc_comparison
:
24603 case tcc_expression
:
24606 int i
, len
= cp_tree_operand_length (expression
);
24608 for (i
= 0; i
< len
; i
++)
24610 tree t
= TREE_OPERAND (expression
, i
);
24612 /* In some cases, some of the operands may be missing.
24613 (For example, in the case of PREDECREMENT_EXPR, the
24614 amount to increment by may be missing.) That doesn't
24615 make the expression dependent. */
24616 if (t
&& value_dependent_expression_p (t
))
24627 /* The expression is not value-dependent. */
24631 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24632 [temp.dep.expr]. Note that an expression with no type is
24633 considered dependent. Other parts of the compiler arrange for an
24634 expression with type-dependent subexpressions to have no type, so
24635 this function doesn't have to be fully recursive. */
24638 type_dependent_expression_p (tree expression
)
24640 if (!processing_template_decl
)
24643 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24646 STRIP_ANY_LOCATION_WRAPPER (expression
);
24648 /* An unresolved name is always dependent. */
24649 if (identifier_p (expression
)
24650 || TREE_CODE (expression
) == USING_DECL
24651 || TREE_CODE (expression
) == WILDCARD_DECL
)
24654 /* A fold expression is type-dependent. */
24655 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24656 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24657 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24658 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24661 /* Some expression forms are never type-dependent. */
24662 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24663 || TREE_CODE (expression
) == SIZEOF_EXPR
24664 || TREE_CODE (expression
) == ALIGNOF_EXPR
24665 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24666 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24667 || TREE_CODE (expression
) == TRAIT_EXPR
24668 || TREE_CODE (expression
) == TYPEID_EXPR
24669 || TREE_CODE (expression
) == DELETE_EXPR
24670 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24671 || TREE_CODE (expression
) == THROW_EXPR
24672 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24675 /* The types of these expressions depends only on the type to which
24676 the cast occurs. */
24677 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24678 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24679 || TREE_CODE (expression
) == CONST_CAST_EXPR
24680 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24681 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24682 || TREE_CODE (expression
) == CAST_EXPR
)
24683 return dependent_type_p (TREE_TYPE (expression
));
24685 /* The types of these expressions depends only on the type created
24686 by the expression. */
24687 if (TREE_CODE (expression
) == NEW_EXPR
24688 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24690 /* For NEW_EXPR tree nodes created inside a template, either
24691 the object type itself or a TREE_LIST may appear as the
24693 tree type
= TREE_OPERAND (expression
, 1);
24694 if (TREE_CODE (type
) == TREE_LIST
)
24695 /* This is an array type. We need to check array dimensions
24697 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24698 || value_dependent_expression_p
24699 (TREE_OPERAND (TREE_VALUE (type
), 1));
24701 return dependent_type_p (type
);
24704 if (TREE_CODE (expression
) == SCOPE_REF
)
24706 tree scope
= TREE_OPERAND (expression
, 0);
24707 tree name
= TREE_OPERAND (expression
, 1);
24709 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24710 contains an identifier associated by name lookup with one or more
24711 declarations declared with a dependent type, or...a
24712 nested-name-specifier or qualified-id that names a member of an
24713 unknown specialization. */
24714 return (type_dependent_expression_p (name
)
24715 || dependent_scope_p (scope
));
24718 if (TREE_CODE (expression
) == TEMPLATE_DECL
24719 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24720 return uses_outer_template_parms (expression
);
24722 if (TREE_CODE (expression
) == STMT_EXPR
)
24723 expression
= stmt_expr_value_expr (expression
);
24725 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24730 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24732 if (type_dependent_expression_p (elt
))
24738 /* A static data member of the current instantiation with incomplete
24739 array type is type-dependent, as the definition and specializations
24740 can have different bounds. */
24741 if (VAR_P (expression
)
24742 && DECL_CLASS_SCOPE_P (expression
)
24743 && dependent_type_p (DECL_CONTEXT (expression
))
24744 && VAR_HAD_UNKNOWN_BOUND (expression
))
24747 /* An array of unknown bound depending on a variadic parameter, eg:
24749 template<typename... Args>
24750 void foo (Args... args)
24752 int arr[] = { args... };
24755 template<int... vals>
24758 int arr[] = { vals... };
24761 If the array has no length and has an initializer, it must be that
24762 we couldn't determine its length in cp_complete_array_type because
24763 it is dependent. */
24764 if (VAR_P (expression
)
24765 && TREE_TYPE (expression
) != NULL_TREE
24766 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24767 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24768 && DECL_INITIAL (expression
))
24771 /* A function or variable template-id is type-dependent if it has any
24772 dependent template arguments. */
24773 if (VAR_OR_FUNCTION_DECL_P (expression
)
24774 && DECL_LANG_SPECIFIC (expression
)
24775 && DECL_TEMPLATE_INFO (expression
))
24777 /* Consider the innermost template arguments, since those are the ones
24778 that come from the template-id; the template arguments for the
24779 enclosing class do not make it type-dependent unless they are used in
24780 the type of the decl. */
24781 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24782 && (any_dependent_template_arguments_p
24783 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24787 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24788 type-dependent. Checking this is important for functions with auto return
24789 type, which looks like a dependent type. */
24790 if (TREE_CODE (expression
) == FUNCTION_DECL
24791 && !(DECL_CLASS_SCOPE_P (expression
)
24792 && dependent_type_p (DECL_CONTEXT (expression
)))
24793 && !(DECL_LANG_SPECIFIC (expression
)
24794 && DECL_FRIEND_P (expression
)
24795 && (!DECL_FRIEND_CONTEXT (expression
)
24796 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24797 && !DECL_LOCAL_FUNCTION_P (expression
))
24799 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24800 || undeduced_auto_decl (expression
));
24804 /* Always dependent, on the number of arguments if nothing else. */
24805 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24808 if (TREE_TYPE (expression
) == unknown_type_node
)
24810 if (TREE_CODE (expression
) == ADDR_EXPR
)
24811 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24812 if (TREE_CODE (expression
) == COMPONENT_REF
24813 || TREE_CODE (expression
) == OFFSET_REF
)
24815 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24817 expression
= TREE_OPERAND (expression
, 1);
24818 if (identifier_p (expression
))
24821 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24822 if (TREE_CODE (expression
) == SCOPE_REF
)
24825 if (BASELINK_P (expression
))
24827 if (BASELINK_OPTYPE (expression
)
24828 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24830 expression
= BASELINK_FUNCTIONS (expression
);
24833 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24835 if (any_dependent_template_arguments_p
24836 (TREE_OPERAND (expression
, 1)))
24838 expression
= TREE_OPERAND (expression
, 0);
24839 if (identifier_p (expression
))
24843 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24844 || TREE_CODE (expression
) == FUNCTION_DECL
);
24846 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24847 if (type_dependent_expression_p (*iter
))
24853 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24855 /* Dependent type attributes might not have made it from the decl to
24857 if (DECL_P (expression
)
24858 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24861 return (dependent_type_p (TREE_TYPE (expression
)));
24864 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24865 type-dependent if the expression refers to a member of the current
24866 instantiation and the type of the referenced member is dependent, or the
24867 class member access expression refers to a member of an unknown
24870 This function returns true if the OBJECT in such a class member access
24871 expression is of an unknown specialization. */
24874 type_dependent_object_expression_p (tree object
)
24876 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24878 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24880 tree scope
= TREE_TYPE (object
);
24881 return (!scope
|| dependent_scope_p (scope
));
24884 /* walk_tree callback function for instantiation_dependent_expression_p,
24885 below. Returns non-zero if a dependent subexpression is found. */
24888 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24893 /* We don't have to worry about decltype currently because decltype
24894 of an instantiation-dependent expr is a dependent type. This
24895 might change depending on the resolution of DR 1172. */
24896 *walk_subtrees
= false;
24899 enum tree_code code
= TREE_CODE (*tp
);
24902 /* Don't treat an argument list as dependent just because it has no
24908 case TEMPLATE_PARM_INDEX
:
24911 /* Handle expressions with type operands. */
24915 case AT_ENCODE_EXPR
:
24917 tree op
= TREE_OPERAND (*tp
, 0);
24918 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24919 op
= TREE_TYPE (op
);
24922 if (dependent_type_p (op
))
24926 *walk_subtrees
= false;
24933 case COMPONENT_REF
:
24934 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24935 /* In a template, finish_class_member_access_expr creates a
24936 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24937 type-dependent, so that we can check access control at
24938 instantiation time (PR 42277). See also Core issue 1273. */
24943 if (instantiation_dependent_scope_ref_p (*tp
))
24948 /* Treat statement-expressions as dependent. */
24952 /* Treat requires-expressions as dependent. */
24953 case REQUIRES_EXPR
:
24957 /* Treat calls to function concepts as dependent. */
24958 if (function_concept_check_p (*tp
))
24962 case TEMPLATE_ID_EXPR
:
24963 /* And variable concepts. */
24964 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24972 if (type_dependent_expression_p (*tp
))
24978 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24979 sense defined by the ABI:
24981 "An expression is instantiation-dependent if it is type-dependent
24982 or value-dependent, or it has a subexpression that is type-dependent
24983 or value-dependent."
24985 Except don't actually check value-dependence for unevaluated expressions,
24986 because in sizeof(i) we don't care about the value of i. Checking
24987 type-dependence will in turn check value-dependence of array bounds/template
24988 arguments as needed. */
24991 instantiation_dependent_uneval_expression_p (tree expression
)
24995 if (!processing_template_decl
)
24998 if (expression
== error_mark_node
)
25001 result
= cp_walk_tree_without_duplicates (&expression
,
25002 instantiation_dependent_r
, NULL
);
25003 return result
!= NULL_TREE
;
25006 /* As above, but also check value-dependence of the expression as a whole. */
25009 instantiation_dependent_expression_p (tree expression
)
25011 return (instantiation_dependent_uneval_expression_p (expression
)
25012 || value_dependent_expression_p (expression
));
25015 /* Like type_dependent_expression_p, but it also works while not processing
25016 a template definition, i.e. during substitution or mangling. */
25019 type_dependent_expression_p_push (tree expr
)
25022 ++processing_template_decl
;
25023 b
= type_dependent_expression_p (expr
);
25024 --processing_template_decl
;
25028 /* Returns TRUE if ARGS contains a type-dependent expression. */
25031 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
25036 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
25038 if (type_dependent_expression_p (arg
))
25044 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25045 expressions) contains any type-dependent expressions. */
25048 any_type_dependent_elements_p (const_tree list
)
25050 for (; list
; list
= TREE_CHAIN (list
))
25051 if (type_dependent_expression_p (TREE_VALUE (list
)))
25057 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25058 expressions) contains any value-dependent expressions. */
25061 any_value_dependent_elements_p (const_tree list
)
25063 for (; list
; list
= TREE_CHAIN (list
))
25064 if (value_dependent_expression_p (TREE_VALUE (list
)))
25070 /* Returns TRUE if the ARG (a template argument) is dependent. */
25073 dependent_template_arg_p (tree arg
)
25075 if (!processing_template_decl
)
25078 /* Assume a template argument that was wrongly written by the user
25079 is dependent. This is consistent with what
25080 any_dependent_template_arguments_p [that calls this function]
25082 if (!arg
|| arg
== error_mark_node
)
25085 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
25086 arg
= argument_pack_select_arg (arg
);
25088 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
25090 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25092 if (DECL_TEMPLATE_PARM_P (arg
))
25094 /* A member template of a dependent class is not necessarily
25095 type-dependent, but it is a dependent template argument because it
25096 will be a member of an unknown specialization to that template. */
25097 tree scope
= CP_DECL_CONTEXT (arg
);
25098 return TYPE_P (scope
) && dependent_type_p (scope
);
25100 else if (ARGUMENT_PACK_P (arg
))
25102 tree args
= ARGUMENT_PACK_ARGS (arg
);
25103 int i
, len
= TREE_VEC_LENGTH (args
);
25104 for (i
= 0; i
< len
; ++i
)
25106 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25112 else if (TYPE_P (arg
))
25113 return dependent_type_p (arg
);
25115 return (type_dependent_expression_p (arg
)
25116 || value_dependent_expression_p (arg
));
25119 /* Returns true if ARGS (a collection of template arguments) contains
25120 any types that require structural equality testing. */
25123 any_template_arguments_need_structural_equality_p (tree args
)
25130 if (args
== error_mark_node
)
25133 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25135 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25136 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25138 tree arg
= TREE_VEC_ELT (level
, j
);
25139 tree packed_args
= NULL_TREE
;
25142 if (ARGUMENT_PACK_P (arg
))
25144 /* Look inside the argument pack. */
25145 packed_args
= ARGUMENT_PACK_ARGS (arg
);
25146 len
= TREE_VEC_LENGTH (packed_args
);
25149 for (k
= 0; k
< len
; ++k
)
25152 arg
= TREE_VEC_ELT (packed_args
, k
);
25154 if (error_operand_p (arg
))
25156 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25158 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
25160 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
25161 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
25170 /* Returns true if ARGS (a collection of template arguments) contains
25171 any dependent arguments. */
25174 any_dependent_template_arguments_p (const_tree args
)
25181 if (args
== error_mark_node
)
25184 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25186 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25187 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25188 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
25195 /* Returns true if ARGS contains any errors. */
25198 any_erroneous_template_args_p (const_tree args
)
25203 if (args
== error_mark_node
)
25206 if (args
&& TREE_CODE (args
) != TREE_VEC
)
25208 if (tree ti
= get_template_info (args
))
25209 args
= TI_ARGS (ti
);
25217 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25219 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25220 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25221 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
25228 /* Returns TRUE if the template TMPL is type-dependent. */
25231 dependent_template_p (tree tmpl
)
25233 if (TREE_CODE (tmpl
) == OVERLOAD
)
25235 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
25236 if (dependent_template_p (*iter
))
25241 /* Template template parameters are dependent. */
25242 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
25243 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
25245 /* So are names that have not been looked up. */
25246 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
25251 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25254 dependent_template_id_p (tree tmpl
, tree args
)
25256 return (dependent_template_p (tmpl
)
25257 || any_dependent_template_arguments_p (args
));
25260 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25264 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
25268 if (!processing_template_decl
)
25271 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
25273 tree decl
= TREE_VEC_ELT (declv
, i
);
25274 tree init
= TREE_VEC_ELT (initv
, i
);
25275 tree cond
= TREE_VEC_ELT (condv
, i
);
25276 tree incr
= TREE_VEC_ELT (incrv
, i
);
25278 if (type_dependent_expression_p (decl
)
25279 || TREE_CODE (decl
) == SCOPE_REF
)
25282 if (init
&& type_dependent_expression_p (init
))
25285 if (type_dependent_expression_p (cond
))
25288 if (COMPARISON_CLASS_P (cond
)
25289 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
25290 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
25293 if (TREE_CODE (incr
) == MODOP_EXPR
)
25295 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
25296 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
25299 else if (type_dependent_expression_p (incr
))
25301 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
25303 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
25305 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
25307 tree t
= TREE_OPERAND (incr
, 1);
25308 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
25309 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
25318 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25319 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25320 no such TYPE can be found. Note that this function peers inside
25321 uninstantiated templates and therefore should be used only in
25322 extremely limited situations. ONLY_CURRENT_P restricts this
25323 peering to the currently open classes hierarchy (which is required
25324 when comparing types). */
25327 resolve_typename_type (tree type
, bool only_current_p
)
25336 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
25338 scope
= TYPE_CONTEXT (type
);
25339 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25340 gcc_checking_assert (uses_template_parms (scope
));
25342 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25343 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25344 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25345 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25346 identifier of the TYPENAME_TYPE anymore.
25347 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25348 TYPENAME_TYPE instead, we avoid messing up with a possible
25349 typedef variant case. */
25350 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25352 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25353 it first before we can figure out what NAME refers to. */
25354 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25356 if (TYPENAME_IS_RESOLVING_P (scope
))
25357 /* Given a class template A with a dependent base with nested type C,
25358 typedef typename A::C::C C will land us here, as trying to resolve
25359 the initial A::C leads to the local C typedef, which leads back to
25360 A::C::C. So we break the recursion now. */
25363 scope
= resolve_typename_type (scope
, only_current_p
);
25365 /* If we don't know what SCOPE refers to, then we cannot resolve the
25367 if (!CLASS_TYPE_P (scope
))
25369 /* If this is a typedef, we don't want to look inside (c++/11987). */
25370 if (typedef_variant_p (type
))
25372 /* If SCOPE isn't the template itself, it will not have a valid
25373 TYPE_FIELDS list. */
25374 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25375 /* scope is either the template itself or a compatible instantiation
25376 like X<T>, so look up the name in the original template. */
25377 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25378 /* If scope has no fields, it can't be a current instantiation. Check this
25379 before currently_open_class to avoid infinite recursion (71515). */
25380 if (!TYPE_FIELDS (scope
))
25382 /* If the SCOPE is not the current instantiation, there's no reason
25383 to look inside it. */
25384 if (only_current_p
&& !currently_open_class (scope
))
25386 /* Enter the SCOPE so that name lookup will be resolved as if we
25387 were in the class definition. In particular, SCOPE will no
25388 longer be considered a dependent type. */
25389 pushed_scope
= push_scope (scope
);
25390 /* Look up the declaration. */
25391 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25392 tf_warning_or_error
);
25394 result
= NULL_TREE
;
25396 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25397 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25398 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25401 else if (identifier_p (fullname
)
25402 && TREE_CODE (decl
) == TYPE_DECL
)
25404 result
= TREE_TYPE (decl
);
25405 if (result
== error_mark_node
)
25406 result
= NULL_TREE
;
25408 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25409 && DECL_CLASS_TEMPLATE_P (decl
))
25411 /* Obtain the template and the arguments. */
25412 tree tmpl
= TREE_OPERAND (fullname
, 0);
25413 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25415 /* We get here with a plain identifier because a previous tentative
25416 parse of the nested-name-specifier as part of a ptr-operator saw
25417 ::template X<A>. The use of ::template is necessary in a
25418 ptr-operator, but wrong in a declarator-id.
25420 [temp.names]: In a qualified-id of a declarator-id, the keyword
25421 template shall not appear at the top level. */
25422 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25423 "keyword %<template%> not allowed in declarator-id");
25426 tree args
= TREE_OPERAND (fullname
, 1);
25427 /* Instantiate the template. */
25428 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25429 /*entering_scope=*/true,
25430 tf_error
| tf_user
);
25431 if (result
== error_mark_node
)
25432 result
= NULL_TREE
;
25435 /* Leave the SCOPE. */
25437 pop_scope (pushed_scope
);
25439 /* If we failed to resolve it, return the original typename. */
25443 /* If lookup found a typename type, resolve that too. */
25444 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25446 /* Ill-formed programs can cause infinite recursion here, so we
25447 must catch that. */
25448 TYPENAME_IS_RESOLVING_P (result
) = 1;
25449 result
= resolve_typename_type (result
, only_current_p
);
25450 TYPENAME_IS_RESOLVING_P (result
) = 0;
25453 /* Qualify the resulting type. */
25454 quals
= cp_type_quals (type
);
25456 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25461 /* EXPR is an expression which is not type-dependent. Return a proxy
25462 for EXPR that can be used to compute the types of larger
25463 expressions containing EXPR. */
25466 build_non_dependent_expr (tree expr
)
25468 tree orig_expr
= expr
;
25471 /* When checking, try to get a constant value for all non-dependent
25472 expressions in order to expose bugs in *_dependent_expression_p
25473 and constexpr. This can affect code generation, see PR70704, so
25474 only do this for -fchecking=2. */
25475 if (flag_checking
> 1
25476 && cxx_dialect
>= cxx11
25477 /* Don't do this during nsdmi parsing as it can lead to
25478 unexpected recursive instantiations. */
25479 && !parsing_nsdmi ()
25480 /* Don't do this during concept expansion either and for
25481 the same reason. */
25482 && !expanding_concept ())
25483 fold_non_dependent_expr (expr
);
25485 STRIP_ANY_LOCATION_WRAPPER (expr
);
25487 /* Preserve OVERLOADs; the functions must be available to resolve
25490 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25491 inner_expr
= stmt_expr_value_expr (inner_expr
);
25492 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25493 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25494 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25495 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25496 if (is_overloaded_fn (inner_expr
)
25497 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25499 /* There is no need to return a proxy for a variable. */
25502 /* Preserve string constants; conversions from string constants to
25503 "char *" are allowed, even though normally a "const char *"
25504 cannot be used to initialize a "char *". */
25505 if (TREE_CODE (expr
) == STRING_CST
)
25507 /* Preserve void and arithmetic constants, as an optimization -- there is no
25508 reason to create a new node. */
25509 if (TREE_CODE (expr
) == VOID_CST
25510 || TREE_CODE (expr
) == INTEGER_CST
25511 || TREE_CODE (expr
) == REAL_CST
)
25513 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25514 There is at least one place where we want to know that a
25515 particular expression is a throw-expression: when checking a ?:
25516 expression, there are special rules if the second or third
25517 argument is a throw-expression. */
25518 if (TREE_CODE (expr
) == THROW_EXPR
)
25521 /* Don't wrap an initializer list, we need to be able to look inside. */
25522 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25525 /* Don't wrap a dummy object, we need to be able to test for it. */
25526 if (is_dummy_object (expr
))
25529 if (TREE_CODE (expr
) == COND_EXPR
)
25530 return build3 (COND_EXPR
,
25532 TREE_OPERAND (expr
, 0),
25533 (TREE_OPERAND (expr
, 1)
25534 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25535 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25536 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25537 if (TREE_CODE (expr
) == COMPOUND_EXPR
25538 && !COMPOUND_EXPR_OVERLOADED (expr
))
25539 return build2 (COMPOUND_EXPR
,
25541 TREE_OPERAND (expr
, 0),
25542 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25544 /* If the type is unknown, it can't really be non-dependent */
25545 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25547 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25548 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
25549 TREE_TYPE (expr
), expr
);
25552 /* ARGS is a vector of expressions as arguments to a function call.
25553 Replace the arguments with equivalent non-dependent expressions.
25554 This modifies ARGS in place. */
25557 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25562 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25564 tree newarg
= build_non_dependent_expr (arg
);
25566 (*args
)[ix
] = newarg
;
25570 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25571 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25572 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25575 make_auto_1 (tree name
, bool set_canonical
)
25577 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25578 TYPE_NAME (au
) = build_decl (input_location
,
25579 TYPE_DECL
, name
, au
);
25580 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25581 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25582 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25583 TYPE_NAME (au
), NULL_TREE
);
25585 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25586 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25587 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25593 make_decltype_auto (void)
25595 return make_auto_1 (decltype_auto_identifier
, true);
25601 return make_auto_1 (auto_identifier
, true);
25604 /* Return a C++17 deduction placeholder for class template TMPL. */
25607 make_template_placeholder (tree tmpl
)
25609 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25610 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25614 /* True iff T is a C++17 class template deduction placeholder. */
25617 template_placeholder_p (tree t
)
25619 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25622 /* Make a "constrained auto" type-specifier. This is an
25623 auto type with constraints that must be associated after
25624 deduction. The constraint is formed from the given
25625 CONC and its optional sequence of arguments, which are
25626 non-null if written as partial-concept-id. */
25629 make_constrained_auto (tree con
, tree args
)
25631 tree type
= make_auto_1 (auto_identifier
, false);
25633 /* Build the constraint. */
25634 tree tmpl
= DECL_TI_TEMPLATE (con
);
25635 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25636 expr
= build_concept_check (expr
, type
, args
);
25638 tree constr
= normalize_expression (expr
);
25639 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25641 /* Our canonical type depends on the constraint. */
25642 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25644 /* Attach the constraint to the type declaration. */
25645 tree decl
= TYPE_NAME (type
);
25649 /* Given type ARG, return std::initializer_list<ARG>. */
25654 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25656 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25658 gcc_rich_location
richloc (input_location
);
25659 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25660 error_at (&richloc
,
25661 "deducing from brace-enclosed initializer list"
25662 " requires %<#include <initializer_list>%>");
25664 return error_mark_node
;
25666 tree argvec
= make_tree_vec (1);
25667 TREE_VEC_ELT (argvec
, 0) = arg
;
25669 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25670 NULL_TREE
, 0, tf_warning_or_error
);
25673 /* Replace auto in TYPE with std::initializer_list<auto>. */
25676 listify_autos (tree type
, tree auto_node
)
25678 tree init_auto
= listify (auto_node
);
25679 tree argvec
= make_tree_vec (1);
25680 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25681 if (processing_template_decl
)
25682 argvec
= add_to_template_args (current_template_args (), argvec
);
25683 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25686 /* Hash traits for hashing possibly constrained 'auto'
25687 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25689 struct auto_hash
: default_hash_traits
<tree
>
25691 static inline hashval_t
hash (tree
);
25692 static inline bool equal (tree
, tree
);
25695 /* Hash the 'auto' T. */
25698 auto_hash::hash (tree t
)
25700 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25701 /* Matching constrained-type-specifiers denote the same template
25702 parameter, so hash the constraint. */
25703 return hash_placeholder_constraint (c
);
25705 /* But unconstrained autos are all separate, so just hash the pointer. */
25706 return iterative_hash_object (t
, 0);
25709 /* Compare two 'auto's. */
25712 auto_hash::equal (tree t1
, tree t2
)
25717 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25718 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25720 /* Two unconstrained autos are distinct. */
25724 return equivalent_placeholder_constraints (c1
, c2
);
25727 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25728 constrained) auto, add it to the vector. */
25731 extract_autos_r (tree t
, void *data
)
25733 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25736 /* All the autos were built with index 0; fix that up now. */
25737 tree
*p
= hash
.find_slot (t
, INSERT
);
25740 /* If this is a repeated constrained-type-specifier, use the index we
25742 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25745 /* Otherwise this is new, so use the current count. */
25747 idx
= hash
.elements () - 1;
25749 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25752 /* Always keep walking. */
25756 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25757 says they can appear anywhere in the type. */
25760 extract_autos (tree type
)
25762 hash_set
<tree
> visited
;
25763 hash_table
<auto_hash
> hash (2);
25765 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25767 tree tree_vec
= make_tree_vec (hash
.elements());
25768 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25769 iter
!= hash
.end(); ++iter
)
25772 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25773 TREE_VEC_ELT (tree_vec
, i
)
25774 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25780 /* The stem for deduction guide names. */
25781 const char *const dguide_base
= "__dguide_";
25783 /* Return the name for a deduction guide for class template TMPL. */
25786 dguide_name (tree tmpl
)
25788 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25789 tree tname
= TYPE_IDENTIFIER (type
);
25790 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25791 + IDENTIFIER_LENGTH (tname
));
25792 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25793 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25794 IDENTIFIER_LENGTH (tname
) + 1);
25795 tree dname
= get_identifier (buf
);
25796 TREE_TYPE (dname
) = type
;
25800 /* True if NAME is the name of a deduction guide. */
25803 dguide_name_p (tree name
)
25805 return (TREE_CODE (name
) == IDENTIFIER_NODE
25806 && TREE_TYPE (name
)
25807 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25808 strlen (dguide_base
)));
25811 /* True if FN is a deduction guide. */
25814 deduction_guide_p (const_tree fn
)
25817 if (tree name
= DECL_NAME (fn
))
25818 return dguide_name_p (name
);
25822 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25825 copy_guide_p (const_tree fn
)
25827 gcc_assert (deduction_guide_p (fn
));
25828 if (!DECL_ARTIFICIAL (fn
))
25830 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25831 return (TREE_CHAIN (parms
) == void_list_node
25832 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25835 /* True if FN is a guide generated from a constructor template. */
25838 template_guide_p (const_tree fn
)
25840 gcc_assert (deduction_guide_p (fn
));
25841 if (!DECL_ARTIFICIAL (fn
))
25843 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25844 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25845 return PRIMARY_TEMPLATE_P (org
);
25849 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25850 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25851 template parameter types. Note that the handling of template template
25852 parameters relies on current_template_parms being set appropriately for the
25856 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25857 tree tsubst_args
, tsubst_flags_t complain
)
25859 if (olddecl
== error_mark_node
)
25860 return error_mark_node
;
25862 tree oldidx
= get_template_parm_index (olddecl
);
25865 if (TREE_CODE (olddecl
) == TYPE_DECL
25866 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25868 tree oldtype
= TREE_TYPE (olddecl
);
25869 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25870 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25871 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25872 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25873 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25877 newtype
= TREE_TYPE (olddecl
);
25878 if (type_uses_auto (newtype
))
25880 // Substitute once to fix references to other template parameters.
25881 newtype
= tsubst (newtype
, tsubst_args
,
25882 complain
|tf_partial
, NULL_TREE
);
25883 // Now substitute again to reduce the level of the auto.
25884 newtype
= tsubst (newtype
, current_template_args (),
25885 complain
, NULL_TREE
);
25888 newtype
= tsubst (newtype
, tsubst_args
,
25889 complain
, NULL_TREE
);
25893 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25894 DECL_NAME (olddecl
), newtype
);
25895 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25898 if (TREE_CODE (olddecl
) == TYPE_DECL
25899 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25901 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25902 = build_template_parm_index (index
, level
, level
,
25904 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25905 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25906 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25907 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25909 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25911 DECL_TEMPLATE_RESULT (newdecl
)
25912 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25913 DECL_NAME (olddecl
), newtype
);
25914 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25915 // First create a copy (ttargs) of tsubst_args with an
25916 // additional level for the template template parameter's own
25917 // template parameters (ttparms).
25918 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25919 (DECL_TEMPLATE_PARMS (olddecl
)));
25920 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25921 tree ttargs
= make_tree_vec (depth
+ 1);
25922 for (int i
= 0; i
< depth
; ++i
)
25923 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25924 TREE_VEC_ELT (ttargs
, depth
)
25925 = template_parms_level_to_args (ttparms
);
25926 // Substitute ttargs into ttparms to fix references to
25927 // other template parameters.
25928 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25929 complain
|tf_partial
);
25930 // Now substitute again with args based on tparms, to reduce
25931 // the level of the ttparms.
25932 ttargs
= current_template_args ();
25933 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25935 // Finally, tack the adjusted parms onto tparms.
25936 ttparms
= tree_cons (size_int (depth
), ttparms
,
25937 current_template_parms
);
25938 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25943 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25945 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25946 TREE_CODE (oldconst
),
25947 DECL_NAME (oldconst
), newtype
);
25948 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25949 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25950 SET_DECL_TEMPLATE_PARM_P (newconst
);
25951 newidx
= build_template_parm_index (index
, level
, level
,
25952 newconst
, newtype
);
25953 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25954 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25955 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25961 /* Returns a C++17 class deduction guide template based on the constructor
25962 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25963 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25966 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25968 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25969 bool memtmpl
= false;
25972 tree fn_tmpl
= NULL_TREE
;
25977 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25980 type
= TREE_TYPE (type
);
25981 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25984 fparms
= void_list_node
;
25986 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25987 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25988 targs
= CLASSTYPE_TI_ARGS (type
);
25991 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25992 explicit_p
= false;
25996 ++processing_template_decl
;
26000 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
26001 : DECL_TI_TEMPLATE (ctor
));
26003 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
26004 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
26006 type
= DECL_CONTEXT (ctor
);
26008 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
26009 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26010 fully specialized args for the enclosing class. Strip those off, as
26011 the deduction guide won't have those template parameters. */
26012 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
26013 TMPL_PARMS_DEPTH (tparms
));
26014 /* Discard the 'this' parameter. */
26015 fparms
= FUNCTION_ARG_CHAIN (ctor
);
26016 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
26017 ci
= get_constraints (ctor
);
26018 loc
= DECL_SOURCE_LOCATION (ctor
);
26019 explicit_p
= DECL_NONCONVERTING_P (ctor
);
26021 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
26025 /* For a member template constructor, we need to flatten the two
26026 template parameter lists into one, and then adjust the function
26027 signature accordingly. This gets...complicated. */
26028 tree save_parms
= current_template_parms
;
26030 /* For a member template we should have two levels of parms/args, one
26031 for the class and one for the constructor. We stripped
26032 specialized args for further enclosing classes above. */
26033 const int depth
= 2;
26034 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
26036 /* Template args for translating references to the two-level template
26037 parameters into references to the one-level template parameters we
26039 tree tsubst_args
= copy_node (targs
);
26040 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
26041 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
26043 /* Template parms for the constructor template. */
26044 tree ftparms
= TREE_VALUE (tparms
);
26045 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
26046 /* Template parms for the class template. */
26047 tparms
= TREE_CHAIN (tparms
);
26048 tree ctparms
= TREE_VALUE (tparms
);
26049 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
26050 /* Template parms for the deduction guide start as a copy of the
26051 template parms for the class. We set current_template_parms for
26052 lookup_template_class_1. */
26053 current_template_parms
= tparms
= copy_node (tparms
);
26054 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
26055 for (unsigned i
= 0; i
< clen
; ++i
)
26056 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
26058 /* Now we need to rewrite the constructor parms to append them to the
26060 for (unsigned i
= 0; i
< flen
; ++i
)
26062 unsigned index
= i
+ clen
;
26063 unsigned level
= 1;
26064 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
26065 tree olddecl
= TREE_VALUE (oldelt
);
26066 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
26067 tsubst_args
, complain
);
26068 if (newdecl
== error_mark_node
)
26070 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
26071 tsubst_args
, complain
, ctor
);
26072 tree list
= build_tree_list (newdef
, newdecl
);
26073 TEMPLATE_PARM_CONSTRAINTS (list
)
26074 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
26075 tsubst_args
, complain
, ctor
);
26076 TREE_VEC_ELT (new_vec
, index
) = list
;
26077 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
26080 /* Now we have a final set of template parms to substitute into the
26081 function signature. */
26082 targs
= template_parms_to_args (tparms
);
26083 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
26085 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
26087 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
26089 current_template_parms
= save_parms
;
26092 --processing_template_decl
;
26094 return error_mark_node
;
26099 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26100 tparms
= copy_node (tparms
);
26101 INNERMOST_TEMPLATE_PARMS (tparms
)
26102 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
26105 tree fntype
= build_function_type (type
, fparms
);
26106 tree ded_fn
= build_lang_decl_loc (loc
,
26108 dguide_name (type
), fntype
);
26109 DECL_ARGUMENTS (ded_fn
) = fargs
;
26110 DECL_ARTIFICIAL (ded_fn
) = true;
26111 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
26112 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
26113 DECL_ARTIFICIAL (ded_tmpl
) = true;
26114 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
26115 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
26116 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
26117 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
26119 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
26121 set_constraints (ded_tmpl
, ci
);
26126 /* Deduce template arguments for the class template placeholder PTYPE for
26127 template TMPL based on the initializer INIT, and return the resulting
26131 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
26132 tsubst_flags_t complain
)
26134 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
26136 /* We should have handled this in the caller. */
26137 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26139 if (complain
& tf_error
)
26140 error ("non-class template %qT used without template arguments", tmpl
);
26141 return error_mark_node
;
26144 tree type
= TREE_TYPE (tmpl
);
26146 bool try_list_ctor
= false;
26148 vec
<tree
,va_gc
> *args
;
26149 if (init
== NULL_TREE
26150 || TREE_CODE (init
) == TREE_LIST
)
26151 args
= make_tree_vector_from_list (init
);
26152 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26154 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
26155 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
26157 /* As an exception, the first phase in 16.3.1.7 (considering the
26158 initializer list as a single argument) is omitted if the
26159 initializer list consists of a single expression of type cv U,
26160 where U is a specialization of C or a class derived from a
26161 specialization of C. */
26162 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
26163 tree etype
= TREE_TYPE (elt
);
26165 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
26166 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26167 int err
= unify (tparms
, targs
, type
, etype
,
26168 UNIFY_ALLOW_DERIVED
, /*explain*/false);
26170 try_list_ctor
= false;
26173 if (try_list_ctor
|| is_std_init_list (type
))
26174 args
= make_tree_vector_single (init
);
26176 args
= make_tree_vector_from_ctor (init
);
26179 args
= make_tree_vector_single (init
);
26181 tree dname
= dguide_name (tmpl
);
26182 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
26183 /*type*/false, /*complain*/false,
26185 bool elided
= false;
26186 if (cands
== error_mark_node
)
26189 /* Prune explicit deduction guides in copy-initialization context. */
26190 if (flags
& LOOKUP_ONLYCONVERTING
)
26192 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
26193 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
26198 /* Found a nonconverting guide, prune the candidates. */
26199 tree pruned
= NULL_TREE
;
26200 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26201 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
26202 pruned
= lookup_add (*iter
, pruned
);
26208 tree outer_args
= NULL_TREE
;
26209 if (DECL_CLASS_SCOPE_P (tmpl
)
26210 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
26212 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
26213 type
= TREE_TYPE (most_general_template (tmpl
));
26216 bool saw_ctor
= false;
26217 // FIXME cache artificial deduction guides
26218 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
26220 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
26221 if (guide
== error_mark_node
)
26222 return error_mark_node
;
26223 if ((flags
& LOOKUP_ONLYCONVERTING
)
26224 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
26227 cands
= lookup_add (guide
, cands
);
26232 tree call
= error_mark_node
;
26234 /* If this is list-initialization and the class has a list constructor, first
26235 try deducing from the list as a single argument, as [over.match.list]. */
26236 tree list_cands
= NULL_TREE
;
26237 if (try_list_ctor
&& cands
)
26238 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26241 if (is_list_ctor (dg
))
26242 list_cands
= lookup_add (dg
, list_cands
);
26246 ++cp_unevaluated_operand
;
26247 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
26248 --cp_unevaluated_operand
;
26250 if (call
== error_mark_node
)
26252 /* That didn't work, now try treating the list as a sequence of
26254 release_tree_vector (args
);
26255 args
= make_tree_vector_from_ctor (init
);
26259 /* Maybe generate an implicit deduction guide. */
26260 if (call
== error_mark_node
&& args
->length () < 2)
26262 tree gtype
= NULL_TREE
;
26264 if (args
->length () == 1)
26265 /* Generate a copy guide. */
26266 gtype
= build_reference_type (type
);
26267 else if (!saw_ctor
)
26268 /* Generate a default guide. */
26273 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
26274 if (guide
== error_mark_node
)
26275 return error_mark_node
;
26276 cands
= lookup_add (guide
, cands
);
26280 if (elided
&& !cands
)
26282 error ("cannot deduce template arguments for copy-initialization"
26283 " of %qT, as it has no non-explicit deduction guides or "
26284 "user-declared constructors", type
);
26285 return error_mark_node
;
26287 else if (!cands
&& call
== error_mark_node
)
26289 error ("cannot deduce template arguments of %qT, as it has no viable "
26290 "deduction guides", type
);
26291 return error_mark_node
;
26294 if (call
== error_mark_node
)
26296 ++cp_unevaluated_operand
;
26297 call
= build_new_function_call (cands
, &args
, tf_decltype
);
26298 --cp_unevaluated_operand
;
26301 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
26303 error ("class template argument deduction failed:");
26305 ++cp_unevaluated_operand
;
26306 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
26307 --cp_unevaluated_operand
;
26310 inform (input_location
, "explicit deduction guides not considered "
26311 "for copy-initialization");
26314 release_tree_vector (args
);
26316 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
26319 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26320 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26321 The CONTEXT determines the context in which auto deduction is performed
26322 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26323 OUTER_TARGS are used during template argument deduction
26324 (context == adc_unify) to properly substitute the result, and is ignored
26327 For partial-concept-ids, extra args may be appended to the list of deduced
26328 template arguments prior to determining constraint satisfaction. */
26331 do_auto_deduction (tree type
, tree init
, tree auto_node
,
26332 tsubst_flags_t complain
, auto_deduction_context context
,
26333 tree outer_targs
, int flags
)
26337 if (init
== error_mark_node
)
26338 return error_mark_node
;
26340 if (init
&& type_dependent_expression_p (init
)
26341 && context
!= adc_unify
)
26342 /* Defining a subset of type-dependent expressions that we can deduce
26343 from ahead of time isn't worth the trouble. */
26346 /* Similarly, we can't deduce from another undeduced decl. */
26347 if (init
&& undeduced_auto_decl (init
))
26350 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
26351 /* C++17 class template argument deduction. */
26352 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
26354 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
26355 /* Nothing we can do with this, even in deduction context. */
26358 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26359 with either a new invented type template parameter U or, if the
26360 initializer is a braced-init-list (8.5.4), with
26361 std::initializer_list<U>. */
26362 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26364 if (!DIRECT_LIST_INIT_P (init
))
26365 type
= listify_autos (type
, auto_node
);
26366 else if (CONSTRUCTOR_NELTS (init
) == 1)
26367 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26370 if (complain
& tf_warning_or_error
)
26372 if (permerror (input_location
, "direct-list-initialization of "
26373 "%<auto%> requires exactly one element"))
26374 inform (input_location
,
26375 "for deduction to %<std::initializer_list%>, use copy-"
26376 "list-initialization (i.e. add %<=%> before the %<{%>)");
26378 type
= listify_autos (type
, auto_node
);
26382 if (type
== error_mark_node
)
26383 return error_mark_node
;
26385 init
= resolve_nondeduced_context (init
, complain
);
26387 if (context
== adc_decomp_type
26388 && auto_node
== type
26389 && init
!= error_mark_node
26390 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26391 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26392 and initializer has array type, deduce cv-qualified array type. */
26393 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26395 else if (AUTO_IS_DECLTYPE (auto_node
))
26397 bool id
= (DECL_P (init
)
26398 || ((TREE_CODE (init
) == COMPONENT_REF
26399 || TREE_CODE (init
) == SCOPE_REF
)
26400 && !REF_PARENTHESIZED_P (init
)));
26401 targs
= make_tree_vec (1);
26402 TREE_VEC_ELT (targs
, 0)
26403 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26404 if (type
!= auto_node
)
26406 if (complain
& tf_error
)
26407 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26408 return error_mark_node
;
26413 tree parms
= build_tree_list (NULL_TREE
, type
);
26417 tparms
= extract_autos (type
);
26420 tparms
= make_tree_vec (1);
26421 TREE_VEC_ELT (tparms
, 0)
26422 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26425 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26426 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26427 DEDUCE_CALL
, LOOKUP_NORMAL
,
26428 NULL
, /*explain_p=*/false);
26431 if (processing_template_decl
)
26432 /* Try again at instantiation time. */
26434 if (type
&& type
!= error_mark_node
26435 && (complain
& tf_error
))
26436 /* If type is error_mark_node a diagnostic must have been
26437 emitted by now. Also, having a mention to '<type error>'
26438 in the diagnostic is not really useful to the user. */
26440 if (cfun
&& auto_node
== current_function_auto_return_pattern
26441 && LAMBDA_FUNCTION_P (current_function_decl
))
26442 error ("unable to deduce lambda return type from %qE", init
);
26444 error ("unable to deduce %qT from %qE", type
, init
);
26445 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26446 DEDUCE_CALL
, LOOKUP_NORMAL
,
26447 NULL
, /*explain_p=*/true);
26449 return error_mark_node
;
26453 /* Check any placeholder constraints against the deduced type. */
26454 if (flag_concepts
&& !processing_template_decl
)
26455 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26457 /* Use the deduced type to check the associated constraints. If we
26458 have a partial-concept-id, rebuild the argument list so that
26459 we check using the extra arguments. */
26460 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26461 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26462 if (TREE_VEC_LENGTH (cargs
) > 1)
26464 cargs
= copy_node (cargs
);
26465 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26469 if (!constraints_satisfied_p (constr
, cargs
))
26471 if (complain
& tf_warning_or_error
)
26475 case adc_unspecified
:
26477 error("placeholder constraints not satisfied");
26479 case adc_variable_type
:
26480 case adc_decomp_type
:
26481 error ("deduced initializer does not satisfy "
26482 "placeholder constraints");
26484 case adc_return_type
:
26485 error ("deduced return type does not satisfy "
26486 "placeholder constraints");
26488 case adc_requirement
:
26489 error ("deduced expression type does not satisfy "
26490 "placeholder constraints");
26493 diagnose_constraints (input_location
, constr
, targs
);
26495 return error_mark_node
;
26499 if (processing_template_decl
&& context
!= adc_unify
)
26500 outer_targs
= current_template_args ();
26501 targs
= add_to_template_args (outer_targs
, targs
);
26502 return tsubst (type
, targs
, complain
, NULL_TREE
);
26505 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26509 splice_late_return_type (tree type
, tree late_return_type
)
26511 if (is_auto (type
))
26513 if (late_return_type
)
26514 return late_return_type
;
26516 tree idx
= get_template_parm_index (type
);
26517 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26518 /* In an abbreviated function template we didn't know we were dealing
26519 with a function template when we saw the auto return type, so update
26520 it to have the correct level. */
26521 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26526 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26527 'decltype(auto)' or a deduced class template. */
26530 is_auto (const_tree type
)
26532 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26533 && (TYPE_IDENTIFIER (type
) == auto_identifier
26534 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26535 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26541 /* for_each_template_parm callback for type_uses_auto. */
26544 is_auto_r (tree tp
, void */
*data*/
)
26546 return is_auto (tp
);
26549 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26550 a use of `auto'. Returns NULL_TREE otherwise. */
26553 type_uses_auto (tree type
)
26555 if (type
== NULL_TREE
)
26557 else if (flag_concepts
)
26559 /* The Concepts TS allows multiple autos in one type-specifier; just
26560 return the first one we find, do_auto_deduction will collect all of
26562 if (uses_template_parms (type
))
26563 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26564 /*visited*/NULL
, /*nondeduced*/true);
26569 return find_type_usage (type
, is_auto
);
26572 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
26573 concepts are enabled, auto is acceptable in template arguments, but
26574 only when TEMPL identifies a template class. Return TRUE if any
26575 such errors were reported. */
26578 check_auto_in_tmpl_args (tree tmpl
, tree args
)
26580 /* If there were previous errors, nevermind. */
26581 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
26584 /* If TMPL is an identifier, we're parsing and we can't tell yet
26585 whether TMPL is supposed to be a type, a function or a variable.
26586 We'll only be able to tell during template substitution, so we
26587 expect to be called again then. If concepts are enabled and we
26588 know we have a type, we're ok. */
26590 && (identifier_p (tmpl
)
26592 && (DECL_TYPE_TEMPLATE_P (tmpl
)
26593 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
26596 /* Quickly search for any occurrences of auto; usually there won't
26597 be any, and then we'll avoid allocating the vector. */
26598 if (!type_uses_auto (args
))
26601 bool errors
= false;
26603 tree vec
= extract_autos (args
);
26604 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
26606 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
26607 error_at (DECL_SOURCE_LOCATION (xauto
),
26608 "invalid use of %qT in template argument", xauto
);
26615 /* For a given template T, return the vector of typedefs referenced
26616 in T for which access check is needed at T instantiation time.
26617 T is either a FUNCTION_DECL or a RECORD_TYPE.
26618 Those typedefs were added to T by the function
26619 append_type_to_template_for_access_check. */
26621 vec
<qualified_typedef_usage_t
, va_gc
> *
26622 get_types_needing_access_check (tree t
)
26625 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26627 if (!t
|| t
== error_mark_node
)
26630 if (!(ti
= get_template_info (t
)))
26633 if (CLASS_TYPE_P (t
)
26634 || TREE_CODE (t
) == FUNCTION_DECL
)
26636 if (!TI_TEMPLATE (ti
))
26639 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26645 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26646 tied to T. That list of typedefs will be access checked at
26647 T instantiation time.
26648 T is either a FUNCTION_DECL or a RECORD_TYPE.
26649 TYPE_DECL is a TYPE_DECL node representing a typedef.
26650 SCOPE is the scope through which TYPE_DECL is accessed.
26651 LOCATION is the location of the usage point of TYPE_DECL.
26653 This function is a subroutine of
26654 append_type_to_template_for_access_check. */
26657 append_type_to_template_for_access_check_1 (tree t
,
26660 location_t location
)
26662 qualified_typedef_usage_t typedef_usage
;
26665 if (!t
|| t
== error_mark_node
)
26668 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26669 || CLASS_TYPE_P (t
))
26671 && TREE_CODE (type_decl
) == TYPE_DECL
26674 if (!(ti
= get_template_info (t
)))
26677 gcc_assert (TI_TEMPLATE (ti
));
26679 typedef_usage
.typedef_decl
= type_decl
;
26680 typedef_usage
.context
= scope
;
26681 typedef_usage
.locus
= location
;
26683 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26686 /* Append TYPE_DECL to the template TEMPL.
26687 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26688 At TEMPL instanciation time, TYPE_DECL will be checked to see
26689 if it can be accessed through SCOPE.
26690 LOCATION is the location of the usage point of TYPE_DECL.
26692 e.g. consider the following code snippet:
26699 template<class U> struct S
26701 C::myint mi; // <-- usage point of the typedef C::myint
26706 At S<char> instantiation time, we need to check the access of C::myint
26707 In other words, we need to check the access of the myint typedef through
26708 the C scope. For that purpose, this function will add the myint typedef
26709 and the scope C through which its being accessed to a list of typedefs
26710 tied to the template S. That list will be walked at template instantiation
26711 time and access check performed on each typedefs it contains.
26712 Note that this particular code snippet should yield an error because
26713 myint is private to C. */
26716 append_type_to_template_for_access_check (tree templ
,
26719 location_t location
)
26721 qualified_typedef_usage_t
*iter
;
26724 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26726 /* Make sure we don't append the type to the template twice. */
26727 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26728 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26731 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26735 /* Convert the generic type parameters in PARM that match the types given in the
26736 range [START_IDX, END_IDX) from the current_template_parms into generic type
26740 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26742 tree current
= current_template_parms
;
26743 int depth
= TMPL_PARMS_DEPTH (current
);
26744 current
= INNERMOST_TEMPLATE_PARMS (current
);
26745 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26747 for (int i
= 0; i
< start_idx
; ++i
)
26748 TREE_VEC_ELT (replacement
, i
)
26749 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26751 for (int i
= start_idx
; i
< end_idx
; ++i
)
26753 /* Create a distinct parameter pack type from the current parm and add it
26754 to the replacement args to tsubst below into the generic function
26757 tree o
= TREE_TYPE (TREE_VALUE
26758 (TREE_VEC_ELT (current
, i
)));
26759 tree t
= copy_type (o
);
26760 TEMPLATE_TYPE_PARM_INDEX (t
)
26761 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26763 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26764 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26765 TYPE_MAIN_VARIANT (t
) = t
;
26766 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26767 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26768 TREE_VEC_ELT (replacement
, i
) = t
;
26769 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26772 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26773 TREE_VEC_ELT (replacement
, i
)
26774 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26776 /* If there are more levels then build up the replacement with the outer
26779 replacement
= add_to_template_args (template_parms_to_args
26780 (TREE_CHAIN (current_template_parms
)),
26783 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26786 /* Entries in the decl_constraint hash table. */
26787 struct GTY((for_user
)) constr_entry
26793 /* Hashing function and equality for constraint entries. */
26794 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26796 static hashval_t
hash (constr_entry
*e
)
26798 return (hashval_t
)DECL_UID (e
->decl
);
26801 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26803 return e1
->decl
== e2
->decl
;
26807 /* A mapping from declarations to constraint information. Note that
26808 both templates and their underlying declarations are mapped to the
26809 same constraint information.
26811 FIXME: This is defined in pt.c because garbage collection
26812 code is not being generated for constraint.cc. */
26814 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26816 /* Returns the template constraints of declaration T. If T is not
26817 constrained, return NULL_TREE. Note that T must be non-null. */
26820 get_constraints (tree t
)
26822 if (!flag_concepts
)
26825 gcc_assert (DECL_P (t
));
26826 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26827 t
= DECL_TEMPLATE_RESULT (t
);
26828 constr_entry elt
= { t
, NULL_TREE
};
26829 constr_entry
* found
= decl_constraints
->find (&elt
);
26836 /* Associate the given constraint information CI with the declaration
26837 T. If T is a template, then the constraints are associated with
26838 its underlying declaration. Don't build associations if CI is
26842 set_constraints (tree t
, tree ci
)
26846 gcc_assert (t
&& flag_concepts
);
26847 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26848 t
= DECL_TEMPLATE_RESULT (t
);
26849 gcc_assert (!get_constraints (t
));
26850 constr_entry elt
= {t
, ci
};
26851 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26852 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26857 /* Remove the associated constraints of the declaration T. */
26860 remove_constraints (tree t
)
26862 gcc_assert (DECL_P (t
));
26863 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26864 t
= DECL_TEMPLATE_RESULT (t
);
26866 constr_entry elt
= {t
, NULL_TREE
};
26867 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26869 decl_constraints
->clear_slot (slot
);
26872 /* Memoized satisfaction results for declarations. This
26873 maps the pair (constraint_info, arguments) to the result computed
26874 by constraints_satisfied_p. */
26876 struct GTY((for_user
)) constraint_sat_entry
26883 /* Hashing function and equality for constraint entries. */
26885 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26887 static hashval_t
hash (constraint_sat_entry
*e
)
26889 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26890 return iterative_hash_template_arg (e
->args
, val
);
26893 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26895 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26899 /* Memoized satisfaction results for concept checks. */
26901 struct GTY((for_user
)) concept_spec_entry
26908 /* Hashing function and equality for constraint entries. */
26910 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26912 static hashval_t
hash (concept_spec_entry
*e
)
26914 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26917 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26919 ++comparing_specializations
;
26920 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26921 --comparing_specializations
;
26926 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26927 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26929 /* Search for a memoized satisfaction result. Returns one of the
26930 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26933 lookup_constraint_satisfaction (tree ci
, tree args
)
26935 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26936 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26938 return found
->result
;
26943 /* Memoize the result of a satisfication test. Returns the saved result. */
26946 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26948 constraint_sat_entry elt
= {ci
, args
, result
};
26949 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26950 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26956 /* Search for a memoized satisfaction result for a concept. */
26959 lookup_concept_satisfaction (tree tmpl
, tree args
)
26961 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26962 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26964 return found
->result
;
26969 /* Memoize the result of a concept check. Returns the saved result. */
26972 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26974 concept_spec_entry elt
= {tmpl
, args
, result
};
26975 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26976 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26982 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26984 /* Returns a prior concept specialization. This returns the substituted
26985 and normalized constraints defined by the concept. */
26988 get_concept_expansion (tree tmpl
, tree args
)
26990 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26991 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26993 return found
->result
;
26998 /* Save a concept expansion for later. */
27001 save_concept_expansion (tree tmpl
, tree args
, tree def
)
27003 concept_spec_entry elt
= {tmpl
, args
, def
};
27004 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
27005 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27012 hash_subsumption_args (tree t1
, tree t2
)
27014 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
27015 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
27017 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
27018 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
27019 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
27020 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
27024 /* Compare the constraints of two subsumption entries. The LEFT1 and
27025 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27026 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27029 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
27031 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
27032 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
27033 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
27034 CHECK_CONSTR_ARGS (right1
)))
27035 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
27036 CHECK_CONSTR_ARGS (right2
));
27040 /* Key/value pair for learning and memoizing subsumption results. This
27041 associates a pair of check constraints (including arguments) with
27042 a boolean value indicating the result. */
27044 struct GTY((for_user
)) subsumption_entry
27051 /* Hashing function and equality for constraint entries. */
27053 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
27055 static hashval_t
hash (subsumption_entry
*e
)
27057 return hash_subsumption_args (e
->t1
, e
->t2
);
27060 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
27062 ++comparing_specializations
;
27063 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
27064 --comparing_specializations
;
27069 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
27071 /* Search for a previously cached subsumption result. */
27074 lookup_subsumption_result (tree t1
, tree t2
)
27076 subsumption_entry elt
= { t1
, t2
, false };
27077 subsumption_entry
* found
= subsumption_table
->find (&elt
);
27079 return &found
->result
;
27084 /* Save a subsumption result. */
27087 save_subsumption_result (tree t1
, tree t2
, bool result
)
27089 subsumption_entry elt
= {t1
, t2
, result
};
27090 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
27091 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
27097 /* Set up the hash table for constraint association. */
27100 init_constraint_processing (void)
27102 if (!flag_concepts
)
27105 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
27106 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
27107 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27108 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27109 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
27112 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27116 declare_integer_pack (void)
27118 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
27119 build_function_type_list (integer_type_node
,
27122 NULL_TREE
, ECF_CONST
);
27123 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
27124 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
27127 /* Set up the hash tables for template instantiations. */
27130 init_template_processing (void)
27132 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27133 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27135 if (cxx_dialect
>= cxx11
)
27136 declare_integer_pack ();
27139 /* Print stats about the template hash tables for -fstats. */
27142 print_template_statistics (void)
27144 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
27145 "%f collisions\n", (long) decl_specializations
->size (),
27146 (long) decl_specializations
->elements (),
27147 decl_specializations
->collisions ());
27148 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
27149 "%f collisions\n", (long) type_specializations
->size (),
27150 (long) type_specializations
->elements (),
27151 type_specializations
->collisions ());
27156 namespace selftest
{
27158 /* Verify that build_non_dependent_expr () works, for various expressions,
27159 and that location wrappers don't affect the results. */
27162 test_build_non_dependent_expr ()
27164 location_t loc
= BUILTINS_LOCATION
;
27166 /* Verify constants, without and with location wrappers. */
27167 tree int_cst
= build_int_cst (integer_type_node
, 42);
27168 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
27170 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
27171 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
27172 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
27174 tree string_lit
= build_string (4, "foo");
27175 TREE_TYPE (string_lit
) = char_array_type_node
;
27176 string_lit
= fix_string_type (string_lit
);
27177 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
27179 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
27180 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
27181 ASSERT_EQ (wrapped_string_lit
,
27182 build_non_dependent_expr (wrapped_string_lit
));
27185 /* Verify that type_dependent_expression_p () works correctly, even
27186 in the presence of location wrapper nodes. */
27189 test_type_dependent_expression_p ()
27191 location_t loc
= BUILTINS_LOCATION
;
27193 tree name
= get_identifier ("foo");
27195 /* If no templates are involved, nothing is type-dependent. */
27196 gcc_assert (!processing_template_decl
);
27197 ASSERT_FALSE (type_dependent_expression_p (name
));
27199 ++processing_template_decl
;
27201 /* Within a template, an unresolved name is always type-dependent. */
27202 ASSERT_TRUE (type_dependent_expression_p (name
));
27204 /* Ensure it copes with NULL_TREE and errors. */
27205 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
27206 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
27208 /* A USING_DECL in a template should be type-dependent, even if wrapped
27209 with a location wrapper (PR c++/83799). */
27210 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
27211 TREE_TYPE (using_decl
) = integer_type_node
;
27212 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
27213 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
27214 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
27215 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
27217 --processing_template_decl
;
27220 /* Run all of the selftests within this file. */
27225 test_build_non_dependent_expr ();
27226 test_type_dependent_expression_p ();
27229 } // namespace selftest
27231 #endif /* #if CHECKING_P */
27233 #include "gt-cp-pt.h"