1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
81 local_specialization_stack::local_specialization_stack (lss_policy policy
)
82 : saved (local_specializations
)
84 if (policy
== lss_blank
|| !saved
)
85 local_specializations
= new hash_map
<tree
, tree
>;
87 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations
;
93 local_specializations
= saved
;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth
;
99 struct GTY((for_user
)) spec_entry
106 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
108 static hashval_t
hash (spec_entry
*);
109 static bool equal (spec_entry
*, spec_entry
*);
112 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
114 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result
{
133 tbr_ambiguous_baseclass
,
137 static void push_access_scope (tree
);
138 static void pop_access_scope (tree
);
139 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
140 unification_kind_t
, int,
142 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
143 unification_kind_t
, int, bool, bool);
144 static int unify (tree
, tree
, tree
, tree
, int, bool);
145 static void add_pending_template (tree
);
146 static tree
reopen_tinst_level (struct tinst_level
*);
147 static tree
tsubst_initializer_list (tree
, tree
);
148 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
149 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
151 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
153 static void tsubst_enum (tree
, tree
, tree
);
154 static tree
add_to_template_args (tree
, tree
);
155 static tree
add_outermost_template_args (tree
, tree
);
156 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
157 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
159 static int type_unification_real (tree
, tree
, tree
, const tree
*,
160 unsigned int, int, unification_kind_t
, int,
161 vec
<deferred_access_check
, va_gc
> **,
163 static void note_template_header (int);
164 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
165 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
166 static tree
convert_template_argument (tree
, tree
, tree
,
167 tsubst_flags_t
, int, tree
);
168 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
169 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
170 static tree
expand_template_argument_pack (tree
);
171 static tree
build_template_parm_index (int, int, int, tree
, tree
);
172 static bool inline_needs_template_parms (tree
, bool);
173 static void push_inline_template_parms_recursive (tree
, int);
174 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
175 static int mark_template_parm (tree
, void *);
176 static int template_parm_this_level_p (tree
, void *);
177 static tree
tsubst_friend_function (tree
, tree
);
178 static tree
tsubst_friend_class (tree
, tree
);
179 static int can_complete_type_without_circularity (tree
);
180 static tree
get_bindings (tree
, tree
, tree
, bool);
181 static int template_decl_level (tree
);
182 static int check_cv_quals_for_unify (int, tree
, tree
);
183 static void template_parm_level_and_index (tree
, int*, int*);
184 static int unify_pack_expansion (tree
, tree
, tree
,
185 tree
, unification_kind_t
, bool, bool);
186 static tree
copy_template_args (tree
);
187 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
189 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
190 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
191 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
192 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
193 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
194 static bool check_specialization_scope (void);
195 static tree
process_partial_specialization (tree
);
196 static void set_current_access_from_decl (tree
);
197 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
199 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
200 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
202 static bool template_template_parm_bindings_ok_p (tree
, tree
);
203 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
204 static tree
for_each_template_parm_r (tree
*, int *, void *);
205 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
206 static void copy_default_args_to_explicit_spec (tree
);
207 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
208 static bool dependent_template_arg_p (tree
);
209 static bool any_template_arguments_need_structural_equality_p (tree
);
210 static bool dependent_type_p_r (tree
);
211 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
212 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
213 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
214 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
216 static tree
listify (tree
);
217 static tree
listify_autos (tree
, tree
);
218 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
219 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
220 static bool complex_alias_template_p (const_tree tmpl
);
221 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
222 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
223 static tree
make_argument_pack (tree
);
224 static void register_parameter_specializations (tree
, tree
);
225 static tree
enclosing_instantiation_of (tree tctx
);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
233 push_access_scope (tree t
)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
236 || TREE_CODE (t
) == TYPE_DECL
);
238 if (DECL_FRIEND_CONTEXT (t
))
239 push_nested_class (DECL_FRIEND_CONTEXT (t
));
240 else if (DECL_CLASS_SCOPE_P (t
))
241 push_nested_class (DECL_CONTEXT (t
));
243 push_to_top_level ();
245 if (TREE_CODE (t
) == FUNCTION_DECL
)
247 saved_access_scope
= tree_cons
248 (NULL_TREE
, current_function_decl
, saved_access_scope
);
249 current_function_decl
= t
;
253 /* Restore the scope set up by push_access_scope. T is the node we
257 pop_access_scope (tree t
)
259 if (TREE_CODE (t
) == FUNCTION_DECL
)
261 current_function_decl
= TREE_VALUE (saved_access_scope
);
262 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
265 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
277 finish_member_template_decl (tree decl
)
279 if (decl
== error_mark_node
)
280 return error_mark_node
;
282 gcc_assert (DECL_P (decl
));
284 if (TREE_CODE (decl
) == TYPE_DECL
)
288 type
= TREE_TYPE (decl
);
289 if (type
== error_mark_node
)
290 return error_mark_node
;
291 if (MAYBE_CLASS_TYPE_P (type
)
292 && CLASSTYPE_TEMPLATE_INFO (type
)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
295 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
296 check_member_template (tmpl
);
301 else if (TREE_CODE (decl
) == FIELD_DECL
)
302 error ("data member %qD cannot be a member template", decl
);
303 else if (DECL_TEMPLATE_INFO (decl
))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
307 check_member_template (DECL_TI_TEMPLATE (decl
));
308 return DECL_TI_TEMPLATE (decl
);
314 error ("invalid member template declaration %qD", decl
);
316 return error_mark_node
;
319 /* Create a template info node. */
322 build_template_info (tree template_decl
, tree template_args
)
324 tree result
= make_node (TEMPLATE_INFO
);
325 TI_TEMPLATE (result
) = template_decl
;
326 TI_ARGS (result
) = template_args
;
330 /* Return the template info node corresponding to T, whatever T is. */
333 get_template_info (const_tree t
)
335 tree tinfo
= NULL_TREE
;
337 if (!t
|| t
== error_mark_node
)
340 if (TREE_CODE (t
) == NAMESPACE_DECL
341 || TREE_CODE (t
) == PARM_DECL
)
344 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
345 tinfo
= DECL_TEMPLATE_INFO (t
);
347 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
350 if (OVERLOAD_TYPE_P (t
))
351 tinfo
= TYPE_TEMPLATE_INFO (t
);
352 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
353 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
358 /* Returns the template nesting level of the indicated class TYPE.
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
377 template_class_depth (tree type
)
381 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
383 tree tinfo
= get_template_info (type
);
385 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
390 type
= CP_DECL_CONTEXT (type
);
391 else if (LAMBDA_TYPE_P (type
))
392 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
394 type
= CP_TYPE_CONTEXT (type
);
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
404 inline_needs_template_parms (tree decl
, bool nsdmi
)
406 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
410 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
419 push_inline_template_parms_recursive (tree parmlist
, int levels
)
421 tree parms
= TREE_VALUE (parmlist
);
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
427 ++processing_template_decl
;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl
),
430 parms
, current_template_parms
);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
435 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
437 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
439 if (error_operand_p (parm
))
442 gcc_assert (DECL_P (parm
));
444 switch (TREE_CODE (parm
))
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
467 maybe_begin_member_template_processing (tree decl
)
471 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
475 tree ctx
= DECL_CONTEXT (decl
);
476 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx
)
479 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
482 if (inline_needs_template_parms (decl
, nsdmi
))
484 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
485 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
490 parms
= TREE_CHAIN (parms
);
493 push_inline_template_parms_recursive (parms
, levels
);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels
.safe_push (levels
);
501 /* Undo the effects of maybe_begin_member_template_processing. */
504 maybe_end_member_template_processing (void)
509 if (inline_parm_levels
.length () == 0)
512 last
= inline_parm_levels
.pop ();
513 for (i
= 0; i
< last
; ++i
)
515 --processing_template_decl
;
516 current_template_parms
= TREE_CHAIN (current_template_parms
);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
525 add_to_template_args (tree args
, tree extra_args
)
532 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
535 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
536 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
538 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
539 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
541 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
555 add_outermost_template_args (tree args
, tree extra_args
)
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
565 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
571 new_args
= add_to_template_args (args
, extra_args
);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
579 /* Return the N levels of innermost template arguments from the ARGS. */
582 get_innermost_template_args (tree args
, int n
)
590 /* If N is 1, just return the innermost set of template arguments. */
592 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
594 /* If we're not removing anything, just return the arguments we were
596 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
597 gcc_assert (extra_levels
>= 0);
598 if (extra_levels
== 0)
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args
= make_tree_vec (n
);
603 for (i
= 1; i
<= n
; ++i
)
604 SET_TMPL_ARGS_LEVEL (new_args
, i
,
605 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
614 strip_innermost_template_args (tree args
, int extra_levels
)
617 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
622 /* If N is 1, just return the outermost set of template arguments. */
624 return TMPL_ARGS_LEVEL (args
, 1);
626 /* If we're not removing anything, just return the arguments we were
628 gcc_assert (extra_levels
>= 0);
629 if (extra_levels
== 0)
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args
= make_tree_vec (n
);
634 for (i
= 1; i
<= n
; ++i
)
635 SET_TMPL_ARGS_LEVEL (new_args
, i
,
636 TMPL_ARGS_LEVEL (args
, i
));
641 /* We've got a template header coming up; push to a new level for storing
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms
, NULL
);
662 ++processing_template_decl
;
663 ++processing_template_parmlist
;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl
),
670 current_template_parms
);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
678 check_specialization_scope (void)
680 tree scope
= current_scope ();
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
691 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
693 error ("explicit specialization in non-namespace scope %qD", scope
);
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms
)
707 error ("enclosing class templates are not explicitly specialized");
714 /* We've just seen template <>. */
717 begin_specialization (void)
719 begin_scope (sk_template_spec
, NULL
);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
728 end_specialization (void)
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
738 reset_specialization (void)
740 processing_specialization
= 0;
741 template_header_count
= 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
748 note_template_header (int specialization
)
750 processing_specialization
= specialization
;
751 template_header_count
++;
754 /* We're beginning an explicit instantiation. */
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation
);
760 processing_explicit_instantiation
= true;
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation
);
768 processing_explicit_instantiation
= false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
777 check_specialization_namespace (tree tmpl
)
779 tree tpl_ns
= decl_namespace_context (tmpl
);
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl
)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl
);
795 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
796 /* Same or enclosing namespace. */
800 permerror (input_location
,
801 "specialization of %qD in different namespace", tmpl
);
802 inform (DECL_SOURCE_LOCATION (tmpl
),
803 " from definition of %q#D", tmpl
);
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
812 check_explicit_instantiation_namespace (tree spec
)
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns
= decl_namespace_context (spec
);
819 if (!is_nested_namespace (current_namespace
, ns
))
820 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec
, current_namespace
, ns
);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
829 maybe_new_partial_specialization (tree type
)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
837 // template<typename T>
840 // Here, S<T*> is an implicit instantiation of S whose type
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
863 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
864 tree args
= CLASSTYPE_TI_ARGS (type
);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms
)
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr
= current_template_constraints ();
879 if (type
== TREE_TYPE (tmpl
))
881 tree main_constr
= get_constraints (tmpl
);
882 if (equivalent_constraints (type_constr
, main_constr
))
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
891 tree spec_tmpl
= TREE_VALUE (specs
);
892 tree spec_args
= TREE_PURPOSE (specs
);
893 tree spec_constr
= get_constraints (spec_tmpl
);
894 if (comp_template_args (args
, spec_args
)
895 && equivalent_constraints (type_constr
, spec_constr
))
897 specs
= TREE_CHAIN (specs
);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t
= make_class_type (TREE_CODE (type
));
903 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
904 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
911 // Build the corresponding type decl.
912 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
913 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
914 DECL_SOURCE_LOCATION (d
) = input_location
;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
926 maybe_process_partial_specialization (tree type
)
930 if (type
== error_mark_node
)
931 return error_mark_node
;
933 /* A lambda that appears in specialization context is not itself a
935 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
938 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
940 error ("name of class shadows template template parameter %qD",
942 return error_mark_node
;
945 context
= TYPE_CONTEXT (type
);
947 if (TYPE_ALIAS_P (type
))
949 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
951 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo
));
955 error ("explicit specialization of non-template %qT", type
);
956 return error_mark_node
;
958 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t
= maybe_new_partial_specialization (type
))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
974 && !at_namespace_scope_p ())
975 return error_mark_node
;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
978 if (processing_template_decl
)
980 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
981 if (decl
== error_mark_node
)
982 return error_mark_node
;
983 return TREE_TYPE (decl
);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
987 error ("specialization of %qT after instantiation", type
);
988 else if (errorcount
&& !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node
;
996 else if (CLASS_TYPE_P (type
)
997 && !CLASSTYPE_USE_TEMPLATE (type
)
998 && CLASSTYPE_TEMPLATE_INFO (type
)
999 && context
&& CLASS_TYPE_P (context
)
1000 && CLASSTYPE_TEMPLATE_INFO (context
))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1019 && !COMPLETE_TYPE_P (type
))
1022 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl
))
1027 permerror (input_location
,
1028 "specializing %q#T in different namespace", type
);
1029 permerror (DECL_SOURCE_LOCATION (tmpl
),
1030 " from definition of %q#D", tmpl
);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1039 t
; t
= TREE_CHAIN (t
))
1041 tree inst
= TREE_VALUE (t
);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1052 elt
.tmpl
= most_general_template (tmpl
);
1053 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1056 type_specializations
->remove_elt (&elt
);
1059 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1062 = type_specializations
->find_slot (&elt
, INSERT
);
1063 entry
= ggc_alloc
<spec_entry
> ();
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1079 CLASSTYPE_TI_ARGS (type
)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1083 else if (processing_specialization
)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1088 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1089 "of %qD not allowed by ISO C++", type
);
1092 error ("explicit specialization of non-template %qT", type
);
1093 return error_mark_node
;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1105 optimize_specialization_lookup_p (tree tmpl
)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1108 && DECL_CLASS_SCOPE_P (tmpl
)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1119 && !DECL_CONV_FN_P (tmpl
)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1137 verify_unstripped_args (tree args
)
1139 ++processing_template_decl
;
1140 if (!any_dependent_template_arguments_p (args
))
1142 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1143 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1145 tree arg
= TREE_VEC_ELT (inner
, i
);
1146 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1148 else if (TYPE_P (arg
))
1149 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1150 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1154 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1157 --processing_template_decl
;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1175 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1177 if (tmpl
== NULL_TREE
)
1180 if (args
== error_mark_node
)
1183 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl
) == FIELD_DECL
);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args
)
1189 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1191 : template_class_depth (DECL_CONTEXT (tmpl
))));
1194 verify_unstripped_args (args
);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl
))
1201 if (optimize_specialization_lookup_p (tmpl
))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1206 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1207 tree class_specialization
1208 = retrieve_specialization (class_template
, args
, 0);
1209 if (!class_specialization
)
1212 /* Find the instance of TMPL. */
1213 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1214 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1217 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn
) == class_specialization
)
1229 hash_table
<spec_hasher
> *specializations
;
1233 elt
.spec
= NULL_TREE
;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1236 specializations
= type_specializations
;
1238 specializations
= decl_specializations
;
1241 hash
= spec_hasher::hash (&elt
);
1242 found
= specializations
->find_with_hash (&elt
, hash
);
1250 /* Like retrieve_specialization, but for local declarations. */
1253 retrieve_local_specialization (tree tmpl
)
1255 if (local_specializations
== NULL
)
1258 tree
*slot
= local_specializations
->get (tmpl
);
1259 return slot
? *slot
: NULL_TREE
;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl
, tree tmpl
)
1269 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1273 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1279 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1281 for (t
= TREE_TYPE (decl
);
1283 t
= CLASSTYPE_USE_TEMPLATE (t
)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1285 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1296 is_specialization_of_friend (tree decl
, tree friend_decl
)
1298 bool need_template
= true;
1301 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1302 || TREE_CODE (decl
) == TYPE_DECL
);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1307 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl
)
1309 && !DECL_USE_TEMPLATE (friend_decl
))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1313 need_template
= false;
1315 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl
))
1317 need_template
= false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1323 if (is_specialization_of (decl
, friend_decl
))
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1348 && DECL_CLASS_SCOPE_P (decl
)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context
= DECL_CONTEXT (decl
);
1369 tree args
= NULL_TREE
;
1370 int current_depth
= 0;
1372 while (current_depth
< template_depth
)
1374 if (CLASSTYPE_TEMPLATE_INFO (context
))
1376 if (current_depth
== 0)
1377 args
= TYPE_TI_ARGS (context
);
1379 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1382 context
= TYPE_CONTEXT (context
);
1385 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1390 tree friend_args_type
;
1391 tree decl_args_type
;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1395 is_template
= DECL_TEMPLATE_INFO (decl
)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1397 if (need_template
^ is_template
)
1399 else if (is_template
)
1401 /* If both are templates, check template parameter list. */
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1410 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1413 decl_type
= TREE_TYPE (decl
);
1415 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1416 tf_none
, NULL_TREE
);
1417 if (friend_type
== error_mark_node
)
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1427 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1429 friend_args_type
= TREE_CHAIN (friend_args_type
);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1431 decl_args_type
= TREE_CHAIN (decl_args_type
);
1433 return compparms (decl_args_type
, friend_args_type
);
1437 /* DECL is a TYPE_DECL */
1439 tree decl_type
= TREE_TYPE (decl
);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1447 if (need_template
^ is_template
)
1449 else if (is_template
)
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1455 != DECL_NAME (friend_decl
))
1458 /* Now check template parameter list. */
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1467 return (DECL_NAME (decl
)
1468 == DECL_NAME (friend_decl
));
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. ATTRLIST is the list of
1477 attributes that the specialization is declared with or NULL when
1478 it isn't. Returns SPEC, or an equivalent prior declaration, if
1481 We also store instantiations of field packs in the hash table, even
1482 though they are not themselves templates, to make lookup easier. */
1485 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1489 spec_entry
**slot
= NULL
;
1492 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1493 || (TREE_CODE (tmpl
) == FIELD_DECL
1494 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1496 if (TREE_CODE (spec
) == FUNCTION_DECL
1497 && uses_template_parms (DECL_TI_ARGS (spec
)))
1498 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1499 register it; we want the corresponding TEMPLATE_DECL instead.
1500 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1501 the more obvious `uses_template_parms (spec)' to avoid problems
1502 with default function arguments. In particular, given
1503 something like this:
1505 template <class T> void f(T t1, T t = T())
1507 the default argument expression is not substituted for in an
1508 instantiation unless and until it is actually needed. */
1511 if (optimize_specialization_lookup_p (tmpl
))
1512 /* We don't put these specializations in the hash table, but we might
1513 want to give an error about a mismatch. */
1514 fn
= retrieve_specialization (tmpl
, args
, 0);
1522 hash
= spec_hasher::hash (&elt
);
1525 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1527 fn
= ((spec_entry
*) *slot
)->spec
;
1532 /* We can sometimes try to re-register a specialization that we've
1533 already got. In particular, regenerate_decl_from_template calls
1534 duplicate_decls which will update the specialization list. But,
1535 we'll still get called again here anyhow. It's more convenient
1536 to simply allow this than to try to prevent it. */
1539 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1541 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1543 if (DECL_ODR_USED (fn
)
1544 || DECL_EXPLICIT_INSTANTIATION (fn
))
1546 error ("specialization of %qD after instantiation",
1548 return error_mark_node
;
1553 /* This situation should occur only if the first
1554 specialization is an implicit instantiation, the
1555 second is an explicit specialization, and the
1556 implicit instantiation has not yet been used. That
1557 situation can occur if we have implicitly
1558 instantiated a member function and then specialized
1561 We can also wind up here if a friend declaration that
1562 looked like an instantiation turns out to be a
1565 template <class T> void foo(T);
1566 class S { friend void foo<>(int) };
1567 template <> void foo(int);
1569 We transform the existing DECL in place so that any
1570 pointers to it become pointers to the updated
1573 If there was a definition for the template, but not
1574 for the specialization, we want this to look as if
1575 there were no definition, and vice versa. */
1576 DECL_INITIAL (fn
) = NULL_TREE
;
1577 duplicate_decls (spec
, fn
, is_friend
);
1578 /* The call to duplicate_decls will have applied
1581 An explicit specialization of a function template
1582 is inline only if it is explicitly declared to be,
1583 and independently of whether its function template
1586 to the primary function; now copy the inline bits to
1587 the various clones. */
1588 FOR_EACH_CLONE (clone
, fn
)
1590 DECL_DECLARED_INLINE_P (clone
)
1591 = DECL_DECLARED_INLINE_P (fn
);
1592 DECL_SOURCE_LOCATION (clone
)
1593 = DECL_SOURCE_LOCATION (fn
);
1594 DECL_DELETED_FN (clone
)
1595 = DECL_DELETED_FN (fn
);
1597 check_specialization_namespace (tmpl
);
1602 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1604 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1605 if (dd
== error_mark_node
)
1606 /* We've already complained in duplicate_decls. */
1607 return error_mark_node
;
1609 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1610 /* Dup decl failed, but this is a new definition. Set the
1611 line number so any errors match this new
1613 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1619 return duplicate_decls (spec
, fn
, is_friend
);
1621 /* A specialization must be declared in the same namespace as the
1622 template it is specializing. */
1623 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1624 && !check_specialization_namespace (tmpl
))
1625 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1627 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1629 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1630 gcc_assert (tmpl
&& args
&& spec
);
1633 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1634 && PRIMARY_TEMPLATE_P (tmpl
)
1635 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1636 || variable_template_p (tmpl
))
1637 /* If TMPL is a forward declaration of a template function, keep a list
1638 of all specializations in case we need to reassign them to a friend
1639 template later in tsubst_friend_function.
1641 Also keep a list of all variable template instantiations so that
1642 process_partial_specialization can check whether a later partial
1643 specialization would have used it. */
1644 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1645 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1651 /* Returns true iff two spec_entry nodes are equivalent. */
1653 int comparing_specializations
;
1656 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1660 ++comparing_specializations
;
1661 equal
= (e1
->tmpl
== e2
->tmpl
1662 && comp_template_args (e1
->args
, e2
->args
));
1663 if (equal
&& flag_concepts
1664 /* tmpl could be a FIELD_DECL for a capture pack. */
1665 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1666 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1667 && uses_template_parms (e1
->args
))
1669 /* Partial specializations of a variable template can be distinguished by
1671 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1672 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1673 equal
= equivalent_constraints (c1
, c2
);
1675 --comparing_specializations
;
1680 /* Returns a hash for a template TMPL and template arguments ARGS. */
1683 hash_tmpl_and_args (tree tmpl
, tree args
)
1685 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1686 return iterative_hash_template_arg (args
, val
);
1689 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1693 spec_hasher::hash (spec_entry
*e
)
1695 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1698 /* Recursively calculate a hash value for a template argument ARG, for use
1699 in the hash tables of template specializations. */
1702 iterative_hash_template_arg (tree arg
, hashval_t val
)
1704 unsigned HOST_WIDE_INT i
;
1705 enum tree_code code
;
1708 if (arg
== NULL_TREE
)
1709 return iterative_hash_object (arg
, val
);
1714 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1717 code
= TREE_CODE (arg
);
1718 tclass
= TREE_CODE_CLASS (code
);
1720 val
= iterative_hash_object (code
, val
);
1727 case IDENTIFIER_NODE
:
1728 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1732 int i
, len
= TREE_VEC_LENGTH (arg
);
1733 for (i
= 0; i
< len
; ++i
)
1734 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1738 case TYPE_PACK_EXPANSION
:
1739 case EXPR_PACK_EXPANSION
:
1740 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1741 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1743 case TYPE_ARGUMENT_PACK
:
1744 case NONTYPE_ARGUMENT_PACK
:
1745 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1748 for (; arg
; arg
= TREE_CHAIN (arg
))
1749 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1753 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1754 val
= iterative_hash_template_arg (*iter
, val
);
1760 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1763 val
= iterative_hash_template_arg (field
, val
);
1764 val
= iterative_hash_template_arg (value
, val
);
1770 if (!DECL_ARTIFICIAL (arg
))
1772 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1773 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1775 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1778 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1781 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1782 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1784 case TEMPLATE_PARM_INDEX
:
1785 val
= iterative_hash_template_arg
1786 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1787 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1788 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1791 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1792 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1793 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1796 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1798 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1802 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1803 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1804 val
= iterative_hash_object (code
, val
);
1805 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1808 /* A lambda can't appear in a template arg, but don't crash on
1810 gcc_assert (seen_error ());
1814 case IMPLICIT_CONV_EXPR
:
1815 case STATIC_CAST_EXPR
:
1816 case REINTERPRET_CAST_EXPR
:
1817 case CONST_CAST_EXPR
:
1818 case DYNAMIC_CAST_EXPR
:
1820 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1821 /* Now hash operands as usual. */
1831 if (alias_template_specialization_p (arg
))
1833 // We want an alias specialization that survived strip_typedefs
1834 // to hash differently from its TYPE_CANONICAL, to avoid hash
1835 // collisions that compare as different in template_args_equal.
1836 // These could be dependent specializations that strip_typedefs
1837 // left alone, or untouched specializations because
1838 // coerce_template_parms returns the unconverted template
1839 // arguments if it sees incomplete argument packs.
1840 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1841 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1843 if (TYPE_CANONICAL (arg
))
1844 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1846 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1847 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1848 /* Otherwise just compare the types during lookup. */
1851 case tcc_declaration
:
1853 return iterative_hash_expr (arg
, val
);
1856 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1858 unsigned n
= cp_tree_operand_length (arg
);
1859 for (i
= 0; i
< n
; ++i
)
1860 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1868 /* Unregister the specialization SPEC as a specialization of TMPL.
1869 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1870 if the SPEC was listed as a specialization of TMPL.
1872 Note that SPEC has been ggc_freed, so we can't look inside it. */
1875 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1880 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1881 elt
.args
= TI_ARGS (tinfo
);
1882 elt
.spec
= NULL_TREE
;
1884 entry
= decl_specializations
->find (&elt
);
1887 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1888 gcc_assert (new_spec
!= NULL_TREE
);
1889 entry
->spec
= new_spec
;
1896 /* Like register_specialization, but for local declarations. We are
1897 registering SPEC, an instantiation of TMPL. */
1900 register_local_specialization (tree spec
, tree tmpl
)
1902 gcc_assert (tmpl
!= spec
);
1903 local_specializations
->put (tmpl
, spec
);
1906 /* TYPE is a class type. Returns true if TYPE is an explicitly
1907 specialized class. */
1910 explicit_class_specialization_p (tree type
)
1912 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1914 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1917 /* Print the list of functions at FNS, going through all the overloads
1918 for each element of the list. Alternatively, FNS can not be a
1919 TREE_LIST, in which case it will be printed together with all the
1922 MORE and *STR should respectively be FALSE and NULL when the function
1923 is called from the outside. They are used internally on recursive
1924 calls. print_candidates manages the two parameters and leaves NULL
1925 in *STR when it ends. */
1928 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1930 if (TREE_CODE (fns
) == TREE_LIST
)
1931 for (; fns
; fns
= TREE_CHAIN (fns
))
1932 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1934 for (lkp_iterator
iter (fns
); iter
;)
1939 const char *pfx
= *str
;
1943 pfx
= _("candidates are:");
1945 pfx
= _("candidate is:");
1946 *str
= get_spaces (pfx
);
1948 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1952 /* Print the list of candidate FNS in an error message. FNS can also
1953 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1956 print_candidates (tree fns
)
1959 print_candidates_1 (fns
, &str
);
1963 /* Get a (possibly) constrained template declaration for the
1964 purpose of ordering candidates. */
1966 get_template_for_ordering (tree list
)
1968 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1969 tree f
= TREE_VALUE (list
);
1970 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1971 return TI_TEMPLATE (ti
);
1975 /* Among candidates having the same signature, return the
1976 most constrained or NULL_TREE if there is no best candidate.
1977 If the signatures of candidates vary (e.g., template
1978 specialization vs. member function), then there can be no
1981 Note that we don't compare constraints on the functions
1982 themselves, but rather those of their templates. */
1984 most_constrained_function (tree candidates
)
1986 // Try to find the best candidate in a first pass.
1987 tree champ
= candidates
;
1988 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1990 int winner
= more_constrained (get_template_for_ordering (champ
),
1991 get_template_for_ordering (c
));
1993 champ
= c
; // The candidate is more constrained
1994 else if (winner
== 0)
1995 return NULL_TREE
; // Neither is more constrained
1998 // Verify that the champ is better than previous candidates.
1999 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2000 if (!more_constrained (get_template_for_ordering (champ
),
2001 get_template_for_ordering (c
)))
2009 /* Returns the template (one of the functions given by TEMPLATE_ID)
2010 which can be specialized to match the indicated DECL with the
2011 explicit template args given in TEMPLATE_ID. The DECL may be
2012 NULL_TREE if none is available. In that case, the functions in
2013 TEMPLATE_ID are non-members.
2015 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2016 specialization of a member template.
2018 The TEMPLATE_COUNT is the number of references to qualifying
2019 template classes that appeared in the name of the function. See
2020 check_explicit_specialization for a more accurate description.
2022 TSK indicates what kind of template declaration (if any) is being
2023 declared. TSK_TEMPLATE indicates that the declaration given by
2024 DECL, though a FUNCTION_DECL, has template parameters, and is
2025 therefore a template function.
2027 The template args (those explicitly specified and those deduced)
2028 are output in a newly created vector *TARGS_OUT.
2030 If it is impossible to determine the result, an error message is
2031 issued. The error_mark_node is returned to indicate failure. */
2034 determine_specialization (tree template_id
,
2037 int need_member_template
,
2043 tree explicit_targs
;
2044 tree candidates
= NULL_TREE
;
2046 /* A TREE_LIST of templates of which DECL may be a specialization.
2047 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2048 corresponding TREE_PURPOSE is the set of template arguments that,
2049 when used to instantiate the template, would produce a function
2050 with the signature of DECL. */
2051 tree templates
= NULL_TREE
;
2053 cp_binding_level
*b
;
2055 *targs_out
= NULL_TREE
;
2057 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2058 return error_mark_node
;
2060 /* We shouldn't be specializing a member template of an
2061 unspecialized class template; we already gave an error in
2062 check_specialization_scope, now avoid crashing. */
2064 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2065 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2067 gcc_assert (errorcount
);
2068 return error_mark_node
;
2071 fns
= TREE_OPERAND (template_id
, 0);
2072 explicit_targs
= TREE_OPERAND (template_id
, 1);
2074 if (fns
== error_mark_node
)
2075 return error_mark_node
;
2077 /* Check for baselinks. */
2078 if (BASELINK_P (fns
))
2079 fns
= BASELINK_FUNCTIONS (fns
);
2081 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2083 error ("%qD is not a function template", fns
);
2084 return error_mark_node
;
2086 else if (VAR_P (decl
) && !variable_template_p (fns
))
2088 error ("%qD is not a variable template", fns
);
2089 return error_mark_node
;
2092 /* Count the number of template headers specified for this
2095 for (b
= current_binding_level
;
2096 b
->kind
== sk_template_parms
;
2100 tree orig_fns
= fns
;
2102 if (variable_template_p (fns
))
2104 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2105 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2106 tf_warning_or_error
,
2107 /*req_all*/true, /*use_defarg*/true);
2108 if (targs
!= error_mark_node
)
2109 templates
= tree_cons (targs
, fns
, templates
);
2111 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2115 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2117 tree decl_arg_types
;
2121 /* In case of explicit specialization, we need to check if
2122 the number of template headers appearing in the specialization
2123 is correct. This is usually done in check_explicit_specialization,
2124 but the check done there cannot be exhaustive when specializing
2125 member functions. Consider the following code:
2127 template <> void A<int>::f(int);
2128 template <> template <> void A<int>::f(int);
2130 Assuming that A<int> is not itself an explicit specialization
2131 already, the first line specializes "f" which is a non-template
2132 member function, whilst the second line specializes "f" which
2133 is a template member function. So both lines are syntactically
2134 correct, and check_explicit_specialization does not reject
2137 Here, we can do better, as we are matching the specialization
2138 against the declarations. We count the number of template
2139 headers, and we check if they match TEMPLATE_COUNT + 1
2140 (TEMPLATE_COUNT is the number of qualifying template classes,
2141 plus there must be another header for the member template
2144 Notice that if header_count is zero, this is not a
2145 specialization but rather a template instantiation, so there
2146 is no check we can perform here. */
2147 if (header_count
&& header_count
!= template_count
+ 1)
2150 /* Check that the number of template arguments at the
2151 innermost level for DECL is the same as for FN. */
2152 if (current_binding_level
->kind
== sk_template_parms
2153 && !current_binding_level
->explicit_spec_p
2154 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2155 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2156 (current_template_parms
))))
2159 /* DECL might be a specialization of FN. */
2160 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2161 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2163 /* For a non-static member function, we need to make sure
2164 that the const qualification is the same. Since
2165 get_bindings does not try to merge the "this" parameter,
2166 we must do the comparison explicitly. */
2167 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2169 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2170 TREE_VALUE (decl_arg_types
)))
2173 /* And the ref-qualification. */
2174 if (type_memfn_rqual (TREE_TYPE (decl
))
2175 != type_memfn_rqual (TREE_TYPE (fn
)))
2179 /* Skip the "this" parameter and, for constructors of
2180 classes with virtual bases, the VTT parameter. A
2181 full specialization of a constructor will have a VTT
2182 parameter, but a template never will. */
2184 = skip_artificial_parms_for (decl
, decl_arg_types
);
2186 = skip_artificial_parms_for (fn
, fn_arg_types
);
2188 /* Function templates cannot be specializations; there are
2189 no partial specializations of functions. Therefore, if
2190 the type of DECL does not match FN, there is no
2193 Note that it should never be the case that we have both
2194 candidates added here, and for regular member functions
2196 if (tsk
== tsk_template
)
2198 if (compparms (fn_arg_types
, decl_arg_types
))
2199 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2203 /* See whether this function might be a specialization of this
2204 template. Suppress access control because we might be trying
2205 to make this specialization a friend, and we have already done
2206 access control for the declaration of the specialization. */
2207 push_deferring_access_checks (dk_no_check
);
2208 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2209 pop_deferring_access_checks ();
2212 /* We cannot deduce template arguments that when used to
2213 specialize TMPL will produce DECL. */
2216 if (uses_template_parms (targs
))
2217 /* We deduced something involving 'auto', which isn't a valid
2218 template argument. */
2221 /* Remove, from the set of candidates, all those functions
2222 whose constraints are not satisfied. */
2223 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2226 // Then, try to form the new function type.
2227 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2228 if (insttype
== error_mark_node
)
2231 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2232 if (!compparms (fn_arg_types
, decl_arg_types
))
2235 /* Save this template, and the arguments deduced. */
2236 templates
= tree_cons (targs
, fn
, templates
);
2238 else if (need_member_template
)
2239 /* FN is an ordinary member function, and we need a
2240 specialization of a member template. */
2242 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2243 /* We can get IDENTIFIER_NODEs here in certain erroneous
2246 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2247 /* This is just an ordinary non-member function. Nothing can
2248 be a specialization of that. */
2250 else if (DECL_ARTIFICIAL (fn
))
2251 /* Cannot specialize functions that are created implicitly. */
2255 tree decl_arg_types
;
2257 /* This is an ordinary member function. However, since
2258 we're here, we can assume its enclosing class is a
2259 template class. For example,
2261 template <typename T> struct S { void f(); };
2262 template <> void S<int>::f() {}
2264 Here, S<int>::f is a non-template, but S<int> is a
2265 template class. If FN has the same type as DECL, we
2266 might be in business. */
2268 if (!DECL_TEMPLATE_INFO (fn
))
2269 /* Its enclosing class is an explicit specialization
2270 of a template class. This is not a candidate. */
2273 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2274 TREE_TYPE (TREE_TYPE (fn
))))
2275 /* The return types differ. */
2278 /* Adjust the type of DECL in case FN is a static member. */
2279 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2280 if (DECL_STATIC_FUNCTION_P (fn
)
2281 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2282 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2284 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2288 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2289 && (type_memfn_rqual (TREE_TYPE (decl
))
2290 != type_memfn_rqual (TREE_TYPE (fn
))))
2293 // If the deduced arguments do not satisfy the constraints,
2294 // this is not a candidate.
2295 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2298 // Add the candidate.
2299 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2303 if (templates
&& TREE_CHAIN (templates
))
2309 It is possible for a specialization with a given function
2310 signature to be instantiated from more than one function
2311 template. In such cases, explicit specification of the
2312 template arguments must be used to uniquely identify the
2313 function template specialization being specialized.
2315 Note that here, there's no suggestion that we're supposed to
2316 determine which of the candidate templates is most
2317 specialized. However, we, also have:
2321 Partial ordering of overloaded function template
2322 declarations is used in the following contexts to select
2323 the function template to which a function template
2324 specialization refers:
2326 -- when an explicit specialization refers to a function
2329 So, we do use the partial ordering rules, at least for now.
2330 This extension can only serve to make invalid programs valid,
2331 so it's safe. And, there is strong anecdotal evidence that
2332 the committee intended the partial ordering rules to apply;
2333 the EDG front end has that behavior, and John Spicer claims
2334 that the committee simply forgot to delete the wording in
2335 [temp.expl.spec]. */
2336 tree tmpl
= most_specialized_instantiation (templates
);
2337 if (tmpl
!= error_mark_node
)
2340 TREE_CHAIN (templates
) = NULL_TREE
;
2344 // Concepts allows multiple declarations of member functions
2345 // with the same signature. Like above, we need to rely on
2346 // on the partial ordering of those candidates to determine which
2348 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2350 if (tree cand
= most_constrained_function (candidates
))
2353 TREE_CHAIN (cand
) = NULL_TREE
;
2357 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2359 error ("template-id %qD for %q+D does not match any template "
2360 "declaration", template_id
, decl
);
2361 if (header_count
&& header_count
!= template_count
+ 1)
2362 inform (input_location
, "saw %d %<template<>%>, need %d for "
2363 "specializing a member function template",
2364 header_count
, template_count
+ 1);
2366 print_candidates (orig_fns
);
2367 return error_mark_node
;
2369 else if ((templates
&& TREE_CHAIN (templates
))
2370 || (candidates
&& TREE_CHAIN (candidates
))
2371 || (templates
&& candidates
))
2373 error ("ambiguous template specialization %qD for %q+D",
2375 candidates
= chainon (candidates
, templates
);
2376 print_candidates (candidates
);
2377 return error_mark_node
;
2380 /* We have one, and exactly one, match. */
2383 tree fn
= TREE_VALUE (candidates
);
2384 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2386 // Propagate the candidate's constraints to the declaration.
2387 set_constraints (decl
, get_constraints (fn
));
2389 /* DECL is a re-declaration or partial instantiation of a template
2391 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2393 /* It was a specialization of an ordinary member function in a
2395 return DECL_TI_TEMPLATE (fn
);
2398 /* It was a specialization of a template. */
2399 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2400 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2402 *targs_out
= copy_node (targs
);
2403 SET_TMPL_ARGS_LEVEL (*targs_out
,
2404 TMPL_ARGS_DEPTH (*targs_out
),
2405 TREE_PURPOSE (templates
));
2408 *targs_out
= TREE_PURPOSE (templates
);
2409 return TREE_VALUE (templates
);
2412 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2413 but with the default argument values filled in from those in the
2417 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2420 tree new_spec_types
;
2425 if (spec_types
== void_list_node
)
2426 return void_list_node
;
2428 /* Substitute into the rest of the list. */
2430 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2431 TREE_CHAIN (tmpl_types
));
2433 /* Add the default argument for this parameter. */
2434 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2435 TREE_VALUE (spec_types
),
2439 /* DECL is an explicit specialization. Replicate default arguments
2440 from the template it specializes. (That way, code like:
2442 template <class T> void f(T = 3);
2443 template <> void f(double);
2446 works, as required.) An alternative approach would be to look up
2447 the correct default arguments at the call-site, but this approach
2448 is consistent with how implicit instantiations are handled. */
2451 copy_default_args_to_explicit_spec (tree decl
)
2456 tree new_spec_types
;
2460 tree object_type
= NULL_TREE
;
2461 tree in_charge
= NULL_TREE
;
2462 tree vtt
= NULL_TREE
;
2464 /* See if there's anything we need to do. */
2465 tmpl
= DECL_TI_TEMPLATE (decl
);
2466 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2467 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2468 if (TREE_PURPOSE (t
))
2473 old_type
= TREE_TYPE (decl
);
2474 spec_types
= TYPE_ARG_TYPES (old_type
);
2476 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2478 /* Remove the this pointer, but remember the object's type for
2480 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2481 spec_types
= TREE_CHAIN (spec_types
);
2482 tmpl_types
= TREE_CHAIN (tmpl_types
);
2484 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2486 /* DECL may contain more parameters than TMPL due to the extra
2487 in-charge parameter in constructors and destructors. */
2488 in_charge
= spec_types
;
2489 spec_types
= TREE_CHAIN (spec_types
);
2491 if (DECL_HAS_VTT_PARM_P (decl
))
2494 spec_types
= TREE_CHAIN (spec_types
);
2498 /* Compute the merged default arguments. */
2500 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2502 /* Compute the new FUNCTION_TYPE. */
2506 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2511 /* Put the in-charge parameter back. */
2512 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2513 TREE_VALUE (in_charge
),
2516 new_type
= build_method_type_directly (object_type
,
2517 TREE_TYPE (old_type
),
2521 new_type
= build_function_type (TREE_TYPE (old_type
),
2523 new_type
= cp_build_type_attribute_variant (new_type
,
2524 TYPE_ATTRIBUTES (old_type
));
2525 new_type
= build_exception_variant (new_type
,
2526 TYPE_RAISES_EXCEPTIONS (old_type
));
2528 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2529 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2531 TREE_TYPE (decl
) = new_type
;
2534 /* Return the number of template headers we expect to see for a definition
2535 or specialization of CTYPE or one of its non-template members. */
2538 num_template_headers_for_class (tree ctype
)
2540 int num_templates
= 0;
2542 while (ctype
&& CLASS_TYPE_P (ctype
))
2544 /* You're supposed to have one `template <...>' for every
2545 template class, but you don't need one for a full
2546 specialization. For example:
2548 template <class T> struct S{};
2549 template <> struct S<int> { void f(); };
2550 void S<int>::f () {}
2552 is correct; there shouldn't be a `template <>' for the
2553 definition of `S<int>::f'. */
2554 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2555 /* If CTYPE does not have template information of any
2556 kind, then it is not a template, nor is it nested
2557 within a template. */
2559 if (explicit_class_specialization_p (ctype
))
2561 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2564 ctype
= TYPE_CONTEXT (ctype
);
2567 return num_templates
;
2570 /* Do a simple sanity check on the template headers that precede the
2571 variable declaration DECL. */
2574 check_template_variable (tree decl
)
2576 tree ctx
= CP_DECL_CONTEXT (decl
);
2577 int wanted
= num_template_headers_for_class (ctx
);
2578 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2579 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2581 if (cxx_dialect
< cxx14
)
2582 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2583 "variable templates only available with "
2584 "-std=c++14 or -std=gnu++14");
2586 // Namespace-scope variable templates should have a template header.
2589 if (template_header_count
> wanted
)
2591 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2592 "too many template headers for %qD "
2595 if (warned
&& CLASS_TYPE_P (ctx
)
2596 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2597 inform (DECL_SOURCE_LOCATION (decl
),
2598 "members of an explicitly specialized class are defined "
2599 "without a template header");
2603 /* An explicit specialization whose declarator-id or class-head-name is not
2604 qualified shall be declared in the nearest enclosing namespace of the
2605 template, or, if the namespace is inline (7.3.1), any namespace from its
2606 enclosing namespace set.
2608 If the name declared in the explicit instantiation is an unqualified name,
2609 the explicit instantiation shall appear in the namespace where its template
2610 is declared or, if that namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set. */
2614 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2616 tree tmpl
= most_general_template (t
);
2617 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2618 && !is_nested_namespace (current_namespace
,
2619 CP_DECL_CONTEXT (tmpl
), true))
2621 if (processing_specialization
)
2622 permerror (loc
, "explicit specialization of %qD outside its "
2623 "namespace must use a nested-name-specifier", tmpl
);
2624 else if (processing_explicit_instantiation
2625 && cxx_dialect
>= cxx11
)
2626 /* This was allowed in C++98, so only pedwarn. */
2627 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2628 "outside its namespace must use a nested-name-"
2633 /* Warn for a template specialization SPEC that is missing some of a set
2634 of function or type attributes that the template TEMPL is declared with.
2635 ATTRLIST is a list of additional attributes that SPEC should be taken
2636 to ultimately be declared with. */
2639 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2641 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2642 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2644 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2647 /* Avoid warning if either declaration or its type is deprecated. */
2648 if (TREE_DEPRECATED (tmpl
)
2649 || TREE_DEPRECATED (spec
))
2652 tree tmpl_type
= TREE_TYPE (tmpl
);
2653 tree spec_type
= TREE_TYPE (spec
);
2655 if (TREE_DEPRECATED (tmpl_type
)
2656 || TREE_DEPRECATED (spec_type
)
2657 || TREE_DEPRECATED (TREE_TYPE (tmpl_type
))
2658 || TREE_DEPRECATED (TREE_TYPE (spec_type
)))
2661 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpl_type
) };
2662 tree spec_attrs
[] = { DECL_ATTRIBUTES (spec
), TYPE_ATTRIBUTES (spec_type
) };
2665 spec_attrs
[0] = attrlist
;
2666 else if (!spec_attrs
[1])
2667 spec_attrs
[1] = attrlist
;
2669 /* Avoid warning if the primary has no attributes. */
2670 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2673 /* Avoid warning if either declaration contains an attribute on
2674 the white list below. */
2675 const char* const whitelist
[] = {
2679 for (unsigned i
= 0; i
!= 2; ++i
)
2680 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
2681 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2682 || lookup_attribute (whitelist
[j
], spec_attrs
[i
]))
2685 /* Avoid warning if the difference between the primary and
2686 the specialization is not in one of the attributes below. */
2687 const char* const blacklist
[] = {
2688 "alloc_align", "alloc_size", "assume_aligned", "format",
2689 "format_arg", "malloc", "nonnull"
2692 /* Put together a list of the black listed attributes that the primary
2693 template is declared with that the specialization is not, in case
2694 it's not apparent from the most recent declaration of the primary. */
2695 unsigned nattrs
= 0;
2698 for (unsigned i
= 0; i
!= sizeof blacklist
/ sizeof *blacklist
; ++i
)
2700 for (unsigned j
= 0; j
!= 2; ++j
)
2702 if (!lookup_attribute (blacklist
[i
], tmpl_attrs
[j
]))
2705 for (unsigned k
= 0; k
!= 1 + !!spec_attrs
[1]; ++k
)
2707 if (lookup_attribute (blacklist
[i
], spec_attrs
[k
]))
2711 pp_string (&str
, ", ");
2712 pp_begin_quote (&str
, pp_show_color (global_dc
->printer
));
2713 pp_string (&str
, blacklist
[i
]);
2714 pp_end_quote (&str
, pp_show_color (global_dc
->printer
));
2723 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2724 "explicit specialization %q#D may be missing attributes",
2726 inform (DECL_SOURCE_LOCATION (tmpl
),
2728 ? G_("missing primary template attributes %s")
2729 : G_("missing primary template attribute %s"),
2730 pp_formatted_text (&str
));
2733 /* Check to see if the function just declared, as indicated in
2734 DECLARATOR, and in DECL, is a specialization of a function
2735 template. We may also discover that the declaration is an explicit
2736 instantiation at this point.
2738 Returns DECL, or an equivalent declaration that should be used
2739 instead if all goes well. Issues an error message if something is
2740 amiss. Returns error_mark_node if the error is not easily
2743 FLAGS is a bitmask consisting of the following flags:
2745 2: The function has a definition.
2746 4: The function is a friend.
2748 The TEMPLATE_COUNT is the number of references to qualifying
2749 template classes that appeared in the name of the function. For
2752 template <class T> struct S { void f(); };
2755 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2756 classes are not counted in the TEMPLATE_COUNT, so that in
2758 template <class T> struct S {};
2759 template <> struct S<int> { void f(); }
2760 template <> void S<int>::f();
2762 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2763 invalid; there should be no template <>.)
2765 If the function is a specialization, it is marked as such via
2766 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2767 is set up correctly, and it is added to the list of specializations
2768 for that template. */
2771 check_explicit_specialization (tree declarator
,
2777 int have_def
= flags
& 2;
2778 int is_friend
= flags
& 4;
2779 bool is_concept
= flags
& 8;
2780 int specialization
= 0;
2781 int explicit_instantiation
= 0;
2782 int member_specialization
= 0;
2783 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2784 tree dname
= DECL_NAME (decl
);
2789 if (!processing_specialization
)
2792 tsk
= tsk_excessive_parms
;
2795 tsk
= current_tmpl_spec_kind (template_count
);
2800 if (processing_specialization
&& !VAR_P (decl
))
2803 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2805 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2808 /* This could be something like:
2810 template <class T> void f(T);
2811 class S { friend void f<>(int); } */
2815 /* This case handles bogus declarations like template <>
2816 template <class T> void f<int>(); */
2818 error ("template-id %qD in declaration of primary template",
2825 case tsk_invalid_member_spec
:
2826 /* The error has already been reported in
2827 check_specialization_scope. */
2828 return error_mark_node
;
2830 case tsk_invalid_expl_inst
:
2831 error ("template parameter list used in explicit instantiation");
2837 error ("definition provided for explicit instantiation");
2839 explicit_instantiation
= 1;
2842 case tsk_excessive_parms
:
2843 case tsk_insufficient_parms
:
2844 if (tsk
== tsk_excessive_parms
)
2845 error ("too many template parameter lists in declaration of %qD",
2847 else if (template_header_count
)
2848 error("too few template parameter lists in declaration of %qD", decl
);
2850 error("explicit specialization of %qD must be introduced by "
2851 "%<template <>%>", decl
);
2856 error ("explicit specialization declared %<concept%>");
2858 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2859 /* In cases like template<> constexpr bool v = true;
2860 We'll give an error in check_template_variable. */
2863 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2865 member_specialization
= 1;
2871 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2873 /* This case handles bogus declarations like template <>
2874 template <class T> void f<int>(); */
2876 if (!uses_template_parms (declarator
))
2877 error ("template-id %qD in declaration of primary template",
2879 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2881 /* Partial specialization of variable template. */
2882 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2886 else if (cxx_dialect
< cxx14
)
2887 error ("non-type partial specialization %qD "
2888 "is not allowed", declarator
);
2890 error ("non-class, non-variable partial specialization %qD "
2891 "is not allowed", declarator
);
2896 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2897 /* This is a specialization of a member template, without
2898 specialization the containing class. Something like:
2900 template <class T> struct S {
2901 template <class U> void f (U);
2903 template <> template <class U> void S<int>::f(U) {}
2905 That's a specialization -- but of the entire template. */
2913 if ((specialization
|| member_specialization
)
2914 /* This doesn't apply to variable templates. */
2915 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2916 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2918 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2919 for (; t
; t
= TREE_CHAIN (t
))
2920 if (TREE_PURPOSE (t
))
2922 permerror (input_location
,
2923 "default argument specified in explicit specialization");
2928 if (specialization
|| member_specialization
|| explicit_instantiation
)
2930 tree tmpl
= NULL_TREE
;
2931 tree targs
= NULL_TREE
;
2932 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2934 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2935 if (!was_template_id
)
2939 gcc_assert (identifier_p (declarator
));
2944 /* If there is no class context, the explicit instantiation
2945 must be at namespace scope. */
2946 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2948 /* Find the namespace binding, using the declaration
2950 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2952 if (fns
== error_mark_node
)
2953 /* If lookup fails, look for a friend declaration so we can
2954 give a better diagnostic. */
2955 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2956 /*type*/false, /*complain*/true,
2959 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2961 error ("%qD is not a template function", dname
);
2962 fns
= error_mark_node
;
2966 declarator
= lookup_template_function (fns
, NULL_TREE
);
2969 if (declarator
== error_mark_node
)
2970 return error_mark_node
;
2972 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2974 if (!explicit_instantiation
)
2975 /* A specialization in class scope. This is invalid,
2976 but the error will already have been flagged by
2977 check_specialization_scope. */
2978 return error_mark_node
;
2981 /* It's not valid to write an explicit instantiation in
2984 class C { template void f(); }
2986 This case is caught by the parser. However, on
2989 template class C { void f(); };
2991 (which is invalid) we can get here. The error will be
2998 else if (ctype
!= NULL_TREE
2999 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3001 // We'll match variable templates in start_decl.
3005 /* Find the list of functions in ctype that have the same
3006 name as the declared function. */
3007 tree name
= TREE_OPERAND (declarator
, 0);
3009 if (constructor_name_p (name
, ctype
))
3011 if (DECL_CONSTRUCTOR_P (decl
)
3012 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3013 : !CLASSTYPE_DESTRUCTOR (ctype
))
3015 /* From [temp.expl.spec]:
3017 If such an explicit specialization for the member
3018 of a class template names an implicitly-declared
3019 special member function (clause _special_), the
3020 program is ill-formed.
3022 Similar language is found in [temp.explicit]. */
3023 error ("specialization of implicitly-declared special member function");
3024 return error_mark_node
;
3027 name
= DECL_NAME (decl
);
3030 /* For a type-conversion operator, We might be looking for
3031 `operator int' which will be a specialization of
3032 `operator T'. Grab all the conversion operators, and
3033 then select from them. */
3034 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3035 ? conv_op_identifier
: name
);
3037 if (fns
== NULL_TREE
)
3039 error ("no member function %qD declared in %qT", name
, ctype
);
3040 return error_mark_node
;
3043 TREE_OPERAND (declarator
, 0) = fns
;
3046 /* Figure out what exactly is being specialized at this point.
3047 Note that for an explicit instantiation, even one for a
3048 member function, we cannot tell a priori whether the
3049 instantiation is for a member template, or just a member
3050 function of a template class. Even if a member template is
3051 being instantiated, the member template arguments may be
3052 elided if they can be deduced from the rest of the
3054 tmpl
= determine_specialization (declarator
, decl
,
3056 member_specialization
,
3060 if (!tmpl
|| tmpl
== error_mark_node
)
3061 /* We couldn't figure out what this declaration was
3063 return error_mark_node
;
3066 if (TREE_CODE (decl
) == FUNCTION_DECL
3067 && DECL_HIDDEN_FRIEND_P (tmpl
))
3069 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3070 "friend declaration %qD is not visible to "
3071 "explicit specialization", tmpl
))
3072 inform (DECL_SOURCE_LOCATION (tmpl
),
3073 "friend declaration here");
3075 else if (!ctype
&& !is_friend
3076 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3077 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3079 tree gen_tmpl
= most_general_template (tmpl
);
3081 if (explicit_instantiation
)
3083 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3084 is done by do_decl_instantiation later. */
3086 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3087 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3089 if (arg_depth
> parm_depth
)
3091 /* If TMPL is not the most general template (for
3092 example, if TMPL is a friend template that is
3093 injected into namespace scope), then there will
3094 be too many levels of TARGS. Remove some of them
3099 new_targs
= make_tree_vec (parm_depth
);
3100 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3101 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3102 = TREE_VEC_ELT (targs
, i
);
3106 return instantiate_template (tmpl
, targs
, tf_error
);
3109 /* If we thought that the DECL was a member function, but it
3110 turns out to be specializing a static member function,
3111 make DECL a static member function as well. */
3112 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3113 && DECL_STATIC_FUNCTION_P (tmpl
)
3114 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3115 revert_static_member_fn (decl
);
3117 /* If this is a specialization of a member template of a
3118 template class, we want to return the TEMPLATE_DECL, not
3119 the specialization of it. */
3120 if (tsk
== tsk_template
&& !was_template_id
)
3122 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3123 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3124 DECL_INITIAL (result
) = NULL_TREE
;
3128 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3129 DECL_SOURCE_LOCATION (result
)
3130 = DECL_SOURCE_LOCATION (decl
);
3131 /* We want to use the argument list specified in the
3132 definition, not in the original declaration. */
3133 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3134 for (parm
= DECL_ARGUMENTS (result
); parm
;
3135 parm
= DECL_CHAIN (parm
))
3136 DECL_CONTEXT (parm
) = result
;
3138 return register_specialization (tmpl
, gen_tmpl
, targs
,
3142 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3143 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3145 if (was_template_id
)
3146 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3148 /* Inherit default function arguments from the template
3149 DECL is specializing. */
3150 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3151 copy_default_args_to_explicit_spec (decl
);
3153 /* This specialization has the same protection as the
3154 template it specializes. */
3155 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3156 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3158 /* 7.1.1-1 [dcl.stc]
3160 A storage-class-specifier shall not be specified in an
3161 explicit specialization...
3163 The parser rejects these, so unless action is taken here,
3164 explicit function specializations will always appear with
3167 The action recommended by the C++ CWG in response to C++
3168 defect report 605 is to make the storage class and linkage
3169 of the explicit specialization match the templated function:
3171 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3173 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3175 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3176 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3178 /* A concept cannot be specialized. */
3179 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3181 error ("explicit specialization of function concept %qD",
3183 return error_mark_node
;
3186 /* This specialization has the same linkage and visibility as
3187 the function template it specializes. */
3188 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3189 if (! TREE_PUBLIC (decl
))
3191 DECL_INTERFACE_KNOWN (decl
) = 1;
3192 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3194 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3195 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3197 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3198 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3202 /* If DECL is a friend declaration, declared using an
3203 unqualified name, the namespace associated with DECL may
3204 have been set incorrectly. For example, in:
3206 template <typename T> void f(T);
3208 struct S { friend void f<int>(int); }
3211 we will have set the DECL_CONTEXT for the friend
3212 declaration to N, rather than to the global namespace. */
3213 if (DECL_NAMESPACE_SCOPE_P (decl
))
3214 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3216 if (is_friend
&& !have_def
)
3217 /* This is not really a declaration of a specialization.
3218 It's just the name of an instantiation. But, it's not
3219 a request for an instantiation, either. */
3220 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3221 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3222 /* A specialization is not necessarily COMDAT. */
3223 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3224 && DECL_DECLARED_INLINE_P (decl
));
3225 else if (VAR_P (decl
))
3226 DECL_COMDAT (decl
) = false;
3228 /* If this is a full specialization, register it so that we can find
3229 it again. Partial specializations will be registered in
3230 process_partial_specialization. */
3231 if (!processing_template_decl
)
3233 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3235 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3240 /* A 'structor should already have clones. */
3241 gcc_assert (decl
== error_mark_node
3242 || variable_template_p (tmpl
)
3243 || !(DECL_CONSTRUCTOR_P (decl
)
3244 || DECL_DESTRUCTOR_P (decl
))
3245 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3252 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3253 parameters. These are represented in the same format used for
3254 DECL_TEMPLATE_PARMS. */
3257 comp_template_parms (const_tree parms1
, const_tree parms2
)
3262 if (parms1
== parms2
)
3265 for (p1
= parms1
, p2
= parms2
;
3266 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3267 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3269 tree t1
= TREE_VALUE (p1
);
3270 tree t2
= TREE_VALUE (p2
);
3273 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3274 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3276 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3279 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3281 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3282 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3284 /* If either of the template parameters are invalid, assume
3285 they match for the sake of error recovery. */
3286 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3289 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3292 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3293 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3294 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3296 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3301 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3302 /* One set of parameters has more parameters lists than the
3309 /* Determine whether PARM is a parameter pack. */
3312 template_parameter_pack_p (const_tree parm
)
3314 /* Determine if we have a non-type template parameter pack. */
3315 if (TREE_CODE (parm
) == PARM_DECL
)
3316 return (DECL_TEMPLATE_PARM_P (parm
)
3317 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3318 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3319 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3321 /* If this is a list of template parameters, we could get a
3322 TYPE_DECL or a TEMPLATE_DECL. */
3323 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3324 parm
= TREE_TYPE (parm
);
3326 /* Otherwise it must be a type template parameter. */
3327 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3328 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3329 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3332 /* Determine if T is a function parameter pack. */
3335 function_parameter_pack_p (const_tree t
)
3337 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3338 return DECL_PACK_P (t
);
3342 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3343 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3346 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3348 if (! primary_func_tmpl_inst
3349 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3350 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3353 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3356 /* Return true iff the function parameter PARAM_DECL was expanded
3357 from the function parameter pack PACK. */
3360 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3362 if (DECL_ARTIFICIAL (param_decl
)
3363 || !function_parameter_pack_p (pack
))
3366 /* The parameter pack and its pack arguments have the same
3368 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3371 /* Determine whether ARGS describes a variadic template args list,
3372 i.e., one that is terminated by a template argument pack. */
3375 template_args_variadic_p (tree args
)
3380 if (args
== NULL_TREE
)
3383 args
= INNERMOST_TEMPLATE_ARGS (args
);
3384 nargs
= TREE_VEC_LENGTH (args
);
3389 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3391 return ARGUMENT_PACK_P (last_parm
);
3394 /* Generate a new name for the parameter pack name NAME (an
3395 IDENTIFIER_NODE) that incorporates its */
3398 make_ith_pack_parameter_name (tree name
, int i
)
3400 /* Munge the name to include the parameter index. */
3401 #define NUMBUF_LEN 128
3402 char numbuf
[NUMBUF_LEN
];
3406 if (name
== NULL_TREE
)
3408 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3409 newname_len
= IDENTIFIER_LENGTH (name
)
3410 + strlen (numbuf
) + 2;
3411 newname
= (char*)alloca (newname_len
);
3412 snprintf (newname
, newname_len
,
3413 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3414 return get_identifier (newname
);
3417 /* Return true if T is a primary function, class or alias template
3418 specialization, not including the template pattern. */
3421 primary_template_specialization_p (const_tree t
)
3426 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3427 return (DECL_LANG_SPECIFIC (t
)
3428 && DECL_USE_TEMPLATE (t
)
3429 && DECL_TEMPLATE_INFO (t
)
3430 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3431 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3432 return (CLASSTYPE_TEMPLATE_INFO (t
)
3433 && CLASSTYPE_USE_TEMPLATE (t
)
3434 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3435 else if (alias_template_specialization_p (t
))
3440 /* Return true if PARM is a template template parameter. */
3443 template_template_parameter_p (const_tree parm
)
3445 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3448 /* Return true iff PARM is a DECL representing a type template
3452 template_type_parameter_p (const_tree parm
)
3455 && (TREE_CODE (parm
) == TYPE_DECL
3456 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3457 && DECL_TEMPLATE_PARM_P (parm
));
3460 /* Return the template parameters of T if T is a
3461 primary template instantiation, NULL otherwise. */
3464 get_primary_template_innermost_parameters (const_tree t
)
3466 tree parms
= NULL
, template_info
= NULL
;
3468 if ((template_info
= get_template_info (t
))
3469 && primary_template_specialization_p (t
))
3470 parms
= INNERMOST_TEMPLATE_PARMS
3471 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3476 /* Return the template parameters of the LEVELth level from the full list
3477 of template parameters PARMS. */
3480 get_template_parms_at_level (tree parms
, int level
)
3484 || TREE_CODE (parms
) != TREE_LIST
3485 || level
> TMPL_PARMS_DEPTH (parms
))
3488 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3489 if (TMPL_PARMS_DEPTH (p
) == level
)
3495 /* Returns the template arguments of T if T is a template instantiation,
3499 get_template_innermost_arguments (const_tree t
)
3501 tree args
= NULL
, template_info
= NULL
;
3503 if ((template_info
= get_template_info (t
))
3504 && TI_ARGS (template_info
))
3505 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3510 /* Return the argument pack elements of T if T is a template argument pack,
3514 get_template_argument_pack_elems (const_tree t
)
3516 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3517 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3520 return ARGUMENT_PACK_ARGS (t
);
3523 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3524 ARGUMENT_PACK_SELECT represents. */
3527 argument_pack_select_arg (tree t
)
3529 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3530 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3532 /* If the selected argument is an expansion E, that most likely means we were
3533 called from gen_elem_of_pack_expansion_instantiation during the
3534 substituting of an argument pack (of which the Ith element is a pack
3535 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3536 In this case, the Ith element resulting from this substituting is going to
3537 be a pack expansion, which pattern is the pattern of E. Let's return the
3538 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3539 resulting pack expansion from it. */
3540 if (PACK_EXPANSION_P (arg
))
3542 /* Make sure we aren't throwing away arg info. */
3543 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3544 arg
= PACK_EXPANSION_PATTERN (arg
);
3551 /* True iff FN is a function representing a built-in variadic parameter
3555 builtin_pack_fn_p (tree fn
)
3558 || TREE_CODE (fn
) != FUNCTION_DECL
3559 || !DECL_IS_BUILTIN (fn
))
3562 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3568 /* True iff CALL is a call to a function representing a built-in variadic
3572 builtin_pack_call_p (tree call
)
3574 if (TREE_CODE (call
) != CALL_EXPR
)
3576 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3579 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3582 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3585 tree ohi
= CALL_EXPR_ARG (call
, 0);
3586 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3587 false/*fn*/, true/*int_cst*/);
3589 if (value_dependent_expression_p (hi
))
3593 call
= copy_node (call
);
3594 CALL_EXPR_ARG (call
, 0) = hi
;
3596 tree ex
= make_pack_expansion (call
, complain
);
3597 tree vec
= make_tree_vec (1);
3598 TREE_VEC_ELT (vec
, 0) = ex
;
3603 hi
= cxx_constant_value (hi
);
3604 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3606 /* Calculate the largest value of len that won't make the size of the vec
3607 overflow an int. The compiler will exceed resource limits long before
3608 this, but it seems a decent place to diagnose. */
3609 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3611 if (len
< 0 || len
> max
)
3613 if ((complain
& tf_error
)
3614 && hi
!= error_mark_node
)
3615 error ("argument to __integer_pack must be between 0 and %d", max
);
3616 return error_mark_node
;
3619 tree vec
= make_tree_vec (len
);
3621 for (int i
= 0; i
< len
; ++i
)
3622 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3628 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3632 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3635 if (!builtin_pack_call_p (call
))
3638 tree fn
= CALL_EXPR_FN (call
);
3640 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3641 return expand_integer_pack (call
, args
, complain
, in_decl
);
3646 /* Structure used to track the progress of find_parameter_packs_r. */
3647 struct find_parameter_pack_data
3649 /* TREE_LIST that will contain all of the parameter packs found by
3651 tree
* parameter_packs
;
3653 /* Set of AST nodes that have been visited by the traversal. */
3654 hash_set
<tree
> *visited
;
3656 /* True iff we're making a type pack expansion. */
3657 bool type_pack_expansion_p
;
3660 /* Identifies all of the argument packs that occur in a template
3661 argument and appends them to the TREE_LIST inside DATA, which is a
3662 find_parameter_pack_data structure. This is a subroutine of
3663 make_pack_expansion and uses_parameter_packs. */
3665 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3668 struct find_parameter_pack_data
* ppd
=
3669 (struct find_parameter_pack_data
*)data
;
3670 bool parameter_pack_p
= false;
3672 /* Handle type aliases/typedefs. */
3673 if (TYPE_ALIAS_P (t
))
3675 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3676 cp_walk_tree (&TI_ARGS (tinfo
),
3677 &find_parameter_packs_r
,
3683 /* Identify whether this is a parameter pack or not. */
3684 switch (TREE_CODE (t
))
3686 case TEMPLATE_PARM_INDEX
:
3687 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3688 parameter_pack_p
= true;
3691 case TEMPLATE_TYPE_PARM
:
3692 t
= TYPE_MAIN_VARIANT (t
);
3694 case TEMPLATE_TEMPLATE_PARM
:
3695 /* If the placeholder appears in the decl-specifier-seq of a function
3696 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3697 is a pack expansion, the invented template parameter is a template
3699 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3700 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3701 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3702 parameter_pack_p
= true;
3707 if (DECL_PACK_P (t
))
3709 /* We don't want to walk into the type of a PARM_DECL,
3710 because we don't want to see the type parameter pack. */
3712 parameter_pack_p
= true;
3717 if (DECL_PACK_P (t
))
3719 /* We don't want to walk into the type of a variadic capture proxy,
3720 because we don't want to see the type parameter pack. */
3722 parameter_pack_p
= true;
3724 else if (variable_template_specialization_p (t
))
3726 cp_walk_tree (&DECL_TI_ARGS (t
),
3727 find_parameter_packs_r
,
3734 if (builtin_pack_call_p (t
))
3735 parameter_pack_p
= true;
3739 parameter_pack_p
= true;
3742 /* Not a parameter pack. */
3746 if (parameter_pack_p
)
3748 /* Add this parameter pack to the list. */
3749 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3753 cp_walk_tree (&TYPE_CONTEXT (t
),
3754 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3756 /* This switch statement will return immediately if we don't find a
3758 switch (TREE_CODE (t
))
3760 case TEMPLATE_PARM_INDEX
:
3763 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3764 /* Check the template itself. */
3765 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3766 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3767 /* Check the template arguments. */
3768 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3773 case TEMPLATE_TYPE_PARM
:
3774 case TEMPLATE_TEMPLATE_PARM
:
3781 /* Ignore the declaration of a capture proxy for a parameter pack. */
3782 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3787 if (TYPE_PTRMEMFUNC_P (t
))
3793 if (TYPE_TEMPLATE_INFO (t
))
3794 cp_walk_tree (&TYPE_TI_ARGS (t
),
3795 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3801 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3806 cp_walk_tree (&TREE_TYPE (t
),
3807 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3811 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3816 case TYPE_PACK_EXPANSION
:
3817 case EXPR_PACK_EXPANSION
:
3822 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3827 case IDENTIFIER_NODE
:
3828 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3835 /* Look at explicit captures. */
3836 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3837 cap
; cap
= TREE_CHAIN (cap
))
3838 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3840 /* Since we defer implicit capture, look in the body as well. */
3841 tree fn
= lambda_function (t
);
3842 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3850 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3851 type_pack_expansion_p to false so that any placeholders
3852 within the expression don't get marked as parameter packs. */
3853 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3854 ppd
->type_pack_expansion_p
= false;
3855 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3857 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3869 /* Determines if the expression or type T uses any parameter packs. */
3871 uses_parameter_packs (tree t
)
3873 tree parameter_packs
= NULL_TREE
;
3874 struct find_parameter_pack_data ppd
;
3875 ppd
.parameter_packs
= ¶meter_packs
;
3876 ppd
.visited
= new hash_set
<tree
>;
3877 ppd
.type_pack_expansion_p
= false;
3878 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3880 return parameter_packs
!= NULL_TREE
;
3883 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3884 representation a base-class initializer into a parameter pack
3885 expansion. If all goes well, the resulting node will be an
3886 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3889 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3892 tree parameter_packs
= NULL_TREE
;
3893 bool for_types
= false;
3894 struct find_parameter_pack_data ppd
;
3896 if (!arg
|| arg
== error_mark_node
)
3899 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3901 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3902 class initializer. In this case, the TREE_PURPOSE will be a
3903 _TYPE node (representing the base class expansion we're
3904 initializing) and the TREE_VALUE will be a TREE_LIST
3905 containing the initialization arguments.
3907 The resulting expansion looks somewhat different from most
3908 expansions. Rather than returning just one _EXPANSION, we
3909 return a TREE_LIST whose TREE_PURPOSE is a
3910 TYPE_PACK_EXPANSION containing the bases that will be
3911 initialized. The TREE_VALUE will be identical to the
3912 original TREE_VALUE, which is a list of arguments that will
3913 be passed to each base. We do not introduce any new pack
3914 expansion nodes into the TREE_VALUE (although it is possible
3915 that some already exist), because the TREE_PURPOSE and
3916 TREE_VALUE all need to be expanded together with the same
3917 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3918 resulting TREE_PURPOSE will mention the parameter packs in
3919 both the bases and the arguments to the bases. */
3922 tree parameter_packs
= NULL_TREE
;
3924 /* Determine which parameter packs will be used by the base
3926 ppd
.visited
= new hash_set
<tree
>;
3927 ppd
.parameter_packs
= ¶meter_packs
;
3928 ppd
.type_pack_expansion_p
= true;
3929 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3930 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3933 if (parameter_packs
== NULL_TREE
)
3935 if (complain
& tf_error
)
3936 error ("base initializer expansion %qT contains no parameter packs",
3939 return error_mark_node
;
3942 if (TREE_VALUE (arg
) != void_type_node
)
3944 /* Collect the sets of parameter packs used in each of the
3945 initialization arguments. */
3946 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3948 /* Determine which parameter packs will be expanded in this
3950 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3957 /* Create the pack expansion type for the base type. */
3958 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3959 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3960 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3961 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3963 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3964 they will rarely be compared to anything. */
3965 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3967 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3970 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3973 /* Build the PACK_EXPANSION_* node. */
3975 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3976 : make_node (EXPR_PACK_EXPANSION
);
3977 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3978 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3980 /* Propagate type and const-expression information. */
3981 TREE_TYPE (result
) = TREE_TYPE (arg
);
3982 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3983 /* Mark this read now, since the expansion might be length 0. */
3984 mark_exp_read (arg
);
3987 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3988 they will rarely be compared to anything. */
3989 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3991 /* Determine which parameter packs will be expanded. */
3992 ppd
.parameter_packs
= ¶meter_packs
;
3993 ppd
.visited
= new hash_set
<tree
>;
3994 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3995 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3998 /* Make sure we found some parameter packs. */
3999 if (parameter_packs
== NULL_TREE
)
4001 if (complain
& tf_error
)
4004 error ("expansion pattern %qT contains no argument packs", arg
);
4006 error ("expansion pattern %qE contains no argument packs", arg
);
4008 return error_mark_node
;
4010 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4012 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4017 /* Checks T for any "bare" parameter packs, which have not yet been
4018 expanded, and issues an error if any are found. This operation can
4019 only be done on full expressions or types (e.g., an expression
4020 statement, "if" condition, etc.), because we could have expressions like:
4022 foo(f(g(h(args)))...)
4024 where "args" is a parameter pack. check_for_bare_parameter_packs
4025 should not be called for the subexpressions args, h(args),
4026 g(h(args)), or f(g(h(args))), because we would produce erroneous
4029 Returns TRUE and emits an error if there were bare parameter packs,
4030 returns FALSE otherwise. */
4032 check_for_bare_parameter_packs (tree t
)
4034 tree parameter_packs
= NULL_TREE
;
4035 struct find_parameter_pack_data ppd
;
4037 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4040 /* A lambda might use a parameter pack from the containing context. */
4041 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
4044 if (TREE_CODE (t
) == TYPE_DECL
)
4047 ppd
.parameter_packs
= ¶meter_packs
;
4048 ppd
.visited
= new hash_set
<tree
>;
4049 ppd
.type_pack_expansion_p
= false;
4050 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4053 if (parameter_packs
)
4055 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
4056 error_at (loc
, "parameter packs not expanded with %<...%>:");
4057 while (parameter_packs
)
4059 tree pack
= TREE_VALUE (parameter_packs
);
4060 tree name
= NULL_TREE
;
4062 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4063 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4064 name
= TYPE_NAME (pack
);
4065 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4066 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4067 else if (TREE_CODE (pack
) == CALL_EXPR
)
4068 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4070 name
= DECL_NAME (pack
);
4073 inform (loc
, " %qD", name
);
4075 inform (loc
, " <anonymous>");
4077 parameter_packs
= TREE_CHAIN (parameter_packs
);
4086 /* Expand any parameter packs that occur in the template arguments in
4089 expand_template_argument_pack (tree args
)
4091 if (args
== error_mark_node
)
4092 return error_mark_node
;
4094 tree result_args
= NULL_TREE
;
4095 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4096 int num_result_args
= -1;
4097 int non_default_args_count
= -1;
4099 /* First, determine if we need to expand anything, and the number of
4100 slots we'll need. */
4101 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4103 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4104 if (arg
== NULL_TREE
)
4106 if (ARGUMENT_PACK_P (arg
))
4108 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4109 if (num_result_args
< 0)
4110 num_result_args
= in_arg
+ num_packed
;
4112 num_result_args
+= num_packed
;
4116 if (num_result_args
>= 0)
4121 /* If no expansion is necessary, we're done. */
4122 if (num_result_args
< 0)
4125 /* Expand arguments. */
4126 result_args
= make_tree_vec (num_result_args
);
4127 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4128 non_default_args_count
=
4129 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4130 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4132 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4133 if (ARGUMENT_PACK_P (arg
))
4135 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4136 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4137 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4138 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4139 if (non_default_args_count
> 0)
4140 non_default_args_count
+= num_packed
- 1;
4144 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4148 if (non_default_args_count
>= 0)
4149 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4153 /* Checks if DECL shadows a template parameter.
4155 [temp.local]: A template-parameter shall not be redeclared within its
4156 scope (including nested scopes).
4158 Emits an error and returns TRUE if the DECL shadows a parameter,
4159 returns FALSE otherwise. */
4162 check_template_shadow (tree decl
)
4166 /* If we're not in a template, we can't possibly shadow a template
4168 if (!current_template_parms
)
4171 /* Figure out what we're shadowing. */
4172 decl
= OVL_FIRST (decl
);
4173 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4175 /* If there's no previous binding for this name, we're not shadowing
4176 anything, let alone a template parameter. */
4180 /* If we're not shadowing a template parameter, we're done. Note
4181 that OLDDECL might be an OVERLOAD (or perhaps even an
4182 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4184 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4187 /* We check for decl != olddecl to avoid bogus errors for using a
4188 name inside a class. We check TPFI to avoid duplicate errors for
4189 inline member templates. */
4191 || (DECL_TEMPLATE_PARM_P (decl
)
4192 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4195 /* Don't complain about the injected class name, as we've already
4196 complained about the class itself. */
4197 if (DECL_SELF_REFERENCE_P (decl
))
4200 if (DECL_TEMPLATE_PARM_P (decl
))
4201 error ("declaration of template parameter %q+D shadows "
4202 "template parameter", decl
);
4204 error ("declaration of %q+#D shadows template parameter", decl
);
4205 inform (DECL_SOURCE_LOCATION (olddecl
),
4206 "template parameter %qD declared here", olddecl
);
4210 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4211 ORIG_LEVEL, DECL, and TYPE. */
4214 build_template_parm_index (int index
,
4220 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4221 TEMPLATE_PARM_IDX (t
) = index
;
4222 TEMPLATE_PARM_LEVEL (t
) = level
;
4223 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4224 TEMPLATE_PARM_DECL (t
) = decl
;
4225 TREE_TYPE (t
) = type
;
4226 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4227 TREE_READONLY (t
) = TREE_READONLY (decl
);
4232 /* Find the canonical type parameter for the given template type
4233 parameter. Returns the canonical type parameter, which may be TYPE
4234 if no such parameter existed. */
4237 canonical_type_parameter (tree type
)
4240 int idx
= TEMPLATE_TYPE_IDX (type
);
4241 if (!canonical_template_parms
)
4242 vec_alloc (canonical_template_parms
, idx
+ 1);
4244 if (canonical_template_parms
->length () <= (unsigned) idx
)
4245 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4247 list
= (*canonical_template_parms
)[idx
];
4248 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4249 list
= TREE_CHAIN (list
);
4252 return TREE_VALUE (list
);
4255 (*canonical_template_parms
)[idx
]
4256 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4261 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4262 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4263 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4264 new one is created. */
4267 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4268 tsubst_flags_t complain
)
4270 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4271 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4272 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4273 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4275 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4278 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4279 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4280 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4281 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4282 DECL_ARTIFICIAL (decl
) = 1;
4283 SET_DECL_TEMPLATE_PARM_P (decl
);
4285 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4286 TEMPLATE_PARM_LEVEL (index
) - levels
,
4287 TEMPLATE_PARM_ORIG_LEVEL (index
),
4289 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4290 TEMPLATE_PARM_PARAMETER_PACK (t
)
4291 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4293 /* Template template parameters need this. */
4294 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4296 DECL_TEMPLATE_RESULT (decl
)
4297 = build_decl (DECL_SOURCE_LOCATION (decl
),
4298 TYPE_DECL
, DECL_NAME (decl
), type
);
4299 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4300 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4301 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4305 return TEMPLATE_PARM_DESCENDANTS (index
);
4308 /* Process information from new template parameter PARM and append it
4309 to the LIST being built. This new parameter is a non-type
4310 parameter iff IS_NON_TYPE is true. This new parameter is a
4311 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4315 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4316 bool is_non_type
, bool is_parameter_pack
)
4321 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4322 tree defval
= TREE_PURPOSE (parm
);
4323 tree constr
= TREE_TYPE (parm
);
4327 tree p
= tree_last (list
);
4329 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4332 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4333 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4335 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4343 parm
= TREE_VALUE (parm
);
4345 SET_DECL_TEMPLATE_PARM_P (parm
);
4347 if (TREE_TYPE (parm
) != error_mark_node
)
4351 The top-level cv-qualifiers on the template-parameter are
4352 ignored when determining its type. */
4353 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4354 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4355 TREE_TYPE (parm
) = error_mark_node
;
4356 else if (uses_parameter_packs (TREE_TYPE (parm
))
4357 && !is_parameter_pack
4358 /* If we're in a nested template parameter list, the template
4359 template parameter could be a parameter pack. */
4360 && processing_template_parmlist
== 1)
4362 /* This template parameter is not a parameter pack, but it
4363 should be. Complain about "bare" parameter packs. */
4364 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4366 /* Recover by calling this a parameter pack. */
4367 is_parameter_pack
= true;
4371 /* A template parameter is not modifiable. */
4372 TREE_CONSTANT (parm
) = 1;
4373 TREE_READONLY (parm
) = 1;
4374 decl
= build_decl (parm_loc
,
4375 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4376 TREE_CONSTANT (decl
) = 1;
4377 TREE_READONLY (decl
) = 1;
4378 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4379 = build_template_parm_index (idx
, processing_template_decl
,
4380 processing_template_decl
,
4381 decl
, TREE_TYPE (parm
));
4383 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4384 = is_parameter_pack
;
4389 parm
= TREE_VALUE (TREE_VALUE (parm
));
4391 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4393 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4394 /* This is for distinguishing between real templates and template
4395 template parameters */
4396 TREE_TYPE (parm
) = t
;
4397 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4402 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4403 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4404 decl
= build_decl (parm_loc
,
4405 TYPE_DECL
, parm
, t
);
4408 TYPE_NAME (t
) = decl
;
4409 TYPE_STUB_DECL (t
) = decl
;
4411 TEMPLATE_TYPE_PARM_INDEX (t
)
4412 = build_template_parm_index (idx
, processing_template_decl
,
4413 processing_template_decl
,
4414 decl
, TREE_TYPE (parm
));
4415 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4416 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4418 DECL_ARTIFICIAL (decl
) = 1;
4419 SET_DECL_TEMPLATE_PARM_P (decl
);
4421 /* Build requirements for the type/template parameter.
4422 This must be done after SET_DECL_TEMPLATE_PARM_P or
4423 process_template_parm could fail. */
4424 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4428 /* Build the parameter node linking the parameter declaration,
4429 its default argument (if any), and its constraints (if any). */
4430 parm
= build_tree_list (defval
, parm
);
4431 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4433 return chainon (list
, parm
);
4436 /* The end of a template parameter list has been reached. Process the
4437 tree list into a parameter vector, converting each parameter into a more
4438 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4442 end_template_parm_list (tree parms
)
4446 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4448 /* Pop the dummy parameter level and add the real one. */
4449 current_template_parms
= TREE_CHAIN (current_template_parms
);
4451 current_template_parms
4452 = tree_cons (size_int (processing_template_decl
),
4453 saved_parmlist
, current_template_parms
);
4455 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4457 next
= TREE_CHAIN (parm
);
4458 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4459 TREE_CHAIN (parm
) = NULL_TREE
;
4462 --processing_template_parmlist
;
4464 return saved_parmlist
;
4467 // Explicitly indicate the end of the template parameter list. We assume
4468 // that the current template parameters have been constructed and/or
4469 // managed explicitly, as when creating new template template parameters
4470 // from a shorthand constraint.
4472 end_template_parm_list ()
4474 --processing_template_parmlist
;
4477 /* end_template_decl is called after a template declaration is seen. */
4480 end_template_decl (void)
4482 reset_specialization ();
4484 if (! processing_template_decl
)
4487 /* This matches the pushlevel in begin_template_parm_list. */
4490 --processing_template_decl
;
4491 current_template_parms
= TREE_CHAIN (current_template_parms
);
4494 /* Takes a TREE_LIST representing a template parameter and convert it
4495 into an argument suitable to be passed to the type substitution
4496 functions. Note that If the TREE_LIST contains an error_mark
4497 node, the returned argument is error_mark_node. */
4500 template_parm_to_arg (tree t
)
4504 || TREE_CODE (t
) != TREE_LIST
)
4507 if (error_operand_p (TREE_VALUE (t
)))
4508 return error_mark_node
;
4512 if (TREE_CODE (t
) == TYPE_DECL
4513 || TREE_CODE (t
) == TEMPLATE_DECL
)
4517 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4519 /* Turn this argument into a TYPE_ARGUMENT_PACK
4520 with a single element, which expands T. */
4521 tree vec
= make_tree_vec (1);
4523 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4525 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4527 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4528 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4533 t
= DECL_INITIAL (t
);
4535 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4537 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4538 with a single element, which expands T. */
4539 tree vec
= make_tree_vec (1);
4541 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4543 t
= convert_from_reference (t
);
4544 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4546 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4547 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4550 t
= convert_from_reference (t
);
4555 /* Given a single level of template parameters (a TREE_VEC), return it
4556 as a set of template arguments. */
4559 template_parms_level_to_args (tree parms
)
4561 tree a
= copy_node (parms
);
4562 TREE_TYPE (a
) = NULL_TREE
;
4563 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4564 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4567 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4572 /* Given a set of template parameters, return them as a set of template
4573 arguments. The template parameters are represented as a TREE_VEC, in
4574 the form documented in cp-tree.h for template arguments. */
4577 template_parms_to_args (tree parms
)
4580 tree args
= NULL_TREE
;
4581 int length
= TMPL_PARMS_DEPTH (parms
);
4584 /* If there is only one level of template parameters, we do not
4585 create a TREE_VEC of TREE_VECs. Instead, we return a single
4586 TREE_VEC containing the arguments. */
4588 args
= make_tree_vec (length
);
4590 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4592 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4595 TREE_VEC_ELT (args
, --l
) = a
;
4603 /* Within the declaration of a template, return the currently active
4604 template parameters as an argument TREE_VEC. */
4607 current_template_args (void)
4609 return template_parms_to_args (current_template_parms
);
4612 /* Update the declared TYPE by doing any lookups which were thought to be
4613 dependent, but are not now that we know the SCOPE of the declarator. */
4616 maybe_update_decl_type (tree orig_type
, tree scope
)
4618 tree type
= orig_type
;
4620 if (type
== NULL_TREE
)
4623 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4624 type
= TREE_TYPE (type
);
4626 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4627 && dependent_type_p (type
)
4628 /* Don't bother building up the args in this case. */
4629 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4631 /* tsubst in the args corresponding to the template parameters,
4632 including auto if present. Most things will be unchanged, but
4633 make_typename_type and tsubst_qualified_id will resolve
4634 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4635 tree args
= current_template_args ();
4636 tree auto_node
= type_uses_auto (type
);
4640 tree auto_vec
= make_tree_vec (1);
4641 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4642 args
= add_to_template_args (args
, auto_vec
);
4644 pushed
= push_scope (scope
);
4645 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4650 if (type
== error_mark_node
)
4653 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4655 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4658 type
= TYPE_NAME (type
);
4663 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4664 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4665 the new template is a member template. */
4668 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4670 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4671 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4672 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4673 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4674 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4679 struct template_parm_data
4681 /* The level of the template parameters we are currently
4685 /* The index of the specialization argument we are currently
4689 /* An array whose size is the number of template parameters. The
4690 elements are nonzero if the parameter has been used in any one
4691 of the arguments processed so far. */
4694 /* An array whose size is the number of template arguments. The
4695 elements are nonzero if the argument makes use of template
4696 parameters of this level. */
4697 int* arg_uses_template_parms
;
4700 /* Subroutine of push_template_decl used to see if each template
4701 parameter in a partial specialization is used in the explicit
4702 argument list. If T is of the LEVEL given in DATA (which is
4703 treated as a template_parm_data*), then DATA->PARMS is marked
4707 mark_template_parm (tree t
, void* data
)
4711 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4713 template_parm_level_and_index (t
, &level
, &idx
);
4715 if (level
== tpd
->level
)
4717 tpd
->parms
[idx
] = 1;
4718 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4721 /* In C++17 the type of a non-type argument is a deduced context. */
4722 if (cxx_dialect
>= cxx17
4723 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4724 for_each_template_parm (TREE_TYPE (t
),
4725 &mark_template_parm
,
4728 /*include_nondeduced_p=*/false);
4730 /* Return zero so that for_each_template_parm will continue the
4731 traversal of the tree; we want to mark *every* template parm. */
4735 /* Process the partial specialization DECL. */
4738 process_partial_specialization (tree decl
)
4740 tree type
= TREE_TYPE (decl
);
4741 tree tinfo
= get_template_info (decl
);
4742 tree maintmpl
= TI_TEMPLATE (tinfo
);
4743 tree specargs
= TI_ARGS (tinfo
);
4744 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4745 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4748 int nargs
= TREE_VEC_LENGTH (inner_args
);
4751 bool did_error_intro
= false;
4752 struct template_parm_data tpd
;
4753 struct template_parm_data tpd2
;
4755 gcc_assert (current_template_parms
);
4757 /* A concept cannot be specialized. */
4758 if (flag_concepts
&& variable_concept_p (maintmpl
))
4760 error ("specialization of variable concept %q#D", maintmpl
);
4761 return error_mark_node
;
4764 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4765 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4767 /* We check that each of the template parameters given in the
4768 partial specialization is used in the argument list to the
4769 specialization. For example:
4771 template <class T> struct S;
4772 template <class T> struct S<T*>;
4774 The second declaration is OK because `T*' uses the template
4775 parameter T, whereas
4777 template <class T> struct S<int>;
4779 is no good. Even trickier is:
4790 The S2<T> declaration is actually invalid; it is a
4791 full-specialization. Of course,
4794 struct S2<T (*)(U)>;
4796 or some such would have been OK. */
4797 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4798 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4799 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4801 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4802 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4803 for (i
= 0; i
< nargs
; ++i
)
4805 tpd
.current_arg
= i
;
4806 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4807 &mark_template_parm
,
4810 /*include_nondeduced_p=*/false);
4812 for (i
= 0; i
< ntparms
; ++i
)
4813 if (tpd
.parms
[i
] == 0)
4815 /* One of the template parms was not used in a deduced context in the
4817 if (!did_error_intro
)
4819 error ("template parameters not deducible in "
4820 "partial specialization:");
4821 did_error_intro
= true;
4824 inform (input_location
, " %qD",
4825 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4828 if (did_error_intro
)
4829 return error_mark_node
;
4831 /* [temp.class.spec]
4833 The argument list of the specialization shall not be identical to
4834 the implicit argument list of the primary template. */
4836 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4837 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4839 || !strictly_subsumes (current_template_constraints (),
4840 get_constraints (maintmpl
))))
4843 error ("partial specialization %q+D does not specialize "
4844 "any template arguments; to define the primary template, "
4845 "remove the template argument list", decl
);
4847 error ("partial specialization %q+D does not specialize any "
4848 "template arguments and is not more constrained than "
4849 "the primary template; to define the primary template, "
4850 "remove the template argument list", decl
);
4851 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4854 /* A partial specialization that replaces multiple parameters of the
4855 primary template with a pack expansion is less specialized for those
4857 if (nargs
< DECL_NTPARMS (maintmpl
))
4859 error ("partial specialization is not more specialized than the "
4860 "primary template because it replaces multiple parameters "
4861 "with a pack expansion");
4862 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4863 /* Avoid crash in process_partial_specialization. */
4867 /* If we aren't in a dependent class, we can actually try deduction. */
4868 else if (tpd
.level
== 1
4869 /* FIXME we should be able to handle a partial specialization of a
4870 partial instantiation, but currently we can't (c++/41727). */
4871 && TMPL_ARGS_DEPTH (specargs
) == 1
4872 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4874 if (permerror (input_location
, "partial specialization %qD is not "
4875 "more specialized than", decl
))
4876 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4880 /* [temp.class.spec]
4882 A partially specialized non-type argument expression shall not
4883 involve template parameters of the partial specialization except
4884 when the argument expression is a simple identifier.
4886 The type of a template parameter corresponding to a specialized
4887 non-type argument shall not be dependent on a parameter of the
4890 Also, we verify that pack expansions only occur at the
4891 end of the argument list. */
4892 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4894 for (i
= 0; i
< nargs
; ++i
)
4896 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4897 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4898 tree packed_args
= NULL_TREE
;
4901 if (ARGUMENT_PACK_P (arg
))
4903 /* Extract the arguments from the argument pack. We'll be
4904 iterating over these in the following loop. */
4905 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4906 len
= TREE_VEC_LENGTH (packed_args
);
4909 for (j
= 0; j
< len
; j
++)
4912 /* Get the Jth argument in the parameter pack. */
4913 arg
= TREE_VEC_ELT (packed_args
, j
);
4915 if (PACK_EXPANSION_P (arg
))
4917 /* Pack expansions must come at the end of the
4919 if ((packed_args
&& j
< len
- 1)
4920 || (!packed_args
&& i
< nargs
- 1))
4922 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4923 error ("parameter pack argument %qE must be at the "
4924 "end of the template argument list", arg
);
4926 error ("parameter pack argument %qT must be at the "
4927 "end of the template argument list", arg
);
4931 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4932 /* We only care about the pattern. */
4933 arg
= PACK_EXPANSION_PATTERN (arg
);
4935 if (/* These first two lines are the `non-type' bit. */
4937 && TREE_CODE (arg
) != TEMPLATE_DECL
4938 /* This next two lines are the `argument expression is not just a
4939 simple identifier' condition and also the `specialized
4940 non-type argument' bit. */
4941 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4942 && !(REFERENCE_REF_P (arg
)
4943 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4945 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4946 || (packed_args
&& uses_template_parms (arg
)))
4947 error ("template argument %qE involves template parameter(s)",
4951 /* Look at the corresponding template parameter,
4952 marking which template parameters its type depends
4954 tree type
= TREE_TYPE (parm
);
4958 /* We haven't yet initialized TPD2. Do so now. */
4959 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4960 /* The number of parameters here is the number in the
4961 main template, which, as checked in the assertion
4963 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4965 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4968 /* Mark the template parameters. But this time, we're
4969 looking for the template parameters of the main
4970 template, not in the specialization. */
4971 tpd2
.current_arg
= i
;
4972 tpd2
.arg_uses_template_parms
[i
] = 0;
4973 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4974 for_each_template_parm (type
,
4975 &mark_template_parm
,
4978 /*include_nondeduced_p=*/false);
4980 if (tpd2
.arg_uses_template_parms
[i
])
4982 /* The type depended on some template parameters.
4983 If they are fully specialized in the
4984 specialization, that's OK. */
4987 for (j
= 0; j
< nargs
; ++j
)
4988 if (tpd2
.parms
[j
] != 0
4989 && tpd
.arg_uses_template_parms
[j
])
4992 error_n (input_location
, count
,
4993 "type %qT of template argument %qE depends "
4994 "on a template parameter",
4995 "type %qT of template argument %qE depends "
4996 "on template parameters",
5005 /* We should only get here once. */
5006 if (TREE_CODE (decl
) == TYPE_DECL
)
5007 gcc_assert (!COMPLETE_TYPE_P (type
));
5009 // Build the template decl.
5010 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5011 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5012 TREE_TYPE (tmpl
) = type
;
5013 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5014 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5015 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5016 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5018 /* Give template template parms a DECL_CONTEXT of the template
5019 for which they are a parameter. */
5020 for (i
= 0; i
< ntparms
; ++i
)
5022 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5023 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5024 DECL_CONTEXT (parm
) = tmpl
;
5028 /* We didn't register this in check_explicit_specialization so we could
5029 wait until the constraints were set. */
5030 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5032 associate_classtype_constraints (type
);
5034 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5035 = tree_cons (specargs
, tmpl
,
5036 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5037 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5039 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5040 inst
= TREE_CHAIN (inst
))
5042 tree instance
= TREE_VALUE (inst
);
5043 if (TYPE_P (instance
)
5044 ? (COMPLETE_TYPE_P (instance
)
5045 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5046 : DECL_TEMPLATE_INSTANTIATION (instance
))
5048 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5049 tree inst_decl
= (DECL_P (instance
)
5050 ? instance
: TYPE_NAME (instance
));
5053 else if (spec
== error_mark_node
)
5054 permerror (input_location
,
5055 "declaration of %qD ambiguates earlier template "
5056 "instantiation for %qD", decl
, inst_decl
);
5057 else if (TREE_VALUE (spec
) == tmpl
)
5058 permerror (input_location
,
5059 "partial specialization of %qD after instantiation "
5060 "of %qD", decl
, inst_decl
);
5067 /* PARM is a template parameter of some form; return the corresponding
5068 TEMPLATE_PARM_INDEX. */
5071 get_template_parm_index (tree parm
)
5073 if (TREE_CODE (parm
) == PARM_DECL
5074 || TREE_CODE (parm
) == CONST_DECL
)
5075 parm
= DECL_INITIAL (parm
);
5076 else if (TREE_CODE (parm
) == TYPE_DECL
5077 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5078 parm
= TREE_TYPE (parm
);
5079 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5080 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5081 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5082 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5083 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5087 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5088 parameter packs used by the template parameter PARM. */
5091 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5093 /* A type parm can't refer to another parm. */
5094 if (TREE_CODE (parm
) == TYPE_DECL
)
5096 else if (TREE_CODE (parm
) == PARM_DECL
)
5098 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5103 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5105 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5106 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5107 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5110 /* PARM is a template parameter pack. Return any parameter packs used in
5111 its type or the type of any of its template parameters. If there are
5112 any such packs, it will be instantiated into a fixed template parameter
5113 list by partial instantiation rather than be fully deduced. */
5116 fixed_parameter_pack_p (tree parm
)
5118 /* This can only be true in a member template. */
5119 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5121 /* This can only be true for a parameter pack. */
5122 if (!template_parameter_pack_p (parm
))
5124 /* A type parm can't refer to another parm. */
5125 if (TREE_CODE (parm
) == TYPE_DECL
)
5128 tree parameter_packs
= NULL_TREE
;
5129 struct find_parameter_pack_data ppd
;
5130 ppd
.parameter_packs
= ¶meter_packs
;
5131 ppd
.visited
= new hash_set
<tree
>;
5132 ppd
.type_pack_expansion_p
= false;
5134 fixed_parameter_pack_p_1 (parm
, &ppd
);
5137 return parameter_packs
;
5140 /* Check that a template declaration's use of default arguments and
5141 parameter packs is not invalid. Here, PARMS are the template
5142 parameters. IS_PRIMARY is true if DECL is the thing declared by
5143 a primary template. IS_PARTIAL is true if DECL is a partial
5146 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5147 function template declaration or a friend class template
5148 declaration. In the function case, 1 indicates a declaration, 2
5149 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5150 emitted for extraneous default arguments.
5152 Returns TRUE if there were no errors found, FALSE otherwise. */
5155 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5156 bool is_partial
, int is_friend_decl
)
5159 int last_level_to_check
;
5161 bool no_errors
= true;
5165 A default template-argument shall not be specified in a
5166 function template declaration or a function template definition, nor
5167 in the template-parameter-list of the definition of a member of a
5170 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5171 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5172 /* You can't have a function template declaration in a local
5173 scope, nor you can you define a member of a class template in a
5177 if ((TREE_CODE (decl
) == TYPE_DECL
5179 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5180 || (TREE_CODE (decl
) == FUNCTION_DECL
5181 && LAMBDA_FUNCTION_P (decl
)))
5182 /* A lambda doesn't have an explicit declaration; don't complain
5183 about the parms of the enclosing class. */
5186 if (current_class_type
5187 && !TYPE_BEING_DEFINED (current_class_type
)
5188 && DECL_LANG_SPECIFIC (decl
)
5189 && DECL_DECLARES_FUNCTION_P (decl
)
5190 /* If this is either a friend defined in the scope of the class
5191 or a member function. */
5192 && (DECL_FUNCTION_MEMBER_P (decl
)
5193 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5194 : DECL_FRIEND_CONTEXT (decl
)
5195 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5197 /* And, if it was a member function, it really was defined in
5198 the scope of the class. */
5199 && (!DECL_FUNCTION_MEMBER_P (decl
)
5200 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5201 /* We already checked these parameters when the template was
5202 declared, so there's no need to do it again now. This function
5203 was defined in class scope, but we're processing its body now
5204 that the class is complete. */
5207 /* Core issue 226 (C++0x only): the following only applies to class
5210 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5214 If a template-parameter has a default template-argument, all
5215 subsequent template-parameters shall have a default
5216 template-argument supplied. */
5217 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5219 tree inner_parms
= TREE_VALUE (parm_level
);
5220 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5221 int seen_def_arg_p
= 0;
5224 for (i
= 0; i
< ntparms
; ++i
)
5226 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5228 if (parm
== error_mark_node
)
5231 if (TREE_PURPOSE (parm
))
5233 else if (seen_def_arg_p
5234 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5236 error ("no default argument for %qD", TREE_VALUE (parm
));
5237 /* For better subsequent error-recovery, we indicate that
5238 there should have been a default argument. */
5239 TREE_PURPOSE (parm
) = error_mark_node
;
5242 else if (!is_partial
5244 /* Don't complain about an enclosing partial
5246 && parm_level
== parms
5247 && TREE_CODE (decl
) == TYPE_DECL
5249 && template_parameter_pack_p (TREE_VALUE (parm
))
5250 /* A fixed parameter pack will be partially
5251 instantiated into a fixed length list. */
5252 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5254 /* A primary class template can only have one
5255 parameter pack, at the end of the template
5258 error ("parameter pack %q+D must be at the end of the"
5259 " template parameter list", TREE_VALUE (parm
));
5261 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5269 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5273 /* For an ordinary class template, default template arguments are
5274 allowed at the innermost level, e.g.:
5275 template <class T = int>
5277 but, in a partial specialization, they're not allowed even
5278 there, as we have in [temp.class.spec]:
5280 The template parameter list of a specialization shall not
5281 contain default template argument values.
5283 So, for a partial specialization, or for a function template
5284 (in C++98/C++03), we look at all of them. */
5287 /* But, for a primary class template that is not a partial
5288 specialization we look at all template parameters except the
5290 parms
= TREE_CHAIN (parms
);
5292 /* Figure out what error message to issue. */
5293 if (is_friend_decl
== 2)
5294 msg
= G_("default template arguments may not be used in function template "
5295 "friend re-declaration");
5296 else if (is_friend_decl
)
5297 msg
= G_("default template arguments may not be used in template "
5298 "friend declarations");
5299 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5300 msg
= G_("default template arguments may not be used in function templates "
5301 "without -std=c++11 or -std=gnu++11");
5302 else if (is_partial
)
5303 msg
= G_("default template arguments may not be used in "
5304 "partial specializations");
5305 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5306 msg
= G_("default argument for template parameter for class enclosing %qD");
5308 /* Per [temp.param]/9, "A default template-argument shall not be
5309 specified in the template-parameter-lists of the definition of
5310 a member of a class template that appears outside of the member's
5311 class.", thus if we aren't handling a member of a class template
5312 there is no need to examine the parameters. */
5315 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5316 /* If we're inside a class definition, there's no need to
5317 examine the parameters to the class itself. On the one
5318 hand, they will be checked when the class is defined, and,
5319 on the other, default arguments are valid in things like:
5320 template <class T = double>
5321 struct S { template <class U> void f(U); };
5322 Here the default argument for `S' has no bearing on the
5323 declaration of `f'. */
5324 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5326 /* Check everything. */
5327 last_level_to_check
= 0;
5329 for (parm_level
= parms
;
5330 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5331 parm_level
= TREE_CHAIN (parm_level
))
5333 tree inner_parms
= TREE_VALUE (parm_level
);
5337 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5338 for (i
= 0; i
< ntparms
; ++i
)
5340 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5343 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5348 if (is_friend_decl
== 2)
5355 /* Clear out the default argument so that we are not
5357 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5361 /* At this point, if we're still interested in issuing messages,
5362 they must apply to classes surrounding the object declared. */
5364 msg
= G_("default argument for template parameter for class "
5371 /* Worker for push_template_decl_real, called via
5372 for_each_template_parm. DATA is really an int, indicating the
5373 level of the parameters we are interested in. If T is a template
5374 parameter of that level, return nonzero. */
5377 template_parm_this_level_p (tree t
, void* data
)
5379 int this_level
= *(int *)data
;
5382 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5383 level
= TEMPLATE_PARM_LEVEL (t
);
5385 level
= TEMPLATE_TYPE_LEVEL (t
);
5386 return level
== this_level
;
5389 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5390 DATA is really an int, indicating the innermost outer level of parameters.
5391 If T is a template parameter of that level or further out, return
5395 template_parm_outer_level (tree t
, void *data
)
5397 int this_level
= *(int *)data
;
5400 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5401 level
= TEMPLATE_PARM_LEVEL (t
);
5403 level
= TEMPLATE_TYPE_LEVEL (t
);
5404 return level
<= this_level
;
5407 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5408 parameters given by current_template_args, or reuses a
5409 previously existing one, if appropriate. Returns the DECL, or an
5410 equivalent one, if it is replaced via a call to duplicate_decls.
5412 If IS_FRIEND is true, DECL is a friend declaration. */
5415 push_template_decl_real (tree decl
, bool is_friend
)
5423 int new_template_p
= 0;
5424 /* True if the template is a member template, in the sense of
5426 bool member_template_p
= false;
5428 if (decl
== error_mark_node
|| !current_template_parms
)
5429 return error_mark_node
;
5431 /* See if this is a partial specialization. */
5432 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5433 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5434 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5436 && DECL_LANG_SPECIFIC (decl
)
5437 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5438 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5440 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5444 /* For a friend, we want the context of the friend, not
5445 the type of which it is a friend. */
5446 ctx
= CP_DECL_CONTEXT (decl
);
5447 else if (CP_DECL_CONTEXT (decl
)
5448 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5449 /* In the case of a virtual function, we want the class in which
5451 ctx
= CP_DECL_CONTEXT (decl
);
5453 /* Otherwise, if we're currently defining some class, the DECL
5454 is assumed to be a member of the class. */
5455 ctx
= current_scope ();
5457 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5460 if (!DECL_CONTEXT (decl
))
5461 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5463 /* See if this is a primary template. */
5464 if (is_friend
&& ctx
5465 && uses_template_parms_level (ctx
, processing_template_decl
))
5466 /* A friend template that specifies a class context, i.e.
5467 template <typename T> friend void A<T>::f();
5470 else if (TREE_CODE (decl
) == TYPE_DECL
5471 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5474 is_primary
= template_parm_scope_p ();
5478 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5480 if (DECL_CLASS_SCOPE_P (decl
))
5481 member_template_p
= true;
5482 if (TREE_CODE (decl
) == TYPE_DECL
5483 && anon_aggrname_p (DECL_NAME (decl
)))
5485 error ("template class without a name");
5486 return error_mark_node
;
5488 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5490 if (member_template_p
)
5492 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5493 error ("member template %qD may not have virt-specifiers", decl
);
5495 if (DECL_DESTRUCTOR_P (decl
))
5499 A destructor shall not be a member template. */
5500 error ("destructor %qD declared as member template", decl
);
5501 return error_mark_node
;
5503 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5504 && (!prototype_p (TREE_TYPE (decl
))
5505 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5506 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5507 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5508 == void_list_node
)))
5510 /* [basic.stc.dynamic.allocation]
5512 An allocation function can be a function
5513 template. ... Template allocation functions shall
5514 have two or more parameters. */
5515 error ("invalid template declaration of %qD", decl
);
5516 return error_mark_node
;
5519 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5520 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5522 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5523 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5524 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5526 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5527 if (TREE_CODE (t
) == TYPE_DECL
)
5529 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5530 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5533 else if (TREE_CODE (decl
) == TYPE_DECL
5534 && TYPE_DECL_ALIAS_P (decl
))
5535 /* alias-declaration */
5536 gcc_assert (!DECL_ARTIFICIAL (decl
));
5537 else if (VAR_P (decl
))
5538 /* C++14 variable template. */;
5541 error ("template declaration of %q#D", decl
);
5542 return error_mark_node
;
5546 /* Check to see that the rules regarding the use of default
5547 arguments are not being violated. We check args for a friend
5548 functions when we know whether it's a definition, introducing
5549 declaration or re-declaration. */
5550 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5551 check_default_tmpl_args (decl
, current_template_parms
,
5552 is_primary
, is_partial
, is_friend
);
5554 /* Ensure that there are no parameter packs in the type of this
5555 declaration that have not been expanded. */
5556 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5558 /* Check each of the arguments individually to see if there are
5559 any bare parameter packs. */
5560 tree type
= TREE_TYPE (decl
);
5561 tree arg
= DECL_ARGUMENTS (decl
);
5562 tree argtype
= TYPE_ARG_TYPES (type
);
5564 while (arg
&& argtype
)
5566 if (!DECL_PACK_P (arg
)
5567 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5569 /* This is a PARM_DECL that contains unexpanded parameter
5570 packs. We have already complained about this in the
5571 check_for_bare_parameter_packs call, so just replace
5572 these types with ERROR_MARK_NODE. */
5573 TREE_TYPE (arg
) = error_mark_node
;
5574 TREE_VALUE (argtype
) = error_mark_node
;
5577 arg
= DECL_CHAIN (arg
);
5578 argtype
= TREE_CHAIN (argtype
);
5581 /* Check for bare parameter packs in the return type and the
5582 exception specifiers. */
5583 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5584 /* Errors were already issued, set return type to int
5585 as the frontend doesn't expect error_mark_node as
5587 TREE_TYPE (type
) = integer_type_node
;
5588 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5589 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5591 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5592 && TYPE_DECL_ALIAS_P (decl
))
5593 ? DECL_ORIGINAL_TYPE (decl
)
5594 : TREE_TYPE (decl
)))
5596 TREE_TYPE (decl
) = error_mark_node
;
5597 return error_mark_node
;
5601 return process_partial_specialization (decl
);
5603 args
= current_template_args ();
5606 || TREE_CODE (ctx
) == FUNCTION_DECL
5607 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5608 || (TREE_CODE (decl
) == TYPE_DECL
5609 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5610 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5612 if (DECL_LANG_SPECIFIC (decl
)
5613 && DECL_TEMPLATE_INFO (decl
)
5614 && DECL_TI_TEMPLATE (decl
))
5615 tmpl
= DECL_TI_TEMPLATE (decl
);
5616 /* If DECL is a TYPE_DECL for a class-template, then there won't
5617 be DECL_LANG_SPECIFIC. The information equivalent to
5618 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5619 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5620 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5621 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5623 /* Since a template declaration already existed for this
5624 class-type, we must be redeclaring it here. Make sure
5625 that the redeclaration is valid. */
5626 redeclare_class_template (TREE_TYPE (decl
),
5627 current_template_parms
,
5628 current_template_constraints ());
5629 /* We don't need to create a new TEMPLATE_DECL; just use the
5630 one we already had. */
5631 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5635 tmpl
= build_template_decl (decl
, current_template_parms
,
5639 if (DECL_LANG_SPECIFIC (decl
)
5640 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5642 /* A specialization of a member template of a template
5644 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5645 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5646 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5652 tree a
, t
, current
, parms
;
5654 tree tinfo
= get_template_info (decl
);
5658 error ("template definition of non-template %q#D", decl
);
5659 return error_mark_node
;
5662 tmpl
= TI_TEMPLATE (tinfo
);
5664 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5665 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5666 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5667 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5671 /* The declaration is a specialization of a member
5672 template, declared outside the class. Therefore, the
5673 innermost template arguments will be NULL, so we
5674 replace them with the arguments determined by the
5675 earlier call to check_explicit_specialization. */
5676 args
= DECL_TI_ARGS (decl
);
5679 = build_template_decl (decl
, current_template_parms
,
5681 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5682 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5683 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5684 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5685 DECL_TEMPLATE_INFO (new_tmpl
)
5686 = build_template_info (tmpl
, args
);
5688 register_specialization (new_tmpl
,
5689 most_general_template (tmpl
),
5695 /* Make sure the template headers we got make sense. */
5697 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5698 i
= TMPL_PARMS_DEPTH (parms
);
5699 if (TMPL_ARGS_DEPTH (args
) != i
)
5701 error ("expected %d levels of template parms for %q#D, got %d",
5702 i
, decl
, TMPL_ARGS_DEPTH (args
));
5703 DECL_INTERFACE_KNOWN (decl
) = 1;
5704 return error_mark_node
;
5707 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5709 a
= TMPL_ARGS_LEVEL (args
, i
);
5710 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5712 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5714 if (current
== decl
)
5715 error ("got %d template parameters for %q#D",
5716 TREE_VEC_LENGTH (a
), decl
);
5718 error ("got %d template parameters for %q#T",
5719 TREE_VEC_LENGTH (a
), current
);
5720 error (" but %d required", TREE_VEC_LENGTH (t
));
5721 /* Avoid crash in import_export_decl. */
5722 DECL_INTERFACE_KNOWN (decl
) = 1;
5723 return error_mark_node
;
5726 if (current
== decl
)
5728 else if (current
== NULL_TREE
)
5729 /* Can happen in erroneous input. */
5732 current
= get_containing_scope (current
);
5735 /* Check that the parms are used in the appropriate qualifying scopes
5736 in the declarator. */
5737 if (!comp_template_args
5739 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5741 error ("template arguments to %qD do not match original "
5742 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5743 if (!uses_template_parms (TI_ARGS (tinfo
)))
5744 inform (input_location
, "use %<template<>%> for"
5745 " an explicit specialization");
5746 /* Avoid crash in import_export_decl. */
5747 DECL_INTERFACE_KNOWN (decl
) = 1;
5748 return error_mark_node
;
5752 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5753 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5755 /* Push template declarations for global functions and types. Note
5756 that we do not try to push a global template friend declared in a
5757 template class; such a thing may well depend on the template
5758 parameters of the class. */
5759 if (new_template_p
&& !ctx
5760 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5762 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5763 if (tmpl
== error_mark_node
)
5764 return error_mark_node
;
5766 /* Hide template friend classes that haven't been declared yet. */
5767 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5769 DECL_ANTICIPATED (tmpl
) = 1;
5770 DECL_FRIEND_P (tmpl
) = 1;
5776 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5778 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5780 /* Give template template parms a DECL_CONTEXT of the template
5781 for which they are a parameter. */
5782 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5783 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5785 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5786 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5787 DECL_CONTEXT (parm
) = tmpl
;
5790 if (TREE_CODE (decl
) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl
)
5792 && complex_alias_template_p (tmpl
))
5793 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5796 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5797 back to its most general template. If TMPL is a specialization,
5798 ARGS may only have the innermost set of arguments. Add the missing
5799 argument levels if necessary. */
5800 if (DECL_TEMPLATE_INFO (tmpl
))
5801 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5803 info
= build_template_info (tmpl
, args
);
5805 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5806 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5810 retrofit_lang_decl (decl
);
5811 if (DECL_LANG_SPECIFIC (decl
))
5812 DECL_TEMPLATE_INFO (decl
) = info
;
5815 if (flag_implicit_templates
5817 && TREE_PUBLIC (decl
)
5818 && VAR_OR_FUNCTION_DECL_P (decl
))
5819 /* Set DECL_COMDAT on template instantiations; if we force
5820 them to be emitted by explicit instantiation or -frepo,
5821 mark_needed will tell cgraph to do the right thing. */
5822 DECL_COMDAT (decl
) = true;
5824 return DECL_TEMPLATE_RESULT (tmpl
);
5828 push_template_decl (tree decl
)
5830 return push_template_decl_real (decl
, false);
5833 /* FN is an inheriting constructor that inherits from the constructor
5834 template INHERITED; turn FN into a constructor template with a matching
5838 add_inherited_template_parms (tree fn
, tree inherited
)
5841 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5842 inner_parms
= copy_node (inner_parms
);
5844 = tree_cons (size_int (processing_template_decl
+ 1),
5845 inner_parms
, current_template_parms
);
5846 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5847 tree args
= template_parms_to_args (parms
);
5848 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5849 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5850 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5851 DECL_ARTIFICIAL (tmpl
) = true;
5852 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5856 /* Called when a class template TYPE is redeclared with the indicated
5857 template PARMS, e.g.:
5859 template <class T> struct S;
5860 template <class T> struct S {}; */
5863 redeclare_class_template (tree type
, tree parms
, tree cons
)
5869 if (!TYPE_TEMPLATE_INFO (type
))
5871 error ("%qT is not a template type", type
);
5875 tmpl
= TYPE_TI_TEMPLATE (type
);
5876 if (!PRIMARY_TEMPLATE_P (tmpl
))
5877 /* The type is nested in some template class. Nothing to worry
5878 about here; there are no new template parameters for the nested
5884 error ("template specifiers not specified in declaration of %qD",
5889 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5890 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5892 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5894 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5895 "redeclared with %d template parameter",
5896 "redeclared with %d template parameters",
5897 TREE_VEC_LENGTH (parms
));
5898 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5899 "previous declaration %qD used %d template parameter",
5900 "previous declaration %qD used %d template parameters",
5901 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5905 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5912 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5913 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5916 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5917 if (error_operand_p (tmpl_parm
))
5920 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5921 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5922 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5924 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5926 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5927 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5928 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5929 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5930 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5931 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5932 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5933 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5934 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5936 error ("template parameter %q+#D", tmpl_parm
);
5937 error ("redeclared here as %q#D", parm
);
5941 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5943 /* We have in [temp.param]:
5945 A template-parameter may not be given default arguments
5946 by two different declarations in the same scope. */
5947 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5948 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5949 "original definition appeared here");
5953 if (parm_default
!= NULL_TREE
)
5954 /* Update the previous template parameters (which are the ones
5955 that will really count) with the new default value. */
5956 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5957 else if (tmpl_default
!= NULL_TREE
)
5958 /* Update the new parameters, too; they'll be used as the
5959 parameters for any members. */
5960 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5962 /* Give each template template parm in this redeclaration a
5963 DECL_CONTEXT of the template for which they are a parameter. */
5964 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5966 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5967 DECL_CONTEXT (parm
) = tmpl
;
5970 if (TREE_CODE (parm
) == TYPE_DECL
)
5971 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5974 // Cannot redeclare a class template with a different set of constraints.
5975 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5977 error_at (input_location
, "redeclaration %q#D with different "
5978 "constraints", tmpl
);
5979 inform (DECL_SOURCE_LOCATION (tmpl
),
5980 "original declaration appeared here");
5986 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5987 to be used when the caller has already checked
5988 (processing_template_decl
5989 && !instantiation_dependent_expression_p (expr)
5990 && potential_constant_expression (expr))
5991 and cleared processing_template_decl. */
5994 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5996 return tsubst_copy_and_build (expr
,
5999 /*in_decl=*/NULL_TREE
,
6000 /*function_p=*/false,
6001 /*integral_constant_expression_p=*/true);
6004 /* Simplify EXPR if it is a non-dependent expression. Returns the
6005 (possibly simplified) expression. */
6008 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6010 if (expr
== NULL_TREE
)
6013 /* If we're in a template, but EXPR isn't value dependent, simplify
6014 it. We're supposed to treat:
6016 template <typename T> void f(T[1 + 1]);
6017 template <typename T> void f(T[2]);
6019 as two declarations of the same function, for example. */
6020 if (processing_template_decl
6021 && is_nondependent_constant_expression (expr
))
6023 processing_template_decl_sentinel s
;
6024 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6030 instantiate_non_dependent_expr (tree expr
)
6032 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6035 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6036 an uninstantiated expression. */
6039 instantiate_non_dependent_or_null (tree expr
)
6041 if (expr
== NULL_TREE
)
6043 if (processing_template_decl
)
6045 if (!is_nondependent_constant_expression (expr
))
6049 processing_template_decl_sentinel s
;
6050 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6056 /* True iff T is a specialization of a variable template. */
6059 variable_template_specialization_p (tree t
)
6061 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6063 tree tmpl
= DECL_TI_TEMPLATE (t
);
6064 return variable_template_p (tmpl
);
6067 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6068 template declaration, or a TYPE_DECL for an alias declaration. */
6071 alias_type_or_template_p (tree t
)
6075 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6078 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6079 || DECL_ALIAS_TEMPLATE_P (t
));
6082 /* Return TRUE iff T is a specialization of an alias template. */
6085 alias_template_specialization_p (const_tree t
)
6087 /* It's an alias template specialization if it's an alias and its
6088 TYPE_NAME is a specialization of a primary template. */
6089 if (TYPE_ALIAS_P (t
))
6090 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6091 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6096 /* An alias template is complex from a SFINAE perspective if a template-id
6097 using that alias can be ill-formed when the expansion is not, as with
6098 the void_t template. We determine this by checking whether the
6099 expansion for the alias template uses all its template parameters. */
6101 struct uses_all_template_parms_data
6108 uses_all_template_parms_r (tree t
, void *data_
)
6110 struct uses_all_template_parms_data
&data
6111 = *(struct uses_all_template_parms_data
*)data_
;
6112 tree idx
= get_template_parm_index (t
);
6114 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6115 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6120 complex_alias_template_p (const_tree tmpl
)
6122 struct uses_all_template_parms_data data
;
6123 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6124 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6125 data
.level
= TMPL_PARMS_DEPTH (parms
);
6126 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6127 data
.seen
= XALLOCAVEC (bool, len
);
6128 for (int i
= 0; i
< len
; ++i
)
6129 data
.seen
[i
] = false;
6131 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6132 for (int i
= 0; i
< len
; ++i
)
6138 /* Return TRUE iff T is a specialization of a complex alias template with
6139 dependent template-arguments. */
6142 dependent_alias_template_spec_p (const_tree t
)
6144 if (!alias_template_specialization_p (t
))
6147 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6148 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6151 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6152 if (!any_dependent_template_arguments_p (args
))
6158 /* Return the number of innermost template parameters in TMPL. */
6161 num_innermost_template_parms (tree tmpl
)
6163 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6164 return TREE_VEC_LENGTH (parms
);
6167 /* Return either TMPL or another template that it is equivalent to under DR
6168 1286: An alias that just changes the name of a template is equivalent to
6169 the other template. */
6172 get_underlying_template (tree tmpl
)
6174 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6175 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6177 /* Determine if the alias is equivalent to an underlying template. */
6178 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6179 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6183 tree underlying
= TI_TEMPLATE (tinfo
);
6184 if (!PRIMARY_TEMPLATE_P (underlying
)
6185 || (num_innermost_template_parms (tmpl
)
6186 != num_innermost_template_parms (underlying
)))
6189 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6190 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6191 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6194 /* Alias is equivalent. Strip it and repeat. */
6201 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6202 must be a reference-to-function or a pointer-to-function type, as specified
6203 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6204 and check that the resulting function has external linkage. */
6207 convert_nontype_argument_function (tree type
, tree expr
,
6208 tsubst_flags_t complain
)
6212 linkage_kind linkage
;
6214 fn
= instantiate_type (type
, fns
, tf_none
);
6215 if (fn
== error_mark_node
)
6216 return error_mark_node
;
6218 if (value_dependent_expression_p (fn
))
6221 fn_no_ptr
= strip_fnptr_conv (fn
);
6222 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6223 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6224 if (BASELINK_P (fn_no_ptr
))
6225 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6227 /* [temp.arg.nontype]/1
6229 A template-argument for a non-type, non-template template-parameter
6232 -- the address of an object or function with external [C++11: or
6233 internal] linkage. */
6235 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6237 if (complain
& tf_error
)
6239 error ("%qE is not a valid template argument for type %qT",
6241 if (TYPE_PTR_P (type
))
6242 inform (input_location
, "it must be the address of a function "
6243 "with external linkage");
6245 inform (input_location
, "it must be the name of a function with "
6246 "external linkage");
6251 linkage
= decl_linkage (fn_no_ptr
);
6252 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6254 if (complain
& tf_error
)
6256 if (cxx_dialect
>= cxx11
)
6257 error ("%qE is not a valid template argument for type %qT "
6258 "because %qD has no linkage",
6259 expr
, type
, fn_no_ptr
);
6261 error ("%qE is not a valid template argument for type %qT "
6262 "because %qD does not have external linkage",
6263 expr
, type
, fn_no_ptr
);
6269 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6271 if (REFERENCE_REF_P (fn
))
6272 fn
= TREE_OPERAND (fn
, 0);
6274 fn
= build_address (fn
);
6276 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6277 fn
= build_nop (type
, fn
);
6282 /* Subroutine of convert_nontype_argument.
6283 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6284 Emit an error otherwise. */
6287 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6288 tsubst_flags_t complain
)
6290 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6291 tree orig_expr
= expr
;
6293 if (null_ptr_cst_p (expr
))
6295 if (TREE_CODE (expr
) == PTRMEM_CST
6296 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6297 PTRMEM_CST_CLASS (expr
)))
6299 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6301 if (processing_template_decl
6302 && TREE_CODE (expr
) == ADDR_EXPR
6303 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6305 if (complain
& tf_error
)
6307 error_at (loc
, "%qE is not a valid template argument for type %qT",
6309 if (TREE_CODE (expr
) != PTRMEM_CST
)
6310 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6312 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6317 /* Returns TRUE iff the address of OP is value-dependent.
6319 14.6.2.4 [temp.dep.temp]:
6320 A non-integral non-type template-argument is dependent if its type is
6321 dependent or it has either of the following forms
6324 and contains a nested-name-specifier which specifies a class-name that
6325 names a dependent type.
6327 We generalize this to just say that the address of a member of a
6328 dependent class is value-dependent; the above doesn't cover the
6329 address of a static data member named with an unqualified-id. */
6332 has_value_dependent_address (tree op
)
6334 /* We could use get_inner_reference here, but there's no need;
6335 this is only relevant for template non-type arguments, which
6336 can only be expressed as &id-expression. */
6339 tree ctx
= CP_DECL_CONTEXT (op
);
6340 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6347 /* The next set of functions are used for providing helpful explanatory
6348 diagnostics for failed overload resolution. Their messages should be
6349 indented by two spaces for consistency with the messages in
6353 unify_success (bool /*explain_p*/)
6358 /* Other failure functions should call this one, to provide a single function
6359 for setting a breakpoint on. */
6362 unify_invalid (bool /*explain_p*/)
6368 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6371 inform (input_location
,
6372 " couldn't deduce template parameter %qD", parm
);
6373 return unify_invalid (explain_p
);
6377 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6380 inform (input_location
,
6381 " types %qT and %qT have incompatible cv-qualifiers",
6383 return unify_invalid (explain_p
);
6387 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6390 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6391 return unify_invalid (explain_p
);
6395 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6398 inform (input_location
,
6399 " template parameter %qD is not a parameter pack, but "
6402 return unify_invalid (explain_p
);
6406 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6409 inform (input_location
,
6410 " template argument %qE does not match "
6411 "pointer-to-member constant %qE",
6413 return unify_invalid (explain_p
);
6417 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6420 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6421 return unify_invalid (explain_p
);
6425 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6428 inform (input_location
,
6429 " inconsistent parameter pack deduction with %qT and %qT",
6431 return unify_invalid (explain_p
);
6435 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6440 inform (input_location
,
6441 " deduced conflicting types for parameter %qT (%qT and %qT)",
6442 parm
, first
, second
);
6444 inform (input_location
,
6445 " deduced conflicting values for non-type parameter "
6446 "%qE (%qE and %qE)", parm
, first
, second
);
6448 return unify_invalid (explain_p
);
6452 unify_vla_arg (bool explain_p
, tree arg
)
6455 inform (input_location
,
6456 " variable-sized array type %qT is not "
6457 "a valid template argument",
6459 return unify_invalid (explain_p
);
6463 unify_method_type_error (bool explain_p
, tree arg
)
6466 inform (input_location
,
6467 " member function type %qT is not a valid template argument",
6469 return unify_invalid (explain_p
);
6473 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6478 inform_n (input_location
, wanted
,
6479 " candidate expects at least %d argument, %d provided",
6480 " candidate expects at least %d arguments, %d provided",
6483 inform_n (input_location
, wanted
,
6484 " candidate expects %d argument, %d provided",
6485 " candidate expects %d arguments, %d provided",
6488 return unify_invalid (explain_p
);
6492 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6494 return unify_arity (explain_p
, have
, wanted
);
6498 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6499 bool least_p
= false)
6501 return unify_arity (explain_p
, have
, wanted
, least_p
);
6505 unify_arg_conversion (bool explain_p
, tree to_type
,
6506 tree from_type
, tree arg
)
6509 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6510 " cannot convert %qE (type %qT) to type %qT",
6511 arg
, from_type
, to_type
);
6512 return unify_invalid (explain_p
);
6516 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6517 tree parm
, tree arg
)
6522 case tbr_ambiguous_baseclass
:
6523 inform (input_location
, " %qT is an ambiguous base class of %qT",
6527 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6530 return unify_invalid (explain_p
);
6534 unify_inconsistent_template_template_parameters (bool explain_p
)
6537 inform (input_location
,
6538 " template parameters of a template template argument are "
6539 "inconsistent with other deduced template arguments");
6540 return unify_invalid (explain_p
);
6544 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6547 inform (input_location
,
6548 " can't deduce a template for %qT from non-template type %qT",
6550 return unify_invalid (explain_p
);
6554 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6557 inform (input_location
,
6558 " template argument %qE does not match %qE", arg
, parm
);
6559 return unify_invalid (explain_p
);
6562 /* Attempt to convert the non-type template parameter EXPR to the
6563 indicated TYPE. If the conversion is successful, return the
6564 converted value. If the conversion is unsuccessful, return
6565 NULL_TREE if we issued an error message, or error_mark_node if we
6566 did not. We issue error messages for out-and-out bad template
6567 parameters, but not simply because the conversion failed, since we
6568 might be just trying to do argument deduction. Both TYPE and EXPR
6569 must be non-dependent.
6571 The conversion follows the special rules described in
6572 [temp.arg.nontype], and it is much more strict than an implicit
6575 This function is called twice for each template argument (see
6576 lookup_template_class for a more accurate description of this
6577 problem). This means that we need to handle expressions which
6578 are not valid in a C++ source, but can be created from the
6579 first call (for instance, casts to perform conversions). These
6580 hacks can go away after we fix the double coercion problem. */
6583 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6586 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6587 tree orig_expr
= expr
;
6589 /* Detect immediately string literals as invalid non-type argument.
6590 This special-case is not needed for correctness (we would easily
6591 catch this later), but only to provide better diagnostic for this
6592 common user mistake. As suggested by DR 100, we do not mention
6593 linkage issues in the diagnostic as this is not the point. */
6594 /* FIXME we're making this OK. */
6595 if (TREE_CODE (expr
) == STRING_CST
)
6597 if (complain
& tf_error
)
6598 error ("%qE is not a valid template argument for type %qT "
6599 "because string literals can never be used in this context",
6604 /* Add the ADDR_EXPR now for the benefit of
6605 value_dependent_expression_p. */
6606 if (TYPE_PTROBV_P (type
)
6607 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6609 expr
= decay_conversion (expr
, complain
);
6610 if (expr
== error_mark_node
)
6611 return error_mark_node
;
6614 /* If we are in a template, EXPR may be non-dependent, but still
6615 have a syntactic, rather than semantic, form. For example, EXPR
6616 might be a SCOPE_REF, rather than the VAR_DECL to which the
6617 SCOPE_REF refers. Preserving the qualifying scope is necessary
6618 so that access checking can be performed when the template is
6619 instantiated -- but here we need the resolved form so that we can
6620 convert the argument. */
6621 bool non_dep
= false;
6622 if (TYPE_REF_OBJ_P (type
)
6623 && has_value_dependent_address (expr
))
6624 /* If we want the address and it's value-dependent, don't fold. */;
6625 else if (processing_template_decl
6626 && is_nondependent_constant_expression (expr
))
6628 if (error_operand_p (expr
))
6629 return error_mark_node
;
6630 expr_type
= TREE_TYPE (expr
);
6632 /* If the argument is non-dependent, perform any conversions in
6633 non-dependent context as well. */
6634 processing_template_decl_sentinel
s (non_dep
);
6636 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6638 if (value_dependent_expression_p (expr
))
6639 expr
= canonicalize_expr_argument (expr
, complain
);
6641 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6642 to a non-type argument of "nullptr". */
6643 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6644 expr
= fold_simple (convert (type
, expr
));
6646 /* In C++11, integral or enumeration non-type template arguments can be
6647 arbitrary constant expressions. Pointer and pointer to
6648 member arguments can be general constant expressions that evaluate
6649 to a null value, but otherwise still need to be of a specific form. */
6650 if (cxx_dialect
>= cxx11
)
6652 if (TREE_CODE (expr
) == PTRMEM_CST
)
6653 /* A PTRMEM_CST is already constant, and a valid template
6654 argument for a parameter of pointer to member type, we just want
6655 to leave it in that form rather than lower it to a
6657 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6658 || cxx_dialect
>= cxx17
)
6660 /* C++17: A template-argument for a non-type template-parameter shall
6661 be a converted constant expression (8.20) of the type of the
6662 template-parameter. */
6663 expr
= build_converted_constant_expr (type
, expr
, complain
);
6664 if (expr
== error_mark_node
)
6665 return error_mark_node
;
6666 expr
= maybe_constant_value (expr
);
6667 expr
= convert_from_reference (expr
);
6669 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6671 tree folded
= maybe_constant_value (expr
);
6672 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6673 : null_member_pointer_value_p (folded
))
6678 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6679 expr
= mark_lvalue_use (expr
);
6681 expr
= mark_rvalue_use (expr
);
6683 /* HACK: Due to double coercion, we can get a
6684 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6685 which is the tree that we built on the first call (see
6686 below when coercing to reference to object or to reference to
6687 function). We just strip everything and get to the arg.
6688 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6690 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6692 tree probe_type
, probe
= expr
;
6693 if (REFERENCE_REF_P (probe
))
6694 probe
= TREE_OPERAND (probe
, 0);
6695 probe_type
= TREE_TYPE (probe
);
6696 if (TREE_CODE (probe
) == NOP_EXPR
)
6698 /* ??? Maybe we could use convert_from_reference here, but we
6699 would need to relax its constraints because the NOP_EXPR
6700 could actually change the type to something more cv-qualified,
6701 and this is not folded by convert_from_reference. */
6702 tree addr
= TREE_OPERAND (probe
, 0);
6703 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6704 && TREE_CODE (addr
) == ADDR_EXPR
6705 && TYPE_PTR_P (TREE_TYPE (addr
))
6706 && (same_type_ignoring_top_level_qualifiers_p
6707 (TREE_TYPE (probe_type
),
6708 TREE_TYPE (TREE_TYPE (addr
)))))
6710 expr
= TREE_OPERAND (addr
, 0);
6711 expr_type
= TREE_TYPE (probe_type
);
6716 /* [temp.arg.nontype]/5, bullet 1
6718 For a non-type template-parameter of integral or enumeration type,
6719 integral promotions (_conv.prom_) and integral conversions
6720 (_conv.integral_) are applied. */
6721 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6723 if (cxx_dialect
< cxx11
)
6725 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6726 t
= maybe_constant_value (t
);
6727 if (t
!= error_mark_node
)
6731 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6732 return error_mark_node
;
6734 /* Notice that there are constant expressions like '4 % 0' which
6735 do not fold into integer constants. */
6736 if (TREE_CODE (expr
) != INTEGER_CST
6737 && !value_dependent_expression_p (expr
))
6739 if (complain
& tf_error
)
6741 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6742 if (!require_potential_constant_expression (expr
))
6743 expr
= error_mark_node
;
6745 expr
= cxx_constant_value (expr
);
6746 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6747 inform (loc
, "in template argument for type %qT ", type
);
6748 if (expr
== error_mark_node
)
6750 /* else cxx_constant_value complained but gave us
6751 a real constant, so go ahead. */
6752 if (TREE_CODE (expr
) != INTEGER_CST
)
6754 /* Some assemble time constant expressions like
6755 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6756 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6757 as we can emit them into .rodata initializers of
6758 variables, yet they can't fold into an INTEGER_CST at
6759 compile time. Refuse them here. */
6760 gcc_checking_assert (reduced_constant_expression_p (expr
));
6761 error_at (loc
, "template argument %qE for type %qT not "
6762 "a constant integer", expr
, type
);
6770 /* Avoid typedef problems. */
6771 if (TREE_TYPE (expr
) != type
)
6772 expr
= fold_convert (type
, expr
);
6774 /* [temp.arg.nontype]/5, bullet 2
6776 For a non-type template-parameter of type pointer to object,
6777 qualification conversions (_conv.qual_) and the array-to-pointer
6778 conversion (_conv.array_) are applied. */
6779 else if (TYPE_PTROBV_P (type
))
6781 tree decayed
= expr
;
6783 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6784 decay_conversion or an explicit cast. If it's a problematic cast,
6785 we'll complain about it below. */
6786 if (TREE_CODE (expr
) == NOP_EXPR
)
6790 if (TREE_CODE (probe
) == ADDR_EXPR
6791 && TYPE_PTR_P (TREE_TYPE (probe
)))
6794 expr_type
= TREE_TYPE (expr
);
6798 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6800 A template-argument for a non-type, non-template template-parameter
6801 shall be one of: [...]
6803 -- the name of a non-type template-parameter;
6804 -- the address of an object or function with external linkage, [...]
6805 expressed as "& id-expression" where the & is optional if the name
6806 refers to a function or array, or if the corresponding
6807 template-parameter is a reference.
6809 Here, we do not care about functions, as they are invalid anyway
6810 for a parameter of type pointer-to-object. */
6812 if (value_dependent_expression_p (expr
))
6813 /* Non-type template parameters are OK. */
6815 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6816 /* Null pointer values are OK in C++11. */;
6817 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6821 if (complain
& tf_error
)
6822 error ("%qD is not a valid template argument "
6823 "because %qD is a variable, not the address of "
6824 "a variable", orig_expr
, expr
);
6827 if (POINTER_TYPE_P (expr_type
))
6829 if (complain
& tf_error
)
6830 error ("%qE is not a valid template argument for %qT "
6831 "because it is not the address of a variable",
6835 /* Other values, like integer constants, might be valid
6836 non-type arguments of some other type. */
6837 return error_mark_node
;
6841 tree decl
= TREE_OPERAND (expr
, 0);
6845 if (complain
& tf_error
)
6846 error ("%qE is not a valid template argument of type %qT "
6847 "because %qE is not a variable", orig_expr
, type
, decl
);
6850 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6852 if (complain
& tf_error
)
6853 error ("%qE is not a valid template argument of type %qT "
6854 "because %qD does not have external linkage",
6855 orig_expr
, type
, decl
);
6858 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6859 && decl_linkage (decl
) == lk_none
)
6861 if (complain
& tf_error
)
6862 error ("%qE is not a valid template argument of type %qT "
6863 "because %qD has no linkage", orig_expr
, type
, decl
);
6866 /* C++17: For a non-type template-parameter of reference or pointer
6867 type, the value of the constant expression shall not refer to (or
6868 for a pointer type, shall not be the address of):
6869 * a subobject (4.5),
6870 * a temporary object (15.2),
6871 * a string literal (5.13.5),
6872 * the result of a typeid expression (8.2.8), or
6873 * a predefined __func__ variable (11.4.1). */
6874 else if (DECL_ARTIFICIAL (decl
))
6876 if (complain
& tf_error
)
6877 error ("the address of %qD is not a valid template argument",
6881 else if (!same_type_ignoring_top_level_qualifiers_p
6882 (strip_array_types (TREE_TYPE (type
)),
6883 strip_array_types (TREE_TYPE (decl
))))
6885 if (complain
& tf_error
)
6886 error ("the address of the %qT subobject of %qD is not a "
6887 "valid template argument", TREE_TYPE (type
), decl
);
6890 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6892 if (complain
& tf_error
)
6893 error ("the address of %qD is not a valid template argument "
6894 "because it does not have static storage duration",
6902 expr
= perform_qualification_conversions (type
, expr
);
6903 if (expr
== error_mark_node
)
6904 return error_mark_node
;
6906 /* [temp.arg.nontype]/5, bullet 3
6908 For a non-type template-parameter of type reference to object, no
6909 conversions apply. The type referred to by the reference may be more
6910 cv-qualified than the (otherwise identical) type of the
6911 template-argument. The template-parameter is bound directly to the
6912 template-argument, which must be an lvalue. */
6913 else if (TYPE_REF_OBJ_P (type
))
6915 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6917 return error_mark_node
;
6919 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6921 if (complain
& tf_error
)
6922 error ("%qE is not a valid template argument for type %qT "
6923 "because of conflicts in cv-qualification", expr
, type
);
6927 if (!lvalue_p (expr
))
6929 if (complain
& tf_error
)
6930 error ("%qE is not a valid template argument for type %qT "
6931 "because it is not an lvalue", expr
, type
);
6935 /* [temp.arg.nontype]/1
6937 A template-argument for a non-type, non-template template-parameter
6938 shall be one of: [...]
6940 -- the address of an object or function with external linkage. */
6941 if (INDIRECT_REF_P (expr
)
6942 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6944 expr
= TREE_OPERAND (expr
, 0);
6947 if (complain
& tf_error
)
6948 error ("%q#D is not a valid template argument for type %qT "
6949 "because a reference variable does not have a constant "
6950 "address", expr
, type
);
6955 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6956 && value_dependent_expression_p (expr
))
6957 /* OK, dependent reference. We don't want to ask whether a DECL is
6958 itself value-dependent, since what we want here is its address. */;
6963 if (complain
& tf_error
)
6964 error ("%qE is not a valid template argument for type %qT "
6965 "because it is not an object with linkage",
6970 /* DR 1155 allows internal linkage in C++11 and up. */
6971 linkage_kind linkage
= decl_linkage (expr
);
6972 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6974 if (complain
& tf_error
)
6975 error ("%qE is not a valid template argument for type %qT "
6976 "because object %qD does not have linkage",
6981 expr
= build_address (expr
);
6984 if (!same_type_p (type
, TREE_TYPE (expr
)))
6985 expr
= build_nop (type
, expr
);
6987 /* [temp.arg.nontype]/5, bullet 4
6989 For a non-type template-parameter of type pointer to function, only
6990 the function-to-pointer conversion (_conv.func_) is applied. If the
6991 template-argument represents a set of overloaded functions (or a
6992 pointer to such), the matching function is selected from the set
6994 else if (TYPE_PTRFN_P (type
))
6996 /* If the argument is a template-id, we might not have enough
6997 context information to decay the pointer. */
6998 if (!type_unknown_p (expr_type
))
7000 expr
= decay_conversion (expr
, complain
);
7001 if (expr
== error_mark_node
)
7002 return error_mark_node
;
7005 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7006 /* Null pointer values are OK in C++11. */
7007 return perform_qualification_conversions (type
, expr
);
7009 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7010 if (!expr
|| expr
== error_mark_node
)
7013 /* [temp.arg.nontype]/5, bullet 5
7015 For a non-type template-parameter of type reference to function, no
7016 conversions apply. If the template-argument represents a set of
7017 overloaded functions, the matching function is selected from the set
7019 else if (TYPE_REFFN_P (type
))
7021 if (TREE_CODE (expr
) == ADDR_EXPR
)
7023 if (complain
& tf_error
)
7025 error ("%qE is not a valid template argument for type %qT "
7026 "because it is a pointer", expr
, type
);
7027 inform (input_location
, "try using %qE instead",
7028 TREE_OPERAND (expr
, 0));
7033 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7034 if (!expr
|| expr
== error_mark_node
)
7037 /* [temp.arg.nontype]/5, bullet 6
7039 For a non-type template-parameter of type pointer to member function,
7040 no conversions apply. If the template-argument represents a set of
7041 overloaded member functions, the matching member function is selected
7042 from the set (_over.over_). */
7043 else if (TYPE_PTRMEMFUNC_P (type
))
7045 expr
= instantiate_type (type
, expr
, tf_none
);
7046 if (expr
== error_mark_node
)
7047 return error_mark_node
;
7049 /* [temp.arg.nontype] bullet 1 says the pointer to member
7050 expression must be a pointer-to-member constant. */
7051 if (!value_dependent_expression_p (expr
)
7052 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7055 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7056 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7057 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7058 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7060 /* [temp.arg.nontype]/5, bullet 7
7062 For a non-type template-parameter of type pointer to data member,
7063 qualification conversions (_conv.qual_) are applied. */
7064 else if (TYPE_PTRDATAMEM_P (type
))
7066 /* [temp.arg.nontype] bullet 1 says the pointer to member
7067 expression must be a pointer-to-member constant. */
7068 if (!value_dependent_expression_p (expr
)
7069 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7072 expr
= perform_qualification_conversions (type
, expr
);
7073 if (expr
== error_mark_node
)
7076 else if (NULLPTR_TYPE_P (type
))
7078 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7080 if (complain
& tf_error
)
7081 error ("%qE is not a valid template argument for type %qT "
7082 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7087 /* A template non-type parameter must be one of the above. */
7091 /* Sanity check: did we actually convert the argument to the
7093 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7094 (type
, TREE_TYPE (expr
)));
7095 return convert_from_reference (expr
);
7098 /* Subroutine of coerce_template_template_parms, which returns 1 if
7099 PARM_PARM and ARG_PARM match using the rule for the template
7100 parameters of template template parameters. Both PARM and ARG are
7101 template parameters; the rest of the arguments are the same as for
7102 coerce_template_template_parms.
7105 coerce_template_template_parm (tree parm
,
7107 tsubst_flags_t complain
,
7111 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7112 || parm
== NULL_TREE
|| error_operand_p (parm
))
7115 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7118 switch (TREE_CODE (parm
))
7121 /* We encounter instantiations of templates like
7122 template <template <template <class> class> class TT>
7125 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7126 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7128 if (!coerce_template_template_parms
7129 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7135 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7136 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7137 /* Argument is a parameter pack but parameter is not. */
7142 /* The tsubst call is used to handle cases such as
7144 template <int> class C {};
7145 template <class T, template <T> class TT> class D {};
7148 i.e. the parameter list of TT depends on earlier parameters. */
7149 if (!uses_template_parms (TREE_TYPE (arg
)))
7151 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7152 if (!uses_template_parms (t
)
7153 && !same_type_p (t
, TREE_TYPE (arg
)))
7157 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7158 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7159 /* Argument is a parameter pack but parameter is not. */
7171 /* Coerce template argument list ARGLIST for use with template
7172 template-parameter TEMPL. */
7175 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7176 tsubst_flags_t complain
)
7178 /* Consider an example where a template template parameter declared as
7180 template <class T, class U = std::allocator<T> > class TT
7182 The template parameter level of T and U are one level larger than
7183 of TT. To proper process the default argument of U, say when an
7184 instantiation `TT<int>' is seen, we need to build the full
7185 arguments containing {int} as the innermost level. Outer levels,
7186 available when not appearing as default template argument, can be
7187 obtained from the arguments of the enclosing template.
7189 Suppose that TT is later substituted with std::vector. The above
7190 instantiation is `TT<int, std::allocator<T> >' with TT at
7191 level 1, and T at level 2, while the template arguments at level 1
7192 becomes {std::vector} and the inner level 2 is {int}. */
7194 tree outer
= DECL_CONTEXT (templ
);
7197 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7198 /* We want arguments for the partial specialization, not arguments for
7199 the primary template. */
7200 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7202 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7204 else if (current_template_parms
)
7206 /* This is an argument of the current template, so we haven't set
7207 DECL_CONTEXT yet. */
7208 tree relevant_template_parms
;
7210 /* Parameter levels that are greater than the level of the given
7211 template template parm are irrelevant. */
7212 relevant_template_parms
= current_template_parms
;
7213 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7214 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7215 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7217 outer
= template_parms_to_args (relevant_template_parms
);
7221 arglist
= add_to_template_args (outer
, arglist
);
7223 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7224 return coerce_template_parms (parmlist
, arglist
, templ
,
7226 /*require_all_args=*/true,
7227 /*use_default_args=*/true);
7230 /* A cache of template template parameters with match-all default
7232 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7234 store_defaulted_ttp (tree v
, tree t
)
7236 if (!defaulted_ttp_cache
)
7237 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7238 defaulted_ttp_cache
->put (v
, t
);
7241 lookup_defaulted_ttp (tree v
)
7243 if (defaulted_ttp_cache
)
7244 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7249 /* T is a bound template template-parameter. Copy its arguments into default
7250 arguments of the template template-parameter's template parameters. */
7253 add_defaults_to_ttp (tree otmpl
)
7255 if (tree c
= lookup_defaulted_ttp (otmpl
))
7258 tree ntmpl
= copy_node (otmpl
);
7260 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7261 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7262 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7263 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7264 TYPE_NAME (ntype
) = ntmpl
;
7265 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7267 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7268 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7269 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7270 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7272 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7273 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7274 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7275 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7276 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7278 tree o
= TREE_VEC_ELT (vec
, i
);
7279 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7281 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7282 TREE_PURPOSE (n
) = any_targ_node
;
7286 store_defaulted_ttp (otmpl
, ntmpl
);
7290 /* ARG is a bound potential template template-argument, and PARGS is a list
7291 of arguments for the corresponding template template-parameter. Adjust
7292 PARGS as appropriate for application to ARG's template, and if ARG is a
7293 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7294 arguments to the template template parameter. */
7297 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7299 ++processing_template_decl
;
7300 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7301 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7303 /* When comparing two template template-parameters in partial ordering,
7304 rewrite the one currently being used as an argument to have default
7305 arguments for all parameters. */
7306 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7307 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7308 if (pargs
!= error_mark_node
)
7309 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7310 TYPE_TI_ARGS (arg
));
7315 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7316 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7317 /*require_all*/true,
7318 /*use_default*/true);
7320 --processing_template_decl
;
7324 /* Subroutine of unify for the case when PARM is a
7325 BOUND_TEMPLATE_TEMPLATE_PARM. */
7328 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7331 tree parmvec
= TYPE_TI_ARGS (parm
);
7332 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7334 /* The template template parm might be variadic and the argument
7335 not, so flatten both argument lists. */
7336 parmvec
= expand_template_argument_pack (parmvec
);
7337 argvec
= expand_template_argument_pack (argvec
);
7341 /* In keeping with P0522R0, adjust P's template arguments
7342 to apply to A's template; then flatten it again. */
7343 tree nparmvec
= parmvec
;
7344 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7345 nparmvec
= expand_template_argument_pack (nparmvec
);
7347 if (unify (tparms
, targs
, nparmvec
, argvec
,
7348 UNIFY_ALLOW_NONE
, explain_p
))
7351 /* If the P0522 adjustment eliminated a pack expansion, deduce
7354 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7355 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7356 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7361 /* Deduce arguments T, i from TT<T> or TT<i>.
7362 We check each element of PARMVEC and ARGVEC individually
7363 rather than the whole TREE_VEC since they can have
7364 different number of elements, which is allowed under N2555. */
7366 int len
= TREE_VEC_LENGTH (parmvec
);
7368 /* Check if the parameters end in a pack, making them
7370 int parm_variadic_p
= 0;
7372 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7373 parm_variadic_p
= 1;
7375 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7376 /* If the template argument list of P contains a pack
7377 expansion that is not the last template argument, the
7378 entire template argument list is a non-deduced
7380 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7381 return unify_success (explain_p
);
7383 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7384 return unify_too_few_arguments (explain_p
,
7385 TREE_VEC_LENGTH (argvec
), len
);
7387 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7388 if (unify (tparms
, targs
,
7389 TREE_VEC_ELT (parmvec
, i
),
7390 TREE_VEC_ELT (argvec
, i
),
7391 UNIFY_ALLOW_NONE
, explain_p
))
7395 && unify_pack_expansion (tparms
, targs
,
7398 /*subr=*/true, explain_p
))
7405 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7406 template template parameters. Both PARM_PARMS and ARG_PARMS are
7407 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7410 Consider the example:
7411 template <class T> class A;
7412 template<template <class U> class TT> class B;
7414 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7415 the parameters to A, and OUTER_ARGS contains A. */
7418 coerce_template_template_parms (tree parm_parms
,
7420 tsubst_flags_t complain
,
7424 int nparms
, nargs
, i
;
7428 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7429 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7431 nparms
= TREE_VEC_LENGTH (parm_parms
);
7432 nargs
= TREE_VEC_LENGTH (arg_parms
);
7436 /* P0522R0: A template template-parameter P is at least as specialized as
7437 a template template-argument A if, given the following rewrite to two
7438 function templates, the function template corresponding to P is at
7439 least as specialized as the function template corresponding to A
7440 according to the partial ordering rules for function templates
7441 ([temp.func.order]). Given an invented class template X with the
7442 template parameter list of A (including default arguments):
7444 * Each of the two function templates has the same template parameters,
7445 respectively, as P or A.
7447 * Each function template has a single function parameter whose type is
7448 a specialization of X with template arguments corresponding to the
7449 template parameters from the respective function template where, for
7450 each template parameter PP in the template parameter list of the
7451 function template, a corresponding template argument AA is formed. If
7452 PP declares a parameter pack, then AA is the pack expansion
7453 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7455 If the rewrite produces an invalid type, then P is not at least as
7456 specialized as A. */
7458 /* So coerce P's args to apply to A's parms, and then deduce between A's
7459 args and the converted args. If that succeeds, A is at least as
7460 specialized as P, so they match.*/
7461 tree pargs
= template_parms_level_to_args (parm_parms
);
7462 ++processing_template_decl
;
7463 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7464 /*require_all*/true, /*use_default*/true);
7465 --processing_template_decl
;
7466 if (pargs
!= error_mark_node
)
7468 tree targs
= make_tree_vec (nargs
);
7469 tree aargs
= template_parms_level_to_args (arg_parms
);
7470 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7476 /* Determine whether we have a parameter pack at the end of the
7477 template template parameter's template parameter list. */
7478 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7480 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7482 if (error_operand_p (parm
))
7485 switch (TREE_CODE (parm
))
7489 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7494 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7504 && !(variadic_p
&& nargs
>= nparms
- 1))
7507 /* Check all of the template parameters except the parameter pack at
7508 the end (if any). */
7509 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7511 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7512 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7515 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7516 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7518 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7526 /* Check each of the template parameters in the template
7527 argument against the template parameter pack at the end of
7528 the template template parameter. */
7529 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7532 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7534 for (; i
< nargs
; ++i
)
7536 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7539 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7541 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7550 /* Verifies that the deduced template arguments (in TARGS) for the
7551 template template parameters (in TPARMS) represent valid bindings,
7552 by comparing the template parameter list of each template argument
7553 to the template parameter list of its corresponding template
7554 template parameter, in accordance with DR150. This
7555 routine can only be called after all template arguments have been
7556 deduced. It will return TRUE if all of the template template
7557 parameter bindings are okay, FALSE otherwise. */
7559 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7561 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7564 /* We're dealing with template parms in this process. */
7565 ++processing_template_decl
;
7567 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7569 for (i
= 0; i
< ntparms
; ++i
)
7571 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7572 tree targ
= TREE_VEC_ELT (targs
, i
);
7574 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7576 tree packed_args
= NULL_TREE
;
7579 if (ARGUMENT_PACK_P (targ
))
7581 /* Look inside the argument pack. */
7582 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7583 len
= TREE_VEC_LENGTH (packed_args
);
7586 for (idx
= 0; idx
< len
; ++idx
)
7588 tree targ_parms
= NULL_TREE
;
7591 /* Extract the next argument from the argument
7593 targ
= TREE_VEC_ELT (packed_args
, idx
);
7595 if (PACK_EXPANSION_P (targ
))
7596 /* Look at the pattern of the pack expansion. */
7597 targ
= PACK_EXPANSION_PATTERN (targ
);
7599 /* Extract the template parameters from the template
7601 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7602 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7603 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7604 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7606 /* Verify that we can coerce the template template
7607 parameters from the template argument to the template
7608 parameter. This requires an exact match. */
7610 && !coerce_template_template_parms
7611 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7626 --processing_template_decl
;
7630 /* Since type attributes aren't mangled, we need to strip them from
7631 template type arguments. */
7634 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7636 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7638 bool removed_attributes
= false;
7639 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7640 if (removed_attributes
7641 && (complain
& tf_warning
))
7642 warning (OPT_Wignored_attributes
,
7643 "ignoring attributes on template argument %qT", arg
);
7647 /* And from inside dependent non-type arguments like sizeof(Type). */
7650 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7652 if (!arg
|| arg
== error_mark_node
)
7654 bool removed_attributes
= false;
7655 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7656 if (removed_attributes
7657 && (complain
& tf_warning
))
7658 warning (OPT_Wignored_attributes
,
7659 "ignoring attributes in template argument %qE", arg
);
7663 // A template declaration can be substituted for a constrained
7664 // template template parameter only when the argument is more
7665 // constrained than the parameter.
7667 is_compatible_template_arg (tree parm
, tree arg
)
7669 tree parm_cons
= get_constraints (parm
);
7671 /* For now, allow constrained template template arguments
7672 and unconstrained template template parameters. */
7673 if (parm_cons
== NULL_TREE
)
7676 tree arg_cons
= get_constraints (arg
);
7678 // If the template parameter is constrained, we need to rewrite its
7679 // constraints in terms of the ARG's template parameters. This ensures
7680 // that all of the template parameter types will have the same depth.
7682 // Note that this is only valid when coerce_template_template_parm is
7683 // true for the innermost template parameters of PARM and ARG. In other
7684 // words, because coercion is successful, this conversion will be valid.
7687 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7688 parm_cons
= tsubst_constraint_info (parm_cons
,
7689 INNERMOST_TEMPLATE_ARGS (args
),
7690 tf_none
, NULL_TREE
);
7691 if (parm_cons
== error_mark_node
)
7695 return subsumes (parm_cons
, arg_cons
);
7698 // Convert a placeholder argument into a binding to the original
7699 // parameter. The original parameter is saved as the TREE_TYPE of
7702 convert_wildcard_argument (tree parm
, tree arg
)
7704 TREE_TYPE (arg
) = parm
;
7708 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7709 because one of them is dependent. But we need to represent the
7710 conversion for the benefit of cp_tree_equal. */
7713 maybe_convert_nontype_argument (tree type
, tree arg
)
7715 /* Auto parms get no conversion. */
7716 if (type_uses_auto (type
))
7718 /* We don't need or want to add this conversion now if we're going to use the
7719 argument for deduction. */
7720 if (value_dependent_expression_p (arg
))
7723 type
= cv_unqualified (type
);
7724 tree argtype
= TREE_TYPE (arg
);
7725 if (same_type_p (type
, argtype
))
7728 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7729 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7733 /* Convert the indicated template ARG as necessary to match the
7734 indicated template PARM. Returns the converted ARG, or
7735 error_mark_node if the conversion was unsuccessful. Error and
7736 warning messages are issued under control of COMPLAIN. This
7737 conversion is for the Ith parameter in the parameter list. ARGS is
7738 the full set of template arguments deduced so far. */
7741 convert_template_argument (tree parm
,
7744 tsubst_flags_t complain
,
7750 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7752 if (parm
== error_mark_node
)
7753 return error_mark_node
;
7755 /* Trivially convert placeholders. */
7756 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7757 return convert_wildcard_argument (parm
, arg
);
7759 if (arg
== any_targ_node
)
7762 if (TREE_CODE (arg
) == TREE_LIST
7763 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7765 /* The template argument was the name of some
7766 member function. That's usually
7767 invalid, but static members are OK. In any
7768 case, grab the underlying fields/functions
7769 and issue an error later if required. */
7770 orig_arg
= TREE_VALUE (arg
);
7771 TREE_TYPE (arg
) = unknown_type_node
;
7776 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7777 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7778 || requires_tmpl_type
);
7780 /* When determining whether an argument pack expansion is a template,
7781 look at the pattern. */
7782 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7783 arg
= PACK_EXPANSION_PATTERN (arg
);
7785 /* Deal with an injected-class-name used as a template template arg. */
7786 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7788 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7789 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7791 if (cxx_dialect
>= cxx11
)
7792 /* OK under DR 1004. */;
7793 else if (complain
& tf_warning_or_error
)
7794 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7795 " used as template template argument", TYPE_NAME (arg
));
7796 else if (flag_pedantic_errors
)
7804 ((TREE_CODE (arg
) == TEMPLATE_DECL
7805 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7806 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7807 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7808 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7811 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7812 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7813 arg
= TYPE_STUB_DECL (arg
);
7815 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7817 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7818 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7820 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7822 if (complain
& tf_error
)
7823 error ("invalid use of destructor %qE as a type", orig_arg
);
7824 return error_mark_node
;
7827 permerror (input_location
,
7828 "to refer to a type member of a template parameter, "
7829 "use %<typename %E%>", orig_arg
);
7831 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7832 TREE_OPERAND (arg
, 1),
7838 if (is_type
!= requires_type
)
7842 if (complain
& tf_error
)
7844 error ("type/value mismatch at argument %d in template "
7845 "parameter list for %qD",
7848 inform (input_location
,
7849 " expected a constant of type %qT, got %qT",
7851 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7852 else if (requires_tmpl_type
)
7853 inform (input_location
,
7854 " expected a class template, got %qE", orig_arg
);
7856 inform (input_location
,
7857 " expected a type, got %qE", orig_arg
);
7860 return error_mark_node
;
7862 if (is_tmpl_type
^ requires_tmpl_type
)
7864 if (in_decl
&& (complain
& tf_error
))
7866 error ("type/value mismatch at argument %d in template "
7867 "parameter list for %qD",
7870 inform (input_location
,
7871 " expected a type, got %qT", DECL_NAME (arg
));
7873 inform (input_location
,
7874 " expected a class template, got %qT", orig_arg
);
7876 return error_mark_node
;
7879 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7880 /* We already did the appropriate conversion when packing args. */
7884 if (requires_tmpl_type
)
7886 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7887 /* The number of argument required is not known yet.
7888 Just accept it for now. */
7892 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7895 /* Strip alias templates that are equivalent to another
7897 arg
= get_underlying_template (arg
);
7898 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7900 if (coerce_template_template_parms (parmparm
, argparm
,
7906 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7908 if (val
!= error_mark_node
)
7910 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7911 val
= TREE_TYPE (val
);
7912 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7913 val
= make_pack_expansion (val
, complain
);
7918 if (in_decl
&& (complain
& tf_error
))
7920 error ("type/value mismatch at argument %d in "
7921 "template parameter list for %qD",
7923 inform (input_location
,
7924 " expected a template of type %qD, got %qT",
7928 val
= error_mark_node
;
7931 // Check that the constraints are compatible before allowing the
7933 if (val
!= error_mark_node
)
7934 if (!is_compatible_template_arg (parm
, arg
))
7936 if (in_decl
&& (complain
& tf_error
))
7938 error ("constraint mismatch at argument %d in "
7939 "template parameter list for %qD",
7941 inform (input_location
, " expected %qD but got %qD",
7944 val
= error_mark_node
;
7950 /* We only form one instance of each template specialization.
7951 Therefore, if we use a non-canonical variant (i.e., a
7952 typedef), any future messages referring to the type will use
7953 the typedef, which is confusing if those future uses do not
7954 themselves also use the typedef. */
7956 val
= canonicalize_type_argument (val
, complain
);
7960 tree t
= TREE_TYPE (parm
);
7962 if (tree a
= type_uses_auto (t
))
7964 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7965 if (t
== error_mark_node
)
7966 return error_mark_node
;
7969 t
= tsubst (t
, args
, complain
, in_decl
);
7971 if (invalid_nontype_parm_type_p (t
, complain
))
7972 return error_mark_node
;
7974 if (!type_dependent_expression_p (orig_arg
)
7975 && !uses_template_parms (t
))
7976 /* We used to call digest_init here. However, digest_init
7977 will report errors, which we don't want when complain
7978 is zero. More importantly, digest_init will try too
7979 hard to convert things: for example, `0' should not be
7980 converted to pointer type at this point according to
7981 the standard. Accepting this is not merely an
7982 extension, since deciding whether or not these
7983 conversions can occur is part of determining which
7984 function template to call, or whether a given explicit
7985 argument specification is valid. */
7986 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7989 val
= canonicalize_expr_argument (orig_arg
, complain
);
7990 val
= maybe_convert_nontype_argument (t
, val
);
7994 if (val
== NULL_TREE
)
7995 val
= error_mark_node
;
7996 else if (val
== error_mark_node
&& (complain
& tf_error
))
7997 error ("could not convert template argument %qE from %qT to %qT",
7998 orig_arg
, TREE_TYPE (orig_arg
), t
);
8000 if (INDIRECT_REF_P (val
))
8002 /* Reject template arguments that are references to built-in
8003 functions with no library fallbacks. */
8004 const_tree inner
= TREE_OPERAND (val
, 0);
8005 const_tree innertype
= TREE_TYPE (inner
);
8007 && TREE_CODE (innertype
) == REFERENCE_TYPE
8008 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8009 && TREE_OPERAND_LENGTH (inner
) > 0
8010 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8011 return error_mark_node
;
8014 if (TREE_CODE (val
) == SCOPE_REF
)
8016 /* Strip typedefs from the SCOPE_REF. */
8017 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8018 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8020 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8021 QUALIFIED_NAME_IS_TEMPLATE (val
));
8028 /* Coerces the remaining template arguments in INNER_ARGS (from
8029 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8030 Returns the coerced argument pack. PARM_IDX is the position of this
8031 parameter in the template parameter list. ARGS is the original
8032 template argument list. */
8034 coerce_template_parameter_pack (tree parms
,
8042 tsubst_flags_t complain
)
8044 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8045 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8048 tree packed_parms
= NULL_TREE
;
8050 if (arg_idx
> nargs
)
8053 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8055 /* When the template parameter is a non-type template parameter pack
8056 or template template parameter pack whose type or template
8057 parameters use parameter packs, we know exactly how many arguments
8058 we are looking for. Build a vector of the instantiated decls for
8059 these template parameters in PACKED_PARMS. */
8060 /* We can't use make_pack_expansion here because it would interpret a
8061 _DECL as a use rather than a declaration. */
8062 tree decl
= TREE_VALUE (parm
);
8063 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8064 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8065 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8066 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8068 TREE_VEC_LENGTH (args
)--;
8069 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8070 TREE_VEC_LENGTH (args
)++;
8072 if (packed_parms
== error_mark_node
)
8073 return error_mark_node
;
8075 /* If we're doing a partial instantiation of a member template,
8076 verify that all of the types used for the non-type
8077 template parameter pack are, in fact, valid for non-type
8078 template parameters. */
8080 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8082 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8083 for (j
= 0; j
< len
; ++j
)
8085 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8086 if (invalid_nontype_parm_type_p (t
, complain
))
8087 return error_mark_node
;
8089 /* We don't know how many args we have yet, just
8090 use the unconverted ones for now. */
8094 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8096 /* Check if we have a placeholder pack, which indicates we're
8097 in the context of a introduction list. In that case we want
8098 to match this pack to the single placeholder. */
8099 else if (arg_idx
< nargs
8100 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8101 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8103 nargs
= arg_idx
+ 1;
8104 packed_args
= make_tree_vec (1);
8107 packed_args
= make_tree_vec (nargs
- arg_idx
);
8109 /* Convert the remaining arguments, which will be a part of the
8110 parameter pack "parm". */
8111 int first_pack_arg
= arg_idx
;
8112 for (; arg_idx
< nargs
; ++arg_idx
)
8114 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8115 tree actual_parm
= TREE_VALUE (parm
);
8116 int pack_idx
= arg_idx
- first_pack_arg
;
8120 /* Once we've packed as many args as we have types, stop. */
8121 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8123 else if (PACK_EXPANSION_P (arg
))
8124 /* We don't know how many args we have yet, just
8125 use the unconverted ones for now. */
8128 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8131 if (arg
== error_mark_node
)
8133 if (complain
& tf_error
)
8134 error ("template argument %d is invalid", arg_idx
+ 1);
8137 arg
= convert_template_argument (actual_parm
,
8138 arg
, new_args
, complain
, parm_idx
,
8140 if (arg
== error_mark_node
)
8142 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8145 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8146 && TREE_VEC_LENGTH (packed_args
) > 0)
8148 if (complain
& tf_error
)
8149 error ("wrong number of template arguments (%d, should be %d)",
8150 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8151 return error_mark_node
;
8154 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8155 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8156 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8159 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8160 TREE_CONSTANT (argument_pack
) = 1;
8163 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8165 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8166 TREE_VEC_LENGTH (packed_args
));
8167 return argument_pack
;
8170 /* Returns the number of pack expansions in the template argument vector
8174 pack_expansion_args_count (tree args
)
8179 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8181 tree elt
= TREE_VEC_ELT (args
, i
);
8182 if (elt
&& PACK_EXPANSION_P (elt
))
8188 /* Convert all template arguments to their appropriate types, and
8189 return a vector containing the innermost resulting template
8190 arguments. If any error occurs, return error_mark_node. Error and
8191 warning messages are issued under control of COMPLAIN.
8193 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8194 for arguments not specified in ARGS. Otherwise, if
8195 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8196 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8197 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8201 coerce_template_parms (tree parms
,
8204 tsubst_flags_t complain
,
8205 bool require_all_args
,
8206 bool use_default_args
)
8208 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8209 tree orig_inner_args
;
8212 tree new_inner_args
;
8213 int saved_unevaluated_operand
;
8214 int saved_inhibit_evaluation_warnings
;
8216 /* When used as a boolean value, indicates whether this is a
8217 variadic template parameter list. Since it's an int, we can also
8218 subtract it from nparms to get the number of non-variadic
8221 int variadic_args_p
= 0;
8222 int post_variadic_parms
= 0;
8224 /* Likewise for parameters with default arguments. */
8227 if (args
== error_mark_node
)
8228 return error_mark_node
;
8230 nparms
= TREE_VEC_LENGTH (parms
);
8232 /* Determine if there are any parameter packs or default arguments. */
8233 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8235 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8237 ++post_variadic_parms
;
8238 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8240 if (TREE_PURPOSE (parm
))
8244 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8245 /* If there are no parameters that follow a parameter pack, we need to
8246 expand any argument packs so that we can deduce a parameter pack from
8247 some non-packed args followed by an argument pack, as in variadic85.C.
8248 If there are such parameters, we need to leave argument packs intact
8249 so the arguments are assigned properly. This can happen when dealing
8250 with a nested class inside a partial specialization of a class
8251 template, as in variadic92.C, or when deducing a template parameter pack
8252 from a sub-declarator, as in variadic114.C. */
8253 if (!post_variadic_parms
)
8254 inner_args
= expand_template_argument_pack (inner_args
);
8256 /* Count any pack expansion args. */
8257 variadic_args_p
= pack_expansion_args_count (inner_args
);
8259 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8260 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8261 || (nargs
< nparms
- variadic_p
8264 && (!use_default_args
8265 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8266 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8268 if (complain
& tf_error
)
8270 if (variadic_p
|| default_p
)
8272 nparms
-= variadic_p
+ default_p
;
8273 error ("wrong number of template arguments "
8274 "(%d, should be at least %d)", nargs
, nparms
);
8277 error ("wrong number of template arguments "
8278 "(%d, should be %d)", nargs
, nparms
);
8281 inform (DECL_SOURCE_LOCATION (in_decl
),
8282 "provided for %qD", in_decl
);
8285 return error_mark_node
;
8287 /* We can't pass a pack expansion to a non-pack parameter of an alias
8288 template (DR 1430). */
8290 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8291 || concept_template_p (in_decl
))
8293 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8295 if (complain
& tf_error
)
8297 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8299 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8300 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8302 if (PACK_EXPANSION_P (arg
)
8303 && !template_parameter_pack_p (parm
))
8305 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8306 error_at (location_of (arg
),
8307 "pack expansion argument for non-pack parameter "
8308 "%qD of alias template %qD", parm
, in_decl
);
8310 error_at (location_of (arg
),
8311 "pack expansion argument for non-pack parameter "
8312 "%qD of concept %qD", parm
, in_decl
);
8313 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8320 return error_mark_node
;
8323 /* We need to evaluate the template arguments, even though this
8324 template-id may be nested within a "sizeof". */
8325 saved_unevaluated_operand
= cp_unevaluated_operand
;
8326 cp_unevaluated_operand
= 0;
8327 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8328 c_inhibit_evaluation_warnings
= 0;
8329 new_inner_args
= make_tree_vec (nparms
);
8330 new_args
= add_outermost_template_args (args
, new_inner_args
);
8331 int pack_adjust
= 0;
8332 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8337 /* Get the Ith template parameter. */
8338 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8340 if (parm
== error_mark_node
)
8342 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8346 /* Calculate the next argument. */
8347 if (arg_idx
< nargs
)
8348 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8352 if (template_parameter_pack_p (TREE_VALUE (parm
))
8353 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8355 /* Some arguments will be placed in the
8356 template parameter pack PARM. */
8357 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8358 inner_args
, arg_idx
,
8362 if (arg
== NULL_TREE
)
8364 /* We don't know how many args we have yet, just use the
8365 unconverted (and still packed) ones for now. */
8366 new_inner_args
= orig_inner_args
;
8371 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8373 /* Store this argument. */
8374 if (arg
== error_mark_node
)
8377 /* We are done with all of the arguments. */
8382 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8383 arg_idx
+= pack_adjust
;
8390 if (PACK_EXPANSION_P (arg
))
8392 /* "If every valid specialization of a variadic template
8393 requires an empty template parameter pack, the template is
8394 ill-formed, no diagnostic required." So check that the
8395 pattern works with this parameter. */
8396 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8397 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8401 if (conv
== error_mark_node
)
8403 if (complain
& tf_error
)
8404 inform (input_location
, "so any instantiation with a "
8405 "non-empty parameter pack would be ill-formed");
8408 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8409 /* Recover from missing typename. */
8410 TREE_VEC_ELT (inner_args
, arg_idx
)
8411 = make_pack_expansion (conv
, complain
);
8413 /* We don't know how many args we have yet, just
8414 use the unconverted ones for now. */
8415 new_inner_args
= inner_args
;
8420 else if (require_all_args
)
8422 /* There must be a default arg in this case. */
8423 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8425 /* The position of the first default template argument,
8426 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8428 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8429 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8430 arg_idx
- pack_adjust
);
8435 if (arg
== error_mark_node
)
8437 if (complain
& tf_error
)
8438 error ("template argument %d is invalid", arg_idx
+ 1);
8441 /* This only occurs if there was an error in the template
8442 parameter list itself (which we would already have
8443 reported) that we are trying to recover from, e.g., a class
8444 template with a parameter list such as
8445 template<typename..., typename>. */
8448 arg
= convert_template_argument (TREE_VALUE (parm
),
8449 arg
, new_args
, complain
,
8452 if (arg
== error_mark_node
)
8454 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8456 cp_unevaluated_operand
= saved_unevaluated_operand
;
8457 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8459 if (variadic_p
&& arg_idx
< nargs
)
8461 if (complain
& tf_error
)
8463 error ("wrong number of template arguments "
8464 "(%d, should be %d)", nargs
, arg_idx
);
8466 error ("provided for %q+D", in_decl
);
8468 return error_mark_node
;
8472 return error_mark_node
;
8474 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8475 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8476 TREE_VEC_LENGTH (new_inner_args
));
8478 return new_inner_args
;
8481 /* Convert all template arguments to their appropriate types, and
8482 return a vector containing the innermost resulting template
8483 arguments. If any error occurs, return error_mark_node. Error and
8484 warning messages are not issued.
8486 Note that no function argument deduction is performed, and default
8487 arguments are used to fill in unspecified arguments. */
8489 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8491 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8494 /* Convert all template arguments to their appropriate type, and
8495 instantiate default arguments as needed. This returns a vector
8496 containing the innermost resulting template arguments, or
8497 error_mark_node if unsuccessful. */
8499 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8500 tsubst_flags_t complain
)
8502 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8505 /* Like coerce_template_parms. If PARMS represents all template
8506 parameters levels, this function returns a vector of vectors
8507 representing all the resulting argument levels. Note that in this
8508 case, only the innermost arguments are coerced because the
8509 outermost ones are supposed to have been coerced already.
8511 Otherwise, if PARMS represents only (the innermost) vector of
8512 parameters, this function returns a vector containing just the
8513 innermost resulting arguments. */
8516 coerce_innermost_template_parms (tree parms
,
8519 tsubst_flags_t complain
,
8520 bool require_all_args
,
8521 bool use_default_args
)
8523 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8524 int args_depth
= TMPL_ARGS_DEPTH (args
);
8527 if (parms_depth
> 1)
8529 coerced_args
= make_tree_vec (parms_depth
);
8533 for (level
= parms
, cur_depth
= parms_depth
;
8534 parms_depth
> 0 && level
!= NULL_TREE
;
8535 level
= TREE_CHAIN (level
), --cur_depth
)
8538 if (cur_depth
== args_depth
)
8539 l
= coerce_template_parms (TREE_VALUE (level
),
8540 args
, in_decl
, complain
,
8544 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8546 if (l
== error_mark_node
)
8547 return error_mark_node
;
8549 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8553 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8554 args
, in_decl
, complain
,
8557 return coerced_args
;
8560 /* Returns 1 if template args OT and NT are equivalent. */
8563 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8567 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8569 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8572 if (TREE_CODE (nt
) == TREE_VEC
)
8573 /* For member templates */
8574 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8575 else if (PACK_EXPANSION_P (ot
))
8576 return (PACK_EXPANSION_P (nt
)
8577 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8578 PACK_EXPANSION_PATTERN (nt
))
8579 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8580 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8581 else if (ARGUMENT_PACK_P (ot
))
8586 if (!ARGUMENT_PACK_P (nt
))
8589 opack
= ARGUMENT_PACK_ARGS (ot
);
8590 npack
= ARGUMENT_PACK_ARGS (nt
);
8591 len
= TREE_VEC_LENGTH (opack
);
8592 if (TREE_VEC_LENGTH (npack
) != len
)
8594 for (i
= 0; i
< len
; ++i
)
8595 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8596 TREE_VEC_ELT (npack
, i
)))
8600 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8602 else if (TYPE_P (nt
))
8606 /* Don't treat an alias template specialization with dependent
8607 arguments as equivalent to its underlying type when used as a
8608 template argument; we need them to be distinct so that we
8609 substitute into the specialization arguments at instantiation
8610 time. And aliases can't be equivalent without being ==, so
8611 we don't need to look any deeper.
8613 During partial ordering, however, we need to treat them normally so
8614 that we can order uses of the same alias with different
8615 cv-qualification (79960). */
8617 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8620 return same_type_p (ot
, nt
);
8622 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8626 /* Try to treat a template non-type argument that has been converted
8627 to the parameter type as equivalent to one that hasn't yet. */
8628 for (enum tree_code code1
= TREE_CODE (ot
);
8629 CONVERT_EXPR_CODE_P (code1
)
8630 || code1
== NON_LVALUE_EXPR
;
8631 code1
= TREE_CODE (ot
))
8632 ot
= TREE_OPERAND (ot
, 0);
8633 for (enum tree_code code2
= TREE_CODE (nt
);
8634 CONVERT_EXPR_CODE_P (code2
)
8635 || code2
== NON_LVALUE_EXPR
;
8636 code2
= TREE_CODE (nt
))
8637 nt
= TREE_OPERAND (nt
, 0);
8639 return cp_tree_equal (ot
, nt
);
8643 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8644 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8645 NEWARG_PTR with the offending arguments if they are non-NULL. */
8648 comp_template_args (tree oldargs
, tree newargs
,
8649 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8654 if (oldargs
== newargs
)
8657 if (!oldargs
|| !newargs
)
8660 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8663 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8665 tree nt
= TREE_VEC_ELT (newargs
, i
);
8666 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8668 if (! template_args_equal (ot
, nt
, partial_order
))
8670 if (oldarg_ptr
!= NULL
)
8672 if (newarg_ptr
!= NULL
)
8681 comp_template_args_porder (tree oargs
, tree nargs
)
8683 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8687 add_pending_template (tree d
)
8689 tree ti
= (TYPE_P (d
)
8690 ? CLASSTYPE_TEMPLATE_INFO (d
)
8691 : DECL_TEMPLATE_INFO (d
));
8692 struct pending_template
*pt
;
8695 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8698 /* We are called both from instantiate_decl, where we've already had a
8699 tinst_level pushed, and instantiate_template, where we haven't.
8701 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8704 push_tinst_level (d
);
8706 pt
= ggc_alloc
<pending_template
> ();
8708 pt
->tinst
= current_tinst_level
;
8709 if (last_pending_template
)
8710 last_pending_template
->next
= pt
;
8712 pending_templates
= pt
;
8714 last_pending_template
= pt
;
8716 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8723 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8724 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8725 documentation for TEMPLATE_ID_EXPR. */
8728 lookup_template_function (tree fns
, tree arglist
)
8732 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8733 return error_mark_node
;
8735 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8737 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8739 error ("%q#D is not a function template", fns
);
8740 return error_mark_node
;
8743 if (BASELINK_P (fns
))
8745 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8747 BASELINK_FUNCTIONS (fns
),
8752 type
= TREE_TYPE (fns
);
8753 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8754 type
= unknown_type_node
;
8756 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8759 /* Within the scope of a template class S<T>, the name S gets bound
8760 (in build_self_reference) to a TYPE_DECL for the class, not a
8761 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8762 or one of its enclosing classes, and that type is a template,
8763 return the associated TEMPLATE_DECL. Otherwise, the original
8766 Also handle the case when DECL is a TREE_LIST of ambiguous
8767 injected-class-names from different bases. */
8770 maybe_get_template_decl_from_type_decl (tree decl
)
8772 if (decl
== NULL_TREE
)
8775 /* DR 176: A lookup that finds an injected-class-name (10.2
8776 [class.member.lookup]) can result in an ambiguity in certain cases
8777 (for example, if it is found in more than one base class). If all of
8778 the injected-class-names that are found refer to specializations of
8779 the same class template, and if the name is followed by a
8780 template-argument-list, the reference refers to the class template
8781 itself and not a specialization thereof, and is not ambiguous. */
8782 if (TREE_CODE (decl
) == TREE_LIST
)
8784 tree t
, tmpl
= NULL_TREE
;
8785 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8787 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8790 else if (tmpl
!= elt
)
8793 if (tmpl
&& t
== NULL_TREE
)
8799 return (decl
!= NULL_TREE
8800 && DECL_SELF_REFERENCE_P (decl
)
8801 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8802 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8805 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8806 parameters, find the desired type.
8808 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8810 IN_DECL, if non-NULL, is the template declaration we are trying to
8813 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8814 the class we are looking up.
8816 Issue error and warning messages under control of COMPLAIN.
8818 If the template class is really a local class in a template
8819 function, then the FUNCTION_CONTEXT is the function in which it is
8822 ??? Note that this function is currently called *twice* for each
8823 template-id: the first time from the parser, while creating the
8824 incomplete type (finish_template_type), and the second type during the
8825 real instantiation (instantiate_template_class). This is surely something
8826 that we want to avoid. It also causes some problems with argument
8827 coercion (see convert_nontype_argument for more information on this). */
8830 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8831 int entering_scope
, tsubst_flags_t complain
)
8833 tree templ
= NULL_TREE
, parmlist
;
8840 if (identifier_p (d1
))
8842 tree value
= innermost_non_namespace_value (d1
);
8843 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8848 push_decl_namespace (context
);
8849 templ
= lookup_name (d1
);
8850 templ
= maybe_get_template_decl_from_type_decl (templ
);
8852 pop_decl_namespace ();
8855 context
= DECL_CONTEXT (templ
);
8857 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8859 tree type
= TREE_TYPE (d1
);
8861 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8862 an implicit typename for the second A. Deal with it. */
8863 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8864 type
= TREE_TYPE (type
);
8866 if (CLASSTYPE_TEMPLATE_INFO (type
))
8868 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8869 d1
= DECL_NAME (templ
);
8872 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8873 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8875 templ
= TYPE_TI_TEMPLATE (d1
);
8876 d1
= DECL_NAME (templ
);
8878 else if (DECL_TYPE_TEMPLATE_P (d1
))
8881 d1
= DECL_NAME (templ
);
8882 context
= DECL_CONTEXT (templ
);
8884 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8887 d1
= DECL_NAME (templ
);
8890 /* Issue an error message if we didn't find a template. */
8893 if (complain
& tf_error
)
8894 error ("%qT is not a template", d1
);
8895 return error_mark_node
;
8898 if (TREE_CODE (templ
) != TEMPLATE_DECL
8899 /* Make sure it's a user visible template, if it was named by
8901 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8902 && !PRIMARY_TEMPLATE_P (templ
)))
8904 if (complain
& tf_error
)
8906 error ("non-template type %qT used as a template", d1
);
8908 error ("for template declaration %q+D", in_decl
);
8910 return error_mark_node
;
8913 complain
&= ~tf_user
;
8915 /* An alias that just changes the name of a template is equivalent to the
8916 other template, so if any of the arguments are pack expansions, strip
8917 the alias to avoid problems with a pack expansion passed to a non-pack
8918 alias template parameter (DR 1430). */
8919 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8920 templ
= get_underlying_template (templ
);
8922 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8925 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8926 if (arglist2
== error_mark_node
8927 || (!uses_template_parms (arglist2
)
8928 && check_instantiated_args (templ
, arglist2
, complain
)))
8929 return error_mark_node
;
8931 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8936 tree template_type
= TREE_TYPE (templ
);
8939 tree found
= NULL_TREE
;
8942 int is_dependent_type
;
8943 int use_partial_inst_tmpl
= false;
8945 if (template_type
== error_mark_node
)
8946 /* An error occurred while building the template TEMPL, and a
8947 diagnostic has most certainly been emitted for that
8948 already. Let's propagate that error. */
8949 return error_mark_node
;
8951 gen_tmpl
= most_general_template (templ
);
8952 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8953 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8954 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8956 if (arg_depth
== 1 && parm_depth
> 1)
8958 /* We've been given an incomplete set of template arguments.
8961 template <class T> struct S1 {
8962 template <class U> struct S2 {};
8963 template <class U> struct S2<U*> {};
8966 we will be called with an ARGLIST of `U*', but the
8967 TEMPLATE will be `template <class T> template
8968 <class U> struct S1<T>::S2'. We must fill in the missing
8970 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8971 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8972 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8975 /* Now we should have enough arguments. */
8976 gcc_assert (parm_depth
== arg_depth
);
8978 /* From here on, we're only interested in the most general
8981 /* Calculate the BOUND_ARGS. These will be the args that are
8982 actually tsubst'd into the definition to create the
8984 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8986 /*require_all_args=*/true,
8987 /*use_default_args=*/true);
8989 if (arglist
== error_mark_node
)
8990 /* We were unable to bind the arguments. */
8991 return error_mark_node
;
8993 /* In the scope of a template class, explicit references to the
8994 template class refer to the type of the template, not any
8995 instantiation of it. For example, in:
8997 template <class T> class C { void f(C<T>); }
8999 the `C<T>' is just the same as `C'. Outside of the
9000 class, however, such a reference is an instantiation. */
9002 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9003 || currently_open_class (template_type
))
9005 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9007 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9008 return template_type
;
9011 /* If we already have this specialization, return it. */
9012 elt
.tmpl
= gen_tmpl
;
9014 elt
.spec
= NULL_TREE
;
9015 hash
= spec_hasher::hash (&elt
);
9016 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9021 /* If the the template's constraints are not satisfied,
9022 then we cannot form a valid type.
9024 Note that the check is deferred until after the hash
9025 lookup. This prevents redundant checks on previously
9026 instantiated specializations. */
9027 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9029 if (complain
& tf_error
)
9031 error ("template constraint failure");
9032 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9034 return error_mark_node
;
9037 is_dependent_type
= uses_template_parms (arglist
);
9039 /* If the deduced arguments are invalid, then the binding
9041 if (!is_dependent_type
9042 && check_instantiated_args (gen_tmpl
,
9043 INNERMOST_TEMPLATE_ARGS (arglist
),
9045 return error_mark_node
;
9047 if (!is_dependent_type
9048 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9049 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9050 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9052 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9053 DECL_NAME (gen_tmpl
),
9054 /*tag_scope=*/ts_global
);
9058 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
9060 if (context
== error_mark_node
)
9061 return error_mark_node
;
9064 context
= global_namespace
;
9066 /* Create the type. */
9067 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9069 /* The user referred to a specialization of an alias
9070 template represented by GEN_TMPL.
9072 [temp.alias]/2 says:
9074 When a template-id refers to the specialization of an
9075 alias template, it is equivalent to the associated
9076 type obtained by substitution of its
9077 template-arguments for the template-parameters in the
9078 type-id of the alias template. */
9080 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9081 /* Note that the call above (by indirectly calling
9082 register_specialization in tsubst_decl) registers the
9083 TYPE_DECL representing the specialization of the alias
9084 template. So next time someone substitutes ARGLIST for
9085 the template parms into the alias template (GEN_TMPL),
9086 she'll get that TYPE_DECL back. */
9088 if (t
== error_mark_node
)
9091 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9093 if (!is_dependent_type
)
9095 set_current_access_from_decl (TYPE_NAME (template_type
));
9096 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9097 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9098 arglist
, complain
, in_decl
),
9099 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9100 arglist
, complain
, in_decl
),
9101 SCOPED_ENUM_P (template_type
), NULL
);
9103 if (t
== error_mark_node
)
9108 /* We don't want to call start_enum for this type, since
9109 the values for the enumeration constants may involve
9110 template parameters. And, no one should be interested
9111 in the enumeration constants for such a type. */
9112 t
= cxx_make_type (ENUMERAL_TYPE
);
9113 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9115 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9116 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9117 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9119 else if (CLASS_TYPE_P (template_type
))
9121 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9122 instantiated here. */
9123 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9125 t
= make_class_type (TREE_CODE (template_type
));
9126 CLASSTYPE_DECLARED_CLASS (t
)
9127 = CLASSTYPE_DECLARED_CLASS (template_type
);
9128 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9130 /* A local class. Make sure the decl gets registered properly. */
9131 if (context
== current_function_decl
)
9132 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
9134 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9135 /* This instantiation is another name for the primary
9136 template type. Set the TYPE_CANONICAL field
9138 TYPE_CANONICAL (t
) = template_type
;
9139 else if (any_template_arguments_need_structural_equality_p (arglist
))
9140 /* Some of the template arguments require structural
9141 equality testing, so this template class requires
9142 structural equality testing. */
9143 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9148 /* If we called start_enum or pushtag above, this information
9149 will already be set up. */
9152 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9154 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9155 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9156 DECL_SOURCE_LOCATION (type_decl
)
9157 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9160 type_decl
= TYPE_NAME (t
);
9162 if (CLASS_TYPE_P (template_type
))
9164 TREE_PRIVATE (type_decl
)
9165 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9166 TREE_PROTECTED (type_decl
)
9167 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9168 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9170 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9171 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9175 if (OVERLOAD_TYPE_P (t
)
9176 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9178 static const char *tags
[] = {"abi_tag", "may_alias"};
9180 for (unsigned ix
= 0; ix
!= 2; ix
++)
9183 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9187 = tree_cons (TREE_PURPOSE (attributes
),
9188 TREE_VALUE (attributes
),
9189 TYPE_ATTRIBUTES (t
));
9193 /* Let's consider the explicit specialization of a member
9194 of a class template specialization that is implicitly instantiated,
9199 template<class U> struct M {}; //#0
9204 struct S<int>::M<char> //#1
9208 [temp.expl.spec]/4 says this is valid.
9210 In this case, when we write:
9213 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9216 When we encounter #1, we want to store the partial instantiation
9217 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9219 For all cases other than this "explicit specialization of member of a
9220 class template", we just want to store the most general template into
9221 the CLASSTYPE_TI_TEMPLATE of M.
9223 This case of "explicit specialization of member of a class template"
9225 1/ the enclosing class is an instantiation of, and therefore not
9226 the same as, the context of the most general template, and
9227 2/ we aren't looking at the partial instantiation itself, i.e.
9228 the innermost arguments are not the same as the innermost parms of
9229 the most general template.
9231 So it's only when 1/ and 2/ happens that we want to use the partial
9232 instantiation of the member template in lieu of its most general
9235 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9236 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9237 /* the enclosing class must be an instantiation... */
9238 && CLASS_TYPE_P (context
)
9239 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9241 TREE_VEC_LENGTH (arglist
)--;
9242 ++processing_template_decl
;
9243 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9244 tree partial_inst_args
=
9245 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9246 arglist
, complain
, NULL_TREE
);
9247 --processing_template_decl
;
9248 TREE_VEC_LENGTH (arglist
)++;
9249 if (partial_inst_args
== error_mark_node
)
9250 return error_mark_node
;
9251 use_partial_inst_tmpl
=
9252 /*...and we must not be looking at the partial instantiation
9254 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9258 if (!use_partial_inst_tmpl
)
9259 /* This case is easy; there are no member templates involved. */
9263 /* This is a full instantiation of a member template. Find
9264 the partial instantiation of which this is an instance. */
9266 /* Temporarily reduce by one the number of levels in the ARGLIST
9267 so as to avoid comparing the last set of arguments. */
9268 TREE_VEC_LENGTH (arglist
)--;
9269 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9270 TREE_VEC_LENGTH (arglist
)++;
9271 /* FOUND is either a proper class type, or an alias
9272 template specialization. In the later case, it's a
9273 TYPE_DECL, resulting from the substituting of arguments
9274 for parameters in the TYPE_DECL of the alias template
9275 done earlier. So be careful while getting the template
9277 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9279 : (TREE_CODE (found
) == TYPE_DECL
9280 ? DECL_TI_TEMPLATE (found
)
9281 : CLASSTYPE_TI_TEMPLATE (found
)));
9284 // Build template info for the new specialization.
9285 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9288 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9289 entry
= ggc_alloc
<spec_entry
> ();
9293 /* Note this use of the partial instantiation so we can check it
9294 later in maybe_process_partial_specialization. */
9295 DECL_TEMPLATE_INSTANTIATIONS (found
)
9296 = tree_cons (arglist
, t
,
9297 DECL_TEMPLATE_INSTANTIATIONS (found
));
9299 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9300 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9301 /* Now that the type has been registered on the instantiations
9302 list, we set up the enumerators. Because the enumeration
9303 constants may involve the enumeration type itself, we make
9304 sure to register the type first, and then create the
9305 constants. That way, doing tsubst_expr for the enumeration
9306 constants won't result in recursive calls here; we'll find
9307 the instantiation and exit above. */
9308 tsubst_enum (template_type
, t
, arglist
);
9310 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9311 /* If the type makes use of template parameters, the
9312 code that generates debugging information will crash. */
9313 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9315 /* Possibly limit visibility based on template args. */
9316 TREE_PUBLIC (type_decl
) = 1;
9317 determine_visibility (type_decl
);
9319 inherit_targ_abi_tags (t
);
9325 /* Wrapper for lookup_template_class_1. */
9328 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9329 int entering_scope
, tsubst_flags_t complain
)
9332 timevar_push (TV_TEMPLATE_INST
);
9333 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9334 entering_scope
, complain
);
9335 timevar_pop (TV_TEMPLATE_INST
);
9339 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9342 lookup_template_variable (tree templ
, tree arglist
)
9344 /* The type of the expression is NULL_TREE since the template-id could refer
9345 to an explicit or partial specialization. */
9346 tree type
= NULL_TREE
;
9347 if (flag_concepts
&& variable_concept_p (templ
))
9348 /* Except that concepts are always bool. */
9349 type
= boolean_type_node
;
9350 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9353 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9356 finish_template_variable (tree var
, tsubst_flags_t complain
)
9358 tree templ
= TREE_OPERAND (var
, 0);
9359 tree arglist
= TREE_OPERAND (var
, 1);
9361 /* We never want to return a VAR_DECL for a variable concept, since they
9362 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9363 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9364 if (concept_p
&& processing_template_decl
)
9367 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9368 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9370 templ
= most_general_template (templ
);
9371 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9372 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9374 /*use_default*/true);
9376 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9378 if (complain
& tf_error
)
9380 error ("use of invalid variable template %qE", var
);
9381 diagnose_constraints (location_of (var
), templ
, arglist
);
9383 return error_mark_node
;
9386 /* If a template-id refers to a specialization of a variable
9387 concept, then the expression is true if and only if the
9388 concept's constraints are satisfied by the given template
9391 NOTE: This is an extension of Concepts Lite TS that
9392 allows constraints to be used in expressions. */
9395 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9396 return evaluate_variable_concept (decl
, arglist
);
9399 return instantiate_template (templ
, arglist
, complain
);
9402 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9403 TARGS template args, and instantiate it if it's not dependent. */
9406 lookup_and_finish_template_variable (tree templ
, tree targs
,
9407 tsubst_flags_t complain
)
9409 templ
= lookup_template_variable (templ
, targs
);
9410 if (!any_dependent_template_arguments_p (targs
))
9412 templ
= finish_template_variable (templ
, complain
);
9416 return convert_from_reference (templ
);
9425 /* True when we should also visit template parameters that occur in
9426 non-deduced contexts. */
9427 bool include_nondeduced_p
;
9428 hash_set
<tree
> *visited
;
9431 /* Called from for_each_template_parm via walk_tree. */
9434 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9437 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9438 tree_fn_t fn
= pfd
->fn
;
9439 void *data
= pfd
->data
;
9440 tree result
= NULL_TREE
;
9442 #define WALK_SUBTREE(NODE) \
9445 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9446 pfd->include_nondeduced_p, \
9448 if (result) goto out; \
9452 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9456 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9457 WALK_SUBTREE (TYPE_CONTEXT (t
));
9459 switch (TREE_CODE (t
))
9462 if (TYPE_PTRMEMFUNC_P (t
))
9468 if (!TYPE_TEMPLATE_INFO (t
))
9471 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9475 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9476 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9480 /* Since we're not going to walk subtrees, we have to do this
9482 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9486 /* Check the return type. */
9487 WALK_SUBTREE (TREE_TYPE (t
));
9489 /* Check the parameter types. Since default arguments are not
9490 instantiated until they are needed, the TYPE_ARG_TYPES may
9491 contain expressions that involve template parameters. But,
9492 no-one should be looking at them yet. And, once they're
9493 instantiated, they don't contain template parameters, so
9494 there's no point in looking at them then, either. */
9498 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9499 WALK_SUBTREE (TREE_VALUE (parm
));
9501 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9502 want walk_tree walking into them itself. */
9506 if (flag_noexcept_type
)
9508 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9510 WALK_SUBTREE (TREE_PURPOSE (spec
));
9515 case UNDERLYING_TYPE
:
9516 if (pfd
->include_nondeduced_p
9517 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9519 pfd
->include_nondeduced_p
,
9521 return error_mark_node
;
9526 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9527 WALK_SUBTREE (DECL_TI_ARGS (t
));
9532 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9533 WALK_SUBTREE (DECL_INITIAL (t
));
9534 if (DECL_CONTEXT (t
)
9535 && pfd
->include_nondeduced_p
)
9536 WALK_SUBTREE (DECL_CONTEXT (t
));
9539 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9540 /* Record template parameters such as `T' inside `TT<T>'. */
9541 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9544 case TEMPLATE_TEMPLATE_PARM
:
9545 case TEMPLATE_TYPE_PARM
:
9546 case TEMPLATE_PARM_INDEX
:
9547 if (fn
&& (*fn
)(t
, data
))
9554 /* A template template parameter is encountered. */
9555 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9556 WALK_SUBTREE (TREE_TYPE (t
));
9558 /* Already substituted template template parameter */
9563 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9564 partial instantiation. */
9565 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9569 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9570 && pfd
->include_nondeduced_p
)
9571 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9576 /* If there's no type, then this thing must be some expression
9577 involving template parameters. */
9578 if (!fn
&& !TREE_TYPE (t
))
9579 return error_mark_node
;
9584 case IMPLICIT_CONV_EXPR
:
9585 case REINTERPRET_CAST_EXPR
:
9586 case CONST_CAST_EXPR
:
9587 case STATIC_CAST_EXPR
:
9588 case DYNAMIC_CAST_EXPR
:
9592 case PSEUDO_DTOR_EXPR
:
9594 return error_mark_node
;
9603 /* We didn't find any template parameters we liked. */
9608 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9609 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9610 call FN with the parameter and the DATA.
9611 If FN returns nonzero, the iteration is terminated, and
9612 for_each_template_parm returns 1. Otherwise, the iteration
9613 continues. If FN never returns a nonzero value, the value
9614 returned by for_each_template_parm is 0. If FN is NULL, it is
9615 considered to be the function which always returns 1.
9617 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9618 parameters that occur in non-deduced contexts. When false, only
9619 visits those template parameters that can be deduced. */
9622 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9623 hash_set
<tree
> *visited
,
9624 bool include_nondeduced_p
,
9627 struct pair_fn_data pfd
;
9632 pfd
.any_fn
= any_fn
;
9634 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9636 /* Walk the tree. (Conceptually, we would like to walk without
9637 duplicates, but for_each_template_parm_r recursively calls
9638 for_each_template_parm, so we would need to reorganize a fair
9639 bit to use walk_tree_without_duplicates, so we keep our own
9642 pfd
.visited
= visited
;
9644 pfd
.visited
= new hash_set
<tree
>;
9645 result
= cp_walk_tree (&t
,
9646 for_each_template_parm_r
,
9660 /* Returns true if T depends on any template parameter. */
9663 uses_template_parms (tree t
)
9669 int saved_processing_template_decl
;
9671 saved_processing_template_decl
= processing_template_decl
;
9672 if (!saved_processing_template_decl
)
9673 processing_template_decl
= 1;
9675 dependent_p
= dependent_type_p (t
);
9676 else if (TREE_CODE (t
) == TREE_VEC
)
9677 dependent_p
= any_dependent_template_arguments_p (t
);
9678 else if (TREE_CODE (t
) == TREE_LIST
)
9679 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9680 || uses_template_parms (TREE_CHAIN (t
)));
9681 else if (TREE_CODE (t
) == TYPE_DECL
)
9682 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9685 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9686 || TREE_CODE (t
) == OVERLOAD
9689 || TREE_CODE (t
) == TRAIT_EXPR
9690 || TREE_CODE (t
) == CONSTRUCTOR
9691 || CONSTANT_CLASS_P (t
))
9692 dependent_p
= (type_dependent_expression_p (t
)
9693 || value_dependent_expression_p (t
));
9696 gcc_assert (t
== error_mark_node
);
9697 dependent_p
= false;
9700 processing_template_decl
= saved_processing_template_decl
;
9705 /* Returns true iff current_function_decl is an incompletely instantiated
9706 template. Useful instead of processing_template_decl because the latter
9707 is set to 0 during instantiate_non_dependent_expr. */
9710 in_template_function (void)
9712 tree fn
= current_function_decl
;
9714 ++processing_template_decl
;
9715 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9716 && DECL_TEMPLATE_INFO (fn
)
9717 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9718 --processing_template_decl
;
9722 /* Returns true if T depends on any template parameter with level LEVEL. */
9725 uses_template_parms_level (tree t
, int level
)
9727 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9728 /*include_nondeduced_p=*/true);
9731 /* Returns true if the signature of DECL depends on any template parameter from
9732 its enclosing class. */
9735 uses_outer_template_parms (tree decl
)
9737 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9740 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9741 &depth
, NULL
, /*include_nondeduced_p=*/true))
9743 if (PRIMARY_TEMPLATE_P (decl
)
9744 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9745 (DECL_TEMPLATE_PARMS (decl
)),
9746 template_parm_outer_level
,
9747 &depth
, NULL
, /*include_nondeduced_p=*/true))
9749 tree ci
= get_constraints (decl
);
9751 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9752 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9753 &depth
, NULL
, /*nondeduced*/true))
9758 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9759 ill-formed translation unit, i.e. a variable or function that isn't
9760 usable in a constant expression. */
9763 neglectable_inst_p (tree d
)
9766 && !undeduced_auto_decl (d
)
9767 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9768 : decl_maybe_constant_var_p (d
)));
9771 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9772 neglectable and instantiated from within an erroneous instantiation. */
9775 limit_bad_template_recursion (tree decl
)
9777 struct tinst_level
*lev
= current_tinst_level
;
9778 int errs
= errorcount
+ sorrycount
;
9779 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9782 for (; lev
; lev
= lev
->next
)
9783 if (neglectable_inst_p (lev
->decl
))
9786 return (lev
&& errs
> lev
->errors
);
9789 static int tinst_depth
;
9790 extern int max_tinst_depth
;
9793 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9795 /* We're starting to instantiate D; record the template instantiation context
9796 for diagnostics and to restore it later. */
9799 push_tinst_level (tree d
)
9801 return push_tinst_level_loc (d
, input_location
);
9804 /* We're starting to instantiate D; record the template instantiation context
9805 at LOC for diagnostics and to restore it later. */
9808 push_tinst_level_loc (tree d
, location_t loc
)
9810 struct tinst_level
*new_level
;
9812 if (tinst_depth
>= max_tinst_depth
)
9814 /* Tell error.c not to try to instantiate any templates. */
9816 fatal_error (input_location
,
9817 "template instantiation depth exceeds maximum of %d"
9818 " (use -ftemplate-depth= to increase the maximum)",
9823 /* If the current instantiation caused problems, don't let it instantiate
9824 anything else. Do allow deduction substitution and decls usable in
9825 constant expressions. */
9826 if (limit_bad_template_recursion (d
))
9829 /* When not -quiet, dump template instantiations other than functions, since
9830 announce_function will take care of those. */
9832 && TREE_CODE (d
) != TREE_LIST
9833 && TREE_CODE (d
) != FUNCTION_DECL
)
9834 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9836 new_level
= ggc_alloc
<tinst_level
> ();
9837 new_level
->decl
= d
;
9838 new_level
->locus
= loc
;
9839 new_level
->errors
= errorcount
+sorrycount
;
9840 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9841 new_level
->next
= current_tinst_level
;
9842 current_tinst_level
= new_level
;
9845 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9846 depth_reached
= tinst_depth
;
9851 /* We're done instantiating this template; return to the instantiation
9855 pop_tinst_level (void)
9857 /* Restore the filename and line number stashed away when we started
9858 this instantiation. */
9859 input_location
= current_tinst_level
->locus
;
9860 current_tinst_level
= current_tinst_level
->next
;
9864 /* We're instantiating a deferred template; restore the template
9865 instantiation context in which the instantiation was requested, which
9866 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9869 reopen_tinst_level (struct tinst_level
*level
)
9871 struct tinst_level
*t
;
9874 for (t
= level
; t
; t
= t
->next
)
9877 current_tinst_level
= level
;
9879 if (current_tinst_level
)
9880 current_tinst_level
->errors
= errorcount
+sorrycount
;
9884 /* Returns the TINST_LEVEL which gives the original instantiation
9887 struct tinst_level
*
9888 outermost_tinst_level (void)
9890 struct tinst_level
*level
= current_tinst_level
;
9893 level
= level
->next
;
9897 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9898 vector of template arguments, as for tsubst.
9900 Returns an appropriate tsubst'd friend declaration. */
9903 tsubst_friend_function (tree decl
, tree args
)
9907 if (TREE_CODE (decl
) == FUNCTION_DECL
9908 && DECL_TEMPLATE_INSTANTIATION (decl
)
9909 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9910 /* This was a friend declared with an explicit template
9911 argument list, e.g.:
9915 to indicate that f was a template instantiation, not a new
9916 function declaration. Now, we have to figure out what
9917 instantiation of what template. */
9919 tree template_id
, arglist
, fns
;
9922 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9924 /* Friend functions are looked up in the containing namespace scope.
9925 We must enter that scope, to avoid finding member functions of the
9926 current class with same name. */
9927 push_nested_namespace (ns
);
9928 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9929 tf_warning_or_error
, NULL_TREE
,
9930 /*integral_constant_expression_p=*/false);
9931 pop_nested_namespace (ns
);
9932 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9933 tf_warning_or_error
, NULL_TREE
);
9934 template_id
= lookup_template_function (fns
, arglist
);
9936 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9937 tmpl
= determine_specialization (template_id
, new_friend
,
9939 /*need_member_template=*/0,
9940 TREE_VEC_LENGTH (args
),
9942 return instantiate_template (tmpl
, new_args
, tf_error
);
9945 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9947 /* The NEW_FRIEND will look like an instantiation, to the
9948 compiler, but is not an instantiation from the point of view of
9949 the language. For example, we might have had:
9951 template <class T> struct S {
9952 template <class U> friend void f(T, U);
9955 Then, in S<int>, template <class U> void f(int, U) is not an
9956 instantiation of anything. */
9957 if (new_friend
== error_mark_node
)
9958 return error_mark_node
;
9960 DECL_USE_TEMPLATE (new_friend
) = 0;
9961 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9963 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9964 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9965 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9968 /* The mangled name for the NEW_FRIEND is incorrect. The function
9969 is not a template instantiation and should not be mangled like
9970 one. Therefore, we forget the mangling here; we'll recompute it
9971 later if we need it. */
9972 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9974 SET_DECL_RTL (new_friend
, NULL
);
9975 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9978 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9981 tree new_friend_template_info
;
9982 tree new_friend_result_template_info
;
9984 int new_friend_is_defn
;
9986 /* We must save some information from NEW_FRIEND before calling
9987 duplicate decls since that function will free NEW_FRIEND if
9989 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9990 new_friend_is_defn
=
9991 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9992 (template_for_substitution (new_friend
)))
9994 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9996 /* This declaration is a `primary' template. */
9997 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9999 new_friend_result_template_info
10000 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10003 new_friend_result_template_info
= NULL_TREE
;
10005 /* Inside pushdecl_namespace_level, we will push into the
10006 current namespace. However, the friend function should go
10007 into the namespace of the template. */
10008 ns
= decl_namespace_context (new_friend
);
10009 push_nested_namespace (ns
);
10010 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10011 pop_nested_namespace (ns
);
10013 if (old_decl
== error_mark_node
)
10014 return error_mark_node
;
10016 if (old_decl
!= new_friend
)
10018 /* This new friend declaration matched an existing
10019 declaration. For example, given:
10021 template <class T> void f(T);
10022 template <class U> class C {
10023 template <class T> friend void f(T) {}
10026 the friend declaration actually provides the definition
10027 of `f', once C has been instantiated for some type. So,
10028 old_decl will be the out-of-class template declaration,
10029 while new_friend is the in-class definition.
10031 But, if `f' was called before this point, the
10032 instantiation of `f' will have DECL_TI_ARGS corresponding
10033 to `T' but not to `U', references to which might appear
10034 in the definition of `f'. Previously, the most general
10035 template for an instantiation of `f' was the out-of-class
10036 version; now it is the in-class version. Therefore, we
10037 run through all specialization of `f', adding to their
10038 DECL_TI_ARGS appropriately. In particular, they need a
10039 new set of outer arguments, corresponding to the
10040 arguments for this class instantiation.
10042 The same situation can arise with something like this:
10044 friend void f(int);
10045 template <class T> class C {
10046 friend void f(T) {}
10049 when `C<int>' is instantiated. Now, `f(int)' is defined
10052 if (!new_friend_is_defn
)
10053 /* On the other hand, if the in-class declaration does
10054 *not* provide a definition, then we don't want to alter
10055 existing definitions. We can just leave everything
10060 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10061 tree new_args
= TI_ARGS (new_friend_template_info
);
10063 /* Overwrite whatever template info was there before, if
10064 any, with the new template information pertaining to
10065 the declaration. */
10066 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10068 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10070 /* We should have called reregister_specialization in
10071 duplicate_decls. */
10072 gcc_assert (retrieve_specialization (new_template
,
10076 /* Instantiate it if the global has already been used. */
10077 if (DECL_ODR_USED (old_decl
))
10078 instantiate_decl (old_decl
, /*defer_ok=*/true,
10079 /*expl_inst_class_mem_p=*/false);
10085 /* Indicate that the old function template is a partial
10087 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10088 = new_friend_result_template_info
;
10090 gcc_assert (new_template
10091 == most_general_template (new_template
));
10092 gcc_assert (new_template
!= old_decl
);
10094 /* Reassign any specializations already in the hash table
10095 to the new more general template, and add the
10096 additional template args. */
10097 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10099 t
= TREE_CHAIN (t
))
10101 tree spec
= TREE_VALUE (t
);
10104 elt
.tmpl
= old_decl
;
10105 elt
.args
= DECL_TI_ARGS (spec
);
10106 elt
.spec
= NULL_TREE
;
10108 decl_specializations
->remove_elt (&elt
);
10110 DECL_TI_ARGS (spec
)
10111 = add_outermost_template_args (new_args
,
10112 DECL_TI_ARGS (spec
));
10114 register_specialization
10115 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10118 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10122 /* The information from NEW_FRIEND has been merged into OLD_DECL
10123 by duplicate_decls. */
10124 new_friend
= old_decl
;
10129 tree context
= DECL_CONTEXT (new_friend
);
10133 template <class T> class C {
10134 template <class U> friend void C1<U>::f (); // case 1
10135 friend void C2<T>::f (); // case 2
10137 we only need to make sure CONTEXT is a complete type for
10138 case 2. To distinguish between the two cases, we note that
10139 CONTEXT of case 1 remains dependent type after tsubst while
10140 this isn't true for case 2. */
10141 ++processing_template_decl
;
10142 dependent_p
= dependent_type_p (context
);
10143 --processing_template_decl
;
10146 && !complete_type_or_else (context
, NULL_TREE
))
10147 return error_mark_node
;
10149 if (COMPLETE_TYPE_P (context
))
10151 tree fn
= new_friend
;
10152 /* do_friend adds the TEMPLATE_DECL for any member friend
10153 template even if it isn't a member template, i.e.
10154 template <class T> friend A<T>::f();
10155 Look through it in that case. */
10156 if (TREE_CODE (fn
) == TEMPLATE_DECL
10157 && !PRIMARY_TEMPLATE_P (fn
))
10158 fn
= DECL_TEMPLATE_RESULT (fn
);
10159 /* Check to see that the declaration is really present, and,
10160 possibly obtain an improved declaration. */
10161 fn
= check_classfn (context
, fn
, NULL_TREE
);
10171 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10172 template arguments, as for tsubst.
10174 Returns an appropriate tsubst'd friend type or error_mark_node on
10178 tsubst_friend_class (tree friend_tmpl
, tree args
)
10182 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10184 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10185 return TREE_TYPE (tmpl
);
10188 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10189 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10190 push_nested_namespace (context
);
10192 push_nested_class (context
);
10194 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10195 /*non_class=*/false, /*block_p=*/false,
10196 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10198 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10200 /* The friend template has already been declared. Just
10201 check to see that the declarations match, and install any new
10202 default parameters. We must tsubst the default parameters,
10203 of course. We only need the innermost template parameters
10204 because that is all that redeclare_class_template will look
10206 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10207 > TMPL_ARGS_DEPTH (args
))
10209 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10210 args
, tf_warning_or_error
);
10211 location_t saved_input_location
= input_location
;
10212 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10213 tree cons
= get_constraints (tmpl
);
10214 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10215 input_location
= saved_input_location
;
10220 /* The friend template has not already been declared. In this
10221 case, the instantiation of the template class will cause the
10222 injection of this template into the namespace scope. */
10223 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10225 if (tmpl
!= error_mark_node
)
10227 /* The new TMPL is not an instantiation of anything, so we
10228 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10229 for the new type because that is supposed to be the
10230 corresponding template decl, i.e., TMPL. */
10231 DECL_USE_TEMPLATE (tmpl
) = 0;
10232 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10233 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10234 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10235 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10237 /* It is hidden. */
10238 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10239 DECL_ANTICIPATED (tmpl
)
10240 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10242 /* Inject this template into the enclosing namspace scope. */
10243 tmpl
= pushdecl_namespace_level (tmpl
, true);
10247 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10248 pop_nested_namespace (context
);
10250 pop_nested_class ();
10252 return TREE_TYPE (tmpl
);
10255 /* Returns zero if TYPE cannot be completed later due to circularity.
10256 Otherwise returns one. */
10259 can_complete_type_without_circularity (tree type
)
10261 if (type
== NULL_TREE
|| type
== error_mark_node
)
10263 else if (COMPLETE_TYPE_P (type
))
10265 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10266 return can_complete_type_without_circularity (TREE_TYPE (type
));
10267 else if (CLASS_TYPE_P (type
)
10268 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10274 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10275 tsubst_flags_t
, tree
);
10277 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10278 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10281 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10282 tsubst_flags_t complain
, tree in_decl
)
10284 gcc_assert (ATTR_IS_DEPENDENT (t
));
10286 tree val
= TREE_VALUE (t
);
10287 if (val
== NULL_TREE
)
10288 /* Nothing to do. */;
10289 else if ((flag_openmp
|| flag_openmp_simd
)
10290 && is_attribute_p ("omp declare simd",
10291 get_attribute_name (t
)))
10293 tree clauses
= TREE_VALUE (val
);
10294 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10295 complain
, in_decl
);
10296 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10297 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10298 tree parms
= DECL_ARGUMENTS (*decl_p
);
10300 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10302 val
= build_tree_list (NULL_TREE
, clauses
);
10306 /* If the first attribute argument is an identifier, don't
10307 pass it through tsubst. Attributes like mode, format,
10308 cleanup and several target specific attributes expect it
10310 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10313 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10314 /*integral_constant_expression_p=*/false);
10315 if (chain
!= TREE_CHAIN (val
))
10316 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10318 else if (PACK_EXPANSION_P (val
))
10320 /* An attribute pack expansion. */
10321 tree purp
= TREE_PURPOSE (t
);
10322 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10323 if (pack
== error_mark_node
)
10324 return error_mark_node
;
10325 int len
= TREE_VEC_LENGTH (pack
);
10326 tree list
= NULL_TREE
;
10328 for (int i
= 0; i
< len
; ++i
)
10330 tree elt
= TREE_VEC_ELT (pack
, i
);
10331 *q
= build_tree_list (purp
, elt
);
10332 q
= &TREE_CHAIN (*q
);
10337 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10338 /*integral_constant_expression_p=*/false);
10340 if (val
!= TREE_VALUE (t
))
10341 return build_tree_list (TREE_PURPOSE (t
), val
);
10345 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10346 unchanged or a new TREE_LIST chain. */
10349 tsubst_attributes (tree attributes
, tree args
,
10350 tsubst_flags_t complain
, tree in_decl
)
10352 tree last_dep
= NULL_TREE
;
10354 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10355 if (ATTR_IS_DEPENDENT (t
))
10358 attributes
= copy_list (attributes
);
10363 for (tree
*p
= &attributes
; *p
; )
10366 if (ATTR_IS_DEPENDENT (t
))
10368 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10373 p
= &TREE_CHAIN (*p
);
10374 *p
= TREE_CHAIN (t
);
10378 p
= &TREE_CHAIN (*p
);
10384 /* Apply any attributes which had to be deferred until instantiation
10385 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10386 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10389 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10390 tree args
, tsubst_flags_t complain
, tree in_decl
)
10392 tree last_dep
= NULL_TREE
;
10396 if (attributes
== NULL_TREE
)
10399 if (DECL_P (*decl_p
))
10401 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10403 p
= &DECL_ATTRIBUTES (*decl_p
);
10404 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10405 to our attributes parameter. */
10406 gcc_assert (*p
== attributes
);
10410 p
= &TYPE_ATTRIBUTES (*decl_p
);
10411 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10412 lookup_template_class_1, and should be preserved. */
10413 gcc_assert (*p
!= attributes
);
10415 p
= &TREE_CHAIN (*p
);
10418 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10419 if (ATTR_IS_DEPENDENT (t
))
10422 attributes
= copy_list (attributes
);
10429 tree late_attrs
= NULL_TREE
;
10430 tree
*q
= &late_attrs
;
10435 if (ATTR_IS_DEPENDENT (t
))
10437 *p
= TREE_CHAIN (t
);
10438 TREE_CHAIN (t
) = NULL_TREE
;
10439 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10441 q
= &TREE_CHAIN (*q
);
10444 p
= &TREE_CHAIN (t
);
10447 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10451 /* Perform (or defer) access check for typedefs that were referenced
10452 from within the template TMPL code.
10453 This is a subroutine of instantiate_decl and instantiate_class_template.
10454 TMPL is the template to consider and TARGS is the list of arguments of
10458 perform_typedefs_access_check (tree tmpl
, tree targs
)
10460 location_t saved_location
;
10462 qualified_typedef_usage_t
*iter
;
10465 || (!CLASS_TYPE_P (tmpl
)
10466 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10469 saved_location
= input_location
;
10470 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10472 tree type_decl
= iter
->typedef_decl
;
10473 tree type_scope
= iter
->context
;
10475 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10478 if (uses_template_parms (type_decl
))
10479 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10480 if (uses_template_parms (type_scope
))
10481 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10483 /* Make access check error messages point to the location
10484 of the use of the typedef. */
10485 input_location
= iter
->locus
;
10486 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10487 type_decl
, type_decl
,
10488 tf_warning_or_error
);
10490 input_location
= saved_location
;
10494 instantiate_class_template_1 (tree type
)
10496 tree templ
, args
, pattern
, t
, member
;
10500 unsigned int saved_maximum_field_alignment
;
10503 if (type
== error_mark_node
)
10504 return error_mark_node
;
10506 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10507 || uses_template_parms (type
))
10510 /* Figure out which template is being instantiated. */
10511 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10512 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10514 /* Mark the type as in the process of being defined. */
10515 TYPE_BEING_DEFINED (type
) = 1;
10517 /* Determine what specialization of the original template to
10519 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10520 if (t
== error_mark_node
)
10521 return error_mark_node
;
10524 /* This TYPE is actually an instantiation of a partial
10525 specialization. We replace the innermost set of ARGS with
10526 the arguments appropriate for substitution. For example,
10529 template <class T> struct S {};
10530 template <class T> struct S<T*> {};
10532 and supposing that we are instantiating S<int*>, ARGS will
10533 presently be {int*} -- but we need {int}. */
10534 pattern
= TREE_TYPE (t
);
10535 args
= TREE_PURPOSE (t
);
10539 pattern
= TREE_TYPE (templ
);
10540 args
= CLASSTYPE_TI_ARGS (type
);
10543 /* If the template we're instantiating is incomplete, then clearly
10544 there's nothing we can do. */
10545 if (!COMPLETE_TYPE_P (pattern
))
10547 /* We can try again later. */
10548 TYPE_BEING_DEFINED (type
) = 0;
10552 /* If we've recursively instantiated too many templates, stop. */
10553 if (! push_tinst_level (type
))
10556 /* We may be in the middle of deferred access check. Disable
10558 push_deferring_access_checks (dk_no_deferred
);
10560 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10561 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10563 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10564 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10565 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10566 fn_context
= error_mark_node
;
10568 push_to_top_level ();
10571 cp_unevaluated_operand
= 0;
10572 c_inhibit_evaluation_warnings
= 0;
10574 /* Use #pragma pack from the template context. */
10575 saved_maximum_field_alignment
= maximum_field_alignment
;
10576 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10578 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10580 /* Set the input location to the most specialized template definition.
10581 This is needed if tsubsting causes an error. */
10582 typedecl
= TYPE_MAIN_DECL (pattern
);
10583 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10584 DECL_SOURCE_LOCATION (typedecl
);
10586 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10587 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10588 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10589 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10590 if (ANON_AGGR_TYPE_P (pattern
))
10591 SET_ANON_AGGR_TYPE_P (type
);
10592 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10594 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10595 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10596 /* Adjust visibility for template arguments. */
10597 determine_visibility (TYPE_MAIN_DECL (type
));
10599 if (CLASS_TYPE_P (type
))
10600 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10602 pbinfo
= TYPE_BINFO (pattern
);
10604 /* We should never instantiate a nested class before its enclosing
10605 class; we need to look up the nested class by name before we can
10606 instantiate it, and that lookup should instantiate the enclosing
10608 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10609 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10611 base_list
= NULL_TREE
;
10612 if (BINFO_N_BASE_BINFOS (pbinfo
))
10618 /* We must enter the scope containing the type, as that is where
10619 the accessibility of types named in dependent bases are
10621 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10623 /* Substitute into each of the bases to determine the actual
10625 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10628 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10629 tree expanded_bases
= NULL_TREE
;
10632 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10635 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10636 args
, tf_error
, NULL_TREE
);
10637 if (expanded_bases
== error_mark_node
)
10640 len
= TREE_VEC_LENGTH (expanded_bases
);
10643 for (idx
= 0; idx
< len
; idx
++)
10645 if (expanded_bases
)
10646 /* Extract the already-expanded base class. */
10647 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10649 /* Substitute to figure out the base class. */
10650 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10653 if (base
== error_mark_node
)
10656 base_list
= tree_cons (access
, base
, base_list
);
10657 if (BINFO_VIRTUAL_P (pbase_binfo
))
10658 TREE_TYPE (base_list
) = integer_type_node
;
10662 /* The list is now in reverse order; correct that. */
10663 base_list
= nreverse (base_list
);
10666 pop_scope (pushed_scope
);
10668 /* Now call xref_basetypes to set up all the base-class
10670 xref_basetypes (type
, base_list
);
10672 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10673 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10674 args
, tf_error
, NULL_TREE
);
10675 fixup_attribute_variants (type
);
10677 /* Now that our base classes are set up, enter the scope of the
10678 class, so that name lookups into base classes, etc. will work
10679 correctly. This is precisely analogous to what we do in
10680 begin_class_definition when defining an ordinary non-template
10681 class, except we also need to push the enclosing classes. */
10682 push_nested_class (type
);
10684 /* Now members are processed in the order of declaration. */
10685 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10686 member
; member
= TREE_CHAIN (member
))
10688 tree t
= TREE_VALUE (member
);
10690 if (TREE_PURPOSE (member
))
10694 if (LAMBDA_TYPE_P (t
))
10695 /* A closure type for a lambda in an NSDMI or default argument.
10696 Ignore it; it will be regenerated when needed. */
10699 /* Build new CLASSTYPE_NESTED_UTDS. */
10702 bool class_template_p
;
10704 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10705 && TYPE_LANG_SPECIFIC (t
)
10706 && CLASSTYPE_IS_TEMPLATE (t
));
10707 /* If the member is a class template, then -- even after
10708 substitution -- there may be dependent types in the
10709 template argument list for the class. We increment
10710 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10711 that function will assume that no types are dependent
10712 when outside of a template. */
10713 if (class_template_p
)
10714 ++processing_template_decl
;
10715 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10716 if (class_template_p
)
10717 --processing_template_decl
;
10718 if (newtag
== error_mark_node
)
10721 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10723 tree name
= TYPE_IDENTIFIER (t
);
10725 if (class_template_p
)
10726 /* Unfortunately, lookup_template_class sets
10727 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10728 instantiation (i.e., for the type of a member
10729 template class nested within a template class.)
10730 This behavior is required for
10731 maybe_process_partial_specialization to work
10732 correctly, but is not accurate in this case;
10733 the TAG is not an instantiation of anything.
10734 (The corresponding TEMPLATE_DECL is an
10735 instantiation, but the TYPE is not.) */
10736 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10738 /* Now, we call pushtag to put this NEWTAG into the scope of
10739 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10740 pushtag calling push_template_decl. We don't have to do
10741 this for enums because it will already have been done in
10744 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10745 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10748 else if (DECL_DECLARES_FUNCTION_P (t
))
10752 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10753 ++processing_template_decl
;
10754 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10755 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10756 --processing_template_decl
;
10757 set_current_access_from_decl (r
);
10758 finish_member_declaration (r
);
10759 /* Instantiate members marked with attribute used. */
10760 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10762 if (TREE_CODE (r
) == FUNCTION_DECL
10763 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10764 cp_check_omp_declare_reduction (r
);
10766 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
10767 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10768 /* A closure type for a lambda in an NSDMI or default argument.
10769 Ignore it; it will be regenerated when needed. */;
10772 /* Build new TYPE_FIELDS. */
10773 if (TREE_CODE (t
) == STATIC_ASSERT
)
10777 ++c_inhibit_evaluation_warnings
;
10779 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10780 tf_warning_or_error
, NULL_TREE
,
10781 /*integral_constant_expression_p=*/true);
10782 --c_inhibit_evaluation_warnings
;
10784 finish_static_assert (condition
,
10785 STATIC_ASSERT_MESSAGE (t
),
10786 STATIC_ASSERT_SOURCE_LOCATION (t
),
10787 /*member_p=*/true);
10789 else if (TREE_CODE (t
) != CONST_DECL
)
10792 tree vec
= NULL_TREE
;
10795 /* The file and line for this declaration, to
10796 assist in error message reporting. Since we
10797 called push_tinst_level above, we don't need to
10799 input_location
= DECL_SOURCE_LOCATION (t
);
10801 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10802 ++processing_template_decl
;
10803 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10804 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10805 --processing_template_decl
;
10807 if (TREE_CODE (r
) == TREE_VEC
)
10809 /* A capture pack became multiple fields. */
10811 len
= TREE_VEC_LENGTH (vec
);
10814 for (int i
= 0; i
< len
; ++i
)
10817 r
= TREE_VEC_ELT (vec
, i
);
10822 [t]he initialization (and any associated
10823 side-effects) of a static data member does
10824 not occur unless the static data member is
10825 itself used in a way that requires the
10826 definition of the static data member to
10829 Therefore, we do not substitute into the
10830 initialized for the static data member here. */
10831 finish_static_data_member_decl
10833 /*init=*/NULL_TREE
,
10834 /*init_const_expr_p=*/false,
10835 /*asmspec_tree=*/NULL_TREE
,
10837 /* Instantiate members marked with attribute used. */
10838 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10841 else if (TREE_CODE (r
) == FIELD_DECL
)
10843 /* Determine whether R has a valid type and can be
10844 completed later. If R is invalid, then its type
10845 is replaced by error_mark_node. */
10846 tree rtype
= TREE_TYPE (r
);
10847 if (can_complete_type_without_circularity (rtype
))
10848 complete_type (rtype
);
10850 if (!complete_or_array_type_p (rtype
))
10852 /* If R's type couldn't be completed and
10853 it isn't a flexible array member (whose
10854 type is incomplete by definition) give
10856 cxx_incomplete_type_error (r
, rtype
);
10857 TREE_TYPE (r
) = error_mark_node
;
10861 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10862 such a thing will already have been added to the field
10863 list by tsubst_enum in finish_member_declaration in the
10864 CLASSTYPE_NESTED_UTDS case above. */
10865 if (!(TREE_CODE (r
) == TYPE_DECL
10866 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10867 && DECL_ARTIFICIAL (r
)))
10869 set_current_access_from_decl (r
);
10870 finish_member_declaration (r
);
10878 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10879 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10881 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10883 tree friend_type
= t
;
10884 bool adjust_processing_template_decl
= false;
10886 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10888 /* template <class T> friend class C; */
10889 friend_type
= tsubst_friend_class (friend_type
, args
);
10890 adjust_processing_template_decl
= true;
10892 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10894 /* template <class T> friend class C::D; */
10895 friend_type
= tsubst (friend_type
, args
,
10896 tf_warning_or_error
, NULL_TREE
);
10897 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10898 friend_type
= TREE_TYPE (friend_type
);
10899 adjust_processing_template_decl
= true;
10901 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10902 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10904 /* This could be either
10908 when dependent_type_p is false or
10910 template <class U> friend class T::C;
10913 /* Bump processing_template_decl in case this is something like
10914 template <class T> friend struct A<T>::B. */
10915 ++processing_template_decl
;
10916 friend_type
= tsubst (friend_type
, args
,
10917 tf_warning_or_error
, NULL_TREE
);
10918 if (dependent_type_p (friend_type
))
10919 adjust_processing_template_decl
= true;
10920 --processing_template_decl
;
10922 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
10923 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
10924 && TYPE_HIDDEN_P (friend_type
))
10928 where C hasn't been declared yet. Let's lookup name
10929 from namespace scope directly, bypassing any name that
10930 come from dependent base class. */
10931 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10933 /* The call to xref_tag_from_type does injection for friend
10935 push_nested_namespace (ns
);
10937 xref_tag_from_type (friend_type
, NULL_TREE
,
10938 /*tag_scope=*/ts_current
);
10939 pop_nested_namespace (ns
);
10941 else if (uses_template_parms (friend_type
))
10942 /* friend class C<T>; */
10943 friend_type
= tsubst (friend_type
, args
,
10944 tf_warning_or_error
, NULL_TREE
);
10949 where C is already declared or
10951 friend class C<int>;
10953 We don't have to do anything in these cases. */
10955 if (adjust_processing_template_decl
)
10956 /* Trick make_friend_class into realizing that the friend
10957 we're adding is a template, not an ordinary class. It's
10958 important that we use make_friend_class since it will
10959 perform some error-checking and output cross-reference
10961 ++processing_template_decl
;
10963 if (friend_type
!= error_mark_node
)
10964 make_friend_class (type
, friend_type
, /*complain=*/false);
10966 if (adjust_processing_template_decl
)
10967 --processing_template_decl
;
10971 /* Build new DECL_FRIENDLIST. */
10974 /* The file and line for this declaration, to
10975 assist in error message reporting. Since we
10976 called push_tinst_level above, we don't need to
10978 input_location
= DECL_SOURCE_LOCATION (t
);
10980 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10982 ++processing_template_decl
;
10983 push_deferring_access_checks (dk_no_check
);
10986 r
= tsubst_friend_function (t
, args
);
10987 add_friend (type
, r
, /*complain=*/false);
10988 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10990 pop_deferring_access_checks ();
10991 --processing_template_decl
;
10999 /* Restore these before substituting into the lambda capture
11001 cp_unevaluated_operand
= saved_unevaluated_operand
;
11002 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11005 /* Set the file and line number information to whatever is given for
11006 the class itself. This puts error messages involving generated
11007 implicit functions at a predictable point, and the same point
11008 that would be used for non-template classes. */
11009 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11011 unreverse_member_declarations (type
);
11012 finish_struct_1 (type
);
11013 TYPE_BEING_DEFINED (type
) = 0;
11015 /* We don't instantiate default arguments for member functions. 14.7.1:
11017 The implicit instantiation of a class template specialization causes
11018 the implicit instantiation of the declarations, but not of the
11019 definitions or default arguments, of the class member functions,
11020 member classes, static data members and member templates.... */
11022 /* Some typedefs referenced from within the template code need to be access
11023 checked at template instantiation time, i.e now. These types were
11024 added to the template at parsing time. Let's get those and perform
11025 the access checks then. */
11026 perform_typedefs_access_check (pattern
, args
);
11027 perform_deferred_access_checks (tf_warning_or_error
);
11028 pop_nested_class ();
11029 maximum_field_alignment
= saved_maximum_field_alignment
;
11031 pop_from_top_level ();
11032 pop_deferring_access_checks ();
11033 pop_tinst_level ();
11035 /* The vtable for a template class can be emitted in any translation
11036 unit in which the class is instantiated. When there is no key
11037 method, however, finish_struct_1 will already have added TYPE to
11038 the keyed_classes. */
11039 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11040 vec_safe_push (keyed_classes
, type
);
11045 /* Wrapper for instantiate_class_template_1. */
11048 instantiate_class_template (tree type
)
11051 timevar_push (TV_TEMPLATE_INST
);
11052 ret
= instantiate_class_template_1 (type
);
11053 timevar_pop (TV_TEMPLATE_INST
);
11058 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11064 else if (TYPE_P (t
))
11065 r
= tsubst (t
, args
, complain
, in_decl
);
11068 if (!(complain
& tf_warning
))
11069 ++c_inhibit_evaluation_warnings
;
11070 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11071 /*integral_constant_expression_p=*/true);
11072 if (!(complain
& tf_warning
))
11073 --c_inhibit_evaluation_warnings
;
11078 /* Given a function parameter pack TMPL_PARM and some function parameters
11079 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11080 and set *SPEC_P to point at the next point in the list. */
11083 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11085 /* Collect all of the extra "packed" parameters into an
11088 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11089 tree spec_parm
= *spec_p
;
11092 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11094 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11097 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11098 parmvec
= make_tree_vec (len
);
11099 spec_parm
= *spec_p
;
11100 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11102 tree elt
= spec_parm
;
11103 if (DECL_PACK_P (elt
))
11104 elt
= make_pack_expansion (elt
);
11105 TREE_VEC_ELT (parmvec
, i
) = elt
;
11108 /* Build the argument packs. */
11109 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11110 *spec_p
= spec_parm
;
11115 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11116 NONTYPE_ARGUMENT_PACK. */
11119 make_fnparm_pack (tree spec_parm
)
11121 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11124 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11125 pack expansion with no extra args, 2 if it has extra args, or 0
11126 if it is not a pack expansion. */
11129 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11131 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11132 if (i
>= TREE_VEC_LENGTH (vec
))
11134 tree elt
= TREE_VEC_ELT (vec
, i
);
11136 /* A decl pack is itself an expansion. */
11137 elt
= TREE_TYPE (elt
);
11138 if (!PACK_EXPANSION_P (elt
))
11140 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11146 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11149 make_argument_pack_select (tree arg_pack
, unsigned index
)
11151 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11153 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11154 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11159 /* This is a subroutine of tsubst_pack_expansion.
11161 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11162 mechanism to store the (non complete list of) arguments of the
11163 substitution and return a non substituted pack expansion, in order
11164 to wait for when we have enough arguments to really perform the
11168 use_pack_expansion_extra_args_p (tree parm_packs
,
11170 bool has_empty_arg
)
11172 /* If one pack has an expansion and another pack has a normal
11173 argument or if one pack has an empty argument and an another
11174 one hasn't then tsubst_pack_expansion cannot perform the
11175 substitution and need to fall back on the
11176 PACK_EXPANSION_EXTRA mechanism. */
11177 if (parm_packs
== NULL_TREE
)
11179 else if (has_empty_arg
)
11182 bool has_expansion_arg
= false;
11183 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11185 bool has_non_expansion_arg
= false;
11186 for (tree parm_pack
= parm_packs
;
11188 parm_pack
= TREE_CHAIN (parm_pack
))
11190 tree arg
= TREE_VALUE (parm_pack
);
11192 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11194 /* We can't substitute a pack expansion with extra args into
11198 has_expansion_arg
= true;
11200 has_non_expansion_arg
= true;
11203 if (has_expansion_arg
&& has_non_expansion_arg
)
11209 /* [temp.variadic]/6 says that:
11211 The instantiation of a pack expansion [...]
11212 produces a list E1,E2, ..., En, where N is the number of elements
11213 in the pack expansion parameters.
11215 This subroutine of tsubst_pack_expansion produces one of these Ei.
11217 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11218 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11219 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11220 INDEX is the index 'i' of the element Ei to produce. ARGS,
11221 COMPLAIN, and IN_DECL are the same parameters as for the
11222 tsubst_pack_expansion function.
11224 The function returns the resulting Ei upon successful completion,
11225 or error_mark_node.
11227 Note that this function possibly modifies the ARGS parameter, so
11228 it's the responsibility of the caller to restore it. */
11231 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11234 tree args
/* This parm gets
11236 tsubst_flags_t complain
,
11240 bool ith_elem_is_expansion
= false;
11242 /* For each parameter pack, change the substitution of the parameter
11243 pack to the ith argument in its argument pack, then expand the
11245 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11247 tree parm
= TREE_PURPOSE (pack
);
11248 tree arg_pack
= TREE_VALUE (pack
);
11249 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11251 ith_elem_is_expansion
|=
11252 argument_pack_element_is_expansion_p (arg_pack
, index
);
11254 /* Select the Ith argument from the pack. */
11255 if (TREE_CODE (parm
) == PARM_DECL
11257 || TREE_CODE (parm
) == FIELD_DECL
)
11261 aps
= make_argument_pack_select (arg_pack
, index
);
11262 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11263 return error_mark_node
;
11264 register_local_specialization (aps
, parm
);
11267 aps
= retrieve_local_specialization (parm
);
11272 template_parm_level_and_index (parm
, &level
, &idx
);
11276 aps
= make_argument_pack_select (arg_pack
, index
);
11277 /* Update the corresponding argument. */
11278 TMPL_ARG (args
, level
, idx
) = aps
;
11281 /* Re-use the ARGUMENT_PACK_SELECT. */
11282 aps
= TMPL_ARG (args
, level
, idx
);
11284 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11287 /* Substitute into the PATTERN with the (possibly altered)
11289 if (pattern
== in_decl
)
11290 /* Expanding a fixed parameter pack from
11291 coerce_template_parameter_pack. */
11292 t
= tsubst_decl (pattern
, args
, complain
);
11293 else if (pattern
== error_mark_node
)
11294 t
= error_mark_node
;
11295 else if (constraint_p (pattern
))
11297 if (processing_template_decl
)
11298 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11300 t
= (constraints_satisfied_p (pattern
, args
)
11301 ? boolean_true_node
: boolean_false_node
);
11303 else if (!TYPE_P (pattern
))
11304 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11305 /*integral_constant_expression_p=*/false);
11307 t
= tsubst (pattern
, args
, complain
, in_decl
);
11309 /* If the Ith argument pack element is a pack expansion, then
11310 the Ith element resulting from the substituting is going to
11311 be a pack expansion as well. */
11312 if (ith_elem_is_expansion
)
11313 t
= make_pack_expansion (t
, complain
);
11318 /* When the unexpanded parameter pack in a fold expression expands to an empty
11319 sequence, the value of the expression is as follows; the program is
11320 ill-formed if the operator is not listed in this table.
11327 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11329 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11330 if (!FOLD_EXPR_MODIFY_P (t
))
11333 case TRUTH_ANDIF_EXPR
:
11334 return boolean_true_node
;
11335 case TRUTH_ORIF_EXPR
:
11336 return boolean_false_node
;
11337 case COMPOUND_EXPR
:
11343 if (complain
& tf_error
)
11344 error_at (location_of (t
),
11345 "fold of empty expansion over %O", code
);
11346 return error_mark_node
;
11349 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11350 form an expression that combines the two terms using the
11354 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11356 tree op
= FOLD_EXPR_OP (t
);
11357 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11359 // Handle compound assignment operators.
11360 if (FOLD_EXPR_MODIFY_P (t
))
11361 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11365 case COMPOUND_EXPR
:
11366 return build_x_compound_expr (input_location
, left
, right
, complain
);
11368 return build_m_component_ref (left
, right
, complain
);
11370 return build_x_binary_op (input_location
, code
,
11371 left
, TREE_CODE (left
),
11372 right
, TREE_CODE (right
),
11378 /* Substitute ARGS into the pack of a fold expression T. */
11381 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11383 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11386 /* Substitute ARGS into the pack of a fold expression T. */
11389 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11391 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11394 /* Expand a PACK of arguments into a grouped as left fold.
11395 Given a pack containing elements A0, A1, ..., An and an
11396 operator @, this builds the expression:
11398 ((A0 @ A1) @ A2) ... @ An
11400 Note that PACK must not be empty.
11402 The operator is defined by the original fold expression T. */
11405 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11407 tree left
= TREE_VEC_ELT (pack
, 0);
11408 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11410 tree right
= TREE_VEC_ELT (pack
, i
);
11411 left
= fold_expression (t
, left
, right
, complain
);
11416 /* Substitute into a unary left fold expression. */
11419 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11422 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11423 if (pack
== error_mark_node
)
11424 return error_mark_node
;
11425 if (PACK_EXPANSION_P (pack
))
11427 tree r
= copy_node (t
);
11428 FOLD_EXPR_PACK (r
) = pack
;
11431 if (TREE_VEC_LENGTH (pack
) == 0)
11432 return expand_empty_fold (t
, complain
);
11434 return expand_left_fold (t
, pack
, complain
);
11437 /* Substitute into a binary left fold expression.
11439 Do ths by building a single (non-empty) vector of argumnts and
11440 building the expression from those elements. */
11443 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11446 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11447 if (pack
== error_mark_node
)
11448 return error_mark_node
;
11449 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11450 if (init
== error_mark_node
)
11451 return error_mark_node
;
11453 if (PACK_EXPANSION_P (pack
))
11455 tree r
= copy_node (t
);
11456 FOLD_EXPR_PACK (r
) = pack
;
11457 FOLD_EXPR_INIT (r
) = init
;
11461 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11462 TREE_VEC_ELT (vec
, 0) = init
;
11463 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11464 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11466 return expand_left_fold (t
, vec
, complain
);
11469 /* Expand a PACK of arguments into a grouped as right fold.
11470 Given a pack containing elementns A0, A1, ..., and an
11471 operator @, this builds the expression:
11473 A0@ ... (An-2 @ (An-1 @ An))
11475 Note that PACK must not be empty.
11477 The operator is defined by the original fold expression T. */
11480 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11482 // Build the expression.
11483 int n
= TREE_VEC_LENGTH (pack
);
11484 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11485 for (--n
; n
!= 0; --n
)
11487 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11488 right
= fold_expression (t
, left
, right
, complain
);
11493 /* Substitute into a unary right fold expression. */
11496 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11499 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11500 if (pack
== error_mark_node
)
11501 return error_mark_node
;
11502 if (PACK_EXPANSION_P (pack
))
11504 tree r
= copy_node (t
);
11505 FOLD_EXPR_PACK (r
) = pack
;
11508 if (TREE_VEC_LENGTH (pack
) == 0)
11509 return expand_empty_fold (t
, complain
);
11511 return expand_right_fold (t
, pack
, complain
);
11514 /* Substitute into a binary right fold expression.
11516 Do ths by building a single (non-empty) vector of arguments and
11517 building the expression from those elements. */
11520 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11523 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11524 if (pack
== error_mark_node
)
11525 return error_mark_node
;
11526 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11527 if (init
== error_mark_node
)
11528 return error_mark_node
;
11530 if (PACK_EXPANSION_P (pack
))
11532 tree r
= copy_node (t
);
11533 FOLD_EXPR_PACK (r
) = pack
;
11534 FOLD_EXPR_INIT (r
) = init
;
11538 int n
= TREE_VEC_LENGTH (pack
);
11539 tree vec
= make_tree_vec (n
+ 1);
11540 for (int i
= 0; i
< n
; ++i
)
11541 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11542 TREE_VEC_ELT (vec
, n
) = init
;
11544 return expand_right_fold (t
, vec
, complain
);
11547 /* Walk through the pattern of a pack expansion, adding everything in
11548 local_specializations to a list. */
11553 tsubst_flags_t complain
;
11556 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11558 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11559 tree
*extra
= &data
.extra
;
11560 tsubst_flags_t complain
= data
.complain
;
11561 if (tree spec
= retrieve_local_specialization (*tp
))
11563 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11565 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11566 tree args
= ARGUMENT_PACK_ARGS (spec
);
11567 if (TREE_VEC_LENGTH (args
) == 1)
11569 tree elt
= TREE_VEC_ELT (args
, 0);
11570 if (PACK_EXPANSION_P (elt
))
11571 elt
= PACK_EXPANSION_PATTERN (elt
);
11572 if (DECL_PACK_P (elt
))
11575 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11577 /* Handle lambda capture here, since we aren't doing any
11578 substitution now, and so tsubst_copy won't call
11579 process_outer_var_ref. */
11580 tree args
= ARGUMENT_PACK_ARGS (spec
);
11581 int len
= TREE_VEC_LENGTH (args
);
11582 for (int i
= 0; i
< len
; ++i
)
11584 tree arg
= TREE_VEC_ELT (args
, i
);
11586 if (outer_automatic_var_p (arg
))
11587 carg
= process_outer_var_ref (arg
, complain
);
11590 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11594 spec
= copy_node (spec
);
11595 args
= copy_node (args
);
11596 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11597 register_local_specialization (spec
, *tp
);
11599 TREE_VEC_ELT (args
, i
) = carg
;
11604 if (outer_automatic_var_p (spec
))
11605 spec
= process_outer_var_ref (spec
, complain
);
11606 *extra
= tree_cons (*tp
, spec
, *extra
);
11611 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11613 el_data data
= { NULL_TREE
, complain
};
11614 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
11618 /* Substitute ARGS into T, which is an pack expansion
11619 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11620 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11621 (if only a partial substitution could be performed) or
11622 ERROR_MARK_NODE if there was an error. */
11624 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11628 tree pack
, packs
= NULL_TREE
;
11629 bool unsubstituted_packs
= false;
11630 bool unsubstituted_fn_pack
= false;
11633 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11634 bool need_local_specializations
= false;
11637 gcc_assert (PACK_EXPANSION_P (t
));
11638 pattern
= PACK_EXPANSION_PATTERN (t
);
11640 /* Add in any args remembered from an earlier partial instantiation. */
11641 tree extra
= PACK_EXPANSION_EXTRA_ARGS (t
);
11642 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
11644 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
11646 /* The partial instantiation involved local declarations collected in
11647 extract_local_specs; map from the general template to our local
11649 tree gen
= TREE_PURPOSE (elt
);
11650 tree inst
= TREE_VALUE (elt
);
11652 if (tree local
= retrieve_local_specialization (inst
))
11654 /* else inst is already a full instantiation of the pack. */
11655 register_local_specialization (inst
, gen
);
11657 gcc_assert (!TREE_PURPOSE (extra
));
11658 extra
= TREE_VALUE (extra
);
11660 args
= add_to_template_args (extra
, args
);
11662 levels
= TMPL_ARGS_DEPTH (args
);
11664 /* Determine the argument packs that will instantiate the parameter
11665 packs used in the expansion expression. While we're at it,
11666 compute the number of arguments to be expanded and make sure it
11668 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11669 pack
= TREE_CHAIN (pack
))
11671 tree parm_pack
= TREE_VALUE (pack
);
11672 tree arg_pack
= NULL_TREE
;
11673 tree orig_arg
= NULL_TREE
;
11676 if (TREE_CODE (parm_pack
) == BASES
)
11678 gcc_assert (parm_pack
== pattern
);
11679 if (BASES_DIRECT (parm_pack
))
11680 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11681 args
, complain
, in_decl
, false));
11683 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11684 args
, complain
, in_decl
, false));
11686 else if (builtin_pack_call_p (parm_pack
))
11688 /* ??? Support use in other patterns. */
11689 gcc_assert (parm_pack
== pattern
);
11690 return expand_builtin_pack_call (parm_pack
, args
,
11691 complain
, in_decl
);
11693 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11695 /* We know we have correct local_specializations if this
11696 expansion is at function scope, or if we're dealing with a
11697 local parameter in a requires expression; for the latter,
11698 tsubst_requires_expr set it up appropriately. */
11699 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11700 arg_pack
= retrieve_local_specialization (parm_pack
);
11702 /* We can't rely on local_specializations for a parameter
11703 name used later in a function declaration (such as in a
11704 late-specified return type). Even if it exists, it might
11705 have the wrong value for a recursive call. */
11706 need_local_specializations
= true;
11710 /* This parameter pack was used in an unevaluated context. Just
11711 make a dummy decl, since it's only used for its type. */
11712 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11713 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11714 /* Partial instantiation of the parm_pack, we can't build
11715 up an argument pack yet. */
11716 arg_pack
= NULL_TREE
;
11718 arg_pack
= make_fnparm_pack (arg_pack
);
11720 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11721 /* This argument pack isn't fully instantiated yet. We set this
11722 flag rather than clear arg_pack because we do want to do the
11723 optimization below, and we don't want to substitute directly
11724 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11725 where it isn't expected). */
11726 unsubstituted_fn_pack
= true;
11728 else if (is_normal_capture_proxy (parm_pack
))
11730 arg_pack
= retrieve_local_specialization (parm_pack
);
11731 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
11732 unsubstituted_fn_pack
= true;
11737 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11739 if (level
<= levels
)
11740 arg_pack
= TMPL_ARG (args
, level
, idx
);
11743 orig_arg
= arg_pack
;
11744 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11745 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11747 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11748 /* This can only happen if we forget to expand an argument
11749 pack somewhere else. Just return an error, silently. */
11751 result
= make_tree_vec (1);
11752 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11759 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11761 /* Don't bother trying to do a partial substitution with
11762 incomplete packs; we'll try again after deduction. */
11763 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11768 else if (len
!= my_len
11769 && !unsubstituted_fn_pack
)
11771 if (!(complain
& tf_error
))
11772 /* Fail quietly. */;
11773 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11774 error ("mismatched argument pack lengths while expanding %qT",
11777 error ("mismatched argument pack lengths while expanding %qE",
11779 return error_mark_node
;
11782 /* Keep track of the parameter packs and their corresponding
11784 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11785 TREE_TYPE (packs
) = orig_arg
;
11789 /* We can't substitute for this parameter pack. We use a flag as
11790 well as the missing_level counter because function parameter
11791 packs don't have a level. */
11792 gcc_assert (processing_template_decl
);
11793 unsubstituted_packs
= true;
11797 /* If the expansion is just T..., return the matching argument pack, unless
11798 we need to call convert_from_reference on all the elements. This is an
11799 important optimization; see c++/68422. */
11800 if (!unsubstituted_packs
11801 && TREE_PURPOSE (packs
) == pattern
)
11803 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11805 /* If the argument pack is a single pack expansion, pull it out. */
11806 if (TREE_VEC_LENGTH (args
) == 1
11807 && pack_expansion_args_count (args
))
11808 return TREE_VEC_ELT (args
, 0);
11810 /* Types need no adjustment, nor does sizeof..., and if we still have
11811 some pack expansion args we won't do anything yet. */
11812 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11813 || PACK_EXPANSION_SIZEOF_P (t
)
11814 || pack_expansion_args_count (args
))
11816 /* Also optimize expression pack expansions if we can tell that the
11817 elements won't have reference type. */
11818 tree type
= TREE_TYPE (pattern
);
11819 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11820 && !PACK_EXPANSION_P (type
)
11821 && !WILDCARD_TYPE_P (type
))
11823 /* Otherwise use the normal path so we get convert_from_reference. */
11826 /* We cannot expand this expansion expression, because we don't have
11827 all of the argument packs we need. */
11828 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11830 /* We got some full packs, but we can't substitute them in until we
11831 have values for all the packs. So remember these until then. */
11833 t
= make_pack_expansion (pattern
, complain
);
11835 if (local_specializations
)
11836 if (tree locals
= extract_local_specs (pattern
, complain
))
11837 extra
= tree_cons (NULL_TREE
, extra
, locals
);
11838 PACK_EXPANSION_EXTRA_ARGS (t
) = extra
;
11841 else if (unsubstituted_packs
)
11843 /* There were no real arguments, we're just replacing a parameter
11844 pack with another version of itself. Substitute into the
11845 pattern and return a PACK_EXPANSION_*. The caller will need to
11847 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11848 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11849 /*integral_constant_expression_p=*/false);
11851 t
= tsubst (pattern
, args
, complain
, in_decl
);
11852 t
= make_pack_expansion (t
, complain
);
11856 gcc_assert (len
>= 0);
11858 if (need_local_specializations
)
11860 /* We're in a late-specified return type, so create our own local
11861 specializations map; the current map is either NULL or (in the
11862 case of recursive unification) might have bindings that we don't
11863 want to use or alter. */
11864 saved_local_specializations
= local_specializations
;
11865 local_specializations
= new hash_map
<tree
, tree
>;
11868 /* For each argument in each argument pack, substitute into the
11870 result
= make_tree_vec (len
);
11871 tree elem_args
= copy_template_args (args
);
11872 for (i
= 0; i
< len
; ++i
)
11874 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11876 elem_args
, complain
,
11878 TREE_VEC_ELT (result
, i
) = t
;
11879 if (t
== error_mark_node
)
11881 result
= error_mark_node
;
11886 /* Update ARGS to restore the substitution from parameter packs to
11887 their argument packs. */
11888 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11890 tree parm
= TREE_PURPOSE (pack
);
11892 if (TREE_CODE (parm
) == PARM_DECL
11894 || TREE_CODE (parm
) == FIELD_DECL
)
11895 register_local_specialization (TREE_TYPE (pack
), parm
);
11900 if (TREE_VALUE (pack
) == NULL_TREE
)
11903 template_parm_level_and_index (parm
, &level
, &idx
);
11905 /* Update the corresponding argument. */
11906 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11907 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11910 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11914 if (need_local_specializations
)
11916 delete local_specializations
;
11917 local_specializations
= saved_local_specializations
;
11920 /* If the dependent pack arguments were such that we end up with only a
11921 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11922 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11923 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11924 return TREE_VEC_ELT (result
, 0);
11929 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11930 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11931 parameter packs; all parms generated from a function parameter pack will
11932 have the same DECL_PARM_INDEX. */
11935 get_pattern_parm (tree parm
, tree tmpl
)
11937 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11940 if (DECL_ARTIFICIAL (parm
))
11942 for (patparm
= DECL_ARGUMENTS (pattern
);
11943 patparm
; patparm
= DECL_CHAIN (patparm
))
11944 if (DECL_ARTIFICIAL (patparm
)
11945 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11950 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11951 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11952 gcc_assert (DECL_PARM_INDEX (patparm
)
11953 == DECL_PARM_INDEX (parm
));
11959 /* Make an argument pack out of the TREE_VEC VEC. */
11962 make_argument_pack (tree vec
)
11965 tree elt
= TREE_VEC_ELT (vec
, 0);
11967 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11970 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11971 TREE_CONSTANT (pack
) = 1;
11973 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11977 /* Return an exact copy of template args T that can be modified
11981 copy_template_args (tree t
)
11983 if (t
== error_mark_node
)
11986 int len
= TREE_VEC_LENGTH (t
);
11987 tree new_vec
= make_tree_vec (len
);
11989 for (int i
= 0; i
< len
; ++i
)
11991 tree elt
= TREE_VEC_ELT (t
, i
);
11992 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11993 elt
= copy_template_args (elt
);
11994 TREE_VEC_ELT (new_vec
, i
) = elt
;
11997 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11998 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12003 /* Substitute ARGS into the vector or list of template arguments T. */
12006 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12009 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12012 if (t
== error_mark_node
)
12013 return error_mark_node
;
12015 len
= TREE_VEC_LENGTH (t
);
12016 elts
= XALLOCAVEC (tree
, len
);
12018 for (i
= 0; i
< len
; i
++)
12020 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12023 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12024 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12025 else if (PACK_EXPANSION_P (orig_arg
))
12027 /* Substitute into an expansion expression. */
12028 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12030 if (TREE_CODE (new_arg
) == TREE_VEC
)
12031 /* Add to the expanded length adjustment the number of
12032 expanded arguments. We subtract one from this
12033 measurement, because the argument pack expression
12034 itself is already counted as 1 in
12035 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12036 the argument pack is empty. */
12037 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12039 else if (ARGUMENT_PACK_P (orig_arg
))
12041 /* Substitute into each of the arguments. */
12042 new_arg
= TYPE_P (orig_arg
)
12043 ? cxx_make_type (TREE_CODE (orig_arg
))
12044 : make_node (TREE_CODE (orig_arg
));
12046 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12047 args
, complain
, in_decl
);
12048 if (pack_args
== error_mark_node
)
12049 new_arg
= error_mark_node
;
12051 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12053 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12054 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12057 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12059 if (new_arg
== error_mark_node
)
12060 return error_mark_node
;
12063 if (new_arg
!= orig_arg
)
12070 /* Make space for the expanded arguments coming from template
12072 t
= make_tree_vec (len
+ expanded_len_adjust
);
12073 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12074 arguments for a member template.
12075 In that case each TREE_VEC in ORIG_T represents a level of template
12076 arguments, and ORIG_T won't carry any non defaulted argument count.
12077 It will rather be the nested TREE_VECs that will carry one.
12078 In other words, ORIG_T carries a non defaulted argument count only
12079 if it doesn't contain any nested TREE_VEC. */
12080 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12082 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12083 count
+= expanded_len_adjust
;
12084 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12086 for (i
= 0, out
= 0; i
< len
; i
++)
12088 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12089 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12090 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12094 /* Now expand the template argument pack "in place". */
12095 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12096 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12100 TREE_VEC_ELT (t
, out
) = elts
[i
];
12108 /* Substitute ARGS into one level PARMS of template parameters. */
12111 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12113 if (parms
== error_mark_node
)
12114 return error_mark_node
;
12116 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12118 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12120 tree tuple
= TREE_VEC_ELT (parms
, i
);
12122 if (tuple
== error_mark_node
)
12125 TREE_VEC_ELT (new_vec
, i
) =
12126 tsubst_template_parm (tuple
, args
, complain
);
12132 /* Return the result of substituting ARGS into the template parameters
12133 given by PARMS. If there are m levels of ARGS and m + n levels of
12134 PARMS, then the result will contain n levels of PARMS. For
12135 example, if PARMS is `template <class T> template <class U>
12136 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12137 result will be `template <int*, double, class V>'. */
12140 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12142 tree r
= NULL_TREE
;
12145 /* When substituting into a template, we must set
12146 PROCESSING_TEMPLATE_DECL as the template parameters may be
12147 dependent if they are based on one-another, and the dependency
12148 predicates are short-circuit outside of templates. */
12149 ++processing_template_decl
;
12151 for (new_parms
= &r
;
12152 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12153 new_parms
= &(TREE_CHAIN (*new_parms
)),
12154 parms
= TREE_CHAIN (parms
))
12156 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12159 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12160 - TMPL_ARGS_DEPTH (args
)),
12161 new_vec
, NULL_TREE
);
12164 --processing_template_decl
;
12169 /* Return the result of substituting ARGS into one template parameter
12170 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12171 parameter and which TREE_PURPOSE is the default argument of the
12172 template parameter. */
12175 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12177 tree default_value
, parm_decl
;
12179 if (args
== NULL_TREE
12181 || t
== error_mark_node
)
12184 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12186 default_value
= TREE_PURPOSE (t
);
12187 parm_decl
= TREE_VALUE (t
);
12189 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12190 if (TREE_CODE (parm_decl
) == PARM_DECL
12191 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12192 parm_decl
= error_mark_node
;
12193 default_value
= tsubst_template_arg (default_value
, args
,
12194 complain
, NULL_TREE
);
12196 return build_tree_list (default_value
, parm_decl
);
12199 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12200 type T. If T is not an aggregate or enumeration type, it is
12201 handled as if by tsubst. IN_DECL is as for tsubst. If
12202 ENTERING_SCOPE is nonzero, T is the context for a template which
12203 we are presently tsubst'ing. Return the substituted value. */
12206 tsubst_aggr_type (tree t
,
12208 tsubst_flags_t complain
,
12210 int entering_scope
)
12212 if (t
== NULL_TREE
)
12215 switch (TREE_CODE (t
))
12218 if (TYPE_PTRMEMFUNC_P (t
))
12219 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12221 /* Fall through. */
12222 case ENUMERAL_TYPE
:
12224 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12229 int saved_unevaluated_operand
;
12230 int saved_inhibit_evaluation_warnings
;
12232 /* In "sizeof(X<I>)" we need to evaluate "I". */
12233 saved_unevaluated_operand
= cp_unevaluated_operand
;
12234 cp_unevaluated_operand
= 0;
12235 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12236 c_inhibit_evaluation_warnings
= 0;
12238 /* First, determine the context for the type we are looking
12240 context
= TYPE_CONTEXT (t
);
12241 if (context
&& TYPE_P (context
))
12243 context
= tsubst_aggr_type (context
, args
, complain
,
12244 in_decl
, /*entering_scope=*/1);
12245 /* If context is a nested class inside a class template,
12246 it may still need to be instantiated (c++/33959). */
12247 context
= complete_type (context
);
12250 /* Then, figure out what arguments are appropriate for the
12251 type we are trying to find. For example, given:
12253 template <class T> struct S;
12254 template <class T, class U> void f(T, U) { S<U> su; }
12256 and supposing that we are instantiating f<int, double>,
12257 then our ARGS will be {int, double}, but, when looking up
12258 S we only want {double}. */
12259 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12260 complain
, in_decl
);
12261 if (argvec
== error_mark_node
)
12262 r
= error_mark_node
;
12265 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12266 entering_scope
, complain
);
12267 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12270 cp_unevaluated_operand
= saved_unevaluated_operand
;
12271 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12276 /* This is not a template type, so there's nothing to do. */
12280 return tsubst (t
, args
, complain
, in_decl
);
12284 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12286 /* Substitute into the default argument ARG (a default argument for
12287 FN), which has the indicated TYPE. */
12290 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12291 tsubst_flags_t complain
)
12293 tree saved_class_ptr
= NULL_TREE
;
12294 tree saved_class_ref
= NULL_TREE
;
12295 int errs
= errorcount
+ sorrycount
;
12297 /* This can happen in invalid code. */
12298 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12301 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12302 parm
= chain_index (parmnum
, parm
);
12303 tree parmtype
= TREE_TYPE (parm
);
12304 if (DECL_BY_REFERENCE (parm
))
12305 parmtype
= TREE_TYPE (parmtype
);
12306 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12309 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12312 /* This default argument came from a template. Instantiate the
12313 default argument here, not in tsubst. In the case of
12322 we must be careful to do name lookup in the scope of S<T>,
12323 rather than in the current class. */
12324 push_access_scope (fn
);
12325 /* The "this" pointer is not valid in a default argument. */
12328 saved_class_ptr
= current_class_ptr
;
12329 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12330 saved_class_ref
= current_class_ref
;
12331 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12334 start_lambda_scope (parm
);
12336 push_deferring_access_checks(dk_no_deferred
);
12337 /* The default argument expression may cause implicitly defined
12338 member functions to be synthesized, which will result in garbage
12339 collection. We must treat this situation as if we were within
12340 the body of function so as to avoid collecting live data on the
12343 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12344 complain
, NULL_TREE
,
12345 /*integral_constant_expression_p=*/false);
12347 pop_deferring_access_checks();
12349 finish_lambda_scope ();
12351 /* Restore the "this" pointer. */
12354 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12355 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12358 if (errorcount
+sorrycount
> errs
12359 && (complain
& tf_warning_or_error
))
12360 inform (input_location
,
12361 " when instantiating default argument for call to %qD", fn
);
12363 /* Make sure the default argument is reasonable. */
12364 arg
= check_default_argument (type
, arg
, complain
);
12366 pop_access_scope (fn
);
12368 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12371 defarg_inst
= tree_cache_map::create_ggc (37);
12372 defarg_inst
->put (parm
, arg
);
12378 /* Substitute into all the default arguments for FN. */
12381 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12386 tmpl_args
= DECL_TI_ARGS (fn
);
12388 /* If this function is not yet instantiated, we certainly don't need
12389 its default arguments. */
12390 if (uses_template_parms (tmpl_args
))
12392 /* Don't do this again for clones. */
12393 if (DECL_CLONED_FUNCTION_P (fn
))
12397 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12399 arg
= TREE_CHAIN (arg
), ++i
)
12400 if (TREE_PURPOSE (arg
))
12401 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12403 TREE_PURPOSE (arg
),
12407 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12410 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12411 tree lambda_fntype
)
12413 tree gen_tmpl
, argvec
;
12414 hashval_t hash
= 0;
12417 /* Nobody should be tsubst'ing into non-template functions. */
12418 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12420 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12422 /* If T is not dependent, just return it. */
12423 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12426 /* Calculate the most general template of which R is a
12428 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12430 /* We're substituting a lambda function under tsubst_lambda_expr but not
12431 directly from it; find the matching function we're already inside.
12432 But don't do this if T is a generic lambda with a single level of
12433 template parms, as in that case we're doing a normal instantiation. */
12434 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12435 && (!generic_lambda_fn_p (t
)
12436 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12437 return enclosing_instantiation_of (t
);
12439 /* Calculate the complete set of arguments used to
12441 argvec
= tsubst_template_args (DECL_TI_ARGS
12442 (DECL_TEMPLATE_RESULT
12443 (DECL_TI_TEMPLATE (t
))),
12444 args
, complain
, in_decl
);
12445 if (argvec
== error_mark_node
)
12446 return error_mark_node
;
12448 /* Check to see if we already have this specialization. */
12449 if (!lambda_fntype
)
12451 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12452 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12456 /* We can see more levels of arguments than parameters if
12457 there was a specialization of a member template, like
12460 template <class T> struct S { template <class U> void f(); }
12461 template <> template <class U> void S<int>::f(U);
12463 Here, we'll be substituting into the specialization,
12464 because that's where we can find the code we actually
12465 want to generate, but we'll have enough arguments for
12466 the most general template.
12468 We also deal with the peculiar case:
12470 template <class T> struct S {
12471 template <class U> friend void f();
12473 template <class U> void f() {}
12475 template void f<double>();
12477 Here, the ARGS for the instantiation of will be {int,
12478 double}. But, we only need as many ARGS as there are
12479 levels of template parameters in CODE_PATTERN. We are
12480 careful not to get fooled into reducing the ARGS in
12483 template <class T> struct S { template <class U> void f(U); }
12484 template <class T> template <> void S<T>::f(int) {}
12486 which we can spot because the pattern will be a
12487 specialization in this case. */
12488 int args_depth
= TMPL_ARGS_DEPTH (args
);
12490 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12492 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12493 args
= get_innermost_template_args (args
, parms_depth
);
12497 /* This special case arises when we have something like this:
12499 template <class T> struct S {
12500 friend void f<int>(int, double);
12503 Here, the DECL_TI_TEMPLATE for the friend declaration
12504 will be an IDENTIFIER_NODE. We are being called from
12505 tsubst_friend_function, and we want only to create a
12506 new decl (R) with appropriate types so that we can call
12507 determine_specialization. */
12508 gen_tmpl
= NULL_TREE
;
12509 argvec
= NULL_TREE
;
12512 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12514 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12515 bool member
= ctx
&& TYPE_P (ctx
);
12517 if (member
&& !closure
)
12518 ctx
= tsubst_aggr_type (ctx
, args
,
12519 complain
, t
, /*entering_scope=*/1);
12521 tree type
= (lambda_fntype
? lambda_fntype
12522 : tsubst (TREE_TYPE (t
), args
,
12523 complain
| tf_fndecl_type
, in_decl
));
12524 if (type
== error_mark_node
)
12525 return error_mark_node
;
12527 /* If we hit excessive deduction depth, the type is bogus even if
12528 it isn't error_mark_node, so don't build a decl. */
12529 if (excessive_deduction_depth
)
12530 return error_mark_node
;
12532 /* We do NOT check for matching decls pushed separately at this
12533 point, as they may not represent instantiations of this
12534 template, and in any case are considered separate under the
12536 tree r
= copy_decl (t
);
12537 DECL_USE_TEMPLATE (r
) = 0;
12538 TREE_TYPE (r
) = type
;
12539 /* Clear out the mangled name and RTL for the instantiation. */
12540 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12541 SET_DECL_RTL (r
, NULL
);
12542 /* Leave DECL_INITIAL set on deleted instantiations. */
12543 if (!DECL_DELETED_FN (r
))
12544 DECL_INITIAL (r
) = NULL_TREE
;
12545 DECL_CONTEXT (r
) = ctx
;
12547 /* OpenMP UDRs have the only argument a reference to the declared
12548 type. We want to diagnose if the declared type is a reference,
12549 which is invalid, but as references to references are usually
12550 quietly merged, diagnose it here. */
12551 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12554 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12555 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12556 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12557 error_at (DECL_SOURCE_LOCATION (t
),
12558 "reference type %qT in "
12559 "%<#pragma omp declare reduction%>", argtype
);
12560 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12561 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12565 if (member
&& DECL_CONV_FN_P (r
))
12566 /* Type-conversion operator. Reconstruct the name, in
12567 case it's the name of one of the template's parameters. */
12568 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12570 tree parms
= DECL_ARGUMENTS (t
);
12572 parms
= DECL_CHAIN (parms
);
12573 parms
= tsubst (parms
, args
, complain
, t
);
12574 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12575 DECL_CONTEXT (parm
) = r
;
12578 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12579 DECL_CHAIN (tparm
) = parms
;
12582 DECL_ARGUMENTS (r
) = parms
;
12583 DECL_RESULT (r
) = NULL_TREE
;
12585 TREE_STATIC (r
) = 0;
12586 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12587 DECL_EXTERNAL (r
) = 1;
12588 /* If this is an instantiation of a function with internal
12589 linkage, we already know what object file linkage will be
12590 assigned to the instantiation. */
12591 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12592 DECL_DEFER_OUTPUT (r
) = 0;
12593 DECL_CHAIN (r
) = NULL_TREE
;
12594 DECL_PENDING_INLINE_INFO (r
) = 0;
12595 DECL_PENDING_INLINE_P (r
) = 0;
12596 DECL_SAVED_TREE (r
) = NULL_TREE
;
12597 DECL_STRUCT_FUNCTION (r
) = NULL
;
12599 /* We'll re-clone as appropriate in instantiate_template. */
12600 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12602 /* If we aren't complaining now, return on error before we register
12603 the specialization so that we'll complain eventually. */
12604 if ((complain
& tf_error
) == 0
12605 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12606 && !grok_op_properties (r
, /*complain=*/false))
12607 return error_mark_node
;
12609 /* When instantiating a constrained member, substitute
12610 into the constraints to create a new constraint. */
12611 if (tree ci
= get_constraints (t
))
12614 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12615 set_constraints (r
, ci
);
12618 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12619 this in the special friend case mentioned above where
12620 GEN_TMPL is NULL. */
12621 if (gen_tmpl
&& !closure
)
12623 DECL_TEMPLATE_INFO (r
)
12624 = build_template_info (gen_tmpl
, argvec
);
12625 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12628 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12630 /* We instantiated this while substituting into
12631 the type earlier (template/friend54.C). */
12634 /* We're not supposed to instantiate default arguments
12635 until they are called, for a template. But, for a
12638 template <class T> void f ()
12639 { extern void g(int i = T()); }
12641 we should do the substitution when the template is
12642 instantiated. We handle the member function case in
12643 instantiate_class_template since the default arguments
12644 might refer to other members of the class. */
12646 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12647 && !uses_template_parms (argvec
))
12648 tsubst_default_arguments (r
, complain
);
12651 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12653 /* Copy the list of befriending classes. */
12654 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
12656 friends
= &TREE_CHAIN (*friends
))
12658 *friends
= copy_node (*friends
);
12659 TREE_VALUE (*friends
)
12660 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
12663 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12665 maybe_retrofit_in_chrg (r
);
12666 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12667 return error_mark_node
;
12668 /* If this is an instantiation of a member template, clone it.
12669 If it isn't, that'll be handled by
12670 clone_constructors_and_destructors. */
12671 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12672 clone_function_decl (r
, /*update_methods=*/false);
12674 else if ((complain
& tf_error
) != 0
12675 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12676 && !grok_op_properties (r
, /*complain=*/true))
12677 return error_mark_node
;
12679 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12680 SET_DECL_FRIEND_CONTEXT (r
,
12681 tsubst (DECL_FRIEND_CONTEXT (t
),
12682 args
, complain
, in_decl
));
12684 /* Possibly limit visibility based on template args. */
12685 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12686 if (DECL_VISIBILITY_SPECIFIED (t
))
12688 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12689 DECL_ATTRIBUTES (r
)
12690 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12692 determine_visibility (r
);
12693 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12694 && !processing_template_decl
)
12695 defaulted_late_check (r
);
12697 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12698 args
, complain
, in_decl
);
12702 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12705 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
12706 tree lambda_fntype
)
12708 /* We can get here when processing a member function template,
12709 member class template, or template template parameter. */
12710 tree decl
= DECL_TEMPLATE_RESULT (t
);
12716 hashval_t hash
= 0;
12718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12720 /* Template template parameter is treated here. */
12721 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12722 if (new_type
== error_mark_node
)
12723 r
= error_mark_node
;
12724 /* If we get a real template back, return it. This can happen in
12725 the context of most_specialized_partial_spec. */
12726 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12729 /* The new TEMPLATE_DECL was built in
12730 reduce_template_parm_level. */
12731 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12735 if (!lambda_fntype
)
12737 /* We might already have an instance of this template.
12738 The ARGS are for the surrounding class type, so the
12739 full args contain the tsubst'd args for the context,
12740 plus the innermost args from the template decl. */
12741 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12742 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12743 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12744 /* Because this is a template, the arguments will still be
12745 dependent, even after substitution. If
12746 PROCESSING_TEMPLATE_DECL is not set, the dependency
12747 predicates will short-circuit. */
12748 ++processing_template_decl
;
12749 full_args
= tsubst_template_args (tmpl_args
, args
,
12750 complain
, in_decl
);
12751 --processing_template_decl
;
12752 if (full_args
== error_mark_node
)
12753 return error_mark_node
;
12755 /* If this is a default template template argument,
12756 tsubst might not have changed anything. */
12757 if (full_args
== tmpl_args
)
12760 hash
= hash_tmpl_and_args (t
, full_args
);
12761 spec
= retrieve_specialization (t
, full_args
, hash
);
12762 if (spec
!= NULL_TREE
)
12766 /* Make a new template decl. It will be similar to the
12767 original, but will record the current template arguments.
12768 We also create a new function declaration, which is just
12769 like the old one, but points to this new template, rather
12770 than the old one. */
12772 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12773 DECL_CHAIN (r
) = NULL_TREE
;
12775 // Build new template info linking to the original template decl.
12776 if (!lambda_fntype
)
12778 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12779 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12782 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12784 /* The template parameters for this new template are all the
12785 template parameters for the old template, except the
12786 outermost level of parameters. */
12787 DECL_TEMPLATE_PARMS (r
)
12788 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12791 if (TREE_CODE (decl
) == TYPE_DECL
12792 && !TYPE_DECL_ALIAS_P (decl
))
12795 ++processing_template_decl
;
12796 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12797 --processing_template_decl
;
12798 if (new_type
== error_mark_node
)
12799 return error_mark_node
;
12801 TREE_TYPE (r
) = new_type
;
12802 /* For a partial specialization, we need to keep pointing to
12803 the primary template. */
12804 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12805 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12806 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12807 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12808 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12813 ++processing_template_decl
;
12814 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12815 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
12817 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12818 --processing_template_decl
;
12819 if (new_decl
== error_mark_node
)
12820 return error_mark_node
;
12822 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12823 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12824 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12827 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
12828 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
12832 DECL_TI_TEMPLATE (new_decl
) = r
;
12833 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12837 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12838 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12840 if (PRIMARY_TEMPLATE_P (t
))
12841 DECL_PRIMARY_TEMPLATE (r
) = r
;
12843 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
12845 /* Record this non-type partial instantiation. */
12846 register_specialization (r
, t
,
12847 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12853 /* True if FN is the op() for a lambda in an uninstantiated template. */
12856 lambda_fn_in_template_p (tree fn
)
12858 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
12860 tree closure
= DECL_CONTEXT (fn
);
12861 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
12864 /* We're instantiating a variable from template function TCTX. Return the
12865 corresponding current enclosing scope. This gets complicated because lambda
12866 functions in templates are regenerated rather than instantiated, but generic
12867 lambda functions are subsequently instantiated. */
12870 enclosing_instantiation_of (tree otctx
)
12873 tree fn
= current_function_decl
;
12874 int lambda_count
= 0;
12876 for (; tctx
&& lambda_fn_in_template_p (tctx
);
12877 tctx
= decl_function_context (tctx
))
12879 for (; fn
; fn
= decl_function_context (fn
))
12882 int flambda_count
= 0;
12883 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
12884 fn
= decl_function_context (fn
))
12886 if (DECL_TEMPLATE_INFO (fn
)
12887 ? most_general_template (fn
) != most_general_template (tctx
)
12890 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
12891 || DECL_CONV_FN_P (ofn
));
12894 gcc_unreachable ();
12897 /* Substitute the ARGS into the T, which is a _DECL. Return the
12898 result of the substitution. Issue error and warning messages under
12899 control of COMPLAIN. */
12902 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12904 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12905 location_t saved_loc
;
12906 tree r
= NULL_TREE
;
12908 hashval_t hash
= 0;
12910 /* Set the filename and linenumber to improve error-reporting. */
12911 saved_loc
= input_location
;
12912 input_location
= DECL_SOURCE_LOCATION (t
);
12914 switch (TREE_CODE (t
))
12916 case TEMPLATE_DECL
:
12917 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12920 case FUNCTION_DECL
:
12921 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
12926 tree type
= NULL_TREE
;
12928 tree expanded_types
= NULL_TREE
;
12929 tree prev_r
= NULL_TREE
;
12930 tree first_r
= NULL_TREE
;
12932 if (DECL_PACK_P (t
))
12934 /* If there is a local specialization that isn't a
12935 parameter pack, it means that we're doing a "simple"
12936 substitution from inside tsubst_pack_expansion. Just
12937 return the local specialization (which will be a single
12939 tree spec
= retrieve_local_specialization (t
);
12941 && TREE_CODE (spec
) == PARM_DECL
12942 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12945 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12946 the parameters in this function parameter pack. */
12947 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12948 complain
, in_decl
);
12949 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12951 len
= TREE_VEC_LENGTH (expanded_types
);
12953 /* Zero-length parameter packs are boring. Just substitute
12956 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12961 /* All we did was update the type. Make a note of that. */
12962 type
= expanded_types
;
12963 expanded_types
= NULL_TREE
;
12967 /* Loop through all of the parameters we'll build. When T is
12968 a function parameter pack, LEN is the number of expanded
12969 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12971 for (i
= 0; i
< len
; ++i
)
12975 if (DECL_TEMPLATE_PARM_P (t
))
12976 SET_DECL_TEMPLATE_PARM_P (r
);
12978 if (expanded_types
)
12979 /* We're on the Ith parameter of the function parameter
12982 /* Get the Ith type. */
12983 type
= TREE_VEC_ELT (expanded_types
, i
);
12985 /* Rename the parameter to include the index. */
12987 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12990 /* We're dealing with a normal parameter. */
12991 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12993 type
= type_decays_to (type
);
12994 TREE_TYPE (r
) = type
;
12995 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12997 if (DECL_INITIAL (r
))
12999 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13000 DECL_INITIAL (r
) = TREE_TYPE (r
);
13002 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13003 complain
, in_decl
);
13006 DECL_CONTEXT (r
) = NULL_TREE
;
13008 if (!DECL_TEMPLATE_PARM_P (r
))
13009 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13011 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13012 args
, complain
, in_decl
);
13014 /* Keep track of the first new parameter we
13015 generate. That's what will be returned to the
13020 /* Build a proper chain of parameters when substituting
13021 into a function parameter pack. */
13023 DECL_CHAIN (prev_r
) = r
;
13026 /* If cp_unevaluated_operand is set, we're just looking for a
13027 single dummy parameter, so don't keep going. */
13028 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13029 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13030 complain
, DECL_CHAIN (t
));
13032 /* FIRST_R contains the start of the chain we've built. */
13039 tree type
= NULL_TREE
;
13040 tree vec
= NULL_TREE
;
13041 tree expanded_types
= NULL_TREE
;
13044 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13046 /* This field is a lambda capture pack. Return a TREE_VEC of
13047 the expanded fields to instantiate_class_template_1. */
13048 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13049 complain
, in_decl
);
13050 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13052 len
= TREE_VEC_LENGTH (expanded_types
);
13053 vec
= make_tree_vec (len
);
13057 /* All we did was update the type. Make a note of that. */
13058 type
= expanded_types
;
13059 expanded_types
= NULL_TREE
;
13063 for (int i
= 0; i
< len
; ++i
)
13066 if (expanded_types
)
13068 type
= TREE_VEC_ELT (expanded_types
, i
);
13070 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13073 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13075 if (type
== error_mark_node
)
13076 RETURN (error_mark_node
);
13077 TREE_TYPE (r
) = type
;
13078 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13080 if (DECL_C_BIT_FIELD (r
))
13081 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13083 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13084 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13086 /*integral_constant_expression_p=*/true);
13087 if (DECL_INITIAL (t
))
13089 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13090 NSDMI in perform_member_init. Still set DECL_INITIAL
13091 so that we know there is one. */
13092 DECL_INITIAL (r
) = void_node
;
13093 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13094 retrofit_lang_decl (r
);
13095 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13097 /* We don't have to set DECL_CONTEXT here; it is set by
13098 finish_member_declaration. */
13099 DECL_CHAIN (r
) = NULL_TREE
;
13101 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13102 args
, complain
, in_decl
);
13105 TREE_VEC_ELT (vec
, i
) = r
;
13114 /* We reach here only for member using decls. We also need to check
13115 uses_template_parms because DECL_DEPENDENT_P is not set for a
13116 using-declaration that designates a member of the current
13117 instantiation (c++/53549). */
13118 if (DECL_DEPENDENT_P (t
)
13119 || uses_template_parms (USING_DECL_SCOPE (t
)))
13121 tree scope
= USING_DECL_SCOPE (t
);
13122 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13123 if (PACK_EXPANSION_P (scope
))
13125 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13126 int len
= TREE_VEC_LENGTH (vec
);
13127 r
= make_tree_vec (len
);
13128 for (int i
= 0; i
< len
; ++i
)
13130 tree escope
= TREE_VEC_ELT (vec
, i
);
13131 tree elt
= do_class_using_decl (escope
, name
);
13134 r
= error_mark_node
;
13139 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13140 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13142 TREE_VEC_ELT (r
, i
) = elt
;
13147 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13148 complain
, in_decl
);
13149 r
= do_class_using_decl (inst_scope
, name
);
13151 r
= error_mark_node
;
13154 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13155 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13162 DECL_CHAIN (r
) = NULL_TREE
;
13169 tree argvec
= NULL_TREE
;
13170 tree gen_tmpl
= NULL_TREE
;
13172 tree tmpl
= NULL_TREE
;
13174 tree type
= NULL_TREE
;
13177 if (TREE_TYPE (t
) == error_mark_node
)
13178 RETURN (error_mark_node
);
13180 if (TREE_CODE (t
) == TYPE_DECL
13181 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13183 /* If this is the canonical decl, we don't have to
13184 mess with instantiations, and often we can't (for
13185 typename, template type parms and such). Note that
13186 TYPE_NAME is not correct for the above test if
13187 we've copied the type for a typedef. */
13188 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13189 if (type
== error_mark_node
)
13190 RETURN (error_mark_node
);
13191 r
= TYPE_NAME (type
);
13195 /* Check to see if we already have the specialization we
13198 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13200 /* T is a static data member or namespace-scope entity.
13201 We have to substitute into namespace-scope variables
13202 (not just variable templates) because of cases like:
13204 template <class T> void f() { extern T t; }
13206 where the entity referenced is not known until
13207 instantiation time. */
13209 ctx
= DECL_CONTEXT (t
);
13210 if (DECL_CLASS_SCOPE_P (t
))
13212 ctx
= tsubst_aggr_type (ctx
, args
,
13214 in_decl
, /*entering_scope=*/1);
13215 /* If CTX is unchanged, then T is in fact the
13216 specialization we want. That situation occurs when
13217 referencing a static data member within in its own
13218 class. We can use pointer equality, rather than
13219 same_type_p, because DECL_CONTEXT is always
13221 if (ctx
== DECL_CONTEXT (t
)
13222 /* ... unless T is a member template; in which
13223 case our caller can be willing to create a
13224 specialization of that template represented
13226 && !(DECL_TI_TEMPLATE (t
)
13227 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13233 tmpl
= DECL_TI_TEMPLATE (t
);
13234 gen_tmpl
= most_general_template (tmpl
);
13235 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13236 if (argvec
!= error_mark_node
)
13237 argvec
= (coerce_innermost_template_parms
13238 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13239 argvec
, t
, complain
,
13240 /*all*/true, /*defarg*/true));
13241 if (argvec
== error_mark_node
)
13242 RETURN (error_mark_node
);
13243 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13244 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13249 /* A local variable. */
13251 /* Subsequent calls to pushdecl will fill this in. */
13253 /* Unless this is a reference to a static variable from an
13254 enclosing function, in which case we need to fill it in now. */
13255 if (TREE_STATIC (t
))
13257 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13258 if (fn
!= current_function_decl
)
13261 spec
= retrieve_local_specialization (t
);
13263 /* If we already have the specialization we need, there is
13264 nothing more to do. */
13271 /* Create a new node for the specialization we need. */
13273 if (type
== NULL_TREE
)
13275 if (is_typedef_decl (t
))
13276 type
= DECL_ORIGINAL_TYPE (t
);
13278 type
= TREE_TYPE (t
);
13280 && VAR_HAD_UNKNOWN_BOUND (t
)
13281 && type
!= error_mark_node
)
13282 type
= strip_array_domain (type
);
13283 tree sub_args
= args
;
13284 if (tree auto_node
= type_uses_auto (type
))
13286 /* Mask off any template args past the variable's context so we
13287 don't replace the auto with an unrelated argument. */
13288 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13289 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13291 /* This should never happen with the new lambda instantiation
13292 model, but keep the handling just in case. */
13293 gcc_assert (!CHECKING_P
),
13294 sub_args
= strip_innermost_template_args (args
, extra
);
13296 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13300 /* Even if the original location is out of scope, the
13301 newly substituted one is not. */
13302 DECL_DEAD_FOR_LOCAL (r
) = 0;
13303 DECL_INITIALIZED_P (r
) = 0;
13304 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13305 if (type
== error_mark_node
)
13306 RETURN (error_mark_node
);
13307 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13309 /* It may seem that this case cannot occur, since:
13314 declares a function, not a variable. However:
13317 template <typename T> void g() { T t; }
13318 template void g<f>();
13320 is an attempt to declare a variable with function
13322 error ("variable %qD has function type",
13323 /* R is not yet sufficiently initialized, so we
13324 just use its name. */
13326 RETURN (error_mark_node
);
13328 type
= complete_type (type
);
13329 /* Wait until cp_finish_decl to set this again, to handle
13330 circular dependency (template/instantiate6.C). */
13331 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13332 type
= check_var_type (DECL_NAME (r
), type
);
13334 if (DECL_HAS_VALUE_EXPR_P (t
))
13336 tree ve
= DECL_VALUE_EXPR (t
);
13337 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13338 /*constant_expression_p=*/false);
13339 if (REFERENCE_REF_P (ve
))
13341 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
13342 ve
= TREE_OPERAND (ve
, 0);
13344 SET_DECL_VALUE_EXPR (r
, ve
);
13346 if (CP_DECL_THREAD_LOCAL_P (r
)
13347 && !processing_template_decl
)
13348 set_decl_tls_model (r
, decl_default_tls_model (r
));
13350 else if (DECL_SELF_REFERENCE_P (t
))
13351 SET_DECL_SELF_REFERENCE_P (r
);
13352 TREE_TYPE (r
) = type
;
13353 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13354 DECL_CONTEXT (r
) = ctx
;
13355 /* Clear out the mangled name and RTL for the instantiation. */
13356 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13357 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13358 SET_DECL_RTL (r
, NULL
);
13359 /* The initializer must not be expanded until it is required;
13360 see [temp.inst]. */
13361 DECL_INITIAL (r
) = NULL_TREE
;
13362 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13365 if (DECL_LANG_SPECIFIC (r
))
13366 SET_DECL_DEPENDENT_INIT_P (r
, false);
13368 SET_DECL_MODE (r
, VOIDmode
);
13370 /* Possibly limit visibility based on template args. */
13371 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13372 if (DECL_VISIBILITY_SPECIFIED (t
))
13374 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13375 DECL_ATTRIBUTES (r
)
13376 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13378 determine_visibility (r
);
13383 /* A static data member declaration is always marked
13384 external when it is declared in-class, even if an
13385 initializer is present. We mimic the non-template
13386 processing here. */
13387 DECL_EXTERNAL (r
) = 1;
13388 if (DECL_NAMESPACE_SCOPE_P (t
))
13389 DECL_NOT_REALLY_EXTERN (r
) = 1;
13391 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13392 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13393 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13397 if (DECL_LANG_SPECIFIC (r
))
13398 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13399 if (!cp_unevaluated_operand
)
13400 register_local_specialization (r
, t
);
13403 DECL_CHAIN (r
) = NULL_TREE
;
13405 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13407 args
, complain
, in_decl
);
13409 /* Preserve a typedef that names a type. */
13410 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13412 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13413 set_underlying_type (r
);
13414 if (TYPE_DECL_ALIAS_P (r
))
13415 /* An alias template specialization can be dependent
13416 even if its underlying type is not. */
13417 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13420 layout_decl (r
, 0);
13425 gcc_unreachable ();
13430 /* Restore the file and line information. */
13431 input_location
= saved_loc
;
13436 /* Substitute into the ARG_TYPES of a function type.
13437 If END is a TREE_CHAIN, leave it and any following types
13441 tsubst_arg_types (tree arg_types
,
13444 tsubst_flags_t complain
,
13447 tree remaining_arg_types
;
13448 tree type
= NULL_TREE
;
13450 tree expanded_args
= NULL_TREE
;
13453 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13456 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13457 args
, end
, complain
, in_decl
);
13458 if (remaining_arg_types
== error_mark_node
)
13459 return error_mark_node
;
13461 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13463 /* For a pack expansion, perform substitution on the
13464 entire expression. Later on, we'll handle the arguments
13466 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13467 args
, complain
, in_decl
);
13469 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13470 /* So that we'll spin through the parameters, one by one. */
13471 i
= TREE_VEC_LENGTH (expanded_args
);
13474 /* We only partially substituted into the parameter
13475 pack. Our type is TYPE_PACK_EXPANSION. */
13476 type
= expanded_args
;
13477 expanded_args
= NULL_TREE
;
13485 type
= TREE_VEC_ELT (expanded_args
, i
);
13487 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13489 if (type
== error_mark_node
)
13490 return error_mark_node
;
13491 if (VOID_TYPE_P (type
))
13493 if (complain
& tf_error
)
13495 error ("invalid parameter type %qT", type
);
13497 error ("in declaration %q+D", in_decl
);
13499 return error_mark_node
;
13502 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13503 return error_mark_node
;
13505 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13506 top-level qualifiers as required. */
13507 type
= cv_unqualified (type_decays_to (type
));
13509 /* We do not substitute into default arguments here. The standard
13510 mandates that they be instantiated only when needed, which is
13511 done in build_over_call. */
13512 default_arg
= TREE_PURPOSE (arg_types
);
13514 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13515 since the new op() won't have any associated template arguments for us
13516 to refer to later. */
13517 if (lambda_fn_in_template_p (in_decl
))
13518 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13519 false/*fn*/, false/*constexpr*/);
13521 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13523 /* We've instantiated a template before its default arguments
13524 have been parsed. This can happen for a nested template
13525 class, and is not an error unless we require the default
13526 argument in a call of this function. */
13527 remaining_arg_types
=
13528 tree_cons (default_arg
, type
, remaining_arg_types
);
13529 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13532 remaining_arg_types
=
13533 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13536 return remaining_arg_types
;
13539 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13540 *not* handle the exception-specification for FNTYPE, because the
13541 initial substitution of explicitly provided template parameters
13542 during argument deduction forbids substitution into the
13543 exception-specification:
13547 All references in the function type of the function template to the
13548 corresponding template parameters are replaced by the specified tem-
13549 plate argument values. If a substitution in a template parameter or
13550 in the function type of the function template results in an invalid
13551 type, type deduction fails. [Note: The equivalent substitution in
13552 exception specifications is done only when the function is instanti-
13553 ated, at which point a program is ill-formed if the substitution
13554 results in an invalid type.] */
13557 tsubst_function_type (tree t
,
13559 tsubst_flags_t complain
,
13563 tree arg_types
= NULL_TREE
;
13566 /* The TYPE_CONTEXT is not used for function/method types. */
13567 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13569 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13571 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13573 if (late_return_type_p
)
13575 /* Substitute the argument types. */
13576 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13577 complain
, in_decl
);
13578 if (arg_types
== error_mark_node
)
13579 return error_mark_node
;
13581 tree save_ccp
= current_class_ptr
;
13582 tree save_ccr
= current_class_ref
;
13583 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13584 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13585 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13588 /* DR 1207: 'this' is in scope in the trailing return type. */
13589 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13592 /* Substitute the return type. */
13593 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13597 current_class_ptr
= save_ccp
;
13598 current_class_ref
= save_ccr
;
13602 /* Substitute the return type. */
13603 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13605 if (return_type
== error_mark_node
)
13606 return error_mark_node
;
13607 /* DR 486 clarifies that creation of a function type with an
13608 invalid return type is a deduction failure. */
13609 if (TREE_CODE (return_type
) == ARRAY_TYPE
13610 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13612 if (complain
& tf_error
)
13614 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13615 error ("function returning an array");
13617 error ("function returning a function");
13619 return error_mark_node
;
13622 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13623 return error_mark_node
;
13625 if (!late_return_type_p
)
13627 /* Substitute the argument types. */
13628 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13629 complain
, in_decl
);
13630 if (arg_types
== error_mark_node
)
13631 return error_mark_node
;
13634 /* Construct a new type node and return it. */
13635 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13637 fntype
= build_function_type (return_type
, arg_types
);
13638 fntype
= apply_memfn_quals (fntype
,
13639 type_memfn_quals (t
),
13640 type_memfn_rqual (t
));
13644 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13645 /* Don't pick up extra function qualifiers from the basetype. */
13646 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13647 if (! MAYBE_CLASS_TYPE_P (r
))
13651 Type deduction may fail for any of the following
13654 -- Attempting to create "pointer to member of T" when T
13655 is not a class type. */
13656 if (complain
& tf_error
)
13657 error ("creating pointer to member function of non-class type %qT",
13659 return error_mark_node
;
13662 fntype
= build_method_type_directly (r
, return_type
,
13663 TREE_CHAIN (arg_types
));
13664 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13666 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13668 if (late_return_type_p
)
13669 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13674 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13675 ARGS into that specification, and return the substituted
13676 specification. If there is no specification, return NULL_TREE. */
13679 tsubst_exception_specification (tree fntype
,
13681 tsubst_flags_t complain
,
13688 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13689 new_specs
= NULL_TREE
;
13690 if (specs
&& TREE_PURPOSE (specs
))
13692 /* A noexcept-specifier. */
13693 tree expr
= TREE_PURPOSE (specs
);
13694 if (TREE_CODE (expr
) == INTEGER_CST
)
13698 /* Defer instantiation of noexcept-specifiers to avoid
13699 excessive instantiations (c++/49107). */
13700 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13701 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13703 /* We already partially instantiated this member template,
13704 so combine the new args with the old. */
13705 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13706 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13707 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13708 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13712 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13713 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13717 new_specs
= tsubst_copy_and_build
13718 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13719 /*integral_constant_expression_p=*/true);
13720 new_specs
= build_noexcept_spec (new_specs
, complain
);
13724 if (! TREE_VALUE (specs
))
13731 tree expanded_specs
= NULL_TREE
;
13733 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13735 /* Expand the pack expansion type. */
13736 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13740 if (expanded_specs
== error_mark_node
)
13741 return error_mark_node
;
13742 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13743 len
= TREE_VEC_LENGTH (expanded_specs
);
13746 /* We're substituting into a member template, so
13747 we got a TYPE_PACK_EXPANSION back. Add that
13748 expansion and move on. */
13749 gcc_assert (TREE_CODE (expanded_specs
)
13750 == TYPE_PACK_EXPANSION
);
13751 new_specs
= add_exception_specifier (new_specs
,
13754 specs
= TREE_CHAIN (specs
);
13759 for (i
= 0; i
< len
; ++i
)
13761 if (expanded_specs
)
13762 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13764 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13765 if (spec
== error_mark_node
)
13767 new_specs
= add_exception_specifier (new_specs
, spec
,
13771 specs
= TREE_CHAIN (specs
);
13777 /* Take the tree structure T and replace template parameters used
13778 therein with the argument vector ARGS. IN_DECL is an associated
13779 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13780 Issue error and warning messages under control of COMPLAIN. Note
13781 that we must be relatively non-tolerant of extensions here, in
13782 order to preserve conformance; if we allow substitutions that
13783 should not be allowed, we may allow argument deductions that should
13784 not succeed, and therefore report ambiguous overload situations
13785 where there are none. In theory, we could allow the substitution,
13786 but indicate that it should have failed, and allow our caller to
13787 make sure that the right thing happens, but we don't try to do this
13790 This function is used for dealing with types, decls and the like;
13791 for expressions, use tsubst_expr or tsubst_copy. */
13794 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13796 enum tree_code code
;
13797 tree type
, r
= NULL_TREE
;
13799 if (t
== NULL_TREE
|| t
== error_mark_node
13800 || t
== integer_type_node
13801 || t
== void_type_node
13802 || t
== char_type_node
13803 || t
== unknown_type_node
13804 || TREE_CODE (t
) == NAMESPACE_DECL
13805 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13809 return tsubst_decl (t
, args
, complain
);
13811 if (args
== NULL_TREE
)
13814 code
= TREE_CODE (t
);
13816 if (code
== IDENTIFIER_NODE
)
13817 type
= IDENTIFIER_TYPE_VALUE (t
);
13819 type
= TREE_TYPE (t
);
13821 gcc_assert (type
!= unknown_type_node
);
13823 /* Reuse typedefs. We need to do this to handle dependent attributes,
13824 such as attribute aligned. */
13826 && typedef_variant_p (t
))
13828 tree decl
= TYPE_NAME (t
);
13830 if (alias_template_specialization_p (t
))
13832 /* DECL represents an alias template and we want to
13834 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13835 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13836 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13838 else if (DECL_CLASS_SCOPE_P (decl
)
13839 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13840 && uses_template_parms (DECL_CONTEXT (decl
)))
13842 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13843 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13844 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13846 else if (DECL_FUNCTION_SCOPE_P (decl
)
13847 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13848 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13849 r
= retrieve_local_specialization (decl
);
13851 /* The typedef is from a non-template context. */
13857 r
= cp_build_qualified_type_real
13858 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13859 complain
| tf_ignore_bad_quals
);
13864 /* We don't have an instantiation yet, so drop the typedef. */
13865 int quals
= cp_type_quals (t
);
13866 t
= DECL_ORIGINAL_TYPE (decl
);
13867 t
= cp_build_qualified_type_real (t
, quals
,
13868 complain
| tf_ignore_bad_quals
);
13872 bool fndecl_type
= (complain
& tf_fndecl_type
);
13873 complain
&= ~tf_fndecl_type
;
13876 && code
!= TYPENAME_TYPE
13877 && code
!= TEMPLATE_TYPE_PARM
13878 && code
!= TEMPLATE_PARM_INDEX
13879 && code
!= IDENTIFIER_NODE
13880 && code
!= FUNCTION_TYPE
13881 && code
!= METHOD_TYPE
)
13882 type
= tsubst (type
, args
, complain
, in_decl
);
13883 if (type
== error_mark_node
)
13884 return error_mark_node
;
13890 case ENUMERAL_TYPE
:
13891 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13892 /*entering_scope=*/0);
13895 case IDENTIFIER_NODE
:
13906 if (t
== integer_type_node
)
13909 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13910 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13914 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13916 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13917 /*integral_constant_expression_p=*/false);
13919 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13921 if (TREE_CODE (max
) == NOP_EXPR
13922 && TREE_SIDE_EFFECTS (omax
)
13923 && !TREE_TYPE (max
))
13924 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13926 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13927 with TREE_SIDE_EFFECTS that indicates this is not an integral
13928 constant expression. */
13929 if (processing_template_decl
13930 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13932 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13933 TREE_SIDE_EFFECTS (max
) = 1;
13936 return compute_array_index_type (NULL_TREE
, max
, complain
);
13939 case TEMPLATE_TYPE_PARM
:
13940 case TEMPLATE_TEMPLATE_PARM
:
13941 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13942 case TEMPLATE_PARM_INDEX
:
13947 tree arg
= NULL_TREE
;
13949 /* Early in template argument deduction substitution, we don't
13950 want to reduce the level of 'auto', or it will be confused
13951 with a normal template parm in subsequent deduction. */
13952 if (is_auto (t
) && (complain
& tf_partial
))
13957 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13958 template_parm_level_and_index (t
, &level
, &idx
);
13960 levels
= TMPL_ARGS_DEPTH (args
);
13961 if (level
<= levels
13962 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13964 arg
= TMPL_ARG (args
, level
, idx
);
13966 /* See through ARGUMENT_PACK_SELECT arguments. */
13967 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13968 arg
= argument_pack_select_arg (arg
);
13971 if (arg
== error_mark_node
)
13972 return error_mark_node
;
13973 else if (arg
!= NULL_TREE
)
13975 if (ARGUMENT_PACK_P (arg
))
13976 /* If ARG is an argument pack, we don't actually want to
13977 perform a substitution here, because substitutions
13978 for argument packs are only done
13979 element-by-element. We can get to this point when
13980 substituting the type of a non-type template
13981 parameter pack, when that type actually contains
13982 template parameter packs from an outer template, e.g.,
13984 template<typename... Types> struct A {
13985 template<Types... Values> struct B { };
13989 if (code
== TEMPLATE_TYPE_PARM
)
13992 gcc_assert (TYPE_P (arg
));
13994 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13996 return cp_build_qualified_type_real
13997 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13999 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14001 /* We are processing a type constructed from a
14002 template template parameter. */
14003 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14004 args
, complain
, in_decl
);
14005 if (argvec
== error_mark_node
)
14006 return error_mark_node
;
14008 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14009 || TREE_CODE (arg
) == TEMPLATE_DECL
14010 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14012 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14013 /* Consider this code:
14015 template <template <class> class Template>
14017 template <class Arg> using Bind = Template<Arg>;
14020 template <template <class> class Template, class Arg>
14021 using Instantiate = Template<Arg>; //#0
14023 template <template <class> class Template,
14026 Instantiate<Internal<Template>::template Bind,
14029 When #1 is parsed, the
14030 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14031 parameter `Template' in #0 matches the
14032 UNBOUND_CLASS_TEMPLATE representing the argument
14033 `Internal<Template>::template Bind'; We then want
14034 to assemble the type `Bind<Argument>' that can't
14035 be fully created right now, because
14036 `Internal<Template>' not being complete, the Bind
14037 template cannot be looked up in that context. So
14038 we need to "store" `Bind<Argument>' for later
14039 when the context of Bind becomes complete. Let's
14040 store that in a TYPENAME_TYPE. */
14041 return make_typename_type (TYPE_CONTEXT (arg
),
14042 build_nt (TEMPLATE_ID_EXPR
,
14043 TYPE_IDENTIFIER (arg
),
14048 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14049 are resolving nested-types in the signature of a
14050 member function templates. Otherwise ARG is a
14051 TEMPLATE_DECL and is the real template to be
14053 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14054 arg
= TYPE_NAME (arg
);
14056 r
= lookup_template_class (arg
,
14058 DECL_CONTEXT (arg
),
14059 /*entering_scope=*/0,
14061 return cp_build_qualified_type_real
14062 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14064 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14067 /* TEMPLATE_PARM_INDEX. */
14068 return convert_from_reference (unshare_expr (arg
));
14072 /* This can happen during the attempted tsubst'ing in
14073 unify. This means that we don't yet have any information
14074 about the template parameter in question. */
14077 /* If we get here, we must have been looking at a parm for a
14078 more deeply nested template. Make a new version of this
14079 template parameter, but with a lower level. */
14082 case TEMPLATE_TYPE_PARM
:
14083 case TEMPLATE_TEMPLATE_PARM
:
14084 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14085 if (cp_type_quals (t
))
14087 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14088 r
= cp_build_qualified_type_real
14089 (r
, cp_type_quals (t
),
14090 complain
| (code
== TEMPLATE_TYPE_PARM
14091 ? tf_ignore_bad_quals
: 0));
14093 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14094 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14095 && (r
= (TEMPLATE_PARM_DESCENDANTS
14096 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14097 && (r
= TREE_TYPE (r
))
14098 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14099 /* Break infinite recursion when substituting the constraints
14100 of a constrained placeholder. */;
14104 TEMPLATE_TYPE_PARM_INDEX (r
)
14105 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14106 r
, levels
, args
, complain
);
14107 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14108 TYPE_MAIN_VARIANT (r
) = r
;
14109 TYPE_POINTER_TO (r
) = NULL_TREE
;
14110 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14112 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14114 /* Propagate constraints on placeholders. */
14115 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14116 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14117 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14118 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14120 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
14121 pl
= tsubst (pl
, args
, complain
, in_decl
);
14122 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14126 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14127 /* We have reduced the level of the template
14128 template parameter, but not the levels of its
14129 template parameters, so canonical_type_parameter
14130 will not be able to find the canonical template
14131 template parameter for this level. Thus, we
14132 require structural equality checking to compare
14133 TEMPLATE_TEMPLATE_PARMs. */
14134 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14135 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14136 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14138 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14140 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14142 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14143 /* We might need to substitute into the types of non-type
14144 template parameters. */
14145 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14146 complain
, in_decl
);
14147 if (tmpl
== error_mark_node
)
14148 return error_mark_node
;
14149 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14150 complain
, in_decl
);
14151 if (argvec
== error_mark_node
)
14152 return error_mark_node
;
14154 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14155 = build_template_info (tmpl
, argvec
);
14160 case TEMPLATE_PARM_INDEX
:
14161 /* OK, now substitute the type of the non-type parameter. We
14162 couldn't do it earlier because it might be an auto parameter,
14163 and we wouldn't need to if we had an argument. */
14164 type
= tsubst (type
, args
, complain
, in_decl
);
14165 if (type
== error_mark_node
)
14166 return error_mark_node
;
14167 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14171 gcc_unreachable ();
14179 tree purpose
, value
, chain
;
14181 if (t
== void_list_node
)
14184 purpose
= TREE_PURPOSE (t
);
14187 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14188 if (purpose
== error_mark_node
)
14189 return error_mark_node
;
14191 value
= TREE_VALUE (t
);
14194 value
= tsubst (value
, args
, complain
, in_decl
);
14195 if (value
== error_mark_node
)
14196 return error_mark_node
;
14198 chain
= TREE_CHAIN (t
);
14199 if (chain
&& chain
!= void_type_node
)
14201 chain
= tsubst (chain
, args
, complain
, in_decl
);
14202 if (chain
== error_mark_node
)
14203 return error_mark_node
;
14205 if (purpose
== TREE_PURPOSE (t
)
14206 && value
== TREE_VALUE (t
)
14207 && chain
== TREE_CHAIN (t
))
14209 return hash_tree_cons (purpose
, value
, chain
);
14213 /* We should never be tsubsting a binfo. */
14214 gcc_unreachable ();
14217 /* A vector of template arguments. */
14218 gcc_assert (!type
);
14219 return tsubst_template_args (t
, args
, complain
, in_decl
);
14222 case REFERENCE_TYPE
:
14224 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14229 Type deduction may fail for any of the following
14232 -- Attempting to create a pointer to reference type.
14233 -- Attempting to create a reference to a reference type or
14234 a reference to void.
14236 Core issue 106 says that creating a reference to a reference
14237 during instantiation is no longer a cause for failure. We
14238 only enforce this check in strict C++98 mode. */
14239 if ((TREE_CODE (type
) == REFERENCE_TYPE
14240 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14241 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14243 static location_t last_loc
;
14245 /* We keep track of the last time we issued this error
14246 message to avoid spewing a ton of messages during a
14247 single bad template instantiation. */
14248 if (complain
& tf_error
14249 && last_loc
!= input_location
)
14251 if (VOID_TYPE_P (type
))
14252 error ("forming reference to void");
14253 else if (code
== POINTER_TYPE
)
14254 error ("forming pointer to reference type %qT", type
);
14256 error ("forming reference to reference type %qT", type
);
14257 last_loc
= input_location
;
14260 return error_mark_node
;
14262 else if (TREE_CODE (type
) == FUNCTION_TYPE
14263 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14264 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14266 if (complain
& tf_error
)
14268 if (code
== POINTER_TYPE
)
14269 error ("forming pointer to qualified function type %qT",
14272 error ("forming reference to qualified function type %qT",
14275 return error_mark_node
;
14277 else if (code
== POINTER_TYPE
)
14279 r
= build_pointer_type (type
);
14280 if (TREE_CODE (type
) == METHOD_TYPE
)
14281 r
= build_ptrmemfunc_type (r
);
14283 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
14284 /* In C++0x, during template argument substitution, when there is an
14285 attempt to create a reference to a reference type, reference
14286 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14288 "If a template-argument for a template-parameter T names a type
14289 that is a reference to a type A, an attempt to create the type
14290 'lvalue reference to cv T' creates the type 'lvalue reference to
14291 A,' while an attempt to create the type type rvalue reference to
14292 cv T' creates the type T"
14294 r
= cp_build_reference_type
14296 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14298 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14299 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14301 if (r
!= error_mark_node
)
14302 /* Will this ever be needed for TYPE_..._TO values? */
14309 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14310 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14314 Type deduction may fail for any of the following
14317 -- Attempting to create "pointer to member of T" when T
14318 is not a class type. */
14319 if (complain
& tf_error
)
14320 error ("creating pointer to member of non-class type %qT", r
);
14321 return error_mark_node
;
14323 if (TREE_CODE (type
) == REFERENCE_TYPE
)
14325 if (complain
& tf_error
)
14326 error ("creating pointer to member reference type %qT", type
);
14327 return error_mark_node
;
14329 if (VOID_TYPE_P (type
))
14331 if (complain
& tf_error
)
14332 error ("creating pointer to member of type void");
14333 return error_mark_node
;
14335 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14336 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14338 /* The type of the implicit object parameter gets its
14339 cv-qualifiers from the FUNCTION_TYPE. */
14342 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14343 type_memfn_rqual (type
));
14344 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14345 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14349 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14353 case FUNCTION_TYPE
:
14358 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14359 if (fntype
== error_mark_node
)
14360 return error_mark_node
;
14362 /* Substitute the exception specification. */
14363 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14364 /*defer_ok*/fndecl_type
);
14365 if (specs
== error_mark_node
)
14366 return error_mark_node
;
14368 fntype
= build_exception_variant (fntype
, specs
);
14373 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14374 if (domain
== error_mark_node
)
14375 return error_mark_node
;
14377 /* As an optimization, we avoid regenerating the array type if
14378 it will obviously be the same as T. */
14379 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14382 /* These checks should match the ones in create_array_type_for_decl.
14386 The deduction may fail for any of the following reasons:
14388 -- Attempting to create an array with an element type that
14389 is void, a function type, or a reference type, or [DR337]
14390 an abstract class type. */
14391 if (VOID_TYPE_P (type
)
14392 || TREE_CODE (type
) == FUNCTION_TYPE
14393 || (TREE_CODE (type
) == ARRAY_TYPE
14394 && TYPE_DOMAIN (type
) == NULL_TREE
)
14395 || TREE_CODE (type
) == REFERENCE_TYPE
)
14397 if (complain
& tf_error
)
14398 error ("creating array of %qT", type
);
14399 return error_mark_node
;
14402 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14403 return error_mark_node
;
14405 r
= build_cplus_array_type (type
, domain
);
14407 if (TYPE_USER_ALIGN (t
))
14409 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14410 TYPE_USER_ALIGN (r
) = 1;
14416 case TYPENAME_TYPE
:
14418 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14419 in_decl
, /*entering_scope=*/1);
14420 if (ctx
== error_mark_node
)
14421 return error_mark_node
;
14423 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14424 complain
, in_decl
);
14425 if (f
== error_mark_node
)
14426 return error_mark_node
;
14428 if (!MAYBE_CLASS_TYPE_P (ctx
))
14430 if (complain
& tf_error
)
14431 error ("%qT is not a class, struct, or union type", ctx
);
14432 return error_mark_node
;
14434 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14436 /* Normally, make_typename_type does not require that the CTX
14437 have complete type in order to allow things like:
14439 template <class T> struct S { typename S<T>::X Y; };
14441 But, such constructs have already been resolved by this
14442 point, so here CTX really should have complete type, unless
14443 it's a partial instantiation. */
14444 ctx
= complete_type (ctx
);
14445 if (!COMPLETE_TYPE_P (ctx
))
14447 if (complain
& tf_error
)
14448 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14449 return error_mark_node
;
14453 f
= make_typename_type (ctx
, f
, typename_type
,
14454 complain
| tf_keep_type_decl
);
14455 if (f
== error_mark_node
)
14457 if (TREE_CODE (f
) == TYPE_DECL
)
14459 complain
|= tf_ignore_bad_quals
;
14463 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14465 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14467 if (complain
& tf_error
)
14468 error ("%qT resolves to %qT, which is not an enumeration type",
14471 return error_mark_node
;
14473 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14475 if (complain
& tf_error
)
14476 error ("%qT resolves to %qT, which is is not a class type",
14479 return error_mark_node
;
14483 return cp_build_qualified_type_real
14484 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14487 case UNBOUND_CLASS_TEMPLATE
:
14489 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14490 in_decl
, /*entering_scope=*/1);
14491 tree name
= TYPE_IDENTIFIER (t
);
14492 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14494 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14495 return error_mark_node
;
14498 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14499 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14506 ++cp_unevaluated_operand
;
14507 ++c_inhibit_evaluation_warnings
;
14509 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14511 /*integral_constant_expression_p=*/false);
14513 --cp_unevaluated_operand
;
14514 --c_inhibit_evaluation_warnings
;
14516 type
= finish_typeof (type
);
14517 return cp_build_qualified_type_real (type
,
14519 | cp_type_quals (type
),
14523 case DECLTYPE_TYPE
:
14527 ++cp_unevaluated_operand
;
14528 ++c_inhibit_evaluation_warnings
;
14530 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14531 complain
|tf_decltype
, in_decl
,
14532 /*function_p*/false,
14533 /*integral_constant_expression*/false);
14535 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14537 if (type
== NULL_TREE
)
14539 if (complain
& tf_error
)
14540 error ("empty initializer in lambda init-capture");
14541 type
= error_mark_node
;
14543 else if (TREE_CODE (type
) == TREE_LIST
)
14544 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14547 --cp_unevaluated_operand
;
14548 --c_inhibit_evaluation_warnings
;
14550 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14551 type
= lambda_capture_field_type (type
,
14552 DECLTYPE_FOR_INIT_CAPTURE (t
),
14553 DECLTYPE_FOR_REF_CAPTURE (t
));
14554 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14555 type
= lambda_proxy_type (type
);
14558 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14559 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14561 /* In a template ~id could be either a complement expression
14562 or an unqualified-id naming a destructor; if instantiating
14563 it produces an expression, it's not an id-expression or
14566 type
= finish_decltype_type (type
, id
, complain
);
14568 return cp_build_qualified_type_real (type
,
14570 | cp_type_quals (type
),
14571 complain
| tf_ignore_bad_quals
);
14574 case UNDERLYING_TYPE
:
14576 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14577 complain
, in_decl
);
14578 return finish_underlying_type (type
);
14581 case TYPE_ARGUMENT_PACK
:
14582 case NONTYPE_ARGUMENT_PACK
:
14586 if (code
== NONTYPE_ARGUMENT_PACK
)
14587 r
= make_node (code
);
14589 r
= cxx_make_type (code
);
14591 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14592 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14593 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14611 /* We should use one of the expression tsubsts for these codes. */
14612 gcc_unreachable ();
14615 sorry ("use of %qs in template", get_tree_code_name (code
));
14616 return error_mark_node
;
14620 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14621 expression on the left-hand side of the "." or "->" operator. We
14622 only do the lookup if we had a dependent BASELINK. Otherwise we
14623 adjust it onto the instantiated heirarchy. */
14626 tsubst_baselink (tree baselink
, tree object_type
,
14627 tree args
, tsubst_flags_t complain
, tree in_decl
)
14629 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
14630 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14631 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14633 tree optype
= BASELINK_OPTYPE (baselink
);
14634 optype
= tsubst (optype
, args
, complain
, in_decl
);
14636 tree template_args
= NULL_TREE
;
14637 bool template_id_p
= false;
14638 tree fns
= BASELINK_FUNCTIONS (baselink
);
14639 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14641 template_id_p
= true;
14642 template_args
= TREE_OPERAND (fns
, 1);
14643 fns
= TREE_OPERAND (fns
, 0);
14645 template_args
= tsubst_template_args (template_args
, args
,
14646 complain
, in_decl
);
14649 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
14650 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
14651 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
14655 tree name
= OVL_NAME (fns
);
14656 if (IDENTIFIER_CONV_OP_P (name
))
14657 name
= make_conv_op_name (optype
);
14659 if (name
== complete_dtor_identifier
)
14660 /* Treat as-if non-dependent below. */
14661 dependent_p
= false;
14663 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14666 if ((complain
& tf_error
)
14667 && constructor_name_p (name
, qualifying_scope
))
14668 error ("cannot call constructor %<%T::%D%> directly",
14669 qualifying_scope
, name
);
14670 return error_mark_node
;
14673 if (BASELINK_P (baselink
))
14674 fns
= BASELINK_FUNCTIONS (baselink
);
14677 /* We're going to overwrite pieces below, make a duplicate. */
14678 baselink
= copy_node (baselink
);
14680 /* If lookup found a single function, mark it as used at this point.
14681 (If lookup found multiple functions the one selected later by
14682 overload resolution will be marked as used at that point.) */
14683 if (!template_id_p
&& !really_overloaded_fn (fns
)
14684 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14685 return error_mark_node
;
14687 if (BASELINK_P (baselink
))
14689 /* Add back the template arguments, if present. */
14691 BASELINK_FUNCTIONS (baselink
)
14692 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
14694 /* Update the conversion operator type. */
14695 BASELINK_OPTYPE (baselink
) = optype
;
14699 object_type
= current_class_type
;
14701 if (qualified_p
|| !dependent_p
)
14703 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14707 /* We need to call adjust_result_of_qualified_name_lookup in case the
14708 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14709 so that we still get virtual function binding. */
14710 BASELINK_QUALIFIED_P (baselink
) = false;
14716 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14717 true if the qualified-id will be a postfix-expression in-and-of
14718 itself; false if more of the postfix-expression follows the
14719 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14723 tsubst_qualified_id (tree qualified_id
, tree args
,
14724 tsubst_flags_t complain
, tree in_decl
,
14725 bool done
, bool address_p
)
14731 tree template_args
;
14732 location_t loc
= UNKNOWN_LOCATION
;
14734 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14736 /* Figure out what name to look up. */
14737 name
= TREE_OPERAND (qualified_id
, 1);
14738 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14740 is_template
= true;
14741 loc
= EXPR_LOCATION (name
);
14742 template_args
= TREE_OPERAND (name
, 1);
14744 template_args
= tsubst_template_args (template_args
, args
,
14745 complain
, in_decl
);
14746 if (template_args
== error_mark_node
)
14747 return error_mark_node
;
14748 name
= TREE_OPERAND (name
, 0);
14752 is_template
= false;
14753 template_args
= NULL_TREE
;
14756 /* Substitute into the qualifying scope. When there are no ARGS, we
14757 are just trying to simplify a non-dependent expression. In that
14758 case the qualifying scope may be dependent, and, in any case,
14759 substituting will not help. */
14760 scope
= TREE_OPERAND (qualified_id
, 0);
14763 scope
= tsubst (scope
, args
, complain
, in_decl
);
14764 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14769 if (dependent_scope_p (scope
))
14772 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14773 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14774 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14775 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14779 if (!BASELINK_P (name
) && !DECL_P (expr
))
14781 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14783 /* A BIT_NOT_EXPR is used to represent a destructor. */
14784 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14786 error ("qualifying type %qT does not match destructor name ~%qT",
14787 scope
, TREE_OPERAND (expr
, 0));
14788 expr
= error_mark_node
;
14791 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14792 /*is_type_p=*/0, false);
14795 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14796 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14797 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14799 if (complain
& tf_error
)
14801 error ("dependent-name %qE is parsed as a non-type, but "
14802 "instantiation yields a type", qualified_id
);
14803 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14805 return error_mark_node
;
14811 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14813 /* Remember that there was a reference to this entity. */
14814 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14815 return error_mark_node
;
14818 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14820 if (complain
& tf_error
)
14821 qualified_name_lookup_error (scope
,
14822 TREE_OPERAND (qualified_id
, 1),
14823 expr
, input_location
);
14824 return error_mark_node
;
14829 if (variable_template_p (expr
))
14830 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14833 expr
= lookup_template_function (expr
, template_args
);
14836 if (expr
== error_mark_node
&& complain
& tf_error
)
14837 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14838 expr
, input_location
);
14839 else if (TYPE_P (scope
))
14841 expr
= (adjust_result_of_qualified_name_lookup
14842 (expr
, scope
, current_nonlambda_class_type ()));
14843 expr
= (finish_qualified_id_expr
14844 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14845 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14846 /*template_arg_p=*/false, complain
));
14849 /* Expressions do not generally have reference type. */
14850 if (TREE_CODE (expr
) != SCOPE_REF
14851 /* However, if we're about to form a pointer-to-member, we just
14852 want the referenced member referenced. */
14853 && TREE_CODE (expr
) != OFFSET_REF
)
14854 expr
= convert_from_reference (expr
);
14856 if (REF_PARENTHESIZED_P (qualified_id
))
14857 expr
= force_paren_expr (expr
);
14862 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14863 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14867 tsubst_init (tree init
, tree decl
, tree args
,
14868 tsubst_flags_t complain
, tree in_decl
)
14873 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14875 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14877 /* If we had an initializer but it
14878 instantiated to nothing,
14879 value-initialize the object. This will
14880 only occur when the initializer was a
14881 pack expansion where the parameter packs
14882 used in that expansion were of length
14884 init
= build_value_init (TREE_TYPE (decl
),
14886 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14887 init
= get_target_expr_sfinae (init
, complain
);
14888 if (TREE_CODE (init
) == TARGET_EXPR
)
14889 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14895 /* Like tsubst, but deals with expressions. This function just replaces
14896 template parms; to finish processing the resultant expression, use
14897 tsubst_copy_and_build or tsubst_expr. */
14900 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14902 enum tree_code code
;
14905 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14908 code
= TREE_CODE (t
);
14913 r
= retrieve_local_specialization (t
);
14915 if (r
== NULL_TREE
)
14917 /* We get here for a use of 'this' in an NSDMI. */
14918 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
14919 return current_class_ptr
;
14921 /* This can happen for a parameter name used later in a function
14922 declaration (such as in a late-specified return type). Just
14923 make a dummy decl, since it's only used for its type. */
14924 gcc_assert (cp_unevaluated_operand
!= 0);
14925 r
= tsubst_decl (t
, args
, complain
);
14926 /* Give it the template pattern as its context; its true context
14927 hasn't been instantiated yet and this is good enough for
14929 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14932 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14933 r
= argument_pack_select_arg (r
);
14934 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14935 return error_mark_node
;
14943 if (DECL_TEMPLATE_PARM_P (t
))
14944 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14945 /* There is no need to substitute into namespace-scope
14947 if (DECL_NAMESPACE_SCOPE_P (t
))
14949 /* If ARGS is NULL, then T is known to be non-dependent. */
14950 if (args
== NULL_TREE
)
14951 return scalar_constant_value (t
);
14953 /* Unfortunately, we cannot just call lookup_name here.
14956 template <int I> int f() {
14958 struct S { void g() { E e = a; } };
14961 When we instantiate f<7>::S::g(), say, lookup_name is not
14962 clever enough to find f<7>::a. */
14964 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14965 /*entering_scope=*/0);
14967 for (v
= TYPE_VALUES (enum_type
);
14969 v
= TREE_CHAIN (v
))
14970 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14971 return TREE_VALUE (v
);
14973 /* We didn't find the name. That should never happen; if
14974 name-lookup found it during preliminary parsing, we
14975 should find it again here during instantiation. */
14976 gcc_unreachable ();
14981 if (DECL_CONTEXT (t
))
14985 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14986 /*entering_scope=*/1);
14987 if (ctx
!= DECL_CONTEXT (t
))
14989 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14992 if (complain
& tf_error
)
14993 error ("using invalid field %qD", t
);
14994 return error_mark_node
;
15003 case FUNCTION_DECL
:
15004 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15005 r
= tsubst (t
, args
, complain
, in_decl
);
15006 else if (local_variable_p (t
)
15007 && uses_template_parms (DECL_CONTEXT (t
)))
15009 r
= retrieve_local_specialization (t
);
15010 if (r
== NULL_TREE
)
15012 /* First try name lookup to find the instantiation. */
15013 r
= lookup_name (DECL_NAME (t
));
15014 if (r
&& !is_capture_proxy (r
))
15016 /* Make sure that the one we found is the one we want. */
15017 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15018 if (ctx
!= DECL_CONTEXT (r
))
15026 /* This can happen for a variable used in a
15027 late-specified return type of a local lambda, or for a
15028 local static or constant. Building a new VAR_DECL
15029 should be OK in all those cases. */
15030 r
= tsubst_decl (t
, args
, complain
);
15031 if (local_specializations
)
15032 /* Avoid infinite recursion (79640). */
15033 register_local_specialization (r
, t
);
15034 if (decl_maybe_constant_var_p (r
))
15036 /* We can't call cp_finish_decl, so handle the
15037 initializer by hand. */
15038 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15039 complain
, in_decl
);
15040 if (!processing_template_decl
)
15041 init
= maybe_constant_init (init
);
15042 if (processing_template_decl
15043 ? potential_constant_expression (init
)
15044 : reduced_constant_expression_p (init
))
15045 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15046 = TREE_CONSTANT (r
) = true;
15047 DECL_INITIAL (r
) = init
;
15048 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15050 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15051 complain
, adc_variable_type
);
15053 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15054 || decl_constant_var_p (r
)
15055 || errorcount
|| sorrycount
);
15056 if (!processing_template_decl
15057 && !TREE_STATIC (r
))
15058 r
= process_outer_var_ref (r
, complain
);
15060 /* Remember this for subsequent uses. */
15061 if (local_specializations
)
15062 register_local_specialization (r
, t
);
15064 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15065 r
= argument_pack_select_arg (r
);
15069 if (!mark_used (r
, complain
))
15070 return error_mark_node
;
15073 case NAMESPACE_DECL
:
15077 /* An OVERLOAD will always be a non-dependent overload set; an
15078 overload set from function scope will just be represented with an
15079 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15080 gcc_assert (!uses_template_parms (t
));
15081 /* We must have marked any lookups as persistent. */
15082 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
15086 return tsubst_baselink (t
, current_nonlambda_class_type (),
15087 args
, complain
, in_decl
);
15089 case TEMPLATE_DECL
:
15090 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15091 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15092 args
, complain
, in_decl
);
15093 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15094 return tsubst (t
, args
, complain
, in_decl
);
15095 else if (DECL_CLASS_SCOPE_P (t
)
15096 && uses_template_parms (DECL_CONTEXT (t
)))
15098 /* Template template argument like the following example need
15101 template <template <class> class TT> struct C {};
15102 template <class T> struct D {
15103 template <class U> struct E {};
15108 We are processing the template argument `E' in #1 for
15109 the template instantiation #2. Originally, `E' is a
15110 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15111 have to substitute this with one having context `D<int>'. */
15113 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15114 if (dependent_scope_p (context
))
15116 /* When rewriting a constructor into a deduction guide, a
15117 non-dependent name can become dependent, so memtmpl<args>
15118 becomes context::template memtmpl<args>. */
15119 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15120 return build_qualified_name (type
, context
, DECL_NAME (t
),
15123 return lookup_field (context
, DECL_NAME(t
), 0, false);
15126 /* Ordinary template template argument. */
15129 case NON_LVALUE_EXPR
:
15130 case VIEW_CONVERT_EXPR
:
15132 /* Handle location wrappers by substituting the wrapped node
15133 first, *then* reusing the resulting type. Doing the type
15134 first ensures that we handle template parameters and
15135 parameter pack expansions. */
15136 gcc_assert (location_wrapper_p (t
));
15137 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15138 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15142 case REINTERPRET_CAST_EXPR
:
15143 case CONST_CAST_EXPR
:
15144 case STATIC_CAST_EXPR
:
15145 case DYNAMIC_CAST_EXPR
:
15146 case IMPLICIT_CONV_EXPR
:
15150 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15151 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15152 return build1 (code
, type
, op0
);
15156 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15157 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15159 tree expanded
, op
= TREE_OPERAND (t
, 0);
15162 if (SIZEOF_EXPR_TYPE_P (t
))
15163 op
= TREE_TYPE (op
);
15165 ++cp_unevaluated_operand
;
15166 ++c_inhibit_evaluation_warnings
;
15167 /* We only want to compute the number of arguments. */
15168 if (PACK_EXPANSION_P (op
))
15169 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15171 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15172 args
, complain
, in_decl
);
15173 --cp_unevaluated_operand
;
15174 --c_inhibit_evaluation_warnings
;
15176 if (TREE_CODE (expanded
) == TREE_VEC
)
15178 len
= TREE_VEC_LENGTH (expanded
);
15179 /* Set TREE_USED for the benefit of -Wunused. */
15180 for (int i
= 0; i
< len
; i
++)
15181 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15182 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15185 if (expanded
== error_mark_node
)
15186 return error_mark_node
;
15187 else if (PACK_EXPANSION_P (expanded
)
15188 || (TREE_CODE (expanded
) == TREE_VEC
15189 && pack_expansion_args_count (expanded
)))
15192 if (PACK_EXPANSION_P (expanded
))
15194 else if (TREE_VEC_LENGTH (expanded
) == 1)
15195 expanded
= TREE_VEC_ELT (expanded
, 0);
15197 expanded
= make_argument_pack (expanded
);
15199 if (TYPE_P (expanded
))
15200 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15201 complain
& tf_error
);
15203 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15204 complain
& tf_error
);
15207 return build_int_cst (size_type_node
, len
);
15209 if (SIZEOF_EXPR_TYPE_P (t
))
15211 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15212 args
, complain
, in_decl
);
15213 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15214 r
= build1 (SIZEOF_EXPR
,
15215 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15216 SIZEOF_EXPR_TYPE_P (r
) = 1;
15223 case TRUTH_NOT_EXPR
:
15226 case UNARY_PLUS_EXPR
: /* Unary + */
15228 case AT_ENCODE_EXPR
:
15232 case REALPART_EXPR
:
15233 case IMAGPART_EXPR
:
15236 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15237 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15238 return build1 (code
, type
, op0
);
15241 case COMPONENT_REF
:
15246 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15247 name
= TREE_OPERAND (t
, 1);
15248 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15250 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15251 complain
, in_decl
);
15252 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15254 else if (TREE_CODE (name
) == SCOPE_REF
15255 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15257 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15258 complain
, in_decl
);
15259 name
= TREE_OPERAND (name
, 1);
15260 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15261 complain
, in_decl
);
15262 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15263 name
= build_qualified_name (/*type=*/NULL_TREE
,
15265 /*template_p=*/false);
15267 else if (BASELINK_P (name
))
15268 name
= tsubst_baselink (name
,
15269 non_reference (TREE_TYPE (object
)),
15273 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15274 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15280 case TRUNC_DIV_EXPR
:
15281 case CEIL_DIV_EXPR
:
15282 case FLOOR_DIV_EXPR
:
15283 case ROUND_DIV_EXPR
:
15284 case EXACT_DIV_EXPR
:
15288 case TRUNC_MOD_EXPR
:
15289 case FLOOR_MOD_EXPR
:
15290 case TRUTH_ANDIF_EXPR
:
15291 case TRUTH_ORIF_EXPR
:
15292 case TRUTH_AND_EXPR
:
15293 case TRUTH_OR_EXPR
:
15306 case COMPOUND_EXPR
:
15309 case PREDECREMENT_EXPR
:
15310 case PREINCREMENT_EXPR
:
15311 case POSTDECREMENT_EXPR
:
15312 case POSTINCREMENT_EXPR
:
15314 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15315 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15316 return build_nt (code
, op0
, op1
);
15321 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15322 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15323 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15324 QUALIFIED_NAME_IS_TEMPLATE (t
));
15329 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15330 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15331 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15336 int n
= VL_EXP_OPERAND_LENGTH (t
);
15337 tree result
= build_vl_exp (CALL_EXPR
, n
);
15339 for (i
= 0; i
< n
; i
++)
15340 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15341 complain
, in_decl
);
15347 case PSEUDO_DTOR_EXPR
:
15348 case VEC_PERM_EXPR
:
15350 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15351 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15352 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15353 r
= build_nt (code
, op0
, op1
, op2
);
15354 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15360 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15361 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15362 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15363 r
= build_nt (code
, op0
, op1
, op2
);
15364 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15370 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15371 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15372 r
= build_nt (code
, op0
, op1
);
15373 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15374 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15378 case TEMPLATE_ID_EXPR
:
15380 /* Substituted template arguments */
15381 tree fn
= TREE_OPERAND (t
, 0);
15382 tree targs
= TREE_OPERAND (t
, 1);
15384 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15386 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15388 return lookup_template_function (fn
, targs
);
15393 tree purpose
, value
, chain
;
15395 if (t
== void_list_node
)
15398 purpose
= TREE_PURPOSE (t
);
15400 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15401 value
= TREE_VALUE (t
);
15403 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15404 chain
= TREE_CHAIN (t
);
15405 if (chain
&& chain
!= void_type_node
)
15406 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15407 if (purpose
== TREE_PURPOSE (t
)
15408 && value
== TREE_VALUE (t
)
15409 && chain
== TREE_CHAIN (t
))
15411 return tree_cons (purpose
, value
, chain
);
15416 case ENUMERAL_TYPE
:
15418 case TEMPLATE_TYPE_PARM
:
15419 case TEMPLATE_TEMPLATE_PARM
:
15420 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15421 case TEMPLATE_PARM_INDEX
:
15423 case REFERENCE_TYPE
:
15425 case FUNCTION_TYPE
:
15428 case TYPENAME_TYPE
:
15429 case UNBOUND_CLASS_TEMPLATE
:
15431 case DECLTYPE_TYPE
:
15433 return tsubst (t
, args
, complain
, in_decl
);
15437 /* Fall through. */
15438 case IDENTIFIER_NODE
:
15439 if (IDENTIFIER_CONV_OP_P (t
))
15441 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15442 return make_conv_op_name (new_type
);
15448 /* This is handled by tsubst_copy_and_build. */
15449 gcc_unreachable ();
15453 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15454 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15455 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15458 case CLEANUP_POINT_EXPR
:
15459 /* We shouldn't have built any of these during initial template
15460 generation. Instead, they should be built during instantiation
15461 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15462 gcc_unreachable ();
15466 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15467 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15468 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15469 r
= build2 (code
, type
, op0
, op1
);
15470 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15471 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15472 && !(complain
& tf_error
))
15473 return error_mark_node
;
15477 case EXPR_PACK_EXPANSION
:
15478 error ("invalid use of pack expansion expression");
15479 return error_mark_node
;
15481 case NONTYPE_ARGUMENT_PACK
:
15482 error ("use %<...%> to expand argument pack");
15483 return error_mark_node
;
15486 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15494 /* Instantiate any typedefs in the type. */
15495 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15496 r
= fold_convert (type
, t
);
15497 gcc_assert (TREE_CODE (r
) == code
);
15502 /* These can sometimes show up in a partial instantiation, but never
15503 involve template parms. */
15504 gcc_assert (!uses_template_parms (t
));
15507 case UNARY_LEFT_FOLD_EXPR
:
15508 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15509 case UNARY_RIGHT_FOLD_EXPR
:
15510 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15511 case BINARY_LEFT_FOLD_EXPR
:
15512 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15513 case BINARY_RIGHT_FOLD_EXPR
:
15514 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15518 case DEBUG_BEGIN_STMT
:
15519 /* ??? There's no point in copying it for now, but maybe some
15520 day it will contain more information, such as a pointer back
15521 to the containing function, inlined copy or so. */
15525 /* We shouldn't get here, but keep going if !flag_checking. */
15527 gcc_unreachable ();
15532 /* Helper function for tsubst_omp_clauses, used for instantiation of
15533 OMP_CLAUSE_DECL of clauses. */
15536 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15539 if (decl
== NULL_TREE
)
15542 /* Handle an OpenMP array section represented as a TREE_LIST (or
15543 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15544 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15545 TREE_LIST. We can handle it exactly the same as an array section
15546 (purpose, value, and a chain), even though the nomenclature
15547 (low_bound, length, etc) is different. */
15548 if (TREE_CODE (decl
) == TREE_LIST
)
15551 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15552 /*integral_constant_expression_p=*/false);
15553 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15554 /*integral_constant_expression_p=*/false);
15555 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15557 if (TREE_PURPOSE (decl
) == low_bound
15558 && TREE_VALUE (decl
) == length
15559 && TREE_CHAIN (decl
) == chain
)
15561 tree ret
= tree_cons (low_bound
, length
, chain
);
15562 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15563 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15566 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15567 /*integral_constant_expression_p=*/false);
15568 /* Undo convert_from_reference tsubst_expr could have called. */
15570 && REFERENCE_REF_P (ret
)
15571 && !REFERENCE_REF_P (decl
))
15572 ret
= TREE_OPERAND (ret
, 0);
15576 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15579 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15580 tree args
, tsubst_flags_t complain
, tree in_decl
)
15582 tree new_clauses
= NULL_TREE
, nc
, oc
;
15583 tree linear_no_step
= NULL_TREE
;
15585 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15587 nc
= copy_node (oc
);
15588 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15591 switch (OMP_CLAUSE_CODE (nc
))
15593 case OMP_CLAUSE_LASTPRIVATE
:
15594 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15596 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15597 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15598 in_decl
, /*integral_constant_expression_p=*/false);
15599 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15600 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15603 case OMP_CLAUSE_PRIVATE
:
15604 case OMP_CLAUSE_SHARED
:
15605 case OMP_CLAUSE_FIRSTPRIVATE
:
15606 case OMP_CLAUSE_COPYIN
:
15607 case OMP_CLAUSE_COPYPRIVATE
:
15608 case OMP_CLAUSE_UNIFORM
:
15609 case OMP_CLAUSE_DEPEND
:
15610 case OMP_CLAUSE_FROM
:
15611 case OMP_CLAUSE_TO
:
15612 case OMP_CLAUSE_MAP
:
15613 case OMP_CLAUSE_USE_DEVICE_PTR
:
15614 case OMP_CLAUSE_IS_DEVICE_PTR
:
15615 OMP_CLAUSE_DECL (nc
)
15616 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15619 case OMP_CLAUSE_TILE
:
15620 case OMP_CLAUSE_IF
:
15621 case OMP_CLAUSE_NUM_THREADS
:
15622 case OMP_CLAUSE_SCHEDULE
:
15623 case OMP_CLAUSE_COLLAPSE
:
15624 case OMP_CLAUSE_FINAL
:
15625 case OMP_CLAUSE_DEVICE
:
15626 case OMP_CLAUSE_DIST_SCHEDULE
:
15627 case OMP_CLAUSE_NUM_TEAMS
:
15628 case OMP_CLAUSE_THREAD_LIMIT
:
15629 case OMP_CLAUSE_SAFELEN
:
15630 case OMP_CLAUSE_SIMDLEN
:
15631 case OMP_CLAUSE_NUM_TASKS
:
15632 case OMP_CLAUSE_GRAINSIZE
:
15633 case OMP_CLAUSE_PRIORITY
:
15634 case OMP_CLAUSE_ORDERED
:
15635 case OMP_CLAUSE_HINT
:
15636 case OMP_CLAUSE_NUM_GANGS
:
15637 case OMP_CLAUSE_NUM_WORKERS
:
15638 case OMP_CLAUSE_VECTOR_LENGTH
:
15639 case OMP_CLAUSE_WORKER
:
15640 case OMP_CLAUSE_VECTOR
:
15641 case OMP_CLAUSE_ASYNC
:
15642 case OMP_CLAUSE_WAIT
:
15643 OMP_CLAUSE_OPERAND (nc
, 0)
15644 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15645 in_decl
, /*integral_constant_expression_p=*/false);
15647 case OMP_CLAUSE_REDUCTION
:
15648 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15650 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15651 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15653 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15654 complain
, in_decl
);
15655 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15656 = build_qualified_name (NULL_TREE
, scope
,
15657 TREE_OPERAND (placeholder
, 1),
15661 gcc_assert (identifier_p (placeholder
));
15663 OMP_CLAUSE_DECL (nc
)
15664 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15667 case OMP_CLAUSE_GANG
:
15668 case OMP_CLAUSE_ALIGNED
:
15669 OMP_CLAUSE_DECL (nc
)
15670 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15672 OMP_CLAUSE_OPERAND (nc
, 1)
15673 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15674 in_decl
, /*integral_constant_expression_p=*/false);
15676 case OMP_CLAUSE_LINEAR
:
15677 OMP_CLAUSE_DECL (nc
)
15678 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15680 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15682 gcc_assert (!linear_no_step
);
15683 linear_no_step
= nc
;
15685 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15686 OMP_CLAUSE_LINEAR_STEP (nc
)
15687 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15688 complain
, in_decl
);
15690 OMP_CLAUSE_LINEAR_STEP (nc
)
15691 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15693 /*integral_constant_expression_p=*/false);
15695 case OMP_CLAUSE_NOWAIT
:
15696 case OMP_CLAUSE_DEFAULT
:
15697 case OMP_CLAUSE_UNTIED
:
15698 case OMP_CLAUSE_MERGEABLE
:
15699 case OMP_CLAUSE_INBRANCH
:
15700 case OMP_CLAUSE_NOTINBRANCH
:
15701 case OMP_CLAUSE_PROC_BIND
:
15702 case OMP_CLAUSE_FOR
:
15703 case OMP_CLAUSE_PARALLEL
:
15704 case OMP_CLAUSE_SECTIONS
:
15705 case OMP_CLAUSE_TASKGROUP
:
15706 case OMP_CLAUSE_NOGROUP
:
15707 case OMP_CLAUSE_THREADS
:
15708 case OMP_CLAUSE_SIMD
:
15709 case OMP_CLAUSE_DEFAULTMAP
:
15710 case OMP_CLAUSE_INDEPENDENT
:
15711 case OMP_CLAUSE_AUTO
:
15712 case OMP_CLAUSE_SEQ
:
15715 gcc_unreachable ();
15717 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15718 switch (OMP_CLAUSE_CODE (nc
))
15720 case OMP_CLAUSE_SHARED
:
15721 case OMP_CLAUSE_PRIVATE
:
15722 case OMP_CLAUSE_FIRSTPRIVATE
:
15723 case OMP_CLAUSE_LASTPRIVATE
:
15724 case OMP_CLAUSE_COPYPRIVATE
:
15725 case OMP_CLAUSE_LINEAR
:
15726 case OMP_CLAUSE_REDUCTION
:
15727 case OMP_CLAUSE_USE_DEVICE_PTR
:
15728 case OMP_CLAUSE_IS_DEVICE_PTR
:
15729 /* tsubst_expr on SCOPE_REF results in returning
15730 finish_non_static_data_member result. Undo that here. */
15731 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15732 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15733 == IDENTIFIER_NODE
))
15735 tree t
= OMP_CLAUSE_DECL (nc
);
15738 switch (TREE_CODE (v
))
15740 case COMPONENT_REF
:
15744 case POINTER_PLUS_EXPR
:
15745 v
= TREE_OPERAND (v
, 0);
15748 if (DECL_CONTEXT (v
) == current_function_decl
15749 && DECL_ARTIFICIAL (v
)
15750 && DECL_NAME (v
) == this_identifier
)
15751 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15758 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15759 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15760 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15761 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15762 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15764 tree decl
= OMP_CLAUSE_DECL (nc
);
15767 retrofit_lang_decl (decl
);
15768 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15777 new_clauses
= nreverse (new_clauses
);
15778 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15780 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15781 if (linear_no_step
)
15782 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15783 if (nc
== linear_no_step
)
15785 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15789 return new_clauses
;
15792 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15795 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15798 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15800 tree purpose
, value
, chain
;
15805 if (TREE_CODE (t
) != TREE_LIST
)
15806 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15807 /*function_p=*/false,
15808 /*integral_constant_expression_p=*/false);
15810 if (t
== void_list_node
)
15813 purpose
= TREE_PURPOSE (t
);
15815 purpose
= RECUR (purpose
);
15816 value
= TREE_VALUE (t
);
15819 if (TREE_CODE (value
) != LABEL_DECL
)
15820 value
= RECUR (value
);
15823 value
= lookup_label (DECL_NAME (value
));
15824 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15825 TREE_USED (value
) = 1;
15828 chain
= TREE_CHAIN (t
);
15829 if (chain
&& chain
!= void_type_node
)
15830 chain
= RECUR (chain
);
15831 return tree_cons (purpose
, value
, chain
);
15835 /* Used to temporarily communicate the list of #pragma omp parallel
15836 clauses to #pragma omp for instantiation if they are combined
15839 static tree
*omp_parallel_combined_clauses
;
15841 /* Substitute one OMP_FOR iterator. */
15844 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15845 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15846 tree args
, tsubst_flags_t complain
, tree in_decl
,
15847 bool integral_constant_expression_p
)
15849 #define RECUR(NODE) \
15850 tsubst_expr ((NODE), args, complain, in_decl, \
15851 integral_constant_expression_p)
15852 tree decl
, init
, cond
, incr
;
15854 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15855 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15857 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15859 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15860 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15863 decl
= TREE_OPERAND (init
, 0);
15864 init
= TREE_OPERAND (init
, 1);
15865 tree decl_expr
= NULL_TREE
;
15866 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15868 /* We need to jump through some hoops to handle declarations in the
15869 init-statement, since we might need to handle auto deduction,
15870 but we need to keep control of initialization. */
15872 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15873 decl
= tsubst_decl (decl
, args
, complain
);
15877 if (TREE_CODE (decl
) == SCOPE_REF
)
15879 decl
= RECUR (decl
);
15880 if (TREE_CODE (decl
) == COMPONENT_REF
)
15884 switch (TREE_CODE (v
))
15886 case COMPONENT_REF
:
15890 case POINTER_PLUS_EXPR
:
15891 v
= TREE_OPERAND (v
, 0);
15894 if (DECL_CONTEXT (v
) == current_function_decl
15895 && DECL_ARTIFICIAL (v
)
15896 && DECL_NAME (v
) == this_identifier
)
15898 decl
= TREE_OPERAND (decl
, 1);
15899 decl
= omp_privatize_field (decl
, false);
15909 decl
= RECUR (decl
);
15911 init
= RECUR (init
);
15913 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15914 if (auto_node
&& init
)
15916 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
15918 gcc_assert (!type_dependent_expression_p (decl
));
15920 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15924 /* Declare the variable, but don't let that initialize it. */
15925 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15926 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15928 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15931 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15932 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15933 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15935 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15936 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15937 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15938 NOP_EXPR
, rhs
, complain
);
15941 incr
= RECUR (incr
);
15942 TREE_VEC_ELT (declv
, i
) = decl
;
15943 TREE_VEC_ELT (initv
, i
) = init
;
15944 TREE_VEC_ELT (condv
, i
) = cond
;
15945 TREE_VEC_ELT (incrv
, i
) = incr
;
15951 /* Declare and initialize the variable. */
15959 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15961 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15963 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15964 && OMP_CLAUSE_DECL (*pc
) == decl
)
15966 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15967 && OMP_CLAUSE_DECL (*pc
) == decl
)
15971 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15973 *pc
= OMP_CLAUSE_CHAIN (c
);
15974 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15977 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15978 && OMP_CLAUSE_DECL (*pc
) == decl
)
15980 error ("iteration variable %qD should not be firstprivate",
15982 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15984 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15985 && OMP_CLAUSE_DECL (*pc
) == decl
)
15987 error ("iteration variable %qD should not be reduction",
15989 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15992 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15997 if (*pc
== NULL_TREE
)
15999 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16000 OMP_CLAUSE_DECL (c
) = decl
;
16001 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16004 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16009 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16010 if (COMPARISON_CLASS_P (cond
))
16012 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16013 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16014 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16017 cond
= RECUR (cond
);
16018 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16019 switch (TREE_CODE (incr
))
16021 case PREINCREMENT_EXPR
:
16022 case PREDECREMENT_EXPR
:
16023 case POSTINCREMENT_EXPR
:
16024 case POSTDECREMENT_EXPR
:
16025 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16026 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16029 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16030 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16032 tree rhs
= TREE_OPERAND (incr
, 1);
16033 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16034 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16035 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16036 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16037 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16041 incr
= RECUR (incr
);
16044 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16045 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16047 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16048 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16049 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16050 TREE_TYPE (decl
), lhs
,
16051 RECUR (TREE_OPERAND (incr
, 2))));
16053 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16054 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16055 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16057 tree rhs
= TREE_OPERAND (incr
, 2);
16058 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16059 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16060 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16061 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16062 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16066 incr
= RECUR (incr
);
16069 incr
= RECUR (incr
);
16073 TREE_VEC_ELT (declv
, i
) = decl
;
16074 TREE_VEC_ELT (initv
, i
) = init
;
16075 TREE_VEC_ELT (condv
, i
) = cond
;
16076 TREE_VEC_ELT (incrv
, i
) = incr
;
16080 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16081 of OMP_TARGET's body. */
16084 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16086 *walk_subtrees
= 0;
16087 switch (TREE_CODE (*tp
))
16092 case STATEMENT_LIST
:
16093 *walk_subtrees
= 1;
16101 /* Helper function for tsubst_expr. For decomposition declaration
16102 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16103 also the corresponding decls representing the identifiers
16104 of the decomposition declaration. Return DECL if successful
16105 or error_mark_node otherwise, set *FIRST to the first decl
16106 in the list chained through DECL_CHAIN and *CNT to the number
16110 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16111 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16114 tree decl2
, decl3
, prev
= decl
;
16116 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16117 for (decl2
= DECL_CHAIN (pattern_decl
);
16120 && DECL_DECOMPOSITION_P (decl2
)
16121 && DECL_NAME (decl2
);
16122 decl2
= DECL_CHAIN (decl2
))
16124 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16126 gcc_assert (errorcount
);
16127 return error_mark_node
;
16130 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16131 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16132 tree v
= DECL_VALUE_EXPR (decl2
);
16133 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16134 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16135 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16136 SET_DECL_VALUE_EXPR (decl2
, v
);
16137 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16139 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16140 maybe_push_decl (decl3
);
16141 if (error_operand_p (decl3
))
16142 decl
= error_mark_node
;
16143 else if (decl
!= error_mark_node
16144 && DECL_CHAIN (decl3
) != prev
)
16146 gcc_assert (errorcount
);
16147 decl
= error_mark_node
;
16156 /* Like tsubst_copy for expressions, etc. but also does semantic
16160 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16161 bool integral_constant_expression_p
)
16163 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16164 #define RECUR(NODE) \
16165 tsubst_expr ((NODE), args, complain, in_decl, \
16166 integral_constant_expression_p)
16172 if (t
== NULL_TREE
|| t
== error_mark_node
)
16175 loc
= input_location
;
16176 if (EXPR_HAS_LOCATION (t
))
16177 input_location
= EXPR_LOCATION (t
);
16178 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16179 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16181 switch (TREE_CODE (t
))
16183 case STATEMENT_LIST
:
16185 tree_stmt_iterator i
;
16186 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16187 RECUR (tsi_stmt (i
));
16191 case CTOR_INITIALIZER
:
16192 finish_mem_initializers (tsubst_initializer_list
16193 (TREE_OPERAND (t
, 0), args
));
16197 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16201 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16202 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16203 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16205 finish_expr_stmt (tmp
);
16209 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16210 /*attribs=*/NULL_TREE
);
16215 tree decl
, pattern_decl
;
16218 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16219 if (TREE_CODE (decl
) == LABEL_DECL
)
16220 finish_label_decl (DECL_NAME (decl
));
16221 else if (TREE_CODE (decl
) == USING_DECL
)
16223 tree scope
= USING_DECL_SCOPE (decl
);
16224 tree name
= DECL_NAME (decl
);
16226 scope
= tsubst (scope
, args
, complain
, in_decl
);
16227 decl
= lookup_qualified_name (scope
, name
,
16228 /*is_type_p=*/false,
16229 /*complain=*/false);
16230 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16231 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16233 finish_local_using_decl (decl
, scope
, name
);
16235 else if (is_capture_proxy (decl
)
16236 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16238 /* We're in tsubst_lambda_expr, we've already inserted a new
16239 capture proxy, so look it up and register it. */
16241 if (DECL_PACK_P (decl
))
16243 inst
= (retrieve_local_specialization
16244 (DECL_CAPTURED_VARIABLE (decl
)));
16245 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16249 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16250 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16251 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16253 register_local_specialization (inst
, decl
);
16256 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16257 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16258 /* Don't copy the old closure; we'll create a new one in
16259 tsubst_lambda_expr. */
16263 init
= DECL_INITIAL (decl
);
16264 decl
= tsubst (decl
, args
, complain
, in_decl
);
16265 if (decl
!= error_mark_node
)
16267 /* By marking the declaration as instantiated, we avoid
16268 trying to instantiate it. Since instantiate_decl can't
16269 handle local variables, and since we've already done
16270 all that needs to be done, that's the right thing to
16273 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16275 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16276 /* Anonymous aggregates are a special case. */
16277 finish_anon_union (decl
);
16278 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16280 DECL_CONTEXT (decl
) = current_function_decl
;
16281 if (DECL_NAME (decl
) == this_identifier
)
16283 tree lam
= DECL_CONTEXT (current_function_decl
);
16284 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16285 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16287 insert_capture_proxy (decl
);
16289 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16290 /* We already did a pushtag. */;
16291 else if (TREE_CODE (decl
) == FUNCTION_DECL
16292 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16293 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16295 DECL_CONTEXT (decl
) = NULL_TREE
;
16297 DECL_CONTEXT (decl
) = current_function_decl
;
16298 cp_check_omp_declare_reduction (decl
);
16302 int const_init
= false;
16303 maybe_push_decl (decl
);
16305 && DECL_PRETTY_FUNCTION_P (decl
))
16307 /* For __PRETTY_FUNCTION__ we have to adjust the
16309 const char *const name
16310 = cxx_printable_name (current_function_decl
, 2);
16311 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16314 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16317 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16320 && DECL_DECOMPOSITION_P (decl
)
16321 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16326 = tsubst_decomp_names (decl
, pattern_decl
, args
,
16327 complain
, in_decl
, &first
, &cnt
);
16328 if (ndecl
!= error_mark_node
)
16329 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16330 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16331 if (ndecl
!= error_mark_node
)
16332 cp_finish_decomp (ndecl
, first
, cnt
);
16335 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16344 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16345 RECUR (FOR_INIT_STMT (t
));
16346 finish_init_stmt (stmt
);
16347 tmp
= RECUR (FOR_COND (t
));
16348 finish_for_cond (tmp
, stmt
, false, 0);
16349 tmp
= RECUR (FOR_EXPR (t
));
16350 finish_for_expr (tmp
, stmt
);
16352 bool prev
= note_iteration_stmt_body_start ();
16353 RECUR (FOR_BODY (t
));
16354 note_iteration_stmt_body_end (prev
);
16356 finish_for_stmt (stmt
);
16359 case RANGE_FOR_STMT
:
16361 /* Construct another range_for, if this is not a final
16362 substitution (for inside inside a generic lambda of a
16363 template). Otherwise convert to a regular for. */
16365 stmt
= (processing_template_decl
16366 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16367 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16368 decl
= RANGE_FOR_DECL (t
);
16369 decl
= tsubst (decl
, args
, complain
, in_decl
);
16370 maybe_push_decl (decl
);
16371 expr
= RECUR (RANGE_FOR_EXPR (t
));
16373 tree decomp_first
= NULL_TREE
;
16374 unsigned decomp_cnt
= 0;
16375 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16376 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16378 &decomp_first
, &decomp_cnt
);
16380 if (processing_template_decl
)
16382 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16383 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16384 finish_range_for_decl (stmt
, decl
, expr
);
16388 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16389 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16390 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16391 decomp_first
, decomp_cnt
,
16392 RANGE_FOR_IVDEP (t
), unroll
);
16395 bool prev
= note_iteration_stmt_body_start ();
16396 RECUR (RANGE_FOR_BODY (t
));
16397 note_iteration_stmt_body_end (prev
);
16398 finish_for_stmt (stmt
);
16403 stmt
= begin_while_stmt ();
16404 tmp
= RECUR (WHILE_COND (t
));
16405 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16407 bool prev
= note_iteration_stmt_body_start ();
16408 RECUR (WHILE_BODY (t
));
16409 note_iteration_stmt_body_end (prev
);
16411 finish_while_stmt (stmt
);
16415 stmt
= begin_do_stmt ();
16417 bool prev
= note_iteration_stmt_body_start ();
16418 RECUR (DO_BODY (t
));
16419 note_iteration_stmt_body_end (prev
);
16421 finish_do_body (stmt
);
16422 tmp
= RECUR (DO_COND (t
));
16423 finish_do_stmt (tmp
, stmt
, false, 0);
16427 stmt
= begin_if_stmt ();
16428 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16429 tmp
= RECUR (IF_COND (t
));
16430 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16431 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16432 /* Don't instantiate the THEN_CLAUSE. */;
16435 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
16437 ++c_inhibit_evaluation_warnings
;
16438 RECUR (THEN_CLAUSE (t
));
16440 --c_inhibit_evaluation_warnings
;
16442 finish_then_clause (stmt
);
16444 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16445 /* Don't instantiate the ELSE_CLAUSE. */;
16446 else if (ELSE_CLAUSE (t
))
16448 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
16449 begin_else_clause (stmt
);
16451 ++c_inhibit_evaluation_warnings
;
16452 RECUR (ELSE_CLAUSE (t
));
16454 --c_inhibit_evaluation_warnings
;
16455 finish_else_clause (stmt
);
16458 finish_if_stmt (stmt
);
16462 if (BIND_EXPR_BODY_BLOCK (t
))
16463 stmt
= begin_function_body ();
16465 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16466 ? BCS_TRY_BLOCK
: 0);
16468 RECUR (BIND_EXPR_BODY (t
));
16470 if (BIND_EXPR_BODY_BLOCK (t
))
16471 finish_function_body (stmt
);
16473 finish_compound_stmt (stmt
);
16477 finish_break_stmt ();
16480 case CONTINUE_STMT
:
16481 finish_continue_stmt ();
16485 stmt
= begin_switch_stmt ();
16486 tmp
= RECUR (SWITCH_STMT_COND (t
));
16487 finish_switch_cond (tmp
, stmt
);
16488 RECUR (SWITCH_STMT_BODY (t
));
16489 finish_switch_stmt (stmt
);
16492 case CASE_LABEL_EXPR
:
16494 tree low
= RECUR (CASE_LOW (t
));
16495 tree high
= RECUR (CASE_HIGH (t
));
16496 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16497 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16498 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16499 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16505 tree decl
= LABEL_EXPR_LABEL (t
);
16508 label
= finish_label_stmt (DECL_NAME (decl
));
16509 if (TREE_CODE (label
) == LABEL_DECL
)
16510 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16511 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16512 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16517 tmp
= GOTO_DESTINATION (t
);
16518 if (TREE_CODE (tmp
) != LABEL_DECL
)
16519 /* Computed goto's must be tsubst'd into. On the other hand,
16520 non-computed gotos must not be; the identifier in question
16521 will have no binding. */
16524 tmp
= DECL_NAME (tmp
);
16525 finish_goto_stmt (tmp
);
16530 tree string
= RECUR (ASM_STRING (t
));
16531 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16532 complain
, in_decl
);
16533 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16534 complain
, in_decl
);
16535 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16536 complain
, in_decl
);
16537 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16538 complain
, in_decl
);
16539 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16541 tree asm_expr
= tmp
;
16542 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16543 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16544 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16551 stmt
= begin_try_block ();
16552 RECUR (TRY_STMTS (t
));
16553 finish_cleanup_try_block (stmt
);
16554 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16558 tree compound_stmt
= NULL_TREE
;
16560 if (FN_TRY_BLOCK_P (t
))
16561 stmt
= begin_function_try_block (&compound_stmt
);
16563 stmt
= begin_try_block ();
16565 RECUR (TRY_STMTS (t
));
16567 if (FN_TRY_BLOCK_P (t
))
16568 finish_function_try_block (stmt
);
16570 finish_try_block (stmt
);
16572 RECUR (TRY_HANDLERS (t
));
16573 if (FN_TRY_BLOCK_P (t
))
16574 finish_function_handler_sequence (stmt
, compound_stmt
);
16576 finish_handler_sequence (stmt
);
16582 tree decl
= HANDLER_PARMS (t
);
16586 decl
= tsubst (decl
, args
, complain
, in_decl
);
16587 /* Prevent instantiate_decl from trying to instantiate
16588 this variable. We've already done all that needs to be
16590 if (decl
!= error_mark_node
)
16591 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16593 stmt
= begin_handler ();
16594 finish_handler_parms (decl
, stmt
);
16595 RECUR (HANDLER_BODY (t
));
16596 finish_handler (stmt
);
16601 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16602 if (CLASS_TYPE_P (tmp
))
16604 /* Local classes are not independent templates; they are
16605 instantiated along with their containing function. And this
16606 way we don't have to deal with pushing out of one local class
16607 to instantiate a member of another local class. */
16608 /* Closures are handled by the LAMBDA_EXPR. */
16609 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16610 complete_type (tmp
);
16611 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
16613 || (TREE_CODE (fld
) == FUNCTION_DECL
16614 && !DECL_ARTIFICIAL (fld
)))
16615 && DECL_TEMPLATE_INSTANTIATION (fld
))
16616 instantiate_decl (fld
, /*defer_ok=*/false,
16617 /*expl_inst_class=*/false);
16621 case STATIC_ASSERT
:
16625 ++c_inhibit_evaluation_warnings
;
16627 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16630 /*integral_constant_expression_p=*/true);
16631 --c_inhibit_evaluation_warnings
;
16633 finish_static_assert (condition
,
16634 STATIC_ASSERT_MESSAGE (t
),
16635 STATIC_ASSERT_SOURCE_LOCATION (t
),
16636 /*member_p=*/false);
16641 case OACC_PARALLEL
:
16642 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16644 stmt
= begin_omp_parallel ();
16645 RECUR (OMP_BODY (t
));
16646 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16650 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16651 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16652 complain
, in_decl
);
16653 if (OMP_PARALLEL_COMBINED (t
))
16654 omp_parallel_combined_clauses
= &tmp
;
16655 stmt
= begin_omp_parallel ();
16656 RECUR (OMP_PARALLEL_BODY (t
));
16657 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16658 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16659 = OMP_PARALLEL_COMBINED (t
);
16660 pop_omp_privatization_clauses (r
);
16664 r
= push_omp_privatization_clauses (false);
16665 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16666 complain
, in_decl
);
16667 stmt
= begin_omp_task ();
16668 RECUR (OMP_TASK_BODY (t
));
16669 finish_omp_task (tmp
, stmt
);
16670 pop_omp_privatization_clauses (r
);
16675 case OMP_DISTRIBUTE
:
16679 tree clauses
, body
, pre_body
;
16680 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16681 tree orig_declv
= NULL_TREE
;
16682 tree incrv
= NULL_TREE
;
16683 enum c_omp_region_type ort
= C_ORT_OMP
;
16686 if (TREE_CODE (t
) == OACC_LOOP
)
16689 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16690 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16692 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16694 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16695 if (OMP_FOR_ORIG_DECLS (t
))
16696 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16697 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16698 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16699 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16702 stmt
= begin_omp_structured_block ();
16704 pre_body
= push_stmt_list ();
16705 RECUR (OMP_FOR_PRE_BODY (t
));
16706 pre_body
= pop_stmt_list (pre_body
);
16708 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16709 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16710 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16711 incrv
, &clauses
, args
, complain
, in_decl
,
16712 integral_constant_expression_p
);
16713 omp_parallel_combined_clauses
= NULL
;
16715 body
= push_stmt_list ();
16716 RECUR (OMP_FOR_BODY (t
));
16717 body
= pop_stmt_list (body
);
16719 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16720 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16721 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16725 t
= make_node (TREE_CODE (t
));
16726 TREE_TYPE (t
) = void_type_node
;
16727 OMP_FOR_BODY (t
) = body
;
16728 OMP_FOR_PRE_BODY (t
) = pre_body
;
16729 OMP_FOR_CLAUSES (t
) = clauses
;
16730 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16734 add_stmt (finish_omp_structured_block (stmt
));
16735 pop_omp_privatization_clauses (r
);
16740 omp_parallel_combined_clauses
= NULL
;
16745 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16746 && OMP_TEAMS_COMBINED (t
));
16747 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16749 stmt
= push_stmt_list ();
16750 RECUR (OMP_BODY (t
));
16751 stmt
= pop_stmt_list (stmt
);
16754 OMP_BODY (t
) = stmt
;
16755 OMP_CLAUSES (t
) = tmp
;
16757 pop_omp_privatization_clauses (r
);
16761 case OMP_TARGET_DATA
:
16763 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16764 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16766 keep_next_level (true);
16767 stmt
= begin_omp_structured_block ();
16769 RECUR (OMP_BODY (t
));
16770 stmt
= finish_omp_structured_block (stmt
);
16773 OMP_BODY (t
) = stmt
;
16774 OMP_CLAUSES (t
) = tmp
;
16775 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16777 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16780 /* For combined target teams, ensure the num_teams and
16781 thread_limit clause expressions are evaluated on the host,
16782 before entering the target construct. */
16784 for (c
= OMP_TEAMS_CLAUSES (teams
);
16785 c
; c
= OMP_CLAUSE_CHAIN (c
))
16786 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16787 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16788 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16790 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16791 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16792 if (expr
== error_mark_node
)
16794 tmp
= TARGET_EXPR_SLOT (expr
);
16796 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16797 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16798 OMP_CLAUSE_FIRSTPRIVATE
);
16799 OMP_CLAUSE_DECL (tc
) = tmp
;
16800 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16801 OMP_TARGET_CLAUSES (t
) = tc
;
16810 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16811 complain
, in_decl
);
16812 OACC_DECLARE_CLAUSES (t
) = tmp
;
16816 case OMP_TARGET_UPDATE
:
16817 case OMP_TARGET_ENTER_DATA
:
16818 case OMP_TARGET_EXIT_DATA
:
16819 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16820 complain
, in_decl
);
16822 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16826 case OACC_ENTER_DATA
:
16827 case OACC_EXIT_DATA
:
16829 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16830 complain
, in_decl
);
16832 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16837 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16838 complain
, in_decl
);
16839 stmt
= push_stmt_list ();
16840 RECUR (OMP_BODY (t
));
16841 stmt
= pop_stmt_list (stmt
);
16844 OMP_BODY (t
) = stmt
;
16845 OMP_ORDERED_CLAUSES (t
) = tmp
;
16851 case OMP_TASKGROUP
:
16852 stmt
= push_stmt_list ();
16853 RECUR (OMP_BODY (t
));
16854 stmt
= pop_stmt_list (stmt
);
16857 OMP_BODY (t
) = stmt
;
16862 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16863 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16865 tree op1
= TREE_OPERAND (t
, 1);
16866 tree rhs1
= NULL_TREE
;
16868 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16870 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16871 op1
= TREE_OPERAND (op1
, 1);
16873 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16874 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16875 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16876 NULL_TREE
, NULL_TREE
, rhs1
,
16877 OMP_ATOMIC_SEQ_CST (t
));
16881 tree op1
= TREE_OPERAND (t
, 1);
16882 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16883 tree rhs1
= NULL_TREE
;
16884 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16885 enum tree_code opcode
= NOP_EXPR
;
16886 if (code
== OMP_ATOMIC_READ
)
16888 v
= RECUR (TREE_OPERAND (op1
, 0));
16889 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16891 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16892 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16894 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16895 v
= RECUR (TREE_OPERAND (op1
, 0));
16896 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16897 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16899 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16900 op11
= TREE_OPERAND (op11
, 1);
16902 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16903 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16904 opcode
= TREE_CODE (op11
);
16905 if (opcode
== MODIFY_EXPR
)
16911 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16912 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16914 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16915 OMP_ATOMIC_SEQ_CST (t
));
16919 case TRANSACTION_EXPR
:
16922 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16923 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16925 if (TRANSACTION_EXPR_IS_STMT (t
))
16927 tree body
= TRANSACTION_EXPR_BODY (t
);
16928 tree noex
= NULL_TREE
;
16929 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16931 noex
= MUST_NOT_THROW_COND (body
);
16932 if (noex
== NULL_TREE
)
16933 noex
= boolean_true_node
;
16934 body
= TREE_OPERAND (body
, 0);
16936 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16938 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16942 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16943 RECUR (TRANSACTION_EXPR_BODY (t
)),
16950 case MUST_NOT_THROW_EXPR
:
16952 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16953 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16954 RETURN (build_must_not_throw_expr (op0
, cond
));
16957 case EXPR_PACK_EXPANSION
:
16958 error ("invalid use of pack expansion expression");
16959 RETURN (error_mark_node
);
16961 case NONTYPE_ARGUMENT_PACK
:
16962 error ("use %<...%> to expand argument pack");
16963 RETURN (error_mark_node
);
16965 case COMPOUND_EXPR
:
16966 tmp
= RECUR (TREE_OPERAND (t
, 0));
16967 if (tmp
== NULL_TREE
)
16968 /* If the first operand was a statement, we're done with it. */
16969 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16970 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16971 RECUR (TREE_OPERAND (t
, 1)),
16974 case ANNOTATE_EXPR
:
16975 tmp
= RECUR (TREE_OPERAND (t
, 0));
16976 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16977 TREE_TYPE (tmp
), tmp
,
16978 RECUR (TREE_OPERAND (t
, 1)),
16979 RECUR (TREE_OPERAND (t
, 2))));
16982 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16984 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16985 /*function_p=*/false,
16986 integral_constant_expression_p
));
16989 RETURN (NULL_TREE
);
16991 input_location
= loc
;
16997 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16998 function. For description of the body see comment above
16999 cp_parser_omp_declare_reduction_exprs. */
17002 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17004 if (t
== NULL_TREE
|| t
== error_mark_node
)
17007 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17009 tree_stmt_iterator tsi
;
17012 memset (stmts
, 0, sizeof stmts
);
17013 for (i
= 0, tsi
= tsi_start (t
);
17014 i
< 7 && !tsi_end_p (tsi
);
17015 i
++, tsi_next (&tsi
))
17016 stmts
[i
] = tsi_stmt (tsi
);
17017 gcc_assert (tsi_end_p (tsi
));
17021 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17022 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17023 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17024 args
, complain
, in_decl
);
17025 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17026 args
, complain
, in_decl
);
17027 DECL_CONTEXT (omp_out
) = current_function_decl
;
17028 DECL_CONTEXT (omp_in
) = current_function_decl
;
17029 keep_next_level (true);
17030 tree block
= begin_omp_structured_block ();
17031 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17032 block
= finish_omp_structured_block (block
);
17033 block
= maybe_cleanup_point_expr_void (block
);
17034 add_decl_expr (omp_out
);
17035 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17036 TREE_NO_WARNING (omp_out
) = 1;
17037 add_decl_expr (omp_in
);
17038 finish_expr_stmt (block
);
17042 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17043 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17044 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17045 args
, complain
, in_decl
);
17046 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17047 args
, complain
, in_decl
);
17048 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17049 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17050 keep_next_level (true);
17051 tree block
= begin_omp_structured_block ();
17052 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17053 block
= finish_omp_structured_block (block
);
17054 block
= maybe_cleanup_point_expr_void (block
);
17055 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17056 add_decl_expr (omp_priv
);
17057 add_decl_expr (omp_orig
);
17058 finish_expr_stmt (block
);
17060 add_decl_expr (omp_orig
);
17064 /* T is a postfix-expression that is not being used in a function
17065 call. Return the substituted version of T. */
17068 tsubst_non_call_postfix_expression (tree t
, tree args
,
17069 tsubst_flags_t complain
,
17072 if (TREE_CODE (t
) == SCOPE_REF
)
17073 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17074 /*done=*/false, /*address_p=*/false);
17076 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17077 /*function_p=*/false,
17078 /*integral_constant_expression_p=*/false);
17083 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17084 instantiation context. Instantiating a pack expansion containing a lambda
17085 might result in multiple lambdas all based on the same lambda in the
17089 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17091 tree oldfn
= lambda_function (t
);
17094 tree r
= build_lambda_expr ();
17096 LAMBDA_EXPR_LOCATION (r
)
17097 = LAMBDA_EXPR_LOCATION (t
);
17098 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17099 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17100 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17102 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17103 LAMBDA_EXPR_EXTRA_SCOPE (r
) = NULL_TREE
;
17105 record_lambda_scope (r
);
17107 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17108 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17110 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17111 cap
= TREE_CHAIN (cap
))
17113 tree field
= TREE_PURPOSE (cap
);
17114 if (PACK_EXPANSION_P (field
))
17115 field
= PACK_EXPANSION_PATTERN (field
);
17116 field
= tsubst_decl (field
, args
, complain
);
17118 if (field
== error_mark_node
)
17119 return error_mark_node
;
17121 tree init
= TREE_VALUE (cap
);
17122 if (PACK_EXPANSION_P (init
))
17123 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17125 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17126 /*fn*/false, /*constexpr*/false);
17128 if (TREE_CODE (field
) == TREE_VEC
)
17130 int len
= TREE_VEC_LENGTH (field
);
17131 gcc_assert (TREE_CODE (init
) == TREE_VEC
17132 && TREE_VEC_LENGTH (init
) == len
);
17133 for (int i
= 0; i
< len
; ++i
)
17134 LAMBDA_EXPR_CAPTURE_LIST (r
)
17135 = tree_cons (TREE_VEC_ELT (field
, i
),
17136 TREE_VEC_ELT (init
, i
),
17137 LAMBDA_EXPR_CAPTURE_LIST (r
));
17141 LAMBDA_EXPR_CAPTURE_LIST (r
)
17142 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17144 if (id_equal (DECL_NAME (field
), "__this"))
17145 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17149 tree type
= begin_lambda_type (r
);
17151 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17152 determine_visibility (TYPE_NAME (type
));
17154 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17156 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17157 ? DECL_TI_TEMPLATE (oldfn
)
17160 tree fntype
= static_fn_type (oldfn
);
17162 ++processing_template_decl
;
17163 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17165 --processing_template_decl
;
17167 if (fntype
== error_mark_node
)
17168 r
= error_mark_node
;
17171 /* Fix the type of 'this'. */
17172 fntype
= build_memfn_type (fntype
, type
,
17173 type_memfn_quals (fntype
),
17174 type_memfn_rqual (fntype
));
17178 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17179 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17180 finish_member_declaration (tmpl
);
17185 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17186 finish_member_declaration (fn
);
17189 /* Let finish_function set this. */
17190 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17192 bool nested
= cfun
;
17194 push_function_context ();
17196 /* Still increment function_depth so that we don't GC in the
17197 middle of an expression. */
17200 local_specialization_stack
s (lss_copy
);
17202 tree body
= start_lambda_function (fn
, r
);
17204 register_parameter_specializations (oldfn
, fn
);
17206 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17207 /*constexpr*/false);
17209 finish_lambda_function (body
);
17212 pop_function_context ();
17216 /* The capture list was built up in reverse order; fix that now. */
17217 LAMBDA_EXPR_CAPTURE_LIST (r
)
17218 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17220 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17222 maybe_add_lambda_conv_op (type
);
17225 finish_struct (type
, /*attr*/NULL_TREE
);
17227 insert_pending_capture_proxies ();
17232 /* Like tsubst but deals with expressions and performs semantic
17233 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17236 tsubst_copy_and_build (tree t
,
17238 tsubst_flags_t complain
,
17241 bool integral_constant_expression_p
)
17243 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17244 #define RECUR(NODE) \
17245 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17246 /*function_p=*/false, \
17247 integral_constant_expression_p)
17252 if (t
== NULL_TREE
|| t
== error_mark_node
)
17255 loc
= input_location
;
17256 if (EXPR_HAS_LOCATION (t
))
17257 input_location
= EXPR_LOCATION (t
);
17259 /* N3276 decltype magic only applies to calls at the top level or on the
17260 right side of a comma. */
17261 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17262 complain
&= ~tf_decltype
;
17264 switch (TREE_CODE (t
))
17268 /* Fall through. */
17269 case IDENTIFIER_NODE
:
17273 bool non_integral_constant_expression_p
;
17274 const char *error_msg
;
17276 if (IDENTIFIER_CONV_OP_P (t
))
17278 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17279 t
= make_conv_op_name (new_type
);
17282 /* Look up the name. */
17283 decl
= lookup_name (t
);
17285 /* By convention, expressions use ERROR_MARK_NODE to indicate
17286 failure, not NULL_TREE. */
17287 if (decl
== NULL_TREE
)
17288 decl
= error_mark_node
;
17290 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17292 integral_constant_expression_p
,
17293 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17294 &non_integral_constant_expression_p
,
17295 /*template_p=*/false,
17297 /*address_p=*/false,
17298 /*template_arg_p=*/false,
17303 if (!function_p
&& identifier_p (decl
))
17305 if (complain
& tf_error
)
17306 unqualified_name_lookup_error (decl
);
17307 decl
= error_mark_node
;
17312 case TEMPLATE_ID_EXPR
:
17315 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17316 tree targs
= TREE_OPERAND (t
, 1);
17319 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17320 if (targs
== error_mark_node
)
17321 return error_mark_node
;
17323 if (TREE_CODE (templ
) == SCOPE_REF
)
17325 tree name
= TREE_OPERAND (templ
, 1);
17326 tree tid
= lookup_template_function (name
, targs
);
17327 TREE_OPERAND (templ
, 1) = tid
;
17331 if (variable_template_p (templ
))
17332 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17334 if (TREE_CODE (templ
) == COMPONENT_REF
)
17336 object
= TREE_OPERAND (templ
, 0);
17337 templ
= TREE_OPERAND (templ
, 1);
17340 object
= NULL_TREE
;
17341 templ
= lookup_template_function (templ
, targs
);
17344 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17345 object
, templ
, NULL_TREE
));
17347 RETURN (baselink_for_fns (templ
));
17352 tree r
= RECUR (TREE_OPERAND (t
, 0));
17354 if (REFERENCE_REF_P (t
))
17356 /* A type conversion to reference type will be enclosed in
17357 such an indirect ref, but the substitution of the cast
17358 will have also added such an indirect ref. */
17359 r
= convert_from_reference (r
);
17362 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17363 complain
|decltype_flag
);
17365 if (REF_PARENTHESIZED_P (t
))
17366 r
= force_paren_expr (r
);
17373 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17374 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17375 RETURN (build_nop (type
, op0
));
17378 case IMPLICIT_CONV_EXPR
:
17380 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17381 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17382 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17384 retval
= copy_node (t
);
17385 TREE_TYPE (retval
) = type
;
17386 TREE_OPERAND (retval
, 0) = expr
;
17389 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17390 /* We'll pass this to convert_nontype_argument again, we don't need
17391 to actually perform any conversion here. */
17393 int flags
= LOOKUP_IMPLICIT
;
17394 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17395 flags
= LOOKUP_NORMAL
;
17396 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17402 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17403 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17404 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17408 case REINTERPRET_CAST_EXPR
:
17409 case CONST_CAST_EXPR
:
17410 case DYNAMIC_CAST_EXPR
:
17411 case STATIC_CAST_EXPR
:
17414 tree op
, r
= NULL_TREE
;
17416 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17417 if (integral_constant_expression_p
17418 && !cast_valid_in_integral_constant_expression_p (type
))
17420 if (complain
& tf_error
)
17421 error ("a cast to a type other than an integral or "
17422 "enumeration type cannot appear in a constant-expression");
17423 RETURN (error_mark_node
);
17426 op
= RECUR (TREE_OPERAND (t
, 0));
17428 warning_sentinel
s(warn_useless_cast
);
17429 warning_sentinel
s2(warn_ignored_qualifiers
);
17430 switch (TREE_CODE (t
))
17433 r
= build_functional_cast (type
, op
, complain
);
17435 case REINTERPRET_CAST_EXPR
:
17436 r
= build_reinterpret_cast (type
, op
, complain
);
17438 case CONST_CAST_EXPR
:
17439 r
= build_const_cast (type
, op
, complain
);
17441 case DYNAMIC_CAST_EXPR
:
17442 r
= build_dynamic_cast (type
, op
, complain
);
17444 case STATIC_CAST_EXPR
:
17445 r
= build_static_cast (type
, op
, complain
);
17448 gcc_unreachable ();
17454 case POSTDECREMENT_EXPR
:
17455 case POSTINCREMENT_EXPR
:
17456 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17457 args
, complain
, in_decl
);
17458 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17459 complain
|decltype_flag
));
17461 case PREDECREMENT_EXPR
:
17462 case PREINCREMENT_EXPR
:
17466 case TRUTH_NOT_EXPR
:
17467 case UNARY_PLUS_EXPR
: /* Unary + */
17468 case REALPART_EXPR
:
17469 case IMAGPART_EXPR
:
17470 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17471 RECUR (TREE_OPERAND (t
, 0)),
17472 complain
|decltype_flag
));
17474 case FIX_TRUNC_EXPR
:
17475 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
17479 op1
= TREE_OPERAND (t
, 0);
17480 if (TREE_CODE (op1
) == LABEL_DECL
)
17481 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17482 EXPR_LOCATION (op1
)));
17483 if (TREE_CODE (op1
) == SCOPE_REF
)
17484 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17485 /*done=*/true, /*address_p=*/true);
17487 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17489 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17490 complain
|decltype_flag
));
17495 case TRUNC_DIV_EXPR
:
17496 case CEIL_DIV_EXPR
:
17497 case FLOOR_DIV_EXPR
:
17498 case ROUND_DIV_EXPR
:
17499 case EXACT_DIV_EXPR
:
17503 case TRUNC_MOD_EXPR
:
17504 case FLOOR_MOD_EXPR
:
17505 case TRUTH_ANDIF_EXPR
:
17506 case TRUTH_ORIF_EXPR
:
17507 case TRUTH_AND_EXPR
:
17508 case TRUTH_OR_EXPR
:
17524 warning_sentinel
s1(warn_type_limits
);
17525 warning_sentinel
s2(warn_div_by_zero
);
17526 warning_sentinel
s3(warn_logical_op
);
17527 warning_sentinel
s4(warn_tautological_compare
);
17528 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17529 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17530 tree r
= build_x_binary_op
17531 (input_location
, TREE_CODE (t
),
17533 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
17535 : TREE_CODE (TREE_OPERAND (t
, 0))),
17537 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
17539 : TREE_CODE (TREE_OPERAND (t
, 1))),
17541 complain
|decltype_flag
);
17542 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
17543 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17548 case POINTER_PLUS_EXPR
:
17550 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17551 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17552 return fold_build_pointer_plus (op0
, op1
);
17556 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
17557 /*address_p=*/false));
17559 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17560 args
, complain
, in_decl
);
17561 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
17562 RECUR (TREE_OPERAND (t
, 1)),
17563 complain
|decltype_flag
));
17566 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
17567 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
17568 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17575 op1
= TREE_OPERAND (t
, 0);
17576 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
17577 op1
= TREE_TYPE (op1
);
17580 /* When there are no ARGS, we are trying to evaluate a
17581 non-dependent expression from the parser. Trying to do
17582 the substitutions may not work. */
17584 op1
= TREE_TYPE (op1
);
17588 ++cp_unevaluated_operand
;
17589 ++c_inhibit_evaluation_warnings
;
17591 op1
= tsubst (op1
, args
, complain
, in_decl
);
17593 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17594 /*function_p=*/false,
17595 /*integral_constant_expression_p=*/
17597 --cp_unevaluated_operand
;
17598 --c_inhibit_evaluation_warnings
;
17601 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
17602 complain
& tf_error
);
17604 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17605 complain
& tf_error
);
17606 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17608 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17610 if (!processing_template_decl
&& TYPE_P (op1
))
17612 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17613 build1 (NOP_EXPR
, op1
, error_mark_node
));
17614 SIZEOF_EXPR_TYPE_P (r
) = 1;
17617 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17618 TREE_SIDE_EFFECTS (r
) = 0;
17619 TREE_READONLY (r
) = 1;
17621 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17626 case AT_ENCODE_EXPR
:
17628 op1
= TREE_OPERAND (t
, 0);
17629 ++cp_unevaluated_operand
;
17630 ++c_inhibit_evaluation_warnings
;
17631 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17632 /*function_p=*/false,
17633 /*integral_constant_expression_p=*/false);
17634 --cp_unevaluated_operand
;
17635 --c_inhibit_evaluation_warnings
;
17636 RETURN (objc_build_encode_expr (op1
));
17639 case NOEXCEPT_EXPR
:
17640 op1
= TREE_OPERAND (t
, 0);
17641 ++cp_unevaluated_operand
;
17642 ++c_inhibit_evaluation_warnings
;
17643 ++cp_noexcept_operand
;
17644 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17645 /*function_p=*/false,
17646 /*integral_constant_expression_p=*/false);
17647 --cp_unevaluated_operand
;
17648 --c_inhibit_evaluation_warnings
;
17649 --cp_noexcept_operand
;
17650 RETURN (finish_noexcept_expr (op1
, complain
));
17654 warning_sentinel
s(warn_div_by_zero
);
17655 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17656 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17657 tree r
= build_x_modify_expr
17658 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17659 complain
|decltype_flag
);
17660 /* TREE_NO_WARNING must be set if either the expression was
17661 parenthesized or it uses an operator such as >>= rather
17662 than plain assignment. In the former case, it was already
17663 set and must be copied. In the latter case,
17664 build_x_modify_expr sets it and it must not be reset
17666 if (TREE_NO_WARNING (t
))
17667 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17673 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17674 args
, complain
, in_decl
);
17675 /* Remember that there was a reference to this entity. */
17677 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17678 RETURN (error_mark_node
);
17679 RETURN (build_x_arrow (input_location
, op1
, complain
));
17683 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17684 tree init
= RECUR (TREE_OPERAND (t
, 3));
17685 vec
<tree
, va_gc
> *placement_vec
;
17686 vec
<tree
, va_gc
> *init_vec
;
17689 if (placement
== NULL_TREE
)
17690 placement_vec
= NULL
;
17693 placement_vec
= make_tree_vector ();
17694 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17695 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17698 /* If there was an initializer in the original tree, but it
17699 instantiated to an empty list, then we should pass a
17700 non-NULL empty vector to tell build_new that it was an
17701 empty initializer() rather than no initializer. This can
17702 only happen when the initializer is a pack expansion whose
17703 parameter packs are of length zero. */
17704 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17708 init_vec
= make_tree_vector ();
17709 if (init
== void_node
)
17710 gcc_assert (init_vec
!= NULL
);
17713 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17714 vec_safe_push (init_vec
, TREE_VALUE (init
));
17718 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17719 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17720 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17721 NEW_EXPR_USE_GLOBAL (t
),
17724 if (placement_vec
!= NULL
)
17725 release_tree_vector (placement_vec
);
17726 if (init_vec
!= NULL
)
17727 release_tree_vector (init_vec
);
17734 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17735 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17736 RETURN (delete_sanity (op0
, op1
,
17737 DELETE_EXPR_USE_VEC (t
),
17738 DELETE_EXPR_USE_GLOBAL (t
),
17742 case COMPOUND_EXPR
:
17744 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17745 complain
& ~tf_decltype
, in_decl
,
17746 /*function_p=*/false,
17747 integral_constant_expression_p
);
17748 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17750 RECUR (TREE_OPERAND (t
, 1)),
17751 complain
|decltype_flag
));
17757 vec
<tree
, va_gc
> *call_args
;
17758 unsigned int nargs
, i
;
17763 function
= CALL_EXPR_FN (t
);
17764 /* Internal function with no arguments. */
17765 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17768 /* When we parsed the expression, we determined whether or
17769 not Koenig lookup should be performed. */
17770 koenig_p
= KOENIG_LOOKUP_P (t
);
17771 if (function
== NULL_TREE
)
17774 qualified_p
= false;
17776 else if (TREE_CODE (function
) == SCOPE_REF
)
17778 qualified_p
= true;
17779 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17781 /*address_p=*/false);
17783 else if (koenig_p
&& identifier_p (function
))
17785 /* Do nothing; calling tsubst_copy_and_build on an identifier
17786 would incorrectly perform unqualified lookup again.
17788 Note that we can also have an IDENTIFIER_NODE if the earlier
17789 unqualified lookup found a member function; in that case
17790 koenig_p will be false and we do want to do the lookup
17791 again to find the instantiated member function.
17793 FIXME but doing that causes c++/15272, so we need to stop
17794 using IDENTIFIER_NODE in that situation. */
17795 qualified_p
= false;
17799 if (TREE_CODE (function
) == COMPONENT_REF
)
17801 tree op
= TREE_OPERAND (function
, 1);
17803 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17804 || (BASELINK_P (op
)
17805 && BASELINK_QUALIFIED_P (op
)));
17808 qualified_p
= false;
17810 if (TREE_CODE (function
) == ADDR_EXPR
17811 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17812 /* Avoid error about taking the address of a constructor. */
17813 function
= TREE_OPERAND (function
, 0);
17815 function
= tsubst_copy_and_build (function
, args
, complain
,
17818 integral_constant_expression_p
);
17820 if (BASELINK_P (function
))
17821 qualified_p
= true;
17824 nargs
= call_expr_nargs (t
);
17825 call_args
= make_tree_vector ();
17826 for (i
= 0; i
< nargs
; ++i
)
17828 tree arg
= CALL_EXPR_ARG (t
, i
);
17830 if (!PACK_EXPANSION_P (arg
))
17831 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17834 /* Expand the pack expansion and push each entry onto
17836 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17837 if (TREE_CODE (arg
) == TREE_VEC
)
17839 unsigned int len
, j
;
17841 len
= TREE_VEC_LENGTH (arg
);
17842 for (j
= 0; j
< len
; ++j
)
17844 tree value
= TREE_VEC_ELT (arg
, j
);
17845 if (value
!= NULL_TREE
)
17846 value
= convert_from_reference (value
);
17847 vec_safe_push (call_args
, value
);
17852 /* A partial substitution. Add one entry. */
17853 vec_safe_push (call_args
, arg
);
17858 /* We do not perform argument-dependent lookup if normal
17859 lookup finds a non-function, in accordance with the
17860 expected resolution of DR 218. */
17862 && ((is_overloaded_fn (function
)
17863 /* If lookup found a member function, the Koenig lookup is
17864 not appropriate, even if an unqualified-name was used
17865 to denote the function. */
17866 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17867 || identifier_p (function
))
17868 /* Only do this when substitution turns a dependent call
17869 into a non-dependent call. */
17870 && type_dependent_expression_p_push (t
)
17871 && !any_type_dependent_arguments_p (call_args
))
17872 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17874 if (function
!= NULL_TREE
17875 && identifier_p (function
)
17876 && !any_type_dependent_arguments_p (call_args
))
17878 if (koenig_p
&& (complain
& tf_warning_or_error
))
17880 /* For backwards compatibility and good diagnostics, try
17881 the unqualified lookup again if we aren't in SFINAE
17883 tree unq
= (tsubst_copy_and_build
17884 (function
, args
, complain
, in_decl
, true,
17885 integral_constant_expression_p
));
17886 if (unq
== error_mark_node
)
17888 release_tree_vector (call_args
);
17889 RETURN (error_mark_node
);
17892 if (unq
!= function
)
17894 /* In a lambda fn, we have to be careful to not
17895 introduce new this captures. Legacy code can't
17896 be using lambdas anyway, so it's ok to be
17898 bool in_lambda
= (current_class_type
17899 && LAMBDA_TYPE_P (current_class_type
));
17900 char const *const msg
17901 = G_("%qD was not declared in this scope, "
17902 "and no declarations were found by "
17903 "argument-dependent lookup at the point "
17904 "of instantiation");
17908 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17911 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17917 if (INDIRECT_REF_P (fn
))
17918 fn
= TREE_OPERAND (fn
, 0);
17919 if (is_overloaded_fn (fn
))
17920 fn
= get_first_fn (fn
);
17923 /* Can't say anything more. */;
17924 else if (DECL_CLASS_SCOPE_P (fn
))
17926 location_t loc
= EXPR_LOC_OR_LOC (t
,
17929 "declarations in dependent base %qT are "
17930 "not found by unqualified lookup",
17931 DECL_CLASS_CONTEXT (fn
));
17932 if (current_class_ptr
)
17934 "use %<this->%D%> instead", function
);
17937 "use %<%T::%D%> instead",
17938 current_class_name
, function
);
17941 inform (DECL_SOURCE_LOCATION (fn
),
17942 "%qD declared here, later in the "
17943 "translation unit", fn
);
17946 release_tree_vector (call_args
);
17947 RETURN (error_mark_node
);
17954 if (identifier_p (function
))
17956 if (complain
& tf_error
)
17957 unqualified_name_lookup_error (function
);
17958 release_tree_vector (call_args
);
17959 RETURN (error_mark_node
);
17963 /* Remember that there was a reference to this entity. */
17964 if (function
!= NULL_TREE
17965 && DECL_P (function
)
17966 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17968 release_tree_vector (call_args
);
17969 RETURN (error_mark_node
);
17972 /* Put back tf_decltype for the actual call. */
17973 complain
|= decltype_flag
;
17975 if (function
== NULL_TREE
)
17976 switch (CALL_EXPR_IFN (t
))
17979 gcc_assert (nargs
== 1);
17980 if (vec_safe_length (call_args
) != 1)
17982 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17983 "wrong number of arguments to "
17984 "%<__builtin_launder%>");
17985 ret
= error_mark_node
;
17988 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17990 (*call_args
)[0], complain
);
17994 /* Unsupported internal function with arguments. */
17995 gcc_unreachable ();
17997 else if (TREE_CODE (function
) == OFFSET_REF
)
17998 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
18000 else if (TREE_CODE (function
) == COMPONENT_REF
)
18002 tree instance
= TREE_OPERAND (function
, 0);
18003 tree fn
= TREE_OPERAND (function
, 1);
18005 if (processing_template_decl
18006 && (type_dependent_expression_p (instance
)
18007 || (!BASELINK_P (fn
)
18008 && TREE_CODE (fn
) != FIELD_DECL
)
18009 || type_dependent_expression_p (fn
)
18010 || any_type_dependent_arguments_p (call_args
)))
18011 ret
= build_min_nt_call_vec (function
, call_args
);
18012 else if (!BASELINK_P (fn
))
18013 ret
= finish_call_expr (function
, &call_args
,
18014 /*disallow_virtual=*/false,
18015 /*koenig_p=*/false,
18018 ret
= (build_new_method_call
18020 &call_args
, NULL_TREE
,
18021 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18026 ret
= finish_call_expr (function
, &call_args
,
18027 /*disallow_virtual=*/qualified_p
,
18031 release_tree_vector (call_args
);
18033 if (ret
!= error_mark_node
)
18035 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18036 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18037 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18038 bool thk
= CALL_FROM_THUNK_P (t
);
18039 if (op
|| ord
|| rev
|| thk
)
18041 function
= extract_call_expr (ret
);
18042 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18043 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18044 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18047 if (TREE_CODE (function
) == CALL_EXPR
)
18048 CALL_FROM_THUNK_P (function
) = true;
18050 AGGR_INIT_FROM_THUNK_P (function
) = true;
18051 /* The thunk location is not interesting. */
18052 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
18062 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18063 tree folded_cond
= fold_non_dependent_expr (cond
);
18066 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
18068 if (integer_zerop (folded_cond
))
18070 ++c_inhibit_evaluation_warnings
;
18071 exp1
= RECUR (TREE_OPERAND (t
, 1));
18072 --c_inhibit_evaluation_warnings
;
18073 exp2
= RECUR (TREE_OPERAND (t
, 2));
18077 exp1
= RECUR (TREE_OPERAND (t
, 1));
18078 ++c_inhibit_evaluation_warnings
;
18079 exp2
= RECUR (TREE_OPERAND (t
, 2));
18080 --c_inhibit_evaluation_warnings
;
18082 cond
= folded_cond
;
18086 exp1
= RECUR (TREE_OPERAND (t
, 1));
18087 exp2
= RECUR (TREE_OPERAND (t
, 2));
18090 warning_sentinel
s(warn_duplicated_branches
);
18091 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
18092 cond
, exp1
, exp2
, complain
));
18095 case PSEUDO_DTOR_EXPR
:
18097 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18098 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18099 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
18100 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
18106 tree purpose
, value
, chain
;
18108 if (t
== void_list_node
)
18111 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
18112 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
18114 /* We have pack expansions, so expand those and
18115 create a new list out of it. */
18116 tree purposevec
= NULL_TREE
;
18117 tree valuevec
= NULL_TREE
;
18121 /* Expand the argument expressions. */
18122 if (TREE_PURPOSE (t
))
18123 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18124 complain
, in_decl
);
18125 if (TREE_VALUE (t
))
18126 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18127 complain
, in_decl
);
18129 /* Build the rest of the list. */
18130 chain
= TREE_CHAIN (t
);
18131 if (chain
&& chain
!= void_type_node
)
18132 chain
= RECUR (chain
);
18134 /* Determine the number of arguments. */
18135 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18137 len
= TREE_VEC_LENGTH (purposevec
);
18138 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18140 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18141 len
= TREE_VEC_LENGTH (valuevec
);
18144 /* Since we only performed a partial substitution into
18145 the argument pack, we only RETURN (a single list
18147 if (purposevec
== TREE_PURPOSE (t
)
18148 && valuevec
== TREE_VALUE (t
)
18149 && chain
== TREE_CHAIN (t
))
18152 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18155 /* Convert the argument vectors into a TREE_LIST */
18159 /* Grab the Ith values. */
18161 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18164 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18167 /* Build the list (backwards). */
18168 chain
= tree_cons (purpose
, value
, chain
);
18174 purpose
= TREE_PURPOSE (t
);
18176 purpose
= RECUR (purpose
);
18177 value
= TREE_VALUE (t
);
18179 value
= RECUR (value
);
18180 chain
= TREE_CHAIN (t
);
18181 if (chain
&& chain
!= void_type_node
)
18182 chain
= RECUR (chain
);
18183 if (purpose
== TREE_PURPOSE (t
)
18184 && value
== TREE_VALUE (t
)
18185 && chain
== TREE_CHAIN (t
))
18187 RETURN (tree_cons (purpose
, value
, chain
));
18190 case COMPONENT_REF
:
18197 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18198 args
, complain
, in_decl
);
18199 /* Remember that there was a reference to this entity. */
18200 if (DECL_P (object
)
18201 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18202 RETURN (error_mark_node
);
18203 object_type
= TREE_TYPE (object
);
18205 member
= TREE_OPERAND (t
, 1);
18206 if (BASELINK_P (member
))
18207 member
= tsubst_baselink (member
,
18208 non_reference (TREE_TYPE (object
)),
18209 args
, complain
, in_decl
);
18211 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18212 if (member
== error_mark_node
)
18213 RETURN (error_mark_node
);
18215 if (TREE_CODE (member
) == FIELD_DECL
)
18217 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18218 if (TREE_CODE (r
) == COMPONENT_REF
)
18219 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18222 else if (type_dependent_expression_p (object
))
18223 /* We can't do much here. */;
18224 else if (!CLASS_TYPE_P (object_type
))
18226 if (scalarish_type_p (object_type
))
18228 tree s
= NULL_TREE
;
18229 tree dtor
= member
;
18231 if (TREE_CODE (dtor
) == SCOPE_REF
)
18233 s
= TREE_OPERAND (dtor
, 0);
18234 dtor
= TREE_OPERAND (dtor
, 1);
18236 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18238 dtor
= TREE_OPERAND (dtor
, 0);
18240 RETURN (finish_pseudo_destructor_expr
18241 (object
, s
, dtor
, input_location
));
18245 else if (TREE_CODE (member
) == SCOPE_REF
18246 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18248 /* Lookup the template functions now that we know what the
18250 tree scope
= TREE_OPERAND (member
, 0);
18251 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18252 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18253 member
= lookup_qualified_name (scope
, tmpl
,
18254 /*is_type_p=*/false,
18255 /*complain=*/false);
18256 if (BASELINK_P (member
))
18258 BASELINK_FUNCTIONS (member
)
18259 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18261 member
= (adjust_result_of_qualified_name_lookup
18262 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18267 qualified_name_lookup_error (scope
, tmpl
, member
,
18269 RETURN (error_mark_node
);
18272 else if (TREE_CODE (member
) == SCOPE_REF
18273 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18274 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18276 if (complain
& tf_error
)
18278 if (TYPE_P (TREE_OPERAND (member
, 0)))
18279 error ("%qT is not a class or namespace",
18280 TREE_OPERAND (member
, 0));
18282 error ("%qD is not a class or namespace",
18283 TREE_OPERAND (member
, 0));
18285 RETURN (error_mark_node
);
18288 r
= finish_class_member_access_expr (object
, member
,
18289 /*template_p=*/false,
18291 if (TREE_CODE (r
) == COMPONENT_REF
)
18292 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18297 RETURN (build_throw
18298 (RECUR (TREE_OPERAND (t
, 0))));
18302 vec
<constructor_elt
, va_gc
> *n
;
18303 constructor_elt
*ce
;
18304 unsigned HOST_WIDE_INT idx
;
18305 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18306 bool process_index_p
;
18308 bool need_copy_p
= false;
18311 if (type
== error_mark_node
)
18312 RETURN (error_mark_node
);
18314 /* We do not want to process the index of aggregate
18315 initializers as they are identifier nodes which will be
18316 looked up by digest_init. */
18317 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18319 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18320 newlen
= vec_safe_length (n
);
18321 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18323 if (ce
->index
&& process_index_p
18324 /* An identifier index is looked up in the type
18325 being initialized, not the current scope. */
18326 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18327 ce
->index
= RECUR (ce
->index
);
18329 if (PACK_EXPANSION_P (ce
->value
))
18331 /* Substitute into the pack expansion. */
18332 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18335 if (ce
->value
== error_mark_node
18336 || PACK_EXPANSION_P (ce
->value
))
18338 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18339 /* Just move the argument into place. */
18340 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18343 /* Update the length of the final CONSTRUCTOR
18344 arguments vector, and note that we will need to
18346 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18347 need_copy_p
= true;
18351 ce
->value
= RECUR (ce
->value
);
18356 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18358 vec_alloc (n
, newlen
);
18359 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18361 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18363 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18364 for (i
= 0; i
< len
; ++i
)
18365 CONSTRUCTOR_APPEND_ELT (n
, 0,
18366 TREE_VEC_ELT (ce
->value
, i
));
18369 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18373 r
= build_constructor (init_list_type_node
, n
);
18374 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18376 if (TREE_HAS_CONSTRUCTOR (t
))
18378 fcl_t cl
= fcl_functional
;
18379 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18381 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18384 TREE_TYPE (r
) = type
;
18390 tree operand_0
= TREE_OPERAND (t
, 0);
18391 if (TYPE_P (operand_0
))
18393 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18394 RETURN (get_typeid (operand_0
, complain
));
18398 operand_0
= RECUR (operand_0
);
18399 RETURN (build_typeid (operand_0
, complain
));
18410 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18411 /* ??? We're doing a subset of finish_id_expression here. */
18413 && !processing_template_decl
18414 && !cp_unevaluated_operand
18415 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18416 && CP_DECL_THREAD_LOCAL_P (r
))
18418 if (tree wrap
= get_tls_wrapper_fn (r
))
18419 /* Replace an evaluated use of the thread_local variable with
18420 a call to its wrapper. */
18421 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18423 else if (outer_automatic_var_p (r
))
18424 r
= process_outer_var_ref (r
, complain
);
18426 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
18427 /* If the original type was a reference, we'll be wrapped in
18428 the appropriate INDIRECT_REF. */
18429 r
= convert_from_reference (r
);
18435 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18436 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18437 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18440 case OFFSETOF_EXPR
:
18443 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18444 in_decl
, /*function_p=*/false,
18445 /*integral_constant_expression_p=*/false);
18446 RETURN (finish_offsetof (object_ptr
,
18447 RECUR (TREE_OPERAND (t
, 0)),
18448 EXPR_LOCATION (t
)));
18451 case ADDRESSOF_EXPR
:
18452 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18453 RECUR (TREE_OPERAND (t
, 0)), complain
));
18457 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18458 complain
, in_decl
);
18460 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18461 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18462 type2
= RECUR (type2
);
18464 type2
= tsubst (type2
, args
, complain
, in_decl
);
18466 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18471 tree old_stmt_expr
= cur_stmt_expr
;
18472 tree stmt_expr
= begin_stmt_expr ();
18474 cur_stmt_expr
= stmt_expr
;
18475 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18476 integral_constant_expression_p
);
18477 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18478 cur_stmt_expr
= old_stmt_expr
;
18480 /* If the resulting list of expression statement is empty,
18481 fold it further into void_node. */
18482 if (empty_expr_stmt_p (stmt_expr
))
18483 stmt_expr
= void_node
;
18485 RETURN (stmt_expr
);
18490 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18492 RETURN (build_lambda_object (r
));
18496 /* We can get here for a constant initializer of non-dependent type.
18497 FIXME stop folding in cp_parser_initializer_clause. */
18499 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18504 case TRANSACTION_EXPR
:
18505 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18506 integral_constant_expression_p
));
18509 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18511 case VEC_PERM_EXPR
:
18513 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18514 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18515 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18516 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
18520 case REQUIRES_EXPR
:
18521 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
18523 case NON_LVALUE_EXPR
:
18524 case VIEW_CONVERT_EXPR
:
18525 /* We should only see these for location wrapper nodes, or within
18526 instantiate_non_dependent_expr (when args is NULL_TREE). */
18527 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
18528 if (location_wrapper_p (t
))
18529 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
18530 EXPR_LOCATION (t
)));
18534 /* Handle Objective-C++ constructs, if appropriate. */
18537 = objcp_tsubst_copy_and_build (t
, args
, complain
,
18538 in_decl
, /*function_p=*/false);
18542 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18548 input_location
= loc
;
18552 /* Verify that the instantiated ARGS are valid. For type arguments,
18553 make sure that the type's linkage is ok. For non-type arguments,
18554 make sure they are constants if they are integral or enumerations.
18555 Emit an error under control of COMPLAIN, and return TRUE on error. */
18558 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18560 if (dependent_template_arg_p (t
))
18562 if (ARGUMENT_PACK_P (t
))
18564 tree vec
= ARGUMENT_PACK_ARGS (t
);
18565 int len
= TREE_VEC_LENGTH (vec
);
18566 bool result
= false;
18569 for (i
= 0; i
< len
; ++i
)
18570 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18574 else if (TYPE_P (t
))
18576 /* [basic.link]: A name with no linkage (notably, the name
18577 of a class or enumeration declared in a local scope)
18578 shall not be used to declare an entity with linkage.
18579 This implies that names with no linkage cannot be used as
18582 DR 757 relaxes this restriction for C++0x. */
18583 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18584 : no_linkage_check (t
, /*relaxed_p=*/false));
18588 /* DR 488 makes use of a type with no linkage cause
18589 type deduction to fail. */
18590 if (complain
& tf_error
)
18592 if (TYPE_UNNAMED_P (nt
))
18593 error ("%qT is/uses unnamed type", t
);
18595 error ("template argument for %qD uses local type %qT",
18600 /* In order to avoid all sorts of complications, we do not
18601 allow variably-modified types as template arguments. */
18602 else if (variably_modified_type_p (t
, NULL_TREE
))
18604 if (complain
& tf_error
)
18605 error ("%qT is a variably modified type", t
);
18609 /* Class template and alias template arguments should be OK. */
18610 else if (DECL_TYPE_TEMPLATE_P (t
))
18612 /* A non-type argument of integral or enumerated type must be a
18614 else if (TREE_TYPE (t
)
18615 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18616 && !REFERENCE_REF_P (t
)
18617 && !TREE_CONSTANT (t
))
18619 if (complain
& tf_error
)
18620 error ("integral expression %qE is not constant", t
);
18627 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18629 int ix
, len
= DECL_NTPARMS (tmpl
);
18630 bool result
= false;
18632 for (ix
= 0; ix
!= len
; ix
++)
18634 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18637 if (result
&& (complain
& tf_error
))
18638 error (" trying to instantiate %qD", tmpl
);
18642 /* We're out of SFINAE context now, so generate diagnostics for the access
18643 errors we saw earlier when instantiating D from TMPL and ARGS. */
18646 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18648 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18649 tree type
= TREE_TYPE (pattern
);
18650 location_t loc
= input_location
;
18652 push_access_scope (d
);
18653 push_deferring_access_checks (dk_no_deferred
);
18654 input_location
= DECL_SOURCE_LOCATION (pattern
);
18655 tsubst (type
, args
, tf_warning_or_error
, d
);
18656 input_location
= loc
;
18657 pop_deferring_access_checks ();
18658 pop_access_scope (d
);
18661 /* Instantiate the indicated variable, function, or alias template TMPL with
18662 the template arguments in TARG_PTR. */
18665 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18667 tree targ_ptr
= orig_args
;
18671 bool access_ok
= true;
18673 if (tmpl
== error_mark_node
)
18674 return error_mark_node
;
18676 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18678 /* If this function is a clone, handle it specially. */
18679 if (DECL_CLONED_FUNCTION_P (tmpl
))
18684 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18685 DECL_CLONED_FUNCTION. */
18686 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18687 targ_ptr
, complain
);
18688 if (spec
== error_mark_node
)
18689 return error_mark_node
;
18691 /* Look for the clone. */
18692 FOR_EACH_CLONE (clone
, spec
)
18693 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18695 /* We should always have found the clone by now. */
18696 gcc_unreachable ();
18700 if (targ_ptr
== error_mark_node
)
18701 return error_mark_node
;
18703 /* Check to see if we already have this specialization. */
18704 gen_tmpl
= most_general_template (tmpl
);
18705 if (TMPL_ARGS_DEPTH (targ_ptr
)
18706 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18707 /* targ_ptr only has the innermost template args, so add the outer ones
18708 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18709 the case of a non-dependent call within a template definition). */
18710 targ_ptr
= (add_outermost_template_args
18711 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18714 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18715 but it doesn't seem to be on the hot path. */
18716 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18718 gcc_assert (tmpl
== gen_tmpl
18719 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18721 || fndecl
== NULL_TREE
);
18723 if (spec
!= NULL_TREE
)
18725 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18727 if (complain
& tf_error
)
18728 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18729 return error_mark_node
;
18734 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18736 return error_mark_node
;
18738 /* We are building a FUNCTION_DECL, during which the access of its
18739 parameters and return types have to be checked. However this
18740 FUNCTION_DECL which is the desired context for access checking
18741 is not built yet. We solve this chicken-and-egg problem by
18742 deferring all checks until we have the FUNCTION_DECL. */
18743 push_deferring_access_checks (dk_deferred
);
18745 /* Instantiation of the function happens in the context of the function
18746 template, not the context of the overload resolution we're doing. */
18747 push_to_top_level ();
18748 /* If there are dependent arguments, e.g. because we're doing partial
18749 ordering, make sure processing_template_decl stays set. */
18750 if (uses_template_parms (targ_ptr
))
18751 ++processing_template_decl
;
18752 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18754 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18755 complain
, gen_tmpl
, true);
18756 push_nested_class (ctx
);
18759 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18761 fndecl
= NULL_TREE
;
18762 if (VAR_P (pattern
))
18764 /* We need to determine if we're using a partial or explicit
18765 specialization now, because the type of the variable could be
18767 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18768 tree elt
= most_specialized_partial_spec (tid
, complain
);
18769 if (elt
== error_mark_node
)
18770 pattern
= error_mark_node
;
18773 tree partial_tmpl
= TREE_VALUE (elt
);
18774 tree partial_args
= TREE_PURPOSE (elt
);
18775 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18776 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18780 /* Substitute template parameters to obtain the specialization. */
18781 if (fndecl
== NULL_TREE
)
18782 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18783 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18784 pop_nested_class ();
18785 pop_from_top_level ();
18787 if (fndecl
== error_mark_node
)
18789 pop_deferring_access_checks ();
18790 return error_mark_node
;
18793 /* The DECL_TI_TEMPLATE should always be the immediate parent
18794 template, not the most general template. */
18795 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18796 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18798 /* Now we know the specialization, compute access previously
18799 deferred. Do no access control for inheriting constructors,
18800 as we already checked access for the inherited constructor. */
18801 if (!(flag_new_inheriting_ctors
18802 && DECL_INHERITED_CTOR (fndecl
)))
18804 push_access_scope (fndecl
);
18805 if (!perform_deferred_access_checks (complain
))
18807 pop_access_scope (fndecl
);
18809 pop_deferring_access_checks ();
18811 /* If we've just instantiated the main entry point for a function,
18812 instantiate all the alternate entry points as well. We do this
18813 by cloning the instantiation of the main entry point, not by
18814 instantiating the template clones. */
18815 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18816 clone_function_decl (fndecl
, /*update_methods=*/false);
18820 if (!(complain
& tf_error
))
18822 /* Remember to reinstantiate when we're out of SFINAE so the user
18823 can see the errors. */
18824 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18826 return error_mark_node
;
18831 /* Wrapper for instantiate_template_1. */
18834 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18837 timevar_push (TV_TEMPLATE_INST
);
18838 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18839 timevar_pop (TV_TEMPLATE_INST
);
18843 /* Instantiate the alias template TMPL with ARGS. Also push a template
18844 instantiation level, which instantiate_template doesn't do because
18845 functions and variables have sufficient context established by the
18849 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18851 struct pending_template
*old_last_pend
= last_pending_template
;
18852 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18853 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18854 return error_mark_node
;
18855 tree tinst
= build_tree_list (tmpl
, args
);
18856 if (!push_tinst_level (tinst
))
18859 return error_mark_node
;
18863 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18864 args
, tmpl
, complain
,
18865 /*require_all_args=*/true,
18866 /*use_default_args=*/true);
18868 tree r
= instantiate_template (tmpl
, args
, complain
);
18869 pop_tinst_level ();
18870 /* We can't free this if a pending_template entry or last_error_tinst_level
18871 is pointing at it. */
18872 if (last_pending_template
== old_last_pend
18873 && last_error_tinst_level
== old_error_tinst
)
18879 /* PARM is a template parameter pack for FN. Returns true iff
18880 PARM is used in a deducible way in the argument list of FN. */
18883 pack_deducible_p (tree parm
, tree fn
)
18885 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18886 for (; t
; t
= TREE_CHAIN (t
))
18888 tree type
= TREE_VALUE (t
);
18890 if (!PACK_EXPANSION_P (type
))
18892 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18893 packs
; packs
= TREE_CHAIN (packs
))
18894 if (template_args_equal (TREE_VALUE (packs
), parm
))
18896 /* The template parameter pack is used in a function parameter
18897 pack. If this is the end of the parameter list, the
18898 template parameter pack is deducible. */
18899 if (TREE_CHAIN (t
) == void_list_node
)
18902 /* Otherwise, not. Well, it could be deduced from
18903 a non-pack parameter, but doing so would end up with
18904 a deduction mismatch, so don't bother. */
18908 /* The template parameter pack isn't used in any function parameter
18909 packs, but it might be used deeper, e.g. tuple<Args...>. */
18913 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18914 NARGS elements of the arguments that are being used when calling
18915 it. TARGS is a vector into which the deduced template arguments
18918 Returns either a FUNCTION_DECL for the matching specialization of FN or
18919 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18920 true, diagnostics will be printed to explain why it failed.
18922 If FN is a conversion operator, or we are trying to produce a specific
18923 specialization, RETURN_TYPE is the return type desired.
18925 The EXPLICIT_TARGS are explicit template arguments provided via a
18928 The parameter STRICT is one of:
18931 We are deducing arguments for a function call, as in
18932 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18933 deducing arguments for a call to the result of a conversion
18934 function template, as in [over.call.object].
18937 We are deducing arguments for a conversion function, as in
18938 [temp.deduct.conv].
18941 We are deducing arguments when doing an explicit instantiation
18942 as in [temp.explicit], when determining an explicit specialization
18943 as in [temp.expl.spec], or when taking the address of a function
18944 template, as in [temp.deduct.funcaddr]. */
18947 fn_type_unification (tree fn
,
18948 tree explicit_targs
,
18951 unsigned int nargs
,
18953 unification_kind_t strict
,
18960 tree decl
= NULL_TREE
;
18961 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18963 static int deduction_depth
;
18964 struct pending_template
*old_last_pend
= last_pending_template
;
18965 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18968 if (flag_new_inheriting_ctors
)
18969 fn
= strip_inheriting_ctors (fn
);
18971 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18973 tree r
= error_mark_node
;
18975 tree full_targs
= targs
;
18976 if (TMPL_ARGS_DEPTH (targs
)
18977 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18978 full_targs
= (add_outermost_template_args
18979 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18983 complain
|= tf_decltype
;
18985 /* In C++0x, it's possible to have a function template whose type depends
18986 on itself recursively. This is most obvious with decltype, but can also
18987 occur with enumeration scope (c++/48969). So we need to catch infinite
18988 recursion and reject the substitution at deduction time; this function
18989 will return error_mark_node for any repeated substitution.
18991 This also catches excessive recursion such as when f<N> depends on
18992 f<N-1> across all integers, and returns error_mark_node for all the
18993 substitutions back up to the initial one.
18995 This is, of course, not reentrant. */
18996 if (excessive_deduction_depth
)
18997 return error_mark_node
;
18998 tinst
= build_tree_list (fn
, NULL_TREE
);
19001 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
19003 fntype
= TREE_TYPE (fn
);
19004 if (explicit_targs
)
19008 The specified template arguments must match the template
19009 parameters in kind (i.e., type, nontype, template), and there
19010 must not be more arguments than there are parameters;
19011 otherwise type deduction fails.
19013 Nontype arguments must match the types of the corresponding
19014 nontype template parameters, or must be convertible to the
19015 types of the corresponding nontype parameters as specified in
19016 _temp.arg.nontype_, otherwise type deduction fails.
19018 All references in the function type of the function template
19019 to the corresponding template parameters are replaced by the
19020 specified template argument values. If a substitution in a
19021 template parameter or in the function type of the function
19022 template results in an invalid type, type deduction fails. */
19023 int i
, len
= TREE_VEC_LENGTH (tparms
);
19024 location_t loc
= input_location
;
19025 bool incomplete
= false;
19027 if (explicit_targs
== error_mark_node
)
19030 if (TMPL_ARGS_DEPTH (explicit_targs
)
19031 < TMPL_ARGS_DEPTH (full_targs
))
19032 explicit_targs
= add_outermost_template_args (full_targs
,
19035 /* Adjust any explicit template arguments before entering the
19036 substitution context. */
19038 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
19040 /*require_all_args=*/false,
19041 /*use_default_args=*/false));
19042 if (explicit_targs
== error_mark_node
)
19045 /* Substitute the explicit args into the function type. This is
19046 necessary so that, for instance, explicitly declared function
19047 arguments can match null pointed constants. If we were given
19048 an incomplete set of explicit args, we must not do semantic
19049 processing during substitution as we could create partial
19051 for (i
= 0; i
< len
; i
++)
19053 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
19054 bool parameter_pack
= false;
19055 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
19057 /* Dig out the actual parm. */
19058 if (TREE_CODE (parm
) == TYPE_DECL
19059 || TREE_CODE (parm
) == TEMPLATE_DECL
)
19061 parm
= TREE_TYPE (parm
);
19062 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
19064 else if (TREE_CODE (parm
) == PARM_DECL
)
19066 parm
= DECL_INITIAL (parm
);
19067 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
19070 if (!parameter_pack
&& targ
== NULL_TREE
)
19071 /* No explicit argument for this template parameter. */
19074 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
19076 /* Mark the argument pack as "incomplete". We could
19077 still deduce more arguments during unification.
19078 We remove this mark in type_unification_real. */
19081 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
19082 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
19083 = ARGUMENT_PACK_ARGS (targ
);
19086 /* We have some incomplete argument packs. */
19091 TREE_VALUE (tinst
) = explicit_targs
;
19092 if (!push_tinst_level (tinst
))
19094 excessive_deduction_depth
= true;
19097 processing_template_decl
+= incomplete
;
19098 input_location
= DECL_SOURCE_LOCATION (fn
);
19099 /* Ignore any access checks; we'll see them again in
19100 instantiate_template and they might have the wrong
19101 access path at this point. */
19102 push_deferring_access_checks (dk_deferred
);
19103 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
19104 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
19105 pop_deferring_access_checks ();
19106 input_location
= loc
;
19107 processing_template_decl
-= incomplete
;
19108 pop_tinst_level ();
19110 if (fntype
== error_mark_node
)
19113 /* Place the explicitly specified arguments in TARGS. */
19114 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
19115 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
19116 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
19119 /* Never do unification on the 'this' parameter. */
19120 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
19122 if (return_type
&& strict
== DEDUCE_CALL
)
19124 /* We're deducing for a call to the result of a template conversion
19125 function. The parms we really want are in return_type. */
19126 if (POINTER_TYPE_P (return_type
))
19127 return_type
= TREE_TYPE (return_type
);
19128 parms
= TYPE_ARG_TYPES (return_type
);
19130 else if (return_type
)
19134 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19135 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19136 new_args
[0] = return_type
;
19137 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19142 /* We allow incomplete unification without an error message here
19143 because the standard doesn't seem to explicitly prohibit it. Our
19144 callers must be ready to deal with unification failures in any
19147 TREE_VALUE (tinst
) = targs
;
19148 /* If we aren't explaining yet, push tinst context so we can see where
19149 any errors (e.g. from class instantiations triggered by instantiation
19150 of default template arguments) come from. If we are explaining, this
19151 context is redundant. */
19152 if (!explain_p
&& !push_tinst_level (tinst
))
19154 excessive_deduction_depth
= true;
19158 /* type_unification_real will pass back any access checks from default
19159 template argument substitution. */
19160 vec
<deferred_access_check
, va_gc
> *checks
;
19163 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19164 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19165 strict
, flags
, &checks
, explain_p
);
19167 pop_tinst_level ();
19171 /* Now that we have bindings for all of the template arguments,
19172 ensure that the arguments deduced for the template template
19173 parameters have compatible template parameter lists. We cannot
19174 check this property before we have deduced all template
19175 arguments, because the template parameter types of a template
19176 template parameter might depend on prior template parameters
19177 deduced after the template template parameter. The following
19178 ill-formed example illustrates this issue:
19180 template<typename T, template<T> class C> void f(C<5>, T);
19182 template<int N> struct X {};
19185 f(X<5>(), 5l); // error: template argument deduction fails
19188 The template parameter list of 'C' depends on the template type
19189 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19190 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19191 time that we deduce 'C'. */
19192 if (!template_template_parm_bindings_ok_p
19193 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19195 unify_inconsistent_template_template_parameters (explain_p
);
19199 /* All is well so far. Now, check:
19203 When all template arguments have been deduced, all uses of
19204 template parameters in nondeduced contexts are replaced with
19205 the corresponding deduced argument values. If the
19206 substitution results in an invalid type, as described above,
19207 type deduction fails. */
19208 TREE_VALUE (tinst
) = targs
;
19209 if (!push_tinst_level (tinst
))
19211 excessive_deduction_depth
= true;
19215 /* Also collect access checks from the instantiation. */
19216 reopen_deferring_access_checks (checks
);
19218 decl
= instantiate_template (fn
, targs
, complain
);
19220 checks
= get_deferred_access_checks ();
19221 pop_deferring_access_checks ();
19223 pop_tinst_level ();
19225 if (decl
== error_mark_node
)
19228 /* Now perform any access checks encountered during substitution. */
19229 push_access_scope (decl
);
19230 ok
= perform_access_checks (checks
, complain
);
19231 pop_access_scope (decl
);
19235 /* If we're looking for an exact match, check that what we got
19236 is indeed an exact match. It might not be if some template
19237 parameters are used in non-deduced contexts. But don't check
19238 for an exact match if we have dependent template arguments;
19239 in that case we're doing partial ordering, and we already know
19240 that we have two candidates that will provide the actual type. */
19241 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19243 tree substed
= TREE_TYPE (decl
);
19247 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19249 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19250 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19251 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19253 unify_type_mismatch (explain_p
, args
[i
],
19254 TREE_VALUE (sarg
));
19259 /* After doing deduction with the inherited constructor, actually return an
19260 instantiation of the inheriting constructor. */
19262 decl
= instantiate_template (orig_fn
, targs
, complain
);
19268 if (excessive_deduction_depth
)
19270 if (deduction_depth
== 0)
19271 /* Reset once we're all the way out. */
19272 excessive_deduction_depth
= false;
19275 /* We can't free this if a pending_template entry or last_error_tinst_level
19276 is pointing at it. */
19277 if (last_pending_template
== old_last_pend
19278 && last_error_tinst_level
== old_error_tinst
)
19284 /* Adjust types before performing type deduction, as described in
19285 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19286 sections are symmetric. PARM is the type of a function parameter
19287 or the return type of the conversion function. ARG is the type of
19288 the argument passed to the call, or the type of the value
19289 initialized with the result of the conversion function.
19290 ARG_EXPR is the original argument expression, which may be null. */
19293 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19306 /* Swap PARM and ARG throughout the remainder of this
19307 function; the handling is precisely symmetric since PARM
19308 will initialize ARG rather than vice versa. */
19309 std::swap (parm
, arg
);
19313 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19314 too, but here handle it by stripping the reference from PARM
19315 rather than by adding it to ARG. */
19316 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19317 && TYPE_REF_IS_RVALUE (*parm
)
19318 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19319 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19320 && TREE_CODE (*arg
) == REFERENCE_TYPE
19321 && !TYPE_REF_IS_RVALUE (*arg
))
19322 *parm
= TREE_TYPE (*parm
);
19323 /* Nothing else to do in this case. */
19327 gcc_unreachable ();
19330 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
19332 /* [temp.deduct.call]
19334 If P is not a reference type:
19336 --If A is an array type, the pointer type produced by the
19337 array-to-pointer standard conversion (_conv.array_) is
19338 used in place of A for type deduction; otherwise,
19340 --If A is a function type, the pointer type produced by
19341 the function-to-pointer standard conversion
19342 (_conv.func_) is used in place of A for type deduction;
19345 --If A is a cv-qualified type, the top level
19346 cv-qualifiers of A's type are ignored for type
19348 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19349 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19350 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19351 *arg
= build_pointer_type (*arg
);
19353 *arg
= TYPE_MAIN_VARIANT (*arg
);
19356 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19357 reference to a cv-unqualified template parameter that does not represent a
19358 template parameter of a class template (during class template argument
19359 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19360 an lvalue, the type "lvalue reference to A" is used in place of A for type
19362 if (TREE_CODE (*parm
) == REFERENCE_TYPE
19363 && TYPE_REF_IS_RVALUE (*parm
)
19364 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19365 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19366 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19367 && (arg_expr
? lvalue_p (arg_expr
)
19368 /* try_one_overload doesn't provide an arg_expr, but
19369 functions are always lvalues. */
19370 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19371 *arg
= build_reference_type (*arg
);
19373 /* [temp.deduct.call]
19375 If P is a cv-qualified type, the top level cv-qualifiers
19376 of P's type are ignored for type deduction. If P is a
19377 reference type, the type referred to by P is used for
19379 *parm
= TYPE_MAIN_VARIANT (*parm
);
19380 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
19382 *parm
= TREE_TYPE (*parm
);
19383 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19386 /* DR 322. For conversion deduction, remove a reference type on parm
19387 too (which has been swapped into ARG). */
19388 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
19389 *arg
= TREE_TYPE (*arg
);
19394 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19395 template which does contain any deducible template parameters; check if
19396 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19397 unify_one_argument. */
19400 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19401 int flags
, bool explain_p
)
19406 type
= TREE_TYPE (arg
);
19410 if (same_type_p (parm
, type
))
19411 return unify_success (explain_p
);
19413 if (strict
== DEDUCE_CONV
)
19415 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
19416 explain_p
? tf_warning_or_error
: tf_none
))
19417 return unify_success (explain_p
);
19419 else if (strict
!= DEDUCE_EXACT
)
19421 if (can_convert_arg (parm
, type
,
19422 TYPE_P (arg
) ? NULL_TREE
: arg
,
19423 flags
, explain_p
? tf_warning_or_error
: tf_none
))
19424 return unify_success (explain_p
);
19427 if (strict
== DEDUCE_EXACT
)
19428 return unify_type_mismatch (explain_p
, parm
, arg
);
19430 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19433 static bool uses_deducible_template_parms (tree type
);
19435 /* Returns true iff the expression EXPR is one from which a template
19436 argument can be deduced. In other words, if it's an undecorated
19437 use of a template non-type parameter. */
19440 deducible_expression (tree expr
)
19442 /* Strip implicit conversions. */
19443 while (CONVERT_EXPR_P (expr
))
19444 expr
= TREE_OPERAND (expr
, 0);
19445 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19448 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19449 deducible way; that is, if it has a max value of <PARM> - 1. */
19452 deducible_array_bound (tree domain
)
19454 if (domain
== NULL_TREE
)
19457 tree max
= TYPE_MAX_VALUE (domain
);
19458 if (TREE_CODE (max
) != MINUS_EXPR
)
19461 return deducible_expression (TREE_OPERAND (max
, 0));
19464 /* Returns true iff the template arguments ARGS use a template parameter
19465 in a deducible way. */
19468 deducible_template_args (tree args
)
19470 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
19473 tree elt
= TREE_VEC_ELT (args
, i
);
19474 if (ARGUMENT_PACK_P (elt
))
19475 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
19478 if (PACK_EXPANSION_P (elt
))
19479 elt
= PACK_EXPANSION_PATTERN (elt
);
19480 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
19482 else if (TYPE_P (elt
))
19483 deducible
= uses_deducible_template_parms (elt
);
19485 deducible
= deducible_expression (elt
);
19493 /* Returns true iff TYPE contains any deducible references to template
19494 parameters, as per 14.8.2.5. */
19497 uses_deducible_template_parms (tree type
)
19499 if (PACK_EXPANSION_P (type
))
19500 type
= PACK_EXPANSION_PATTERN (type
);
19507 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19508 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19514 if (POINTER_TYPE_P (type
))
19515 return uses_deducible_template_parms (TREE_TYPE (type
));
19517 /* T[integer-constant ]
19519 if (TREE_CODE (type
) == ARRAY_TYPE
)
19520 return (uses_deducible_template_parms (TREE_TYPE (type
))
19521 || deducible_array_bound (TYPE_DOMAIN (type
)));
19533 if (TYPE_PTRMEM_P (type
))
19534 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
19535 || (uses_deducible_template_parms
19536 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
19538 /* template-name <T> (where template-name refers to a class template)
19539 template-name <i> (where template-name refers to a class template) */
19540 if (CLASS_TYPE_P (type
)
19541 && CLASSTYPE_TEMPLATE_INFO (type
)
19542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
19543 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19544 (CLASSTYPE_TI_ARGS (type
)));
19549 if (TREE_CODE (type
) == FUNCTION_TYPE
19550 || TREE_CODE (type
) == METHOD_TYPE
)
19552 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19554 tree parm
= TYPE_ARG_TYPES (type
);
19555 if (TREE_CODE (type
) == METHOD_TYPE
)
19556 parm
= TREE_CHAIN (parm
);
19557 for (; parm
; parm
= TREE_CHAIN (parm
))
19558 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19565 /* Subroutine of type_unification_real and unify_pack_expansion to
19566 handle unification of a single P/A pair. Parameters are as
19567 for those functions. */
19570 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19571 int subr
, unification_kind_t strict
,
19574 tree arg_expr
= NULL_TREE
;
19577 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19578 return unify_invalid (explain_p
);
19579 if (arg
== unknown_type_node
)
19580 /* We can't deduce anything from this, but we might get all the
19581 template args from other function args. */
19582 return unify_success (explain_p
);
19584 /* Implicit conversions (Clause 4) will be performed on a function
19585 argument to convert it to the type of the corresponding function
19586 parameter if the parameter type contains no template-parameters that
19587 participate in template argument deduction. */
19588 if (strict
!= DEDUCE_EXACT
19589 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19590 /* For function parameters with no deducible template parameters,
19591 just return. We'll check non-dependent conversions later. */
19592 return unify_success (explain_p
);
19597 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19598 | UNIFY_ALLOW_MORE_CV_QUAL
19599 | UNIFY_ALLOW_DERIVED
);
19603 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19607 arg_strict
= UNIFY_ALLOW_NONE
;
19611 gcc_unreachable ();
19614 /* We only do these transformations if this is the top-level
19615 parameter_type_list in a call or declaration matching; in other
19616 situations (nested function declarators, template argument lists) we
19617 won't be comparing a type to an expression, and we don't do any type
19623 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19624 if (type_unknown_p (arg
))
19626 /* [temp.deduct.type] A template-argument can be
19627 deduced from a pointer to function or pointer
19628 to member function argument if the set of
19629 overloaded functions does not contain function
19630 templates and at most one of a set of
19631 overloaded functions provides a unique
19633 resolve_overloaded_unification (tparms
, targs
, parm
,
19635 arg_strict
, explain_p
);
19636 /* If a unique match was not found, this is a
19637 non-deduced context, so we still succeed. */
19638 return unify_success (explain_p
);
19642 arg
= unlowered_expr_type (arg
);
19643 if (arg
== error_mark_node
)
19644 return unify_invalid (explain_p
);
19648 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19651 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19652 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19653 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19655 /* For deduction from an init-list we need the actual list. */
19656 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19658 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19661 /* for_each_template_parm callback that always returns 0. */
19664 zero_r (tree
, void *)
19669 /* for_each_template_parm any_fn callback to handle deduction of a template
19670 type argument from the type of an array bound. */
19673 array_deduction_r (tree t
, void *data
)
19675 tree_pair_p d
= (tree_pair_p
)data
;
19676 tree
&tparms
= d
->purpose
;
19677 tree
&targs
= d
->value
;
19679 if (TREE_CODE (t
) == ARRAY_TYPE
)
19680 if (tree dom
= TYPE_DOMAIN (t
))
19681 if (tree max
= TYPE_MAX_VALUE (dom
))
19683 if (TREE_CODE (max
) == MINUS_EXPR
)
19684 max
= TREE_OPERAND (max
, 0);
19685 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19686 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19687 UNIFY_ALLOW_NONE
, /*explain*/false);
19690 /* Keep walking. */
19694 /* Try to deduce any not-yet-deduced template type arguments from the type of
19695 an array bound. This is handled separately from unify because 14.8.2.5 says
19696 "The type of a type parameter is only deduced from an array bound if it is
19697 not otherwise deduced." */
19700 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19702 tree_pair_s data
= { tparms
, targs
};
19703 hash_set
<tree
> visited
;
19704 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19705 /*nondeduced*/false, array_deduction_r
);
19708 /* Most parms like fn_type_unification.
19710 If SUBR is 1, we're being called recursively (to unify the
19711 arguments of a function or method parameter of a function
19714 CHECKS is a pointer to a vector of access checks encountered while
19715 substituting default template arguments. */
19718 type_unification_real (tree tparms
,
19722 unsigned int xnargs
,
19724 unification_kind_t strict
,
19726 vec
<deferred_access_check
, va_gc
> **checks
,
19731 int ntparms
= TREE_VEC_LENGTH (tparms
);
19732 int saw_undeduced
= 0;
19735 unsigned int nargs
;
19738 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19739 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19740 gcc_assert (ntparms
> 0);
19742 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19744 /* Reset the number of non-defaulted template arguments contained
19746 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19754 while (parms
&& parms
!= void_list_node
19757 parm
= TREE_VALUE (parms
);
19759 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19760 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19761 /* For a function parameter pack that occurs at the end of the
19762 parameter-declaration-list, the type A of each remaining
19763 argument of the call is compared with the type P of the
19764 declarator-id of the function parameter pack. */
19767 parms
= TREE_CHAIN (parms
);
19769 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19770 /* For a function parameter pack that does not occur at the
19771 end of the parameter-declaration-list, the type of the
19772 parameter pack is a non-deduced context. */
19778 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19784 && parms
!= void_list_node
19785 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19787 /* Unify the remaining arguments with the pack expansion type. */
19789 tree parmvec
= make_tree_vec (1);
19791 /* Allocate a TREE_VEC and copy in all of the arguments */
19792 argvec
= make_tree_vec (nargs
- ia
);
19793 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19794 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19796 /* Copy the parameter into parmvec. */
19797 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19798 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19799 /*subr=*/subr
, explain_p
))
19802 /* Advance to the end of the list of parameters. */
19803 parms
= TREE_CHAIN (parms
);
19806 /* Fail if we've reached the end of the parm list, and more args
19807 are present, and the parm list isn't variadic. */
19808 if (ia
< nargs
&& parms
== void_list_node
)
19809 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19810 /* Fail if parms are left and they don't have default values and
19811 they aren't all deduced as empty packs (c++/57397). This is
19812 consistent with sufficient_parms_p. */
19813 if (parms
&& parms
!= void_list_node
19814 && TREE_PURPOSE (parms
) == NULL_TREE
)
19816 unsigned int count
= nargs
;
19821 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19824 p
= TREE_CHAIN (p
);
19826 while (p
&& p
!= void_list_node
);
19827 if (count
!= nargs
)
19828 return unify_too_few_arguments (explain_p
, ia
, count
,
19834 tsubst_flags_t complain
= (explain_p
19835 ? tf_warning_or_error
19837 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
19839 for (i
= 0; i
< ntparms
; i
++)
19841 tree targ
= TREE_VEC_ELT (targs
, i
);
19842 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19844 /* Clear the "incomplete" flags on all argument packs now so that
19845 substituting them into later default arguments works. */
19846 if (targ
&& ARGUMENT_PACK_P (targ
))
19848 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19849 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19852 if (targ
|| tparm
== error_mark_node
)
19854 tparm
= TREE_VALUE (tparm
);
19856 if (TREE_CODE (tparm
) == TYPE_DECL
19857 && !tried_array_deduction
)
19859 try_array_deduction (tparms
, targs
, xparms
);
19860 tried_array_deduction
= true;
19861 if (TREE_VEC_ELT (targs
, i
))
19865 /* If this is an undeduced nontype parameter that depends on
19866 a type parameter, try another pass; its type may have been
19867 deduced from a later argument than the one from which
19868 this parameter can be deduced. */
19869 if (TREE_CODE (tparm
) == PARM_DECL
19870 && uses_template_parms (TREE_TYPE (tparm
))
19871 && saw_undeduced
< 2)
19877 /* Core issue #226 (C++0x) [temp.deduct]:
19879 If a template argument has not been deduced, its
19880 default template argument, if any, is used.
19882 When we are in C++98 mode, TREE_PURPOSE will either
19883 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19884 to explicitly check cxx_dialect here. */
19885 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19886 /* OK, there is a default argument. Wait until after the
19887 conversion check to do substitution. */
19890 /* If the type parameter is a parameter pack, then it will
19891 be deduced to an empty parameter pack. */
19892 if (template_parameter_pack_p (tparm
))
19896 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19898 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19899 TREE_CONSTANT (arg
) = 1;
19902 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19904 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19906 TREE_VEC_ELT (targs
, i
) = arg
;
19910 return unify_parameter_deduction_failure (explain_p
, tparm
);
19913 /* DR 1391: All parameters have args, now check non-dependent parms for
19915 if (saw_undeduced
< 2)
19916 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19917 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19919 parm
= TREE_VALUE (parms
);
19921 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19922 && (!TREE_CHAIN (parms
)
19923 || TREE_CHAIN (parms
) == void_list_node
))
19924 /* For a function parameter pack that occurs at the end of the
19925 parameter-declaration-list, the type A of each remaining
19926 argument of the call is compared with the type P of the
19927 declarator-id of the function parameter pack. */
19930 parms
= TREE_CHAIN (parms
);
19932 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19933 /* For a function parameter pack that does not occur at the
19934 end of the parameter-declaration-list, the type of the
19935 parameter pack is a non-deduced context. */
19941 if (uses_template_parms (parm
))
19943 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19948 /* Now substitute into the default template arguments. */
19949 for (i
= 0; i
< ntparms
; i
++)
19951 tree targ
= TREE_VEC_ELT (targs
, i
);
19952 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19954 if (targ
|| tparm
== error_mark_node
)
19956 tree parm
= TREE_VALUE (tparm
);
19958 tsubst_flags_t fcomplain
= complain
;
19959 if (saw_undeduced
== 1)
19961 /* When saw_undeduced == 1, substitution into parm and arg might
19962 fail or not replace all template parameters, and that's
19964 fcomplain
= tf_none
;
19965 if (TREE_CODE (parm
) == PARM_DECL
19966 && uses_template_parms (TREE_TYPE (parm
)))
19970 tree arg
= TREE_PURPOSE (tparm
);
19971 reopen_deferring_access_checks (*checks
);
19972 location_t save_loc
= input_location
;
19974 input_location
= DECL_SOURCE_LOCATION (parm
);
19975 arg
= tsubst_template_arg (arg
, full_targs
, fcomplain
, NULL_TREE
);
19976 if (arg
!= error_mark_node
&& !uses_template_parms (arg
))
19977 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19979 else if (saw_undeduced
== 1)
19982 arg
= error_mark_node
;
19983 input_location
= save_loc
;
19984 *checks
= get_deferred_access_checks ();
19985 pop_deferring_access_checks ();
19986 if (arg
== error_mark_node
)
19990 TREE_VEC_ELT (targs
, i
) = arg
;
19991 /* The position of the first default template argument,
19992 is also the number of non-defaulted arguments in TARGS.
19994 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19999 if (saw_undeduced
++ == 1)
20003 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20004 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
20006 return unify_success (explain_p
);
20009 /* Subroutine of type_unification_real. Args are like the variables
20010 at the call site. ARG is an overloaded function (or template-id);
20011 we try deducing template args from each of the overloads, and if
20012 only one succeeds, we go with that. Modifies TARGS and returns
20013 true on success. */
20016 resolve_overloaded_unification (tree tparms
,
20020 unification_kind_t strict
,
20024 tree tempargs
= copy_node (targs
);
20026 tree goodfn
= NULL_TREE
;
20029 if (TREE_CODE (arg
) == ADDR_EXPR
)
20031 arg
= TREE_OPERAND (arg
, 0);
20037 if (TREE_CODE (arg
) == COMPONENT_REF
)
20038 /* Handle `&x' where `x' is some static or non-static member
20040 arg
= TREE_OPERAND (arg
, 1);
20042 if (TREE_CODE (arg
) == OFFSET_REF
)
20043 arg
= TREE_OPERAND (arg
, 1);
20045 /* Strip baselink information. */
20046 if (BASELINK_P (arg
))
20047 arg
= BASELINK_FUNCTIONS (arg
);
20049 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
20051 /* If we got some explicit template args, we need to plug them into
20052 the affected templates before we try to unify, in case the
20053 explicit args will completely resolve the templates in question. */
20056 tree expl_subargs
= TREE_OPERAND (arg
, 1);
20057 arg
= TREE_OPERAND (arg
, 0);
20059 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20062 tree subargs
, elem
;
20064 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20067 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20068 expl_subargs
, NULL_TREE
, tf_none
,
20069 /*require_all_args=*/true,
20070 /*use_default_args=*/true);
20071 if (subargs
!= error_mark_node
20072 && !any_dependent_template_arguments_p (subargs
))
20074 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
20075 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
20076 elem
, strict
, sub_strict
, addr_p
, explain_p
)
20077 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
20086 /* If no templates (or more than one) are fully resolved by the
20087 explicit arguments, this template-id is a non-deduced context; it
20088 could still be OK if we deduce all template arguments for the
20089 enclosing call through other arguments. */
20093 else if (TREE_CODE (arg
) != OVERLOAD
20094 && TREE_CODE (arg
) != FUNCTION_DECL
)
20095 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20096 -- but the deduction does not succeed because the expression is
20097 not just the function on its own. */
20100 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20103 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
20104 strict
, sub_strict
, addr_p
, explain_p
)
20105 && (!goodfn
|| !decls_match (goodfn
, fn
)))
20112 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20113 to function or pointer to member function argument if the set of
20114 overloaded functions does not contain function templates and at most
20115 one of a set of overloaded functions provides a unique match.
20117 So if we found multiple possibilities, we return success but don't
20118 deduce anything. */
20122 int i
= TREE_VEC_LENGTH (targs
);
20124 if (TREE_VEC_ELT (tempargs
, i
))
20126 tree old
= TREE_VEC_ELT (targs
, i
);
20127 tree new_
= TREE_VEC_ELT (tempargs
, i
);
20128 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
20129 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
20130 /* Don't forget explicit template arguments in a pack. */
20131 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
20132 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
20133 TREE_VEC_ELT (targs
, i
) = new_
;
20142 /* Core DR 115: In contexts where deduction is done and fails, or in
20143 contexts where deduction is not done, if a template argument list is
20144 specified and it, along with any default template arguments, identifies
20145 a single function template specialization, then the template-id is an
20146 lvalue for the function template specialization. */
20149 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20151 tree expr
, offset
, baselink
;
20154 if (!type_unknown_p (orig_expr
))
20159 offset
= NULL_TREE
;
20160 baselink
= NULL_TREE
;
20162 if (TREE_CODE (expr
) == ADDR_EXPR
)
20164 expr
= TREE_OPERAND (expr
, 0);
20167 if (TREE_CODE (expr
) == OFFSET_REF
)
20170 expr
= TREE_OPERAND (expr
, 1);
20172 if (BASELINK_P (expr
))
20175 expr
= BASELINK_FUNCTIONS (expr
);
20178 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20181 tree goodfn
= NULL_TREE
;
20183 /* If we got some explicit template args, we need to plug them into
20184 the affected templates before we try to unify, in case the
20185 explicit args will completely resolve the templates in question. */
20187 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20188 tree arg
= TREE_OPERAND (expr
, 0);
20189 tree badfn
= NULL_TREE
;
20190 tree badargs
= NULL_TREE
;
20192 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20195 tree subargs
, elem
;
20197 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20200 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20201 expl_subargs
, NULL_TREE
, tf_none
,
20202 /*require_all_args=*/true,
20203 /*use_default_args=*/true);
20204 if (subargs
!= error_mark_node
20205 && !any_dependent_template_arguments_p (subargs
))
20207 elem
= instantiate_template (fn
, subargs
, tf_none
);
20208 if (elem
== error_mark_node
)
20213 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20222 mark_used (goodfn
);
20225 expr
= build_baselink (BASELINK_BINFO (baselink
),
20226 BASELINK_ACCESS_BINFO (baselink
),
20227 expr
, BASELINK_OPTYPE (baselink
));
20231 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20232 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20235 expr
= cp_build_addr_expr (expr
, complain
);
20238 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20239 /* There were no good options and at least one bad one, so let the
20240 user know what the problem is. */
20241 instantiate_template (badfn
, badargs
, complain
);
20246 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20247 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20248 different overloads deduce different arguments for a given parm.
20249 ADDR_P is true if the expression for which deduction is being
20250 performed was of the form "& fn" rather than simply "fn".
20252 Returns 1 on success. */
20255 try_one_overload (tree tparms
,
20260 unification_kind_t strict
,
20269 if (arg
== error_mark_node
)
20272 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20273 to function or pointer to member function argument if the set of
20274 overloaded functions does not contain function templates and at most
20275 one of a set of overloaded functions provides a unique match.
20277 So if this is a template, just return success. */
20279 if (uses_template_parms (arg
))
20282 if (TREE_CODE (arg
) == METHOD_TYPE
)
20283 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20285 arg
= build_pointer_type (arg
);
20287 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20289 /* We don't copy orig_targs for this because if we have already deduced
20290 some template args from previous args, unify would complain when we
20291 try to deduce a template parameter for the same argument, even though
20292 there isn't really a conflict. */
20293 nargs
= TREE_VEC_LENGTH (targs
);
20294 tempargs
= make_tree_vec (nargs
);
20296 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20299 /* First make sure we didn't deduce anything that conflicts with
20300 explicitly specified args. */
20301 for (i
= nargs
; i
--; )
20303 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20304 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20308 else if (uses_template_parms (elt
))
20309 /* Since we're unifying against ourselves, we will fill in
20310 template args used in the function parm list with our own
20311 template parms. Discard them. */
20312 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20313 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20315 /* Check that the argument at each index of the deduced argument pack
20316 is equivalent to the corresponding explicitly specified argument.
20317 We may have deduced more arguments than were explicitly specified,
20320 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20321 that's wrong if we deduce the same argument pack from multiple
20322 function arguments: it's only incomplete the first time. */
20324 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20325 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20327 if (TREE_VEC_LENGTH (deduced_pack
)
20328 < TREE_VEC_LENGTH (explicit_pack
))
20331 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20332 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20333 TREE_VEC_ELT (deduced_pack
, j
)))
20336 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20340 for (i
= nargs
; i
--; )
20342 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20345 TREE_VEC_ELT (targs
, i
) = elt
;
20351 /* PARM is a template class (perhaps with unbound template
20352 parameters). ARG is a fully instantiated type. If ARG can be
20353 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20354 TARGS are as for unify. */
20357 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20360 tree copy_of_targs
;
20362 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20364 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20365 /* Matches anything. */;
20366 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20367 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20370 /* We need to make a new template argument vector for the call to
20371 unify. If we used TARGS, we'd clutter it up with the result of
20372 the attempted unification, even if this class didn't work out.
20373 We also don't want to commit ourselves to all the unifications
20374 we've already done, since unification is supposed to be done on
20375 an argument-by-argument basis. In other words, consider the
20376 following pathological case:
20378 template <int I, int J, int K>
20381 template <int I, int J>
20382 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20384 template <int I, int J, int K>
20385 void f(S<I, J, K>, S<I, I, I>);
20394 Now, by the time we consider the unification involving `s2', we
20395 already know that we must have `f<0, 0, 0>'. But, even though
20396 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20397 because there are two ways to unify base classes of S<0, 1, 2>
20398 with S<I, I, I>. If we kept the already deduced knowledge, we
20399 would reject the possibility I=1. */
20400 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20402 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20404 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20409 /* If unification failed, we're done. */
20410 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20411 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20417 /* Given a template type PARM and a class type ARG, find the unique
20418 base type in ARG that is an instance of PARM. We do not examine
20419 ARG itself; only its base-classes. If there is not exactly one
20420 appropriate base class, return NULL_TREE. PARM may be the type of
20421 a partial specialization, as well as a plain template type. Used
20424 static enum template_base_result
20425 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20426 bool explain_p
, tree
*result
)
20428 tree rval
= NULL_TREE
;
20431 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20433 binfo
= TYPE_BINFO (complete_type (arg
));
20436 /* The type could not be completed. */
20437 *result
= NULL_TREE
;
20438 return tbr_incomplete_type
;
20441 /* Walk in inheritance graph order. The search order is not
20442 important, and this avoids multiple walks of virtual bases. */
20443 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20445 tree r
= try_class_unification (tparms
, targs
, parm
,
20446 BINFO_TYPE (binfo
), explain_p
);
20450 /* If there is more than one satisfactory baseclass, then:
20454 If they yield more than one possible deduced A, the type
20458 if (rval
&& !same_type_p (r
, rval
))
20460 *result
= NULL_TREE
;
20461 return tbr_ambiguous_baseclass
;
20469 return tbr_success
;
20472 /* Returns the level of DECL, which declares a template parameter. */
20475 template_decl_level (tree decl
)
20477 switch (TREE_CODE (decl
))
20480 case TEMPLATE_DECL
:
20481 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
20484 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
20487 gcc_unreachable ();
20492 /* Decide whether ARG can be unified with PARM, considering only the
20493 cv-qualifiers of each type, given STRICT as documented for unify.
20494 Returns nonzero iff the unification is OK on that basis. */
20497 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
20499 int arg_quals
= cp_type_quals (arg
);
20500 int parm_quals
= cp_type_quals (parm
);
20502 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20503 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20505 /* Although a CVR qualifier is ignored when being applied to a
20506 substituted template parameter ([8.3.2]/1 for example), that
20507 does not allow us to unify "const T" with "int&" because both
20508 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20509 It is ok when we're allowing additional CV qualifiers
20510 at the outer level [14.8.2.1]/3,1st bullet. */
20511 if ((TREE_CODE (arg
) == REFERENCE_TYPE
20512 || TREE_CODE (arg
) == FUNCTION_TYPE
20513 || TREE_CODE (arg
) == METHOD_TYPE
)
20514 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
20517 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
20518 && (parm_quals
& TYPE_QUAL_RESTRICT
))
20522 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
20523 && (arg_quals
& parm_quals
) != parm_quals
)
20526 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
20527 && (parm_quals
& arg_quals
) != arg_quals
)
20533 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20535 template_parm_level_and_index (tree parm
, int* level
, int* index
)
20537 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20538 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20539 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20541 *index
= TEMPLATE_TYPE_IDX (parm
);
20542 *level
= TEMPLATE_TYPE_LEVEL (parm
);
20546 *index
= TEMPLATE_PARM_IDX (parm
);
20547 *level
= TEMPLATE_PARM_LEVEL (parm
);
20551 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20553 if (unify (TP, TA, P, A, S, EP)) \
20557 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20558 expansion at the end of PACKED_PARMS. Returns 0 if the type
20559 deduction succeeds, 1 otherwise. STRICT is the same as in
20560 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20561 function call argument list. We'll need to adjust the arguments to make them
20562 types. SUBR tells us if this is from a recursive call to
20563 type_unification_real, or for comparing two template argument
20567 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20568 tree packed_args
, unification_kind_t strict
,
20569 bool subr
, bool explain_p
)
20572 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20573 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20574 tree pack
, packs
= NULL_TREE
;
20575 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20577 /* Add in any args remembered from an earlier partial instantiation. */
20578 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
20579 int levels
= TMPL_ARGS_DEPTH (targs
);
20581 packed_args
= expand_template_argument_pack (packed_args
);
20583 int len
= TREE_VEC_LENGTH (packed_args
);
20585 /* Determine the parameter packs we will be deducing from the
20586 pattern, and record their current deductions. */
20587 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20588 pack
; pack
= TREE_CHAIN (pack
))
20590 tree parm_pack
= TREE_VALUE (pack
);
20593 /* Determine the index and level of this parameter pack. */
20594 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20595 if (level
< levels
)
20598 /* Keep track of the parameter packs and their corresponding
20600 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20601 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20604 /* Loop through all of the arguments that have not yet been
20605 unified and unify each with the pattern. */
20606 for (i
= start
; i
< len
; i
++)
20609 bool any_explicit
= false;
20610 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20612 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20613 or the element of its argument pack at the current index if
20614 this argument was explicitly specified. */
20615 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20619 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20622 if (TREE_VALUE (pack
)
20623 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20624 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20626 any_explicit
= true;
20627 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20629 TMPL_ARG (targs
, level
, idx
) = arg
;
20632 /* If we had explicit template arguments, substitute them into the
20633 pattern before deduction. */
20636 /* Some arguments might still be unspecified or dependent. */
20638 ++processing_template_decl
;
20639 dependent
= any_dependent_template_arguments_p (targs
);
20641 --processing_template_decl
;
20642 parm
= tsubst (pattern
, targs
,
20643 explain_p
? tf_warning_or_error
: tf_none
,
20646 --processing_template_decl
;
20647 if (parm
== error_mark_node
)
20653 /* Unify the pattern with the current argument. */
20654 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20658 /* For each parameter pack, collect the deduced value. */
20659 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20662 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20664 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20665 TMPL_ARG (targs
, level
, idx
);
20669 /* Verify that the results of unification with the parameter packs
20670 produce results consistent with what we've seen before, and make
20671 the deduced argument packs available. */
20672 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20674 tree old_pack
= TREE_VALUE (pack
);
20675 tree new_args
= TREE_TYPE (pack
);
20676 int i
, len
= TREE_VEC_LENGTH (new_args
);
20678 bool nondeduced_p
= false;
20680 /* By default keep the original deduced argument pack.
20681 If necessary, more specific code is going to update the
20682 resulting deduced argument later down in this function. */
20683 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20684 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20686 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20687 actually deduce anything. */
20688 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20689 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20690 nondeduced_p
= true;
20694 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20696 /* If we had fewer function args than explicit template args,
20697 just use the explicits. */
20698 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20699 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20700 if (len
< explicit_len
)
20701 new_args
= explicit_args
;
20707 /* Build the deduced *_ARGUMENT_PACK. */
20708 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20710 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20711 TREE_CONSTANT (result
) = 1;
20714 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20716 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20718 /* Note the deduced argument packs for this parameter
20720 TMPL_ARG (targs
, level
, idx
) = result
;
20722 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20723 && (ARGUMENT_PACK_ARGS (old_pack
)
20724 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20726 /* We only had the explicitly-provided arguments before, but
20727 now we have a complete set of arguments. */
20728 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20730 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20731 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20732 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20736 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20737 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20739 if (!comp_template_args (old_args
, new_args
,
20740 &bad_old_arg
, &bad_new_arg
))
20741 /* Inconsistent unification of this parameter pack. */
20742 return unify_parameter_pack_inconsistent (explain_p
,
20748 return unify_success (explain_p
);
20751 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20752 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20753 parameters and return value are as for unify. */
20756 unify_array_domain (tree tparms
, tree targs
,
20757 tree parm_dom
, tree arg_dom
,
20765 /* Our representation of array types uses "N - 1" as the
20766 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20767 not an integer constant. We cannot unify arbitrarily
20768 complex expressions, so we eliminate the MINUS_EXPRs
20770 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20771 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20774 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20775 parm_max
= TREE_OPERAND (parm_max
, 0);
20777 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20778 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20781 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20782 trying to unify the type of a variable with the type
20783 of a template parameter. For example:
20785 template <unsigned int N>
20786 void f (char (&) [N]);
20793 Here, the type of the ARG will be "int [g(i)]", and
20794 may be a SAVE_EXPR, etc. */
20795 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20796 return unify_vla_arg (explain_p
, arg_dom
);
20797 arg_max
= TREE_OPERAND (arg_max
, 0);
20800 /* If only one of the bounds used a MINUS_EXPR, compensate
20801 by adding one to the other bound. */
20802 if (parm_cst
&& !arg_cst
)
20803 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20807 else if (arg_cst
&& !parm_cst
)
20808 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20813 return unify (tparms
, targs
, parm_max
, arg_max
,
20814 UNIFY_ALLOW_INTEGER
, explain_p
);
20817 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20819 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20824 if (PACK_EXPANSION_P (t
))
20825 t
= PACK_EXPANSION_PATTERN (t
);
20826 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20827 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20828 || DECL_TYPE_TEMPLATE_P (t
))
20830 else if (TYPE_P (t
))
20836 /* Deduce the value of template parameters. TPARMS is the (innermost)
20837 set of template parameters to a template. TARGS is the bindings
20838 for those template parameters, as determined thus far; TARGS may
20839 include template arguments for outer levels of template parameters
20840 as well. PARM is a parameter to a template function, or a
20841 subcomponent of that parameter; ARG is the corresponding argument.
20842 This function attempts to match PARM with ARG in a manner
20843 consistent with the existing assignments in TARGS. If more values
20844 are deduced, then TARGS is updated.
20846 Returns 0 if the type deduction succeeds, 1 otherwise. The
20847 parameter STRICT is a bitwise or of the following flags:
20850 Require an exact match between PARM and ARG.
20851 UNIFY_ALLOW_MORE_CV_QUAL:
20852 Allow the deduced ARG to be more cv-qualified (by qualification
20853 conversion) than ARG.
20854 UNIFY_ALLOW_LESS_CV_QUAL:
20855 Allow the deduced ARG to be less cv-qualified than ARG.
20856 UNIFY_ALLOW_DERIVED:
20857 Allow the deduced ARG to be a template base class of ARG,
20858 or a pointer to a template base class of the type pointed to by
20860 UNIFY_ALLOW_INTEGER:
20861 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20862 case for more information.
20863 UNIFY_ALLOW_OUTER_LEVEL:
20864 This is the outermost level of a deduction. Used to determine validity
20865 of qualification conversions. A valid qualification conversion must
20866 have const qualified pointers leading up to the inner type which
20867 requires additional CV quals, except at the outer level, where const
20868 is not required [conv.qual]. It would be normal to set this flag in
20869 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20870 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20871 This is the outermost level of a deduction, and PARM can be more CV
20872 qualified at this point.
20873 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20874 This is the outermost level of a deduction, and PARM can be less CV
20875 qualified at this point. */
20878 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20884 int strict_in
= strict
;
20885 tsubst_flags_t complain
= (explain_p
20886 ? tf_warning_or_error
20889 /* I don't think this will do the right thing with respect to types.
20890 But the only case I've seen it in so far has been array bounds, where
20891 signedness is the only information lost, and I think that will be
20893 while (CONVERT_EXPR_P (parm
))
20894 parm
= TREE_OPERAND (parm
, 0);
20896 if (arg
== error_mark_node
)
20897 return unify_invalid (explain_p
);
20898 if (arg
== unknown_type_node
20899 || arg
== init_list_type_node
)
20900 /* We can't deduce anything from this, but we might get all the
20901 template args from other function args. */
20902 return unify_success (explain_p
);
20904 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20905 return unify_success (explain_p
);
20907 /* If PARM uses template parameters, then we can't bail out here,
20908 even if ARG == PARM, since we won't record unifications for the
20909 template parameters. We might need them if we're trying to
20910 figure out which of two things is more specialized. */
20911 if (arg
== parm
&& !uses_template_parms (parm
))
20912 return unify_success (explain_p
);
20914 /* Handle init lists early, so the rest of the function can assume
20915 we're dealing with a type. */
20916 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20920 tree orig_parm
= parm
;
20922 /* Replace T with std::initializer_list<T> for deduction. */
20923 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20924 && flag_deduce_init_list
)
20925 parm
= listify (parm
);
20927 if (!is_std_init_list (parm
)
20928 && TREE_CODE (parm
) != ARRAY_TYPE
)
20929 /* We can only deduce from an initializer list argument if the
20930 parameter is std::initializer_list or an array; otherwise this
20931 is a non-deduced context. */
20932 return unify_success (explain_p
);
20934 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20935 elttype
= TREE_TYPE (parm
);
20938 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20939 /* Deduction is defined in terms of a single type, so just punt
20940 on the (bizarre) std::initializer_list<T...>. */
20941 if (PACK_EXPANSION_P (elttype
))
20942 return unify_success (explain_p
);
20945 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20947 int elt_strict
= strict
;
20949 if (elt
== error_mark_node
)
20950 return unify_invalid (explain_p
);
20952 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20954 tree type
= TREE_TYPE (elt
);
20955 if (type
== error_mark_node
)
20956 return unify_invalid (explain_p
);
20957 /* It should only be possible to get here for a call. */
20958 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20959 elt_strict
|= maybe_adjust_types_for_deduction
20960 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20964 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20968 if (TREE_CODE (parm
) == ARRAY_TYPE
20969 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20971 /* Also deduce from the length of the initializer list. */
20972 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20973 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20974 if (idx
== error_mark_node
)
20975 return unify_invalid (explain_p
);
20976 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20980 /* If the std::initializer_list<T> deduction worked, replace the
20981 deduced A with std::initializer_list<A>. */
20982 if (orig_parm
!= parm
)
20984 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20985 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20986 targ
= listify (targ
);
20987 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20989 return unify_success (explain_p
);
20992 /* If parm and arg aren't the same kind of thing (template, type, or
20993 expression), fail early. */
20994 if (pa_kind (parm
) != pa_kind (arg
))
20995 return unify_invalid (explain_p
);
20997 /* Immediately reject some pairs that won't unify because of
20998 cv-qualification mismatches. */
20999 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21001 /* It is the elements of the array which hold the cv quals of an array
21002 type, and the elements might be template type parms. We'll check
21003 when we recurse. */
21004 && TREE_CODE (arg
) != ARRAY_TYPE
21005 /* We check the cv-qualifiers when unifying with template type
21006 parameters below. We want to allow ARG `const T' to unify with
21007 PARM `T' for example, when computing which of two templates
21008 is more specialized, for example. */
21009 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
21010 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
21011 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21013 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
21014 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
21015 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
21016 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
21017 strict
&= ~UNIFY_ALLOW_DERIVED
;
21018 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21019 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
21021 switch (TREE_CODE (parm
))
21023 case TYPENAME_TYPE
:
21025 case UNBOUND_CLASS_TEMPLATE
:
21026 /* In a type which contains a nested-name-specifier, template
21027 argument values cannot be deduced for template parameters used
21028 within the nested-name-specifier. */
21029 return unify_success (explain_p
);
21031 case TEMPLATE_TYPE_PARM
:
21032 case TEMPLATE_TEMPLATE_PARM
:
21033 case BOUND_TEMPLATE_TEMPLATE_PARM
:
21034 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21035 if (error_operand_p (tparm
))
21036 return unify_invalid (explain_p
);
21038 if (TEMPLATE_TYPE_LEVEL (parm
)
21039 != template_decl_level (tparm
))
21040 /* The PARM is not one we're trying to unify. Just check
21041 to see if it matches ARG. */
21043 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21044 && (is_auto (parm
) ? is_auto (arg
)
21045 : same_type_p (parm
, arg
)))
21046 return unify_success (explain_p
);
21048 return unify_type_mismatch (explain_p
, parm
, arg
);
21050 idx
= TEMPLATE_TYPE_IDX (parm
);
21051 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21052 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
21053 if (error_operand_p (tparm
))
21054 return unify_invalid (explain_p
);
21056 /* Check for mixed types and values. */
21057 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21058 && TREE_CODE (tparm
) != TYPE_DECL
)
21059 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21060 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
21061 gcc_unreachable ();
21063 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21065 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
21066 && CLASS_TYPE_P (arg
))
21068 /* First try to match ARG directly. */
21069 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
21073 /* Otherwise, look for a suitable base of ARG, as below. */
21074 enum template_base_result r
;
21075 r
= get_template_base (tparms
, targs
, parm
, arg
,
21078 return unify_no_common_base (explain_p
, r
, parm
, arg
);
21082 /* ARG must be constructed from a template class or a template
21083 template parameter. */
21084 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
21085 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21086 return unify_template_deduction_failure (explain_p
, parm
, arg
);
21088 /* Deduce arguments T, i from TT<T> or TT<i>. */
21089 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
21092 arg
= TYPE_TI_TEMPLATE (arg
);
21094 /* Fall through to deduce template name. */
21097 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21098 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21100 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21102 /* Simple cases: Value already set, does match or doesn't. */
21103 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
21104 return unify_success (explain_p
);
21106 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21110 /* If PARM is `const T' and ARG is only `int', we don't have
21111 a match unless we are allowing additional qualification.
21112 If ARG is `const int' and PARM is just `T' that's OK;
21113 that binds `const int' to `T'. */
21114 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
21116 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21118 /* Consider the case where ARG is `const volatile int' and
21119 PARM is `const T'. Then, T should be `volatile int'. */
21120 arg
= cp_build_qualified_type_real
21121 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
21122 if (arg
== error_mark_node
)
21123 return unify_invalid (explain_p
);
21125 /* Simple cases: Value already set, does match or doesn't. */
21126 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
21127 return unify_success (explain_p
);
21129 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21131 /* Make sure that ARG is not a variable-sized array. (Note
21132 that were talking about variable-sized arrays (like
21133 `int[n]'), rather than arrays of unknown size (like
21134 `int[]').) We'll get very confused by such a type since
21135 the bound of the array is not constant, and therefore
21136 not mangleable. Besides, such types are not allowed in
21137 ISO C++, so we can do as we please here. We do allow
21138 them for 'auto' deduction, since that isn't ABI-exposed. */
21139 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21140 return unify_vla_arg (explain_p
, arg
);
21142 /* Strip typedefs as in convert_template_argument. */
21143 arg
= canonicalize_type_argument (arg
, tf_none
);
21146 /* If ARG is a parameter pack or an expansion, we cannot unify
21147 against it unless PARM is also a parameter pack. */
21148 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21149 && !template_parameter_pack_p (parm
))
21150 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21152 /* If the argument deduction results is a METHOD_TYPE,
21153 then there is a problem.
21154 METHOD_TYPE doesn't map to any real C++ type the result of
21155 the deduction can not be of that type. */
21156 if (TREE_CODE (arg
) == METHOD_TYPE
)
21157 return unify_method_type_error (explain_p
, arg
);
21159 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21160 return unify_success (explain_p
);
21162 case TEMPLATE_PARM_INDEX
:
21163 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21164 if (error_operand_p (tparm
))
21165 return unify_invalid (explain_p
);
21167 if (TEMPLATE_PARM_LEVEL (parm
)
21168 != template_decl_level (tparm
))
21170 /* The PARM is not one we're trying to unify. Just check
21171 to see if it matches ARG. */
21172 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21173 && cp_tree_equal (parm
, arg
));
21175 unify_expression_unequal (explain_p
, parm
, arg
);
21179 idx
= TEMPLATE_PARM_IDX (parm
);
21180 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21184 if ((strict
& UNIFY_ALLOW_INTEGER
)
21185 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21186 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21187 /* We're deducing from an array bound, the type doesn't matter. */
21188 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21189 int x
= !cp_tree_equal (targ
, arg
);
21191 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21195 /* [temp.deduct.type] If, in the declaration of a function template
21196 with a non-type template-parameter, the non-type
21197 template-parameter is used in an expression in the function
21198 parameter-list and, if the corresponding template-argument is
21199 deduced, the template-argument type shall match the type of the
21200 template-parameter exactly, except that a template-argument
21201 deduced from an array bound may be of any integral type.
21202 The non-type parameter might use already deduced type parameters. */
21203 ++processing_template_decl
;
21204 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
21205 --processing_template_decl
;
21206 if (tree a
= type_uses_auto (tparm
))
21208 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21209 if (tparm
== error_mark_node
)
21213 if (!TREE_TYPE (arg
))
21214 /* Template-parameter dependent expression. Just accept it for now.
21215 It will later be processed in convert_template_argument. */
21217 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21218 non_reference (tparm
)))
21220 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21221 && CP_INTEGRAL_TYPE_P (tparm
))
21222 /* Convert the ARG to the type of PARM; the deduced non-type
21223 template argument must exactly match the types of the
21224 corresponding parameter. */
21225 arg
= fold (build_nop (tparm
, arg
));
21226 else if (uses_template_parms (tparm
))
21228 /* We haven't deduced the type of this parameter yet. */
21229 if (cxx_dialect
>= cxx17
21230 /* We deduce from array bounds in try_array_deduction. */
21231 && !(strict
& UNIFY_ALLOW_INTEGER
))
21233 /* Deduce it from the non-type argument. */
21234 tree atype
= TREE_TYPE (arg
);
21235 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21237 UNIFY_ALLOW_NONE
, explain_p
);
21240 /* Try again later. */
21241 return unify_success (explain_p
);
21244 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21246 /* If ARG is a parameter pack or an expansion, we cannot unify
21247 against it unless PARM is also a parameter pack. */
21248 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21249 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21250 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21253 bool removed_attr
= false;
21254 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21256 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21257 return unify_success (explain_p
);
21261 /* A pointer-to-member constant can be unified only with
21262 another constant. */
21263 if (TREE_CODE (arg
) != PTRMEM_CST
)
21264 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21266 /* Just unify the class member. It would be useless (and possibly
21267 wrong, depending on the strict flags) to unify also
21268 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21269 arg refer to the same variable, even if through different
21270 classes. For instance:
21272 struct A { int x; };
21275 Unification of &A::x and &B::x must succeed. */
21276 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21277 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21282 if (!TYPE_PTR_P (arg
))
21283 return unify_type_mismatch (explain_p
, parm
, arg
);
21285 /* [temp.deduct.call]
21287 A can be another pointer or pointer to member type that can
21288 be converted to the deduced A via a qualification
21289 conversion (_conv.qual_).
21291 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21292 This will allow for additional cv-qualification of the
21293 pointed-to types if appropriate. */
21295 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21296 /* The derived-to-base conversion only persists through one
21297 level of pointers. */
21298 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21300 return unify (tparms
, targs
, TREE_TYPE (parm
),
21301 TREE_TYPE (arg
), strict
, explain_p
);
21304 case REFERENCE_TYPE
:
21305 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
21306 return unify_type_mismatch (explain_p
, parm
, arg
);
21307 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21308 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21311 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21312 return unify_type_mismatch (explain_p
, parm
, arg
);
21313 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21314 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21315 return unify_type_mismatch (explain_p
, parm
, arg
);
21316 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21317 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21318 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21319 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21320 TYPE_DOMAIN (arg
), explain_p
);
21321 return unify_success (explain_p
);
21328 case ENUMERAL_TYPE
:
21331 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21332 return unify_type_mismatch (explain_p
, parm
, arg
);
21334 /* We have already checked cv-qualification at the top of the
21336 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21337 return unify_type_mismatch (explain_p
, parm
, arg
);
21339 /* As far as unification is concerned, this wins. Later checks
21340 will invalidate it if necessary. */
21341 return unify_success (explain_p
);
21343 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21344 /* Type INTEGER_CST can come from ordinary constant template args. */
21346 while (CONVERT_EXPR_P (arg
))
21347 arg
= TREE_OPERAND (arg
, 0);
21349 if (TREE_CODE (arg
) != INTEGER_CST
)
21350 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21351 return (tree_int_cst_equal (parm
, arg
)
21352 ? unify_success (explain_p
)
21353 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21357 int i
, len
, argslen
;
21358 int parm_variadic_p
= 0;
21360 if (TREE_CODE (arg
) != TREE_VEC
)
21361 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21363 len
= TREE_VEC_LENGTH (parm
);
21364 argslen
= TREE_VEC_LENGTH (arg
);
21366 /* Check for pack expansions in the parameters. */
21367 for (i
= 0; i
< len
; ++i
)
21369 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21372 /* We can unify against something with a trailing
21374 parm_variadic_p
= 1;
21376 /* [temp.deduct.type]/9: If the template argument list of
21377 P contains a pack expansion that is not the last
21378 template argument, the entire template argument list
21379 is a non-deduced context. */
21380 return unify_success (explain_p
);
21384 /* If we don't have enough arguments to satisfy the parameters
21385 (not counting the pack expression at the end), or we have
21386 too many arguments for a parameter list that doesn't end in
21387 a pack expression, we can't unify. */
21388 if (parm_variadic_p
21389 ? argslen
< len
- parm_variadic_p
21391 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21393 /* Unify all of the parameters that precede the (optional)
21394 pack expression. */
21395 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21397 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21398 TREE_VEC_ELT (parm
, i
),
21399 TREE_VEC_ELT (arg
, i
),
21400 UNIFY_ALLOW_NONE
, explain_p
);
21402 if (parm_variadic_p
)
21403 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21405 /*subr=*/true, explain_p
);
21406 return unify_success (explain_p
);
21411 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21412 return unify_type_mismatch (explain_p
, parm
, arg
);
21414 if (TYPE_PTRMEMFUNC_P (parm
))
21416 if (!TYPE_PTRMEMFUNC_P (arg
))
21417 return unify_type_mismatch (explain_p
, parm
, arg
);
21419 return unify (tparms
, targs
,
21420 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21421 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21422 strict
, explain_p
);
21424 else if (TYPE_PTRMEMFUNC_P (arg
))
21425 return unify_type_mismatch (explain_p
, parm
, arg
);
21427 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21429 tree t
= NULL_TREE
;
21431 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21433 /* First, we try to unify the PARM and ARG directly. */
21434 t
= try_class_unification (tparms
, targs
,
21435 parm
, arg
, explain_p
);
21439 /* Fallback to the special case allowed in
21440 [temp.deduct.call]:
21442 If P is a class, and P has the form
21443 template-id, then A can be a derived class of
21444 the deduced A. Likewise, if P is a pointer to
21445 a class of the form template-id, A can be a
21446 pointer to a derived class pointed to by the
21448 enum template_base_result r
;
21449 r
= get_template_base (tparms
, targs
, parm
, arg
,
21454 /* Don't give the derived diagnostic if we're
21455 already dealing with the same template. */
21457 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21458 && (CLASSTYPE_TI_TEMPLATE (parm
)
21459 == CLASSTYPE_TI_TEMPLATE (arg
)));
21460 return unify_no_common_base (explain_p
&& !same_template
,
21465 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
21466 && (CLASSTYPE_TI_TEMPLATE (parm
)
21467 == CLASSTYPE_TI_TEMPLATE (arg
)))
21468 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21469 Then, we should unify `int' and `U'. */
21472 /* There's no chance of unification succeeding. */
21473 return unify_type_mismatch (explain_p
, parm
, arg
);
21475 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
21476 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
21478 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
21479 return unify_type_mismatch (explain_p
, parm
, arg
);
21480 return unify_success (explain_p
);
21483 case FUNCTION_TYPE
:
21485 unsigned int nargs
;
21490 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21491 return unify_type_mismatch (explain_p
, parm
, arg
);
21493 /* CV qualifications for methods can never be deduced, they must
21494 match exactly. We need to check them explicitly here,
21495 because type_unification_real treats them as any other
21496 cv-qualified parameter. */
21497 if (TREE_CODE (parm
) == METHOD_TYPE
21498 && (!check_cv_quals_for_unify
21500 class_of_this_parm (arg
),
21501 class_of_this_parm (parm
))))
21502 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21503 if (TREE_CODE (arg
) == FUNCTION_TYPE
21504 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
21505 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21506 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
21507 return unify_type_mismatch (explain_p
, parm
, arg
);
21509 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
21510 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
21512 nargs
= list_length (TYPE_ARG_TYPES (arg
));
21513 args
= XALLOCAVEC (tree
, nargs
);
21514 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
21515 a
!= NULL_TREE
&& a
!= void_list_node
;
21516 a
= TREE_CHAIN (a
), ++i
)
21517 args
[i
] = TREE_VALUE (a
);
21520 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
21521 args
, nargs
, 1, DEDUCE_EXACT
,
21522 LOOKUP_NORMAL
, NULL
, explain_p
))
21525 if (flag_noexcept_type
)
21527 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
21528 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
21529 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
21530 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
21531 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
21532 && uses_template_parms (TREE_PURPOSE (pspec
)))
21533 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
21534 TREE_PURPOSE (aspec
),
21535 UNIFY_ALLOW_NONE
, explain_p
);
21536 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
21537 return unify_type_mismatch (explain_p
, parm
, arg
);
21544 /* Unify a pointer to member with a pointer to member function, which
21545 deduces the type of the member as a function type. */
21546 if (TYPE_PTRMEMFUNC_P (arg
))
21548 /* Check top-level cv qualifiers */
21549 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
21550 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21552 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21553 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
21554 UNIFY_ALLOW_NONE
, explain_p
);
21556 /* Determine the type of the function we are unifying against. */
21557 tree fntype
= static_fn_type (arg
);
21559 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
21562 if (TREE_CODE (arg
) != OFFSET_TYPE
)
21563 return unify_type_mismatch (explain_p
, parm
, arg
);
21564 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
21565 TYPE_OFFSET_BASETYPE (arg
),
21566 UNIFY_ALLOW_NONE
, explain_p
);
21567 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21568 strict
, explain_p
);
21571 if (DECL_TEMPLATE_PARM_P (parm
))
21572 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21573 if (arg
!= scalar_constant_value (parm
))
21574 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21575 return unify_success (explain_p
);
21578 case TEMPLATE_DECL
:
21579 /* Matched cases are handled by the ARG == PARM test above. */
21580 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21583 /* We might get a variable as a non-type template argument in parm if the
21584 corresponding parameter is type-dependent. Make any necessary
21585 adjustments based on whether arg is a reference. */
21586 if (CONSTANT_CLASS_P (arg
))
21587 parm
= fold_non_dependent_expr (parm
);
21588 else if (REFERENCE_REF_P (arg
))
21590 tree sub
= TREE_OPERAND (arg
, 0);
21592 if (TREE_CODE (sub
) == ADDR_EXPR
)
21593 arg
= TREE_OPERAND (sub
, 0);
21595 /* Now use the normal expression code to check whether they match. */
21598 case TYPE_ARGUMENT_PACK
:
21599 case NONTYPE_ARGUMENT_PACK
:
21600 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21601 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21604 case DECLTYPE_TYPE
:
21605 case UNDERLYING_TYPE
:
21606 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21607 or UNDERLYING_TYPE nodes. */
21608 return unify_success (explain_p
);
21611 /* Unification fails if we hit an error node. */
21612 return unify_invalid (explain_p
);
21615 if (REFERENCE_REF_P (parm
))
21617 bool pexp
= PACK_EXPANSION_P (arg
);
21619 arg
= PACK_EXPANSION_PATTERN (arg
);
21620 if (REFERENCE_REF_P (arg
))
21621 arg
= TREE_OPERAND (arg
, 0);
21623 arg
= make_pack_expansion (arg
, complain
);
21624 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21625 strict
, explain_p
);
21630 /* An unresolved overload is a nondeduced context. */
21631 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21632 return unify_success (explain_p
);
21633 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21635 /* We must be looking at an expression. This can happen with
21639 void foo(S<I>, S<I + 2>);
21641 This is a "nondeduced context":
21645 The nondeduced contexts are:
21647 --A type that is a template-id in which one or more of
21648 the template-arguments is an expression that references
21649 a template-parameter.
21651 In these cases, we assume deduction succeeded, but don't
21652 actually infer any unifications. */
21654 if (!uses_template_parms (parm
)
21655 && !template_args_equal (parm
, arg
))
21656 return unify_expression_unequal (explain_p
, parm
, arg
);
21658 return unify_success (explain_p
);
21661 #undef RECUR_AND_CHECK_FAILURE
21663 /* Note that DECL can be defined in this translation unit, if
21667 mark_definable (tree decl
)
21670 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21671 FOR_EACH_CLONE (clone
, decl
)
21672 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21675 /* Called if RESULT is explicitly instantiated, or is a member of an
21676 explicitly instantiated class. */
21679 mark_decl_instantiated (tree result
, int extern_p
)
21681 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21683 /* If this entity has already been written out, it's too late to
21684 make any modifications. */
21685 if (TREE_ASM_WRITTEN (result
))
21688 /* For anonymous namespace we don't need to do anything. */
21689 if (decl_anon_ns_mem_p (result
))
21691 gcc_assert (!TREE_PUBLIC (result
));
21695 if (TREE_CODE (result
) != FUNCTION_DECL
)
21696 /* The TREE_PUBLIC flag for function declarations will have been
21697 set correctly by tsubst. */
21698 TREE_PUBLIC (result
) = 1;
21700 /* This might have been set by an earlier implicit instantiation. */
21701 DECL_COMDAT (result
) = 0;
21704 DECL_NOT_REALLY_EXTERN (result
) = 0;
21707 mark_definable (result
);
21708 mark_needed (result
);
21709 /* Always make artificials weak. */
21710 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21711 comdat_linkage (result
);
21712 /* For WIN32 we also want to put explicit instantiations in
21713 linkonce sections. */
21714 else if (TREE_PUBLIC (result
))
21715 maybe_make_one_only (result
);
21718 /* If EXTERN_P, then this function will not be emitted -- unless
21719 followed by an explicit instantiation, at which point its linkage
21720 will be adjusted. If !EXTERN_P, then this function will be
21721 emitted here. In neither circumstance do we want
21722 import_export_decl to adjust the linkage. */
21723 DECL_INTERFACE_KNOWN (result
) = 1;
21726 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21727 important template arguments. If any are missing, we check whether
21728 they're important by using error_mark_node for substituting into any
21729 args that were used for partial ordering (the ones between ARGS and END)
21730 and seeing if it bubbles up. */
21733 check_undeduced_parms (tree targs
, tree args
, tree end
)
21735 bool found
= false;
21737 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21738 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21741 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21745 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21746 if (substed
== error_mark_node
)
21752 /* Given two function templates PAT1 and PAT2, return:
21754 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21755 -1 if PAT2 is more specialized than PAT1.
21756 0 if neither is more specialized.
21758 LEN indicates the number of parameters we should consider
21759 (defaulted parameters should not be considered).
21761 The 1998 std underspecified function template partial ordering, and
21762 DR214 addresses the issue. We take pairs of arguments, one from
21763 each of the templates, and deduce them against each other. One of
21764 the templates will be more specialized if all the *other*
21765 template's arguments deduce against its arguments and at least one
21766 of its arguments *does* *not* deduce against the other template's
21767 corresponding argument. Deduction is done as for class templates.
21768 The arguments used in deduction have reference and top level cv
21769 qualifiers removed. Iff both arguments were originally reference
21770 types *and* deduction succeeds in both directions, an lvalue reference
21771 wins against an rvalue reference and otherwise the template
21772 with the more cv-qualified argument wins for that pairing (if
21773 neither is more cv-qualified, they both are equal). Unlike regular
21774 deduction, after all the arguments have been deduced in this way,
21775 we do *not* verify the deduced template argument values can be
21776 substituted into non-deduced contexts.
21778 The logic can be a bit confusing here, because we look at deduce1 and
21779 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21780 can find template arguments for pat1 to make arg1 look like arg2, that
21781 means that arg2 is at least as specialized as arg1. */
21784 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21786 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21787 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21788 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21789 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21790 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21791 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21792 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21793 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21794 tree origs1
, origs2
;
21795 bool lose1
= false;
21796 bool lose2
= false;
21798 /* Remove the this parameter from non-static member functions. If
21799 one is a non-static member function and the other is not a static
21800 member function, remove the first parameter from that function
21801 also. This situation occurs for operator functions where we
21802 locate both a member function (with this pointer) and non-member
21803 operator (with explicit first operand). */
21804 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21806 len
--; /* LEN is the number of significant arguments for DECL1 */
21807 args1
= TREE_CHAIN (args1
);
21808 if (!DECL_STATIC_FUNCTION_P (decl2
))
21809 args2
= TREE_CHAIN (args2
);
21811 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21813 args2
= TREE_CHAIN (args2
);
21814 if (!DECL_STATIC_FUNCTION_P (decl1
))
21817 args1
= TREE_CHAIN (args1
);
21821 /* If only one is a conversion operator, they are unordered. */
21822 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21825 /* Consider the return type for a conversion function */
21826 if (DECL_CONV_FN_P (decl1
))
21828 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21829 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21833 processing_template_decl
++;
21839 /* Stop when an ellipsis is seen. */
21840 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21842 tree arg1
= TREE_VALUE (args1
);
21843 tree arg2
= TREE_VALUE (args2
);
21844 int deduce1
, deduce2
;
21850 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21851 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21853 /* When both arguments are pack expansions, we need only
21854 unify the patterns themselves. */
21855 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21856 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21858 /* This is the last comparison we need to do. */
21862 /* DR 1847: If a particular P contains no template-parameters that
21863 participate in template argument deduction, that P is not used to
21864 determine the ordering. */
21865 if (!uses_deducible_template_parms (arg1
)
21866 && !uses_deducible_template_parms (arg2
))
21869 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21871 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21872 arg1
= TREE_TYPE (arg1
);
21873 quals1
= cp_type_quals (arg1
);
21876 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21878 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21879 arg2
= TREE_TYPE (arg2
);
21880 quals2
= cp_type_quals (arg2
);
21883 arg1
= TYPE_MAIN_VARIANT (arg1
);
21884 arg2
= TYPE_MAIN_VARIANT (arg2
);
21886 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21888 int i
, len2
= remaining_arguments (args2
);
21889 tree parmvec
= make_tree_vec (1);
21890 tree argvec
= make_tree_vec (len2
);
21893 /* Setup the parameter vector, which contains only ARG1. */
21894 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21896 /* Setup the argument vector, which contains the remaining
21898 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21899 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21901 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21902 argvec
, DEDUCE_EXACT
,
21903 /*subr=*/true, /*explain_p=*/false)
21906 /* We cannot deduce in the other direction, because ARG1 is
21907 a pack expansion but ARG2 is not. */
21910 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21912 int i
, len1
= remaining_arguments (args1
);
21913 tree parmvec
= make_tree_vec (1);
21914 tree argvec
= make_tree_vec (len1
);
21917 /* Setup the parameter vector, which contains only ARG1. */
21918 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21920 /* Setup the argument vector, which contains the remaining
21922 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21923 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21925 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21926 argvec
, DEDUCE_EXACT
,
21927 /*subr=*/true, /*explain_p=*/false)
21930 /* We cannot deduce in the other direction, because ARG2 is
21931 a pack expansion but ARG1 is not.*/
21937 /* The normal case, where neither argument is a pack
21939 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21940 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21942 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21943 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21947 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21948 arg2, then arg2 is not as specialized as arg1. */
21954 /* "If, for a given type, deduction succeeds in both directions
21955 (i.e., the types are identical after the transformations above)
21956 and both P and A were reference types (before being replaced with
21957 the type referred to above):
21958 - if the type from the argument template was an lvalue reference and
21959 the type from the parameter template was not, the argument type is
21960 considered to be more specialized than the other; otherwise,
21961 - if the type from the argument template is more cv-qualified
21962 than the type from the parameter template (as described above),
21963 the argument type is considered to be more specialized than the other;
21965 - neither type is more specialized than the other." */
21967 if (deduce1
&& deduce2
)
21969 if (ref1
&& ref2
&& ref1
!= ref2
)
21976 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21978 if ((quals1
& quals2
) == quals2
)
21980 if ((quals1
& quals2
) == quals1
)
21985 if (lose1
&& lose2
)
21986 /* We've failed to deduce something in either direction.
21987 These must be unordered. */
21992 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21993 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21994 /* We have already processed all of the arguments in our
21995 handing of the pack expansion type. */
21998 args1
= TREE_CHAIN (args1
);
21999 args2
= TREE_CHAIN (args2
);
22002 /* "In most cases, all template parameters must have values in order for
22003 deduction to succeed, but for partial ordering purposes a template
22004 parameter may remain without a value provided it is not used in the
22005 types being used for partial ordering."
22007 Thus, if we are missing any of the targs1 we need to substitute into
22008 origs1, then pat2 is not as specialized as pat1. This can happen when
22009 there is a nondeduced context. */
22010 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
22012 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
22015 processing_template_decl
--;
22017 /* If both deductions succeed, the partial ordering selects the more
22018 constrained template. */
22019 if (!lose1
&& !lose2
)
22021 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
22022 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
22023 lose1
= !subsumes_constraints (c1
, c2
);
22024 lose2
= !subsumes_constraints (c2
, c1
);
22027 /* All things being equal, if the next argument is a pack expansion
22028 for one function but not for the other, prefer the
22029 non-variadic function. FIXME this is bogus; see c++/41958. */
22031 && args1
&& TREE_VALUE (args1
)
22032 && args2
&& TREE_VALUE (args2
))
22034 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
22035 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
22038 if (lose1
== lose2
)
22046 /* Determine which of two partial specializations of TMPL is more
22049 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22050 to the first partial specialization. The TREE_PURPOSE is the
22051 innermost set of template parameters for the partial
22052 specialization. PAT2 is similar, but for the second template.
22054 Return 1 if the first partial specialization is more specialized;
22055 -1 if the second is more specialized; 0 if neither is more
22058 See [temp.class.order] for information about determining which of
22059 two templates is more specialized. */
22062 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
22066 bool any_deductions
= false;
22068 tree tmpl1
= TREE_VALUE (pat1
);
22069 tree tmpl2
= TREE_VALUE (pat2
);
22070 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
22071 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
22073 /* Just like what happens for functions, if we are ordering between
22074 different template specializations, we may encounter dependent
22075 types in the arguments, and we need our dependency check functions
22076 to behave correctly. */
22077 ++processing_template_decl
;
22078 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
22082 any_deductions
= true;
22085 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
22089 any_deductions
= true;
22091 --processing_template_decl
;
22093 /* If both deductions succeed, the partial ordering selects the more
22094 constrained template. */
22095 if (!winner
&& any_deductions
)
22096 return more_constrained (tmpl1
, tmpl2
);
22098 /* In the case of a tie where at least one of the templates
22099 has a parameter pack at the end, the template with the most
22100 non-packed parameters wins. */
22103 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
22104 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
22106 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
22107 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
22108 int len1
= TREE_VEC_LENGTH (args1
);
22109 int len2
= TREE_VEC_LENGTH (args2
);
22111 /* We don't count the pack expansion at the end. */
22112 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
22114 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
22119 else if (len1
< len2
)
22126 /* Return the template arguments that will produce the function signature
22127 DECL from the function template FN, with the explicit template
22128 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22129 also match. Return NULL_TREE if no satisfactory arguments could be
22133 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
22135 int ntparms
= DECL_NTPARMS (fn
);
22136 tree targs
= make_tree_vec (ntparms
);
22137 tree decl_type
= TREE_TYPE (decl
);
22138 tree decl_arg_types
;
22140 unsigned int nargs
, ix
;
22143 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22145 /* Never do unification on the 'this' parameter. */
22146 decl_arg_types
= skip_artificial_parms_for (decl
,
22147 TYPE_ARG_TYPES (decl_type
));
22149 nargs
= list_length (decl_arg_types
);
22150 args
= XALLOCAVEC (tree
, nargs
);
22151 for (arg
= decl_arg_types
, ix
= 0;
22152 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22153 arg
= TREE_CHAIN (arg
), ++ix
)
22154 args
[ix
] = TREE_VALUE (arg
);
22156 if (fn_type_unification (fn
, explicit_args
, targs
,
22158 (check_rettype
|| DECL_CONV_FN_P (fn
)
22159 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22160 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
22162 == error_mark_node
)
22168 /* Return the innermost template arguments that, when applied to a partial
22169 specialization SPEC_TMPL of TMPL, yield the ARGS.
22171 For example, suppose we have:
22173 template <class T, class U> struct S {};
22174 template <class T> struct S<T*, int> {};
22176 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22177 partial specialization and the ARGS will be {double*, int}. The resulting
22178 vector will be {double}, indicating that `T' is bound to `double'. */
22181 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22183 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22185 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22186 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22188 tree innermost_deduced_args
;
22190 innermost_deduced_args
= make_tree_vec (ntparms
);
22191 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22193 deduced_args
= copy_node (args
);
22194 SET_TMPL_ARGS_LEVEL (deduced_args
,
22195 TMPL_ARGS_DEPTH (deduced_args
),
22196 innermost_deduced_args
);
22199 deduced_args
= innermost_deduced_args
;
22201 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22203 if (unify (tparms
, deduced_args
,
22204 INNERMOST_TEMPLATE_ARGS (spec_args
),
22205 INNERMOST_TEMPLATE_ARGS (args
),
22206 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22209 for (i
= 0; i
< ntparms
; ++i
)
22210 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22212 if (!tried_array_deduction
)
22214 try_array_deduction (tparms
, innermost_deduced_args
,
22215 INNERMOST_TEMPLATE_ARGS (spec_args
));
22216 tried_array_deduction
= true;
22217 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22223 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
22224 if (!push_tinst_level (tinst
))
22226 excessive_deduction_depth
= true;
22230 /* Verify that nondeduced template arguments agree with the type
22231 obtained from argument deduction.
22235 struct A { typedef int X; };
22236 template <class T, class U> struct C {};
22237 template <class T> struct C<T, typename T::X> {};
22239 Then with the instantiation `C<A, int>', we can deduce that
22240 `T' is `A' but unify () does not check whether `typename T::X'
22242 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22244 if (spec_args
!= error_mark_node
)
22245 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22246 INNERMOST_TEMPLATE_ARGS (spec_args
),
22247 tmpl
, tf_none
, false, false);
22249 pop_tinst_level ();
22251 if (spec_args
== error_mark_node
22252 /* We only need to check the innermost arguments; the other
22253 arguments will always agree. */
22254 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22255 INNERMOST_TEMPLATE_ARGS (args
)))
22258 /* Now that we have bindings for all of the template arguments,
22259 ensure that the arguments deduced for the template template
22260 parameters have compatible template parameter lists. See the use
22261 of template_template_parm_bindings_ok_p in fn_type_unification
22262 for more information. */
22263 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22266 return deduced_args
;
22269 // Compare two function templates T1 and T2 by deducing bindings
22270 // from one against the other. If both deductions succeed, compare
22271 // constraints to see which is more constrained.
22273 more_specialized_inst (tree t1
, tree t2
)
22278 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22284 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22290 // If both deductions succeed, then one may be more constrained.
22291 if (count
== 2 && fate
== 0)
22292 fate
= more_constrained (t1
, t2
);
22297 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22298 Return the TREE_LIST node with the most specialized template, if
22299 any. If there is no most specialized template, the error_mark_node
22302 Note that this function does not look at, or modify, the
22303 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22304 returned is one of the elements of INSTANTIATIONS, callers may
22305 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22306 and retrieve it from the value returned. */
22309 most_specialized_instantiation (tree templates
)
22313 ++processing_template_decl
;
22316 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22318 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22319 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22324 /* Equally specialized, move to next function. If there
22325 is no next function, nothing's most specialized. */
22326 fn
= TREE_CHAIN (fn
);
22334 /* Now verify that champ is better than everything earlier in the
22335 instantiation list. */
22336 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22337 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22344 processing_template_decl
--;
22347 return error_mark_node
;
22352 /* If DECL is a specialization of some template, return the most
22353 general such template. Otherwise, returns NULL_TREE.
22355 For example, given:
22357 template <class T> struct S { template <class U> void f(U); };
22359 if TMPL is `template <class U> void S<int>::f(U)' this will return
22360 the full template. This function will not trace past partial
22361 specializations, however. For example, given in addition:
22363 template <class T> struct S<T*> { template <class U> void f(U); };
22365 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22366 `template <class T> template <class U> S<T*>::f(U)'. */
22369 most_general_template (tree decl
)
22371 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22373 if (tree tinfo
= get_template_info (decl
))
22374 decl
= TI_TEMPLATE (tinfo
);
22375 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22376 template friend, or a FIELD_DECL for a capture pack. */
22377 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22381 /* Look for more and more general templates. */
22382 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22384 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22385 (See cp-tree.h for details.) */
22386 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22389 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22390 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22391 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22394 /* Stop if we run into an explicitly specialized class template. */
22395 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22396 && DECL_CONTEXT (decl
)
22397 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22400 decl
= DECL_TI_TEMPLATE (decl
);
22406 /* Return the most specialized of the template partial specializations
22407 which can produce TARGET, a specialization of some class or variable
22408 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22409 a TEMPLATE_DECL node corresponding to the partial specialization, while
22410 the TREE_PURPOSE is the set of template arguments that must be
22411 substituted into the template pattern in order to generate TARGET.
22413 If the choice of partial specialization is ambiguous, a diagnostic
22414 is issued, and the error_mark_node is returned. If there are no
22415 partial specializations matching TARGET, then NULL_TREE is
22416 returned, indicating that the primary template should be used. */
22419 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22421 tree list
= NULL_TREE
;
22426 tree outer_args
= NULL_TREE
;
22429 if (TYPE_P (target
))
22431 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22432 tmpl
= TI_TEMPLATE (tinfo
);
22433 args
= TI_ARGS (tinfo
);
22435 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22437 tmpl
= TREE_OPERAND (target
, 0);
22438 args
= TREE_OPERAND (target
, 1);
22440 else if (VAR_P (target
))
22442 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22443 tmpl
= TI_TEMPLATE (tinfo
);
22444 args
= TI_ARGS (tinfo
);
22447 gcc_unreachable ();
22449 tree main_tmpl
= most_general_template (tmpl
);
22451 /* For determining which partial specialization to use, only the
22452 innermost args are interesting. */
22453 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22455 outer_args
= strip_innermost_template_args (args
, 1);
22456 args
= INNERMOST_TEMPLATE_ARGS (args
);
22459 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
22462 tree spec_tmpl
= TREE_VALUE (t
);
22466 /* Substitute in the template args from the enclosing class. */
22467 ++processing_template_decl
;
22468 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
22469 --processing_template_decl
;
22472 if (spec_tmpl
== error_mark_node
)
22473 return error_mark_node
;
22475 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
22479 spec_args
= add_to_template_args (outer_args
, spec_args
);
22481 /* Keep the candidate only if the constraints are satisfied,
22482 or if we're not compiling with concepts. */
22484 || constraints_satisfied_p (spec_tmpl
, spec_args
))
22486 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
22487 TREE_TYPE (list
) = TREE_TYPE (t
);
22495 ambiguous_p
= false;
22498 t
= TREE_CHAIN (t
);
22499 for (; t
; t
= TREE_CHAIN (t
))
22501 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22508 t
= TREE_CHAIN (t
);
22511 ambiguous_p
= true;
22520 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
22522 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
22525 ambiguous_p
= true;
22533 char *spaces
= NULL
;
22534 if (!(complain
& tf_error
))
22535 return error_mark_node
;
22536 if (TYPE_P (target
))
22537 error ("ambiguous template instantiation for %q#T", target
);
22539 error ("ambiguous template instantiation for %q#D", target
);
22540 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
22541 for (t
= list
; t
; t
= TREE_CHAIN (t
))
22543 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
22544 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
22545 "%s %#qS", spaces
? spaces
: str
, subst
);
22546 spaces
= spaces
? spaces
: get_spaces (str
);
22549 return error_mark_node
;
22555 /* Explicitly instantiate DECL. */
22558 do_decl_instantiation (tree decl
, tree storage
)
22560 tree result
= NULL_TREE
;
22563 if (!decl
|| decl
== error_mark_node
)
22564 /* An error occurred, for which grokdeclarator has already issued
22565 an appropriate message. */
22567 else if (! DECL_LANG_SPECIFIC (decl
))
22569 error ("explicit instantiation of non-template %q#D", decl
);
22573 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22574 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22576 if (VAR_P (decl
) && !var_templ
)
22578 /* There is an asymmetry here in the way VAR_DECLs and
22579 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22580 the latter, the DECL we get back will be marked as a
22581 template instantiation, and the appropriate
22582 DECL_TEMPLATE_INFO will be set up. This does not happen for
22583 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22584 should handle VAR_DECLs as it currently handles
22586 if (!DECL_CLASS_SCOPE_P (decl
))
22588 error ("%qD is not a static data member of a class template", decl
);
22591 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22592 if (!result
|| !VAR_P (result
))
22594 error ("no matching template for %qD found", decl
);
22597 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22599 error ("type %qT for explicit instantiation %qD does not match "
22600 "declared type %qT", TREE_TYPE (result
), decl
,
22605 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22607 error ("explicit instantiation of %q#D", decl
);
22613 /* Check for various error cases. Note that if the explicit
22614 instantiation is valid the RESULT will currently be marked as an
22615 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22616 until we get here. */
22618 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22620 /* DR 259 [temp.spec].
22622 Both an explicit instantiation and a declaration of an explicit
22623 specialization shall not appear in a program unless the explicit
22624 instantiation follows a declaration of the explicit specialization.
22626 For a given set of template parameters, if an explicit
22627 instantiation of a template appears after a declaration of an
22628 explicit specialization for that template, the explicit
22629 instantiation has no effect. */
22632 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22636 No program shall explicitly instantiate any template more
22639 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22640 the first instantiation was `extern' and the second is not,
22641 and EXTERN_P for the opposite case. */
22642 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22643 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22644 /* If an "extern" explicit instantiation follows an ordinary
22645 explicit instantiation, the template is instantiated. */
22649 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22651 error ("no matching template for %qD found", result
);
22654 else if (!DECL_TEMPLATE_INFO (result
))
22656 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22660 if (storage
== NULL_TREE
)
22662 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22664 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22665 pedwarn (input_location
, OPT_Wpedantic
,
22666 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22671 error ("storage class %qD applied to template instantiation", storage
);
22673 check_explicit_instantiation_namespace (result
);
22674 mark_decl_instantiated (result
, extern_p
);
22676 instantiate_decl (result
, /*defer_ok=*/true,
22677 /*expl_inst_class_mem_p=*/false);
22681 mark_class_instantiated (tree t
, int extern_p
)
22683 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22684 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22685 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22686 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22689 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22690 rest_of_type_compilation (t
, 1);
22694 /* Called from do_type_instantiation through binding_table_foreach to
22695 do recursive instantiation for the type bound in ENTRY. */
22697 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22699 tree storage
= *(tree
*) data
;
22701 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22702 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22703 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22706 /* Perform an explicit instantiation of template class T. STORAGE, if
22707 non-null, is the RID for extern, inline or static. COMPLAIN is
22708 nonzero if this is called from the parser, zero if called recursively,
22709 since the standard is unclear (as detailed below). */
22712 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22717 int previous_instantiation_extern_p
= 0;
22719 if (TREE_CODE (t
) == TYPE_DECL
)
22722 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22725 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22727 error ("explicit instantiation of non-class template %qD", tmpl
);
22729 error ("explicit instantiation of non-template type %qT", t
);
22735 if (!COMPLETE_TYPE_P (t
))
22737 if (complain
& tf_error
)
22738 error ("explicit instantiation of %q#T before definition of template",
22743 if (storage
!= NULL_TREE
)
22745 if (!in_system_header_at (input_location
))
22747 if (storage
== ridpointers
[(int) RID_EXTERN
])
22749 if (cxx_dialect
== cxx98
)
22750 pedwarn (input_location
, OPT_Wpedantic
,
22751 "ISO C++ 1998 forbids the use of %<extern%> on "
22752 "explicit instantiations");
22755 pedwarn (input_location
, OPT_Wpedantic
,
22756 "ISO C++ forbids the use of %qE"
22757 " on explicit instantiations", storage
);
22760 if (storage
== ridpointers
[(int) RID_INLINE
])
22762 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22764 else if (storage
== ridpointers
[(int) RID_STATIC
])
22768 error ("storage class %qD applied to template instantiation",
22774 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22776 /* DR 259 [temp.spec].
22778 Both an explicit instantiation and a declaration of an explicit
22779 specialization shall not appear in a program unless the explicit
22780 instantiation follows a declaration of the explicit specialization.
22782 For a given set of template parameters, if an explicit
22783 instantiation of a template appears after a declaration of an
22784 explicit specialization for that template, the explicit
22785 instantiation has no effect. */
22788 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22792 No program shall explicitly instantiate any template more
22795 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22796 instantiation was `extern'. If EXTERN_P then the second is.
22797 These cases are OK. */
22798 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22800 if (!previous_instantiation_extern_p
&& !extern_p
22801 && (complain
& tf_error
))
22802 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22804 /* If we've already instantiated the template, just return now. */
22805 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22809 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22810 mark_class_instantiated (t
, extern_p
);
22815 /* In contrast to implicit instantiation, where only the
22816 declarations, and not the definitions, of members are
22817 instantiated, we have here:
22821 The explicit instantiation of a class template specialization
22822 implies the instantiation of all of its members not
22823 previously explicitly specialized in the translation unit
22824 containing the explicit instantiation.
22826 Of course, we can't instantiate member template classes, since we
22827 don't have any arguments for them. Note that the standard is
22828 unclear on whether the instantiation of the members are
22829 *explicit* instantiations or not. However, the most natural
22830 interpretation is that it should be an explicit
22832 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
22834 || (TREE_CODE (fld
) == FUNCTION_DECL
22836 && user_provided_p (fld
)))
22837 && DECL_TEMPLATE_INSTANTIATION (fld
))
22839 mark_decl_instantiated (fld
, extern_p
);
22841 instantiate_decl (fld
, /*defer_ok=*/true,
22842 /*expl_inst_class_mem_p=*/true);
22845 if (CLASSTYPE_NESTED_UTDS (t
))
22846 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22847 bt_instantiate_type_proc
, &storage
);
22850 /* Given a function DECL, which is a specialization of TMPL, modify
22851 DECL to be a re-instantiation of TMPL with the same template
22852 arguments. TMPL should be the template into which tsubst'ing
22853 should occur for DECL, not the most general template.
22855 One reason for doing this is a scenario like this:
22858 void f(const T&, int i);
22860 void g() { f(3, 7); }
22863 void f(const T& t, const int i) { }
22865 Note that when the template is first instantiated, with
22866 instantiate_template, the resulting DECL will have no name for the
22867 first parameter, and the wrong type for the second. So, when we go
22868 to instantiate the DECL, we regenerate it. */
22871 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22873 /* The arguments used to instantiate DECL, from the most general
22877 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22879 /* Make sure that we can see identifiers, and compute access
22881 push_access_scope (decl
);
22883 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22891 args_depth
= TMPL_ARGS_DEPTH (args
);
22892 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22893 if (args_depth
> parms_depth
)
22894 args
= get_innermost_template_args (args
, parms_depth
);
22896 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22897 args
, tf_error
, NULL_TREE
,
22898 /*defer_ok*/false);
22899 if (specs
&& specs
!= error_mark_node
)
22900 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22903 /* Merge parameter declarations. */
22904 decl_parm
= skip_artificial_parms_for (decl
,
22905 DECL_ARGUMENTS (decl
));
22907 = skip_artificial_parms_for (code_pattern
,
22908 DECL_ARGUMENTS (code_pattern
));
22909 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22914 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22915 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22916 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22918 parm_type
= type_decays_to (parm_type
);
22919 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22920 TREE_TYPE (decl_parm
) = parm_type
;
22921 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22922 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22924 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22925 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22927 decl_parm
= DECL_CHAIN (decl_parm
);
22928 pattern_parm
= DECL_CHAIN (pattern_parm
);
22930 /* Merge any parameters that match with the function parameter
22932 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22935 tree expanded_types
;
22936 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22937 the parameters in this function parameter pack. */
22938 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22939 args
, tf_error
, NULL_TREE
);
22940 len
= TREE_VEC_LENGTH (expanded_types
);
22941 for (i
= 0; i
< len
; i
++)
22946 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22947 /* Rename the parameter to include the index. */
22948 DECL_NAME (decl_parm
) =
22949 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22950 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22951 parm_type
= type_decays_to (parm_type
);
22952 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22953 TREE_TYPE (decl_parm
) = parm_type
;
22954 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22955 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22957 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22958 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22960 decl_parm
= DECL_CHAIN (decl_parm
);
22963 /* Merge additional specifiers from the CODE_PATTERN. */
22964 if (DECL_DECLARED_INLINE_P (code_pattern
)
22965 && !DECL_DECLARED_INLINE_P (decl
))
22966 DECL_DECLARED_INLINE_P (decl
) = 1;
22968 else if (VAR_P (decl
))
22970 start_lambda_scope (decl
);
22971 DECL_INITIAL (decl
) =
22972 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22973 tf_error
, DECL_TI_TEMPLATE (decl
),
22974 /*integral_constant_expression_p=*/false);
22975 finish_lambda_scope ();
22976 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22977 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22978 tf_error
, DECL_TI_TEMPLATE (decl
));
22981 gcc_unreachable ();
22983 pop_access_scope (decl
);
22986 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22987 substituted to get DECL. */
22990 template_for_substitution (tree decl
)
22992 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22994 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22995 for the instantiation. This is not always the most general
22996 template. Consider, for example:
22999 struct S { template <class U> void f();
23000 template <> void f<int>(); };
23002 and an instantiation of S<double>::f<int>. We want TD to be the
23003 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23004 while (/* An instantiation cannot have a definition, so we need a
23005 more general template. */
23006 DECL_TEMPLATE_INSTANTIATION (tmpl
)
23007 /* We must also deal with friend templates. Given:
23009 template <class T> struct S {
23010 template <class U> friend void f() {};
23013 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23014 so far as the language is concerned, but that's still
23015 where we get the pattern for the instantiation from. On
23016 other hand, if the definition comes outside the class, say:
23018 template <class T> struct S {
23019 template <class U> friend void f();
23021 template <class U> friend void f() {}
23023 we don't need to look any further. That's what the check for
23024 DECL_INITIAL is for. */
23025 || (TREE_CODE (decl
) == FUNCTION_DECL
23026 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
23027 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
23029 /* The present template, TD, should not be a definition. If it
23030 were a definition, we should be using it! Note that we
23031 cannot restructure the loop to just keep going until we find
23032 a template with a definition, since that might go too far if
23033 a specialization was declared, but not defined. */
23035 /* Fetch the more general template. */
23036 tmpl
= DECL_TI_TEMPLATE (tmpl
);
23042 /* Returns true if we need to instantiate this template instance even if we
23043 know we aren't going to emit it. */
23046 always_instantiate_p (tree decl
)
23048 /* We always instantiate inline functions so that we can inline them. An
23049 explicit instantiation declaration prohibits implicit instantiation of
23050 non-inline functions. With high levels of optimization, we would
23051 normally inline non-inline functions -- but we're not allowed to do
23052 that for "extern template" functions. Therefore, we check
23053 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23054 return ((TREE_CODE (decl
) == FUNCTION_DECL
23055 && (DECL_DECLARED_INLINE_P (decl
)
23056 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
23057 /* And we need to instantiate static data members so that
23058 their initializers are available in integral constant
23061 && decl_maybe_constant_var_p (decl
)));
23064 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23065 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23066 error, true otherwise. */
23069 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
23071 tree fntype
, spec
, noex
, clone
;
23073 /* Don't instantiate a noexcept-specification from template context. */
23074 if (processing_template_decl
)
23077 if (DECL_CLONED_FUNCTION_P (fn
))
23078 fn
= DECL_CLONED_FUNCTION (fn
);
23079 fntype
= TREE_TYPE (fn
);
23080 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
23082 if (!spec
|| !TREE_PURPOSE (spec
))
23085 noex
= TREE_PURPOSE (spec
);
23087 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
23089 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
23090 bool added
= false;
23091 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
23092 spec
= get_defaulted_eh_spec (fn
, complain
);
23093 else if (!(added
= !fns
->add (fn
)))
23095 /* If hash_set::add returns true, the element was already there. */
23096 location_t loc
= EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex
),
23097 DECL_SOURCE_LOCATION (fn
));
23099 "exception specification of %qD depends on itself",
23101 spec
= noexcept_false_spec
;
23103 else if (push_tinst_level (fn
))
23105 push_access_scope (fn
);
23106 push_deferring_access_checks (dk_no_deferred
);
23107 input_location
= DECL_SOURCE_LOCATION (fn
);
23108 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
23109 DEFERRED_NOEXCEPT_ARGS (noex
),
23110 tf_warning_or_error
, fn
,
23111 /*function_p=*/false,
23112 /*integral_constant_expression_p=*/true);
23113 pop_deferring_access_checks ();
23114 pop_access_scope (fn
);
23115 pop_tinst_level ();
23116 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
23117 if (spec
== error_mark_node
)
23118 spec
= noexcept_false_spec
;
23121 spec
= noexcept_false_spec
;
23126 if (spec
== error_mark_node
)
23129 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
23132 FOR_EACH_CLONE (clone
, fn
)
23134 if (TREE_TYPE (clone
) == fntype
)
23135 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23137 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23143 /* We're starting to process the function INST, an instantiation of PATTERN;
23144 add their parameters to local_specializations. */
23147 register_parameter_specializations (tree pattern
, tree inst
)
23149 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23150 tree spec_parm
= DECL_ARGUMENTS (inst
);
23151 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23153 register_local_specialization (spec_parm
, tmpl_parm
);
23154 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23155 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23157 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23159 if (!DECL_PACK_P (tmpl_parm
))
23161 register_local_specialization (spec_parm
, tmpl_parm
);
23162 spec_parm
= DECL_CHAIN (spec_parm
);
23166 /* Register the (value) argument pack as a specialization of
23167 TMPL_PARM, then move on. */
23168 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23169 register_local_specialization (argpack
, tmpl_parm
);
23172 gcc_assert (!spec_parm
);
23175 /* Produce the definition of D, a _DECL generated from a template. If
23176 DEFER_OK is true, then we don't have to actually do the
23177 instantiation now; we just have to do it sometime. Normally it is
23178 an error if this is an explicit instantiation but D is undefined.
23179 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23180 instantiated class template. */
23183 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23185 tree tmpl
= DECL_TI_TEMPLATE (d
);
23192 bool pattern_defined
;
23193 location_t saved_loc
= input_location
;
23194 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23195 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23199 /* This function should only be used to instantiate templates for
23200 functions and static member variables. */
23201 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23203 /* A concept is never instantiated. */
23204 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23206 /* Variables are never deferred; if instantiation is required, they
23207 are instantiated right away. That allows for better code in the
23208 case that an expression refers to the value of the variable --
23209 if the variable has a constant value the referring expression can
23210 take advantage of that fact. */
23214 /* Don't instantiate cloned functions. Instead, instantiate the
23215 functions they cloned. */
23216 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23217 d
= DECL_CLONED_FUNCTION (d
);
23219 if (DECL_TEMPLATE_INSTANTIATED (d
)
23220 || (TREE_CODE (d
) == FUNCTION_DECL
23221 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23222 || DECL_TEMPLATE_SPECIALIZATION (d
))
23223 /* D has already been instantiated or explicitly specialized, so
23224 there's nothing for us to do here.
23226 It might seem reasonable to check whether or not D is an explicit
23227 instantiation, and, if so, stop here. But when an explicit
23228 instantiation is deferred until the end of the compilation,
23229 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23230 the instantiation. */
23233 /* Check to see whether we know that this template will be
23234 instantiated in some other file, as with "extern template"
23236 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23238 /* In general, we do not instantiate such templates. */
23239 if (external_p
&& !always_instantiate_p (d
))
23242 gen_tmpl
= most_general_template (tmpl
);
23243 gen_args
= DECL_TI_ARGS (d
);
23245 if (tmpl
!= gen_tmpl
)
23246 /* We should already have the extra args. */
23247 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23248 == TMPL_ARGS_DEPTH (gen_args
));
23249 /* And what's in the hash table should match D. */
23250 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23251 || spec
== NULL_TREE
);
23253 /* This needs to happen before any tsubsting. */
23254 if (! push_tinst_level (d
))
23257 timevar_push (TV_TEMPLATE_INST
);
23259 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23260 for the instantiation. */
23261 td
= template_for_substitution (d
);
23266 /* Look up an explicit specialization, if any. */
23267 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23268 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23269 if (elt
&& elt
!= error_mark_node
)
23271 td
= TREE_VALUE (elt
);
23272 args
= TREE_PURPOSE (elt
);
23276 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23278 /* We should never be trying to instantiate a member of a class
23279 template or partial specialization. */
23280 gcc_assert (d
!= code_pattern
);
23282 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23283 || DECL_TEMPLATE_SPECIALIZATION (td
))
23284 /* In the case of a friend template whose definition is provided
23285 outside the class, we may have too many arguments. Drop the
23286 ones we don't need. The same is true for specializations. */
23287 args
= get_innermost_template_args
23288 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23290 if (TREE_CODE (d
) == FUNCTION_DECL
)
23292 deleted_p
= DECL_DELETED_FN (code_pattern
);
23293 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23294 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23295 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
23301 if (DECL_CLASS_SCOPE_P (code_pattern
))
23302 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23303 || DECL_INLINE_VAR_P (code_pattern
));
23305 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23308 /* We may be in the middle of deferred access check. Disable it now. */
23309 push_deferring_access_checks (dk_no_deferred
);
23311 /* Unless an explicit instantiation directive has already determined
23312 the linkage of D, remember that a definition is available for
23314 if (pattern_defined
23315 && !DECL_INTERFACE_KNOWN (d
)
23316 && !DECL_NOT_REALLY_EXTERN (d
))
23317 mark_definable (d
);
23319 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23320 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23321 input_location
= DECL_SOURCE_LOCATION (d
);
23323 /* If D is a member of an explicitly instantiated class template,
23324 and no definition is available, treat it like an implicit
23326 if (!pattern_defined
&& expl_inst_class_mem_p
23327 && DECL_EXPLICIT_INSTANTIATION (d
))
23329 /* Leave linkage flags alone on instantiations with anonymous
23331 if (TREE_PUBLIC (d
))
23333 DECL_NOT_REALLY_EXTERN (d
) = 0;
23334 DECL_INTERFACE_KNOWN (d
) = 0;
23336 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23339 /* Defer all other templates, unless we have been explicitly
23340 forbidden from doing so. */
23341 if (/* If there is no definition, we cannot instantiate the
23344 /* If it's OK to postpone instantiation, do so. */
23346 /* If this is a static data member that will be defined
23347 elsewhere, we don't want to instantiate the entire data
23348 member, but we do want to instantiate the initializer so that
23349 we can substitute that elsewhere. */
23350 || (external_p
&& VAR_P (d
))
23351 /* Handle here a deleted function too, avoid generating
23352 its body (c++/61080). */
23355 /* The definition of the static data member is now required so
23356 we must substitute the initializer. */
23358 && !DECL_INITIAL (d
)
23359 && DECL_INITIAL (code_pattern
))
23363 bool const_init
= false;
23364 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23366 ns
= decl_namespace_context (d
);
23367 push_nested_namespace (ns
);
23369 push_nested_class (DECL_CONTEXT (d
));
23370 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23372 tf_warning_or_error
, NULL_TREE
,
23373 /*integral_constant_expression_p=*/false);
23374 /* If instantiating the initializer involved instantiating this
23375 again, don't call cp_finish_decl twice. */
23376 if (!DECL_INITIAL (d
))
23378 /* Make sure the initializer is still constant, in case of
23379 circular dependency (template/instantiate6.C). */
23381 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23382 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23383 /*asmspec_tree=*/NULL_TREE
,
23384 LOOKUP_ONLYCONVERTING
);
23387 pop_nested_class ();
23388 pop_nested_namespace (ns
);
23391 /* We restore the source position here because it's used by
23392 add_pending_template. */
23393 input_location
= saved_loc
;
23395 if (at_eof
&& !pattern_defined
23396 && DECL_EXPLICIT_INSTANTIATION (d
)
23397 && DECL_NOT_REALLY_EXTERN (d
))
23400 The definition of a non-exported function template, a
23401 non-exported member function template, or a non-exported
23402 member function or static data member of a class template
23403 shall be present in every translation unit in which it is
23404 explicitly instantiated. */
23405 permerror (input_location
, "explicit instantiation of %qD "
23406 "but no definition available", d
);
23408 /* If we're in unevaluated context, we just wanted to get the
23409 constant value; this isn't an odr use, so don't queue
23410 a full instantiation. */
23411 if (cp_unevaluated_operand
!= 0)
23413 /* ??? Historically, we have instantiated inline functions, even
23414 when marked as "extern template". */
23415 if (!(external_p
&& VAR_P (d
)))
23416 add_pending_template (d
);
23419 /* Tell the repository that D is available in this translation unit
23420 -- and see if it is supposed to be instantiated here. */
23421 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23423 /* In a PCH file, despite the fact that the repository hasn't
23424 requested instantiation in the PCH it is still possible that
23425 an instantiation will be required in a file that includes the
23428 add_pending_template (d
);
23429 /* Instantiate inline functions so that the inliner can do its
23430 job, even though we'll not be emitting a copy of this
23432 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23436 bool push_to_top
, nested
;
23438 fn_context
= decl_function_context (d
);
23439 nested
= current_function_decl
!= NULL_TREE
;
23440 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23442 vec
<tree
> omp_privatization_save
;
23444 save_omp_privatization_clauses (omp_privatization_save
);
23447 push_to_top_level ();
23450 push_function_context ();
23451 cp_unevaluated_operand
= 0;
23452 c_inhibit_evaluation_warnings
= 0;
23455 /* Mark D as instantiated so that recursive calls to
23456 instantiate_decl do not try to instantiate it again. */
23457 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
23459 /* Regenerate the declaration in case the template has been modified
23460 by a subsequent redeclaration. */
23461 regenerate_decl_from_template (d
, td
, args
);
23463 /* We already set the file and line above. Reset them now in case
23464 they changed as a result of calling regenerate_decl_from_template. */
23465 input_location
= DECL_SOURCE_LOCATION (d
);
23470 bool const_init
= false;
23472 /* Clear out DECL_RTL; whatever was there before may not be right
23473 since we've reset the type of the declaration. */
23474 SET_DECL_RTL (d
, NULL
);
23475 DECL_IN_AGGR_P (d
) = 0;
23477 /* The initializer is placed in DECL_INITIAL by
23478 regenerate_decl_from_template so we don't need to
23479 push/pop_access_scope again here. Pull it out so that
23480 cp_finish_decl can process it. */
23481 init
= DECL_INITIAL (d
);
23482 DECL_INITIAL (d
) = NULL_TREE
;
23483 DECL_INITIALIZED_P (d
) = 0;
23485 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23486 initializer. That function will defer actual emission until
23487 we have a chance to determine linkage. */
23488 DECL_EXTERNAL (d
) = 0;
23490 /* Enter the scope of D so that access-checking works correctly. */
23491 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23493 push_nested_class (DECL_CONTEXT (d
));
23495 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23496 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
23499 pop_nested_class ();
23501 if (variable_template_p (gen_tmpl
))
23502 note_variable_template_instantiation (d
);
23504 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
23505 synthesize_method (d
);
23506 else if (TREE_CODE (d
) == FUNCTION_DECL
)
23508 /* Set up the list of local specializations. */
23509 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
23510 tree block
= NULL_TREE
;
23512 /* Set up context. */
23513 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23514 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23515 block
= push_stmt_list ();
23517 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
23519 /* Some typedefs referenced from within the template code need to be
23520 access checked at template instantiation time, i.e now. These
23521 types were added to the template at parsing time. Let's get those
23522 and perform the access checks then. */
23523 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
23526 /* Create substitution entries for the parameters. */
23527 register_parameter_specializations (code_pattern
, d
);
23529 /* Substitute into the body of the function. */
23530 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23531 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
23532 tf_warning_or_error
, tmpl
);
23535 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
23536 tf_warning_or_error
, tmpl
,
23537 /*integral_constant_expression_p=*/false);
23539 /* Set the current input_location to the end of the function
23540 so that finish_function knows where we are. */
23542 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
23544 /* Remember if we saw an infinite loop in the template. */
23545 current_function_infinite_loop
23546 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
23549 /* Finish the function. */
23550 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
23551 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
23552 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
23555 d
= finish_function (/*inline_p=*/false);
23556 expand_or_defer_fn (d
);
23559 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23560 cp_check_omp_declare_reduction (d
);
23563 /* We're not deferring instantiation any more. */
23564 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23567 pop_from_top_level ();
23569 pop_function_context ();
23572 restore_omp_privatization_clauses (omp_privatization_save
);
23575 pop_deferring_access_checks ();
23576 timevar_pop (TV_TEMPLATE_INST
);
23577 pop_tinst_level ();
23578 input_location
= saved_loc
;
23579 cp_unevaluated_operand
= saved_unevaluated_operand
;
23580 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23585 /* Run through the list of templates that we wish we could
23586 instantiate, and instantiate any we can. RETRIES is the
23587 number of times we retry pending template instantiation. */
23590 instantiate_pending_templates (int retries
)
23593 location_t saved_loc
= input_location
;
23595 /* Instantiating templates may trigger vtable generation. This in turn
23596 may require further template instantiations. We place a limit here
23597 to avoid infinite loop. */
23598 if (pending_templates
&& retries
>= max_tinst_depth
)
23600 tree decl
= pending_templates
->tinst
->decl
;
23602 fatal_error (input_location
,
23603 "template instantiation depth exceeds maximum of %d"
23604 " instantiating %q+D, possibly from virtual table generation"
23605 " (use -ftemplate-depth= to increase the maximum)",
23606 max_tinst_depth
, decl
);
23607 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23608 /* Pretend that we defined it. */
23609 DECL_INITIAL (decl
) = error_mark_node
;
23615 struct pending_template
**t
= &pending_templates
;
23616 struct pending_template
*last
= NULL
;
23620 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23621 bool complete
= false;
23623 if (TYPE_P (instantiation
))
23625 if (!COMPLETE_TYPE_P (instantiation
))
23627 instantiate_class_template (instantiation
);
23628 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23629 for (tree fld
= TYPE_FIELDS (instantiation
);
23630 fld
; fld
= TREE_CHAIN (fld
))
23632 || (TREE_CODE (fld
) == FUNCTION_DECL
23633 && !DECL_ARTIFICIAL (fld
)))
23634 && DECL_TEMPLATE_INSTANTIATION (fld
))
23635 instantiate_decl (fld
,
23636 /*defer_ok=*/false,
23637 /*expl_inst_class_mem_p=*/false);
23639 if (COMPLETE_TYPE_P (instantiation
))
23643 complete
= COMPLETE_TYPE_P (instantiation
);
23647 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23648 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23651 = instantiate_decl (instantiation
,
23652 /*defer_ok=*/false,
23653 /*expl_inst_class_mem_p=*/false);
23654 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23658 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23659 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23663 /* If INSTANTIATION has been instantiated, then we don't
23664 need to consider it again in the future. */
23672 current_tinst_level
= NULL
;
23674 last_pending_template
= last
;
23676 while (reconsider
);
23678 input_location
= saved_loc
;
23681 /* Substitute ARGVEC into T, which is a list of initializers for
23682 either base class or a non-static data member. The TREE_PURPOSEs
23683 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23684 instantiate_decl. */
23687 tsubst_initializer_list (tree t
, tree argvec
)
23689 tree inits
= NULL_TREE
;
23690 tree target_ctor
= error_mark_node
;
23692 for (; t
; t
= TREE_CHAIN (t
))
23696 tree expanded_bases
= NULL_TREE
;
23697 tree expanded_arguments
= NULL_TREE
;
23700 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23705 /* Expand the base class expansion type into separate base
23707 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23708 tf_warning_or_error
,
23710 if (expanded_bases
== error_mark_node
)
23713 /* We'll be building separate TREE_LISTs of arguments for
23715 len
= TREE_VEC_LENGTH (expanded_bases
);
23716 expanded_arguments
= make_tree_vec (len
);
23717 for (i
= 0; i
< len
; i
++)
23718 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23720 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23721 expand each argument in the TREE_VALUE of t. */
23722 expr
= make_node (EXPR_PACK_EXPANSION
);
23723 PACK_EXPANSION_LOCAL_P (expr
) = true;
23724 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23725 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23727 if (TREE_VALUE (t
) == void_type_node
)
23728 /* VOID_TYPE_NODE is used to indicate
23729 value-initialization. */
23731 for (i
= 0; i
< len
; i
++)
23732 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23736 /* Substitute parameter packs into each argument in the
23738 in_base_initializer
= 1;
23739 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23741 tree expanded_exprs
;
23743 /* Expand the argument. */
23744 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23746 = tsubst_pack_expansion (expr
, argvec
,
23747 tf_warning_or_error
,
23749 if (expanded_exprs
== error_mark_node
)
23752 /* Prepend each of the expanded expressions to the
23753 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23754 for (i
= 0; i
< len
; i
++)
23756 TREE_VEC_ELT (expanded_arguments
, i
) =
23757 tree_cons (NULL_TREE
,
23758 TREE_VEC_ELT (expanded_exprs
, i
),
23759 TREE_VEC_ELT (expanded_arguments
, i
));
23762 in_base_initializer
= 0;
23764 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23765 since we built them backwards. */
23766 for (i
= 0; i
< len
; i
++)
23768 TREE_VEC_ELT (expanded_arguments
, i
) =
23769 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23774 for (i
= 0; i
< len
; ++i
)
23776 if (expanded_bases
)
23778 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23779 decl
= expand_member_init (decl
);
23780 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23785 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23786 tf_warning_or_error
, NULL_TREE
);
23788 decl
= expand_member_init (decl
);
23789 if (decl
&& !DECL_P (decl
))
23790 in_base_initializer
= 1;
23792 init
= TREE_VALUE (t
);
23794 if (init
!= void_type_node
)
23795 init
= tsubst_expr (init
, argvec
,
23796 tf_warning_or_error
, NULL_TREE
,
23797 /*integral_constant_expression_p=*/false);
23798 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23799 /* If we had an initializer but it instantiated to nothing,
23800 value-initialize the object. This will only occur when
23801 the initializer was a pack expansion where the parameter
23802 packs used in that expansion were of length zero. */
23803 init
= void_type_node
;
23804 in_base_initializer
= 0;
23807 if (target_ctor
!= error_mark_node
23808 && init
!= error_mark_node
)
23810 error ("mem-initializer for %qD follows constructor delegation",
23814 /* Look for a target constructor. */
23815 if (init
!= error_mark_node
23816 && decl
&& CLASS_TYPE_P (decl
)
23817 && same_type_p (decl
, current_class_type
))
23819 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
23822 error ("constructor delegation follows mem-initializer for %qD",
23823 TREE_PURPOSE (inits
));
23826 target_ctor
= init
;
23831 init
= build_tree_list (decl
, init
);
23832 TREE_CHAIN (init
) = inits
;
23840 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23843 set_current_access_from_decl (tree decl
)
23845 if (TREE_PRIVATE (decl
))
23846 current_access_specifier
= access_private_node
;
23847 else if (TREE_PROTECTED (decl
))
23848 current_access_specifier
= access_protected_node
;
23850 current_access_specifier
= access_public_node
;
23853 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23854 is the instantiation (which should have been created with
23855 start_enum) and ARGS are the template arguments to use. */
23858 tsubst_enum (tree tag
, tree newtag
, tree args
)
23862 if (SCOPED_ENUM_P (newtag
))
23863 begin_scope (sk_scoped_enum
, newtag
);
23865 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23870 decl
= TREE_VALUE (e
);
23871 /* Note that in a template enum, the TREE_VALUE is the
23872 CONST_DECL, not the corresponding INTEGER_CST. */
23873 value
= tsubst_expr (DECL_INITIAL (decl
),
23874 args
, tf_warning_or_error
, NULL_TREE
,
23875 /*integral_constant_expression_p=*/true);
23877 /* Give this enumeration constant the correct access. */
23878 set_current_access_from_decl (decl
);
23880 /* Actually build the enumerator itself. Here we're assuming that
23881 enumerators can't have dependent attributes. */
23882 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23883 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23886 if (SCOPED_ENUM_P (newtag
))
23889 finish_enum_value_list (newtag
);
23890 finish_enum (newtag
);
23892 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23893 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23896 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23897 its type -- but without substituting the innermost set of template
23898 arguments. So, innermost set of template parameters will appear in
23902 get_mostly_instantiated_function_type (tree decl
)
23904 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23905 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23908 /* Return truthvalue if we're processing a template different from
23909 the last one involved in diagnostics. */
23911 problematic_instantiation_changed (void)
23913 return current_tinst_level
!= last_error_tinst_level
;
23916 /* Remember current template involved in diagnostics. */
23918 record_last_problematic_instantiation (void)
23920 last_error_tinst_level
= current_tinst_level
;
23923 struct tinst_level
*
23924 current_instantiation (void)
23926 return current_tinst_level
;
23929 /* Return TRUE if current_function_decl is being instantiated, false
23933 instantiating_current_function_p (void)
23935 return (current_instantiation ()
23936 && current_instantiation ()->decl
== current_function_decl
);
23939 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23940 type. Return false for ok, true for disallowed. Issue error and
23941 inform messages under control of COMPLAIN. */
23944 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23946 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23948 else if (POINTER_TYPE_P (type
))
23950 else if (TYPE_PTRMEM_P (type
))
23952 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23954 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23956 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23958 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23960 /* A bound template template parm could later be instantiated to have a valid
23961 nontype parm type via an alias template. */
23962 else if (cxx_dialect
>= cxx11
23963 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23966 if (complain
& tf_error
)
23968 if (type
== error_mark_node
)
23969 inform (input_location
, "invalid template non-type parameter");
23971 error ("%q#T is not a valid type for a template non-type parameter",
23977 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23978 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23981 dependent_type_p_r (tree type
)
23987 A type is dependent if it is:
23989 -- a template parameter. Template template parameters are types
23990 for us (since TYPE_P holds true for them) so we handle
23992 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23993 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23995 /* -- a qualified-id with a nested-name-specifier which contains a
23996 class-name that names a dependent type or whose unqualified-id
23997 names a dependent type. */
23998 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24001 /* An alias template specialization can be dependent even if the
24002 resulting type is not. */
24003 if (dependent_alias_template_spec_p (type
))
24006 /* -- a cv-qualified type where the cv-unqualified type is
24008 No code is necessary for this bullet; the code below handles
24009 cv-qualified types, and we don't want to strip aliases with
24010 TYPE_MAIN_VARIANT because of DR 1558. */
24011 /* -- a compound type constructed from any dependent type. */
24012 if (TYPE_PTRMEM_P (type
))
24013 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
24014 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24016 else if (TYPE_PTR_P (type
)
24017 || TREE_CODE (type
) == REFERENCE_TYPE
)
24018 return dependent_type_p (TREE_TYPE (type
));
24019 else if (TREE_CODE (type
) == FUNCTION_TYPE
24020 || TREE_CODE (type
) == METHOD_TYPE
)
24024 if (dependent_type_p (TREE_TYPE (type
)))
24026 for (arg_type
= TYPE_ARG_TYPES (type
);
24028 arg_type
= TREE_CHAIN (arg_type
))
24029 if (dependent_type_p (TREE_VALUE (arg_type
)))
24031 if (cxx_dialect
>= cxx17
)
24032 /* A value-dependent noexcept-specifier makes the type dependent. */
24033 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
24034 if (tree noex
= TREE_PURPOSE (spec
))
24035 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24036 affect overload resolution and treating it as dependent breaks
24038 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
24039 && value_dependent_expression_p (noex
))
24043 /* -- an array type constructed from any dependent type or whose
24044 size is specified by a constant expression that is
24047 We checked for type- and value-dependence of the bounds in
24048 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24049 if (TREE_CODE (type
) == ARRAY_TYPE
)
24051 if (TYPE_DOMAIN (type
)
24052 && dependent_type_p (TYPE_DOMAIN (type
)))
24054 return dependent_type_p (TREE_TYPE (type
));
24057 /* -- a template-id in which either the template name is a template
24059 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24061 /* ... or any of the template arguments is a dependent type or
24062 an expression that is type-dependent or value-dependent. */
24063 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
24064 && (any_dependent_template_arguments_p
24065 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
24068 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24069 dependent; if the argument of the `typeof' expression is not
24070 type-dependent, then it should already been have resolved. */
24071 if (TREE_CODE (type
) == TYPEOF_TYPE
24072 || TREE_CODE (type
) == DECLTYPE_TYPE
24073 || TREE_CODE (type
) == UNDERLYING_TYPE
)
24076 /* A template argument pack is dependent if any of its packed
24078 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
24080 tree args
= ARGUMENT_PACK_ARGS (type
);
24081 int i
, len
= TREE_VEC_LENGTH (args
);
24082 for (i
= 0; i
< len
; ++i
)
24083 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24087 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24088 be template parameters. */
24089 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
24092 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
24095 /* The standard does not specifically mention types that are local
24096 to template functions or local classes, but they should be
24097 considered dependent too. For example:
24099 template <int I> void f() {
24104 The size of `E' cannot be known until the value of `I' has been
24105 determined. Therefore, `E' must be considered dependent. */
24106 scope
= TYPE_CONTEXT (type
);
24107 if (scope
&& TYPE_P (scope
))
24108 return dependent_type_p (scope
);
24109 /* Don't use type_dependent_expression_p here, as it can lead
24110 to infinite recursion trying to determine whether a lambda
24111 nested in a lambda is dependent (c++/47687). */
24112 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
24113 && DECL_LANG_SPECIFIC (scope
)
24114 && DECL_TEMPLATE_INFO (scope
)
24115 && (any_dependent_template_arguments_p
24116 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
24119 /* Other types are non-dependent. */
24123 /* Returns TRUE if TYPE is dependent, in the sense of
24124 [temp.dep.type]. Note that a NULL type is considered dependent. */
24127 dependent_type_p (tree type
)
24129 /* If there are no template parameters in scope, then there can't be
24130 any dependent types. */
24131 if (!processing_template_decl
)
24133 /* If we are not processing a template, then nobody should be
24134 providing us with a dependent type. */
24136 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
24140 /* If the type is NULL, we have not computed a type for the entity
24141 in question; in that case, the type is dependent. */
24145 /* Erroneous types can be considered non-dependent. */
24146 if (type
== error_mark_node
)
24149 /* Getting here with global_type_node means we improperly called this
24150 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24151 gcc_checking_assert (type
!= global_type_node
);
24153 /* If we have not already computed the appropriate value for TYPE,
24155 if (!TYPE_DEPENDENT_P_VALID (type
))
24157 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
24158 TYPE_DEPENDENT_P_VALID (type
) = 1;
24161 return TYPE_DEPENDENT_P (type
);
24164 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24165 lookup. In other words, a dependent type that is not the current
24169 dependent_scope_p (tree scope
)
24171 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24172 && !currently_open_class (scope
));
24175 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24176 an unknown base of 'this' (and is therefore instantiation-dependent). */
24179 unknown_base_ref_p (tree t
)
24181 if (!current_class_ptr
)
24184 tree mem
= TREE_OPERAND (t
, 1);
24185 if (shared_member_p (mem
))
24188 tree cur
= current_nonlambda_class_type ();
24189 if (!any_dependent_bases_p (cur
))
24192 tree ctx
= TREE_OPERAND (t
, 0);
24193 if (DERIVED_FROM_P (ctx
, cur
))
24199 /* T is a SCOPE_REF; return whether we need to consider it
24200 instantiation-dependent so that we can check access at instantiation
24201 time even though we know which member it resolves to. */
24204 instantiation_dependent_scope_ref_p (tree t
)
24206 if (DECL_P (TREE_OPERAND (t
, 1))
24207 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24208 && !unknown_base_ref_p (t
)
24209 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24210 TREE_OPERAND (t
, 1)))
24216 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24217 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24220 /* Note that this predicate is not appropriate for general expressions;
24221 only constant expressions (that satisfy potential_constant_expression)
24222 can be tested for value dependence. */
24225 value_dependent_expression_p (tree expression
)
24227 if (!processing_template_decl
|| expression
== NULL_TREE
)
24230 /* A type-dependent expression is also value-dependent. */
24231 if (type_dependent_expression_p (expression
))
24234 switch (TREE_CODE (expression
))
24237 /* A dependent member function of the current instantiation. */
24238 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24240 case FUNCTION_DECL
:
24241 /* A dependent member function of the current instantiation. */
24242 if (DECL_CLASS_SCOPE_P (expression
)
24243 && dependent_type_p (DECL_CONTEXT (expression
)))
24247 case IDENTIFIER_NODE
:
24248 /* A name that has not been looked up -- must be dependent. */
24251 case TEMPLATE_PARM_INDEX
:
24252 /* A non-type template parm. */
24256 /* A non-type template parm. */
24257 if (DECL_TEMPLATE_PARM_P (expression
))
24259 return value_dependent_expression_p (DECL_INITIAL (expression
));
24262 /* A constant with literal type and is initialized
24263 with an expression that is value-dependent. */
24264 if (DECL_DEPENDENT_INIT_P (expression
)
24265 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24266 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
)
24268 if (DECL_HAS_VALUE_EXPR_P (expression
))
24270 tree value_expr
= DECL_VALUE_EXPR (expression
);
24271 if (value_dependent_expression_p (value_expr
))
24276 case DYNAMIC_CAST_EXPR
:
24277 case STATIC_CAST_EXPR
:
24278 case CONST_CAST_EXPR
:
24279 case REINTERPRET_CAST_EXPR
:
24281 case IMPLICIT_CONV_EXPR
:
24282 /* These expressions are value-dependent if the type to which
24283 the cast occurs is dependent or the expression being casted
24284 is value-dependent. */
24286 tree type
= TREE_TYPE (expression
);
24288 if (dependent_type_p (type
))
24291 /* A functional cast has a list of operands. */
24292 expression
= TREE_OPERAND (expression
, 0);
24295 /* If there are no operands, it must be an expression such
24296 as "int()". This should not happen for aggregate types
24297 because it would form non-constant expressions. */
24298 gcc_assert (cxx_dialect
>= cxx11
24299 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24304 if (TREE_CODE (expression
) == TREE_LIST
)
24305 return any_value_dependent_elements_p (expression
);
24307 return value_dependent_expression_p (expression
);
24311 if (SIZEOF_EXPR_TYPE_P (expression
))
24312 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24316 /* A `sizeof' expression is value-dependent if the operand is
24317 type-dependent or is a pack expansion. */
24318 expression
= TREE_OPERAND (expression
, 0);
24319 if (PACK_EXPANSION_P (expression
))
24321 else if (TYPE_P (expression
))
24322 return dependent_type_p (expression
);
24323 return instantiation_dependent_uneval_expression_p (expression
);
24325 case AT_ENCODE_EXPR
:
24326 /* An 'encode' expression is value-dependent if the operand is
24328 expression
= TREE_OPERAND (expression
, 0);
24329 return dependent_type_p (expression
);
24331 case NOEXCEPT_EXPR
:
24332 expression
= TREE_OPERAND (expression
, 0);
24333 return instantiation_dependent_uneval_expression_p (expression
);
24336 /* All instantiation-dependent expressions should also be considered
24337 value-dependent. */
24338 return instantiation_dependent_scope_ref_p (expression
);
24340 case COMPONENT_REF
:
24341 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24342 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24344 case NONTYPE_ARGUMENT_PACK
:
24345 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24346 is value-dependent. */
24348 tree values
= ARGUMENT_PACK_ARGS (expression
);
24349 int i
, len
= TREE_VEC_LENGTH (values
);
24351 for (i
= 0; i
< len
; ++i
)
24352 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24360 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24362 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24368 if (TREE_CODE (type2
) != TREE_LIST
)
24369 return dependent_type_p (type2
);
24371 for (; type2
; type2
= TREE_CHAIN (type2
))
24372 if (dependent_type_p (TREE_VALUE (type2
)))
24379 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24380 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24383 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24384 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24388 tree op
= TREE_OPERAND (expression
, 0);
24389 return (value_dependent_expression_p (op
)
24390 || has_value_dependent_address (op
));
24393 case REQUIRES_EXPR
:
24394 /* Treat all requires-expressions as value-dependent so
24395 we don't try to fold them. */
24399 return dependent_type_p (TREE_OPERAND (expression
, 0));
24403 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24405 tree fn
= get_callee_fndecl (expression
);
24407 nargs
= call_expr_nargs (expression
);
24408 for (i
= 0; i
< nargs
; ++i
)
24410 tree op
= CALL_EXPR_ARG (expression
, i
);
24411 /* In a call to a constexpr member function, look through the
24412 implicit ADDR_EXPR on the object argument so that it doesn't
24413 cause the call to be considered value-dependent. We also
24414 look through it in potential_constant_expression. */
24415 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24416 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24417 && TREE_CODE (op
) == ADDR_EXPR
)
24418 op
= TREE_OPERAND (op
, 0);
24419 if (value_dependent_expression_p (op
))
24425 case TEMPLATE_ID_EXPR
:
24426 return variable_concept_p (TREE_OPERAND (expression
, 0));
24432 if (dependent_type_p (TREE_TYPE (expression
)))
24434 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24435 if (value_dependent_expression_p (val
))
24441 /* Treat a GNU statement expression as dependent to avoid crashing
24442 under instantiate_non_dependent_expr; it can't be constant. */
24446 /* A constant expression is value-dependent if any subexpression is
24447 value-dependent. */
24448 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
24450 case tcc_reference
:
24452 case tcc_comparison
:
24454 case tcc_expression
:
24457 int i
, len
= cp_tree_operand_length (expression
);
24459 for (i
= 0; i
< len
; i
++)
24461 tree t
= TREE_OPERAND (expression
, i
);
24463 /* In some cases, some of the operands may be missing.
24464 (For example, in the case of PREDECREMENT_EXPR, the
24465 amount to increment by may be missing.) That doesn't
24466 make the expression dependent. */
24467 if (t
&& value_dependent_expression_p (t
))
24478 /* The expression is not value-dependent. */
24482 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24483 [temp.dep.expr]. Note that an expression with no type is
24484 considered dependent. Other parts of the compiler arrange for an
24485 expression with type-dependent subexpressions to have no type, so
24486 this function doesn't have to be fully recursive. */
24489 type_dependent_expression_p (tree expression
)
24491 if (!processing_template_decl
)
24494 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
24497 STRIP_ANY_LOCATION_WRAPPER (expression
);
24499 /* An unresolved name is always dependent. */
24500 if (identifier_p (expression
)
24501 || TREE_CODE (expression
) == USING_DECL
24502 || TREE_CODE (expression
) == WILDCARD_DECL
)
24505 /* A fold expression is type-dependent. */
24506 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
24507 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
24508 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
24509 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
24512 /* Some expression forms are never type-dependent. */
24513 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
24514 || TREE_CODE (expression
) == SIZEOF_EXPR
24515 || TREE_CODE (expression
) == ALIGNOF_EXPR
24516 || TREE_CODE (expression
) == AT_ENCODE_EXPR
24517 || TREE_CODE (expression
) == NOEXCEPT_EXPR
24518 || TREE_CODE (expression
) == TRAIT_EXPR
24519 || TREE_CODE (expression
) == TYPEID_EXPR
24520 || TREE_CODE (expression
) == DELETE_EXPR
24521 || TREE_CODE (expression
) == VEC_DELETE_EXPR
24522 || TREE_CODE (expression
) == THROW_EXPR
24523 || TREE_CODE (expression
) == REQUIRES_EXPR
)
24526 /* The types of these expressions depends only on the type to which
24527 the cast occurs. */
24528 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
24529 || TREE_CODE (expression
) == STATIC_CAST_EXPR
24530 || TREE_CODE (expression
) == CONST_CAST_EXPR
24531 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
24532 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
24533 || TREE_CODE (expression
) == CAST_EXPR
)
24534 return dependent_type_p (TREE_TYPE (expression
));
24536 /* The types of these expressions depends only on the type created
24537 by the expression. */
24538 if (TREE_CODE (expression
) == NEW_EXPR
24539 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
24541 /* For NEW_EXPR tree nodes created inside a template, either
24542 the object type itself or a TREE_LIST may appear as the
24544 tree type
= TREE_OPERAND (expression
, 1);
24545 if (TREE_CODE (type
) == TREE_LIST
)
24546 /* This is an array type. We need to check array dimensions
24548 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
24549 || value_dependent_expression_p
24550 (TREE_OPERAND (TREE_VALUE (type
), 1));
24552 return dependent_type_p (type
);
24555 if (TREE_CODE (expression
) == SCOPE_REF
)
24557 tree scope
= TREE_OPERAND (expression
, 0);
24558 tree name
= TREE_OPERAND (expression
, 1);
24560 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24561 contains an identifier associated by name lookup with one or more
24562 declarations declared with a dependent type, or...a
24563 nested-name-specifier or qualified-id that names a member of an
24564 unknown specialization. */
24565 return (type_dependent_expression_p (name
)
24566 || dependent_scope_p (scope
));
24569 if (TREE_CODE (expression
) == TEMPLATE_DECL
24570 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
24571 return uses_outer_template_parms (expression
);
24573 if (TREE_CODE (expression
) == STMT_EXPR
)
24574 expression
= stmt_expr_value_expr (expression
);
24576 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
24581 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
24583 if (type_dependent_expression_p (elt
))
24589 /* A static data member of the current instantiation with incomplete
24590 array type is type-dependent, as the definition and specializations
24591 can have different bounds. */
24592 if (VAR_P (expression
)
24593 && DECL_CLASS_SCOPE_P (expression
)
24594 && dependent_type_p (DECL_CONTEXT (expression
))
24595 && VAR_HAD_UNKNOWN_BOUND (expression
))
24598 /* An array of unknown bound depending on a variadic parameter, eg:
24600 template<typename... Args>
24601 void foo (Args... args)
24603 int arr[] = { args... };
24606 template<int... vals>
24609 int arr[] = { vals... };
24612 If the array has no length and has an initializer, it must be that
24613 we couldn't determine its length in cp_complete_array_type because
24614 it is dependent. */
24615 if (VAR_P (expression
)
24616 && TREE_TYPE (expression
) != NULL_TREE
24617 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24618 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24619 && DECL_INITIAL (expression
))
24622 /* A function or variable template-id is type-dependent if it has any
24623 dependent template arguments. */
24624 if (VAR_OR_FUNCTION_DECL_P (expression
)
24625 && DECL_LANG_SPECIFIC (expression
)
24626 && DECL_TEMPLATE_INFO (expression
))
24628 /* Consider the innermost template arguments, since those are the ones
24629 that come from the template-id; the template arguments for the
24630 enclosing class do not make it type-dependent unless they are used in
24631 the type of the decl. */
24632 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24633 && (any_dependent_template_arguments_p
24634 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24638 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24639 type-dependent. Checking this is important for functions with auto return
24640 type, which looks like a dependent type. */
24641 if (TREE_CODE (expression
) == FUNCTION_DECL
24642 && !(DECL_CLASS_SCOPE_P (expression
)
24643 && dependent_type_p (DECL_CONTEXT (expression
)))
24644 && !(DECL_FRIEND_P (expression
)
24645 && (!DECL_FRIEND_CONTEXT (expression
)
24646 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
24647 && !DECL_LOCAL_FUNCTION_P (expression
))
24649 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24650 || undeduced_auto_decl (expression
));
24654 /* Always dependent, on the number of arguments if nothing else. */
24655 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24658 if (TREE_TYPE (expression
) == unknown_type_node
)
24660 if (TREE_CODE (expression
) == ADDR_EXPR
)
24661 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24662 if (TREE_CODE (expression
) == COMPONENT_REF
24663 || TREE_CODE (expression
) == OFFSET_REF
)
24665 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24667 expression
= TREE_OPERAND (expression
, 1);
24668 if (identifier_p (expression
))
24671 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24672 if (TREE_CODE (expression
) == SCOPE_REF
)
24675 if (BASELINK_P (expression
))
24677 if (BASELINK_OPTYPE (expression
)
24678 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24680 expression
= BASELINK_FUNCTIONS (expression
);
24683 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24685 if (any_dependent_template_arguments_p
24686 (TREE_OPERAND (expression
, 1)))
24688 expression
= TREE_OPERAND (expression
, 0);
24689 if (identifier_p (expression
))
24693 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24694 || TREE_CODE (expression
) == FUNCTION_DECL
);
24696 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24697 if (type_dependent_expression_p (*iter
))
24703 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24705 /* Dependent type attributes might not have made it from the decl to
24707 if (DECL_P (expression
)
24708 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24711 return (dependent_type_p (TREE_TYPE (expression
)));
24714 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24715 type-dependent if the expression refers to a member of the current
24716 instantiation and the type of the referenced member is dependent, or the
24717 class member access expression refers to a member of an unknown
24720 This function returns true if the OBJECT in such a class member access
24721 expression is of an unknown specialization. */
24724 type_dependent_object_expression_p (tree object
)
24726 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24728 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24730 tree scope
= TREE_TYPE (object
);
24731 return (!scope
|| dependent_scope_p (scope
));
24734 /* walk_tree callback function for instantiation_dependent_expression_p,
24735 below. Returns non-zero if a dependent subexpression is found. */
24738 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24743 /* We don't have to worry about decltype currently because decltype
24744 of an instantiation-dependent expr is a dependent type. This
24745 might change depending on the resolution of DR 1172. */
24746 *walk_subtrees
= false;
24749 enum tree_code code
= TREE_CODE (*tp
);
24752 /* Don't treat an argument list as dependent just because it has no
24758 case TEMPLATE_PARM_INDEX
:
24761 /* Handle expressions with type operands. */
24765 case AT_ENCODE_EXPR
:
24767 tree op
= TREE_OPERAND (*tp
, 0);
24768 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24769 op
= TREE_TYPE (op
);
24772 if (dependent_type_p (op
))
24776 *walk_subtrees
= false;
24783 case COMPONENT_REF
:
24784 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24785 /* In a template, finish_class_member_access_expr creates a
24786 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24787 type-dependent, so that we can check access control at
24788 instantiation time (PR 42277). See also Core issue 1273. */
24793 if (instantiation_dependent_scope_ref_p (*tp
))
24798 /* Treat statement-expressions as dependent. */
24802 /* Treat requires-expressions as dependent. */
24803 case REQUIRES_EXPR
:
24807 /* Treat calls to function concepts as dependent. */
24808 if (function_concept_check_p (*tp
))
24812 case TEMPLATE_ID_EXPR
:
24813 /* And variable concepts. */
24814 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24822 if (type_dependent_expression_p (*tp
))
24828 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24829 sense defined by the ABI:
24831 "An expression is instantiation-dependent if it is type-dependent
24832 or value-dependent, or it has a subexpression that is type-dependent
24833 or value-dependent."
24835 Except don't actually check value-dependence for unevaluated expressions,
24836 because in sizeof(i) we don't care about the value of i. Checking
24837 type-dependence will in turn check value-dependence of array bounds/template
24838 arguments as needed. */
24841 instantiation_dependent_uneval_expression_p (tree expression
)
24845 if (!processing_template_decl
)
24848 if (expression
== error_mark_node
)
24851 result
= cp_walk_tree_without_duplicates (&expression
,
24852 instantiation_dependent_r
, NULL
);
24853 return result
!= NULL_TREE
;
24856 /* As above, but also check value-dependence of the expression as a whole. */
24859 instantiation_dependent_expression_p (tree expression
)
24861 return (instantiation_dependent_uneval_expression_p (expression
)
24862 || value_dependent_expression_p (expression
));
24865 /* Like type_dependent_expression_p, but it also works while not processing
24866 a template definition, i.e. during substitution or mangling. */
24869 type_dependent_expression_p_push (tree expr
)
24872 ++processing_template_decl
;
24873 b
= type_dependent_expression_p (expr
);
24874 --processing_template_decl
;
24878 /* Returns TRUE if ARGS contains a type-dependent expression. */
24881 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24886 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24888 if (type_dependent_expression_p (arg
))
24894 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24895 expressions) contains any type-dependent expressions. */
24898 any_type_dependent_elements_p (const_tree list
)
24900 for (; list
; list
= TREE_CHAIN (list
))
24901 if (type_dependent_expression_p (TREE_VALUE (list
)))
24907 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24908 expressions) contains any value-dependent expressions. */
24911 any_value_dependent_elements_p (const_tree list
)
24913 for (; list
; list
= TREE_CHAIN (list
))
24914 if (value_dependent_expression_p (TREE_VALUE (list
)))
24920 /* Returns TRUE if the ARG (a template argument) is dependent. */
24923 dependent_template_arg_p (tree arg
)
24925 if (!processing_template_decl
)
24928 /* Assume a template argument that was wrongly written by the user
24929 is dependent. This is consistent with what
24930 any_dependent_template_arguments_p [that calls this function]
24932 if (!arg
|| arg
== error_mark_node
)
24935 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24936 arg
= argument_pack_select_arg (arg
);
24938 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24940 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24942 if (DECL_TEMPLATE_PARM_P (arg
))
24944 /* A member template of a dependent class is not necessarily
24945 type-dependent, but it is a dependent template argument because it
24946 will be a member of an unknown specialization to that template. */
24947 tree scope
= CP_DECL_CONTEXT (arg
);
24948 return TYPE_P (scope
) && dependent_type_p (scope
);
24950 else if (ARGUMENT_PACK_P (arg
))
24952 tree args
= ARGUMENT_PACK_ARGS (arg
);
24953 int i
, len
= TREE_VEC_LENGTH (args
);
24954 for (i
= 0; i
< len
; ++i
)
24956 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24962 else if (TYPE_P (arg
))
24963 return dependent_type_p (arg
);
24965 return (type_dependent_expression_p (arg
)
24966 || value_dependent_expression_p (arg
));
24969 /* Returns true if ARGS (a collection of template arguments) contains
24970 any types that require structural equality testing. */
24973 any_template_arguments_need_structural_equality_p (tree args
)
24980 if (args
== error_mark_node
)
24983 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24985 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24986 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24988 tree arg
= TREE_VEC_ELT (level
, j
);
24989 tree packed_args
= NULL_TREE
;
24992 if (ARGUMENT_PACK_P (arg
))
24994 /* Look inside the argument pack. */
24995 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24996 len
= TREE_VEC_LENGTH (packed_args
);
24999 for (k
= 0; k
< len
; ++k
)
25002 arg
= TREE_VEC_ELT (packed_args
, k
);
25004 if (error_operand_p (arg
))
25006 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25008 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
25010 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
25011 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
25020 /* Returns true if ARGS (a collection of template arguments) contains
25021 any dependent arguments. */
25024 any_dependent_template_arguments_p (const_tree args
)
25031 if (args
== error_mark_node
)
25034 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25036 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25037 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25038 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
25045 /* Returns TRUE if the template TMPL is type-dependent. */
25048 dependent_template_p (tree tmpl
)
25050 if (TREE_CODE (tmpl
) == OVERLOAD
)
25052 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
25053 if (dependent_template_p (*iter
))
25058 /* Template template parameters are dependent. */
25059 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
25060 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
25062 /* So are names that have not been looked up. */
25063 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
25068 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25071 dependent_template_id_p (tree tmpl
, tree args
)
25073 return (dependent_template_p (tmpl
)
25074 || any_dependent_template_arguments_p (args
));
25077 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25081 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
25085 if (!processing_template_decl
)
25088 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
25090 tree decl
= TREE_VEC_ELT (declv
, i
);
25091 tree init
= TREE_VEC_ELT (initv
, i
);
25092 tree cond
= TREE_VEC_ELT (condv
, i
);
25093 tree incr
= TREE_VEC_ELT (incrv
, i
);
25095 if (type_dependent_expression_p (decl
)
25096 || TREE_CODE (decl
) == SCOPE_REF
)
25099 if (init
&& type_dependent_expression_p (init
))
25102 if (type_dependent_expression_p (cond
))
25105 if (COMPARISON_CLASS_P (cond
)
25106 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
25107 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
25110 if (TREE_CODE (incr
) == MODOP_EXPR
)
25112 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
25113 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
25116 else if (type_dependent_expression_p (incr
))
25118 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
25120 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
25122 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
25124 tree t
= TREE_OPERAND (incr
, 1);
25125 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
25126 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
25135 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25136 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25137 no such TYPE can be found. Note that this function peers inside
25138 uninstantiated templates and therefore should be used only in
25139 extremely limited situations. ONLY_CURRENT_P restricts this
25140 peering to the currently open classes hierarchy (which is required
25141 when comparing types). */
25144 resolve_typename_type (tree type
, bool only_current_p
)
25153 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
25155 scope
= TYPE_CONTEXT (type
);
25156 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25157 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25158 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25159 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25160 identifier of the TYPENAME_TYPE anymore.
25161 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25162 TYPENAME_TYPE instead, we avoid messing up with a possible
25163 typedef variant case. */
25164 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25166 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25167 it first before we can figure out what NAME refers to. */
25168 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25170 if (TYPENAME_IS_RESOLVING_P (scope
))
25171 /* Given a class template A with a dependent base with nested type C,
25172 typedef typename A::C::C C will land us here, as trying to resolve
25173 the initial A::C leads to the local C typedef, which leads back to
25174 A::C::C. So we break the recursion now. */
25177 scope
= resolve_typename_type (scope
, only_current_p
);
25179 /* If we don't know what SCOPE refers to, then we cannot resolve the
25181 if (!CLASS_TYPE_P (scope
))
25183 /* If this is a typedef, we don't want to look inside (c++/11987). */
25184 if (typedef_variant_p (type
))
25186 /* If SCOPE isn't the template itself, it will not have a valid
25187 TYPE_FIELDS list. */
25188 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25189 /* scope is either the template itself or a compatible instantiation
25190 like X<T>, so look up the name in the original template. */
25191 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25192 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25193 gcc_checking_assert (uses_template_parms (scope
));
25194 /* If scope has no fields, it can't be a current instantiation. Check this
25195 before currently_open_class to avoid infinite recursion (71515). */
25196 if (!TYPE_FIELDS (scope
))
25198 /* If the SCOPE is not the current instantiation, there's no reason
25199 to look inside it. */
25200 if (only_current_p
&& !currently_open_class (scope
))
25202 /* Enter the SCOPE so that name lookup will be resolved as if we
25203 were in the class definition. In particular, SCOPE will no
25204 longer be considered a dependent type. */
25205 pushed_scope
= push_scope (scope
);
25206 /* Look up the declaration. */
25207 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25208 tf_warning_or_error
);
25210 result
= NULL_TREE
;
25212 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25213 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25214 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25217 else if (identifier_p (fullname
)
25218 && TREE_CODE (decl
) == TYPE_DECL
)
25220 result
= TREE_TYPE (decl
);
25221 if (result
== error_mark_node
)
25222 result
= NULL_TREE
;
25224 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25225 && DECL_CLASS_TEMPLATE_P (decl
))
25227 /* Obtain the template and the arguments. */
25228 tree tmpl
= TREE_OPERAND (fullname
, 0);
25229 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25231 /* We get here with a plain identifier because a previous tentative
25232 parse of the nested-name-specifier as part of a ptr-operator saw
25233 ::template X<A>. The use of ::template is necessary in a
25234 ptr-operator, but wrong in a declarator-id.
25236 [temp.names]: In a qualified-id of a declarator-id, the keyword
25237 template shall not appear at the top level. */
25238 pedwarn (EXPR_LOC_OR_LOC (fullname
, input_location
), OPT_Wpedantic
,
25239 "keyword %<template%> not allowed in declarator-id");
25242 tree args
= TREE_OPERAND (fullname
, 1);
25243 /* Instantiate the template. */
25244 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25245 /*entering_scope=*/true,
25246 tf_error
| tf_user
);
25247 if (result
== error_mark_node
)
25248 result
= NULL_TREE
;
25251 /* Leave the SCOPE. */
25253 pop_scope (pushed_scope
);
25255 /* If we failed to resolve it, return the original typename. */
25259 /* If lookup found a typename type, resolve that too. */
25260 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25262 /* Ill-formed programs can cause infinite recursion here, so we
25263 must catch that. */
25264 TYPENAME_IS_RESOLVING_P (result
) = 1;
25265 result
= resolve_typename_type (result
, only_current_p
);
25266 TYPENAME_IS_RESOLVING_P (result
) = 0;
25269 /* Qualify the resulting type. */
25270 quals
= cp_type_quals (type
);
25272 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25277 /* EXPR is an expression which is not type-dependent. Return a proxy
25278 for EXPR that can be used to compute the types of larger
25279 expressions containing EXPR. */
25282 build_non_dependent_expr (tree expr
)
25284 tree orig_expr
= expr
;
25287 /* When checking, try to get a constant value for all non-dependent
25288 expressions in order to expose bugs in *_dependent_expression_p
25289 and constexpr. This can affect code generation, see PR70704, so
25290 only do this for -fchecking=2. */
25291 if (flag_checking
> 1
25292 && cxx_dialect
>= cxx11
25293 /* Don't do this during nsdmi parsing as it can lead to
25294 unexpected recursive instantiations. */
25295 && !parsing_nsdmi ()
25296 /* Don't do this during concept expansion either and for
25297 the same reason. */
25298 && !expanding_concept ())
25299 fold_non_dependent_expr (expr
);
25301 STRIP_ANY_LOCATION_WRAPPER (expr
);
25303 /* Preserve OVERLOADs; the functions must be available to resolve
25306 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25307 inner_expr
= stmt_expr_value_expr (inner_expr
);
25308 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25309 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25310 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25311 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25312 if (is_overloaded_fn (inner_expr
)
25313 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25315 /* There is no need to return a proxy for a variable. */
25318 /* Preserve string constants; conversions from string constants to
25319 "char *" are allowed, even though normally a "const char *"
25320 cannot be used to initialize a "char *". */
25321 if (TREE_CODE (expr
) == STRING_CST
)
25323 /* Preserve void and arithmetic constants, as an optimization -- there is no
25324 reason to create a new node. */
25325 if (TREE_CODE (expr
) == VOID_CST
25326 || TREE_CODE (expr
) == INTEGER_CST
25327 || TREE_CODE (expr
) == REAL_CST
)
25329 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25330 There is at least one place where we want to know that a
25331 particular expression is a throw-expression: when checking a ?:
25332 expression, there are special rules if the second or third
25333 argument is a throw-expression. */
25334 if (TREE_CODE (expr
) == THROW_EXPR
)
25337 /* Don't wrap an initializer list, we need to be able to look inside. */
25338 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25341 /* Don't wrap a dummy object, we need to be able to test for it. */
25342 if (is_dummy_object (expr
))
25345 if (TREE_CODE (expr
) == COND_EXPR
)
25346 return build3 (COND_EXPR
,
25348 TREE_OPERAND (expr
, 0),
25349 (TREE_OPERAND (expr
, 1)
25350 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25351 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25352 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25353 if (TREE_CODE (expr
) == COMPOUND_EXPR
25354 && !COMPOUND_EXPR_OVERLOADED (expr
))
25355 return build2 (COMPOUND_EXPR
,
25357 TREE_OPERAND (expr
, 0),
25358 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25360 /* If the type is unknown, it can't really be non-dependent */
25361 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25363 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25364 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
25367 /* ARGS is a vector of expressions as arguments to a function call.
25368 Replace the arguments with equivalent non-dependent expressions.
25369 This modifies ARGS in place. */
25372 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25377 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25379 tree newarg
= build_non_dependent_expr (arg
);
25381 (*args
)[ix
] = newarg
;
25385 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25386 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25387 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25390 make_auto_1 (tree name
, bool set_canonical
)
25392 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25393 TYPE_NAME (au
) = build_decl (input_location
,
25394 TYPE_DECL
, name
, au
);
25395 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25396 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25397 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25398 TYPE_NAME (au
), NULL_TREE
);
25400 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25401 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25402 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
25408 make_decltype_auto (void)
25410 return make_auto_1 (decltype_auto_identifier
, true);
25416 return make_auto_1 (auto_identifier
, true);
25419 /* Return a C++17 deduction placeholder for class template TMPL. */
25422 make_template_placeholder (tree tmpl
)
25424 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
25425 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
25429 /* True iff T is a C++17 class template deduction placeholder. */
25432 template_placeholder_p (tree t
)
25434 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
25437 /* Make a "constrained auto" type-specifier. This is an
25438 auto type with constraints that must be associated after
25439 deduction. The constraint is formed from the given
25440 CONC and its optional sequence of arguments, which are
25441 non-null if written as partial-concept-id. */
25444 make_constrained_auto (tree con
, tree args
)
25446 tree type
= make_auto_1 (auto_identifier
, false);
25448 /* Build the constraint. */
25449 tree tmpl
= DECL_TI_TEMPLATE (con
);
25450 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
25451 expr
= build_concept_check (expr
, type
, args
);
25453 tree constr
= normalize_expression (expr
);
25454 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
25456 /* Our canonical type depends on the constraint. */
25457 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
25459 /* Attach the constraint to the type declaration. */
25460 tree decl
= TYPE_NAME (type
);
25464 /* Given type ARG, return std::initializer_list<ARG>. */
25469 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
25471 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
25473 gcc_rich_location
richloc (input_location
);
25474 maybe_add_include_fixit (&richloc
, "<initializer_list>");
25475 error_at (&richloc
,
25476 "deducing from brace-enclosed initializer list"
25477 " requires %<#include <initializer_list>%>");
25479 return error_mark_node
;
25481 tree argvec
= make_tree_vec (1);
25482 TREE_VEC_ELT (argvec
, 0) = arg
;
25484 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
25485 NULL_TREE
, 0, tf_warning_or_error
);
25488 /* Replace auto in TYPE with std::initializer_list<auto>. */
25491 listify_autos (tree type
, tree auto_node
)
25493 tree init_auto
= listify (auto_node
);
25494 tree argvec
= make_tree_vec (1);
25495 TREE_VEC_ELT (argvec
, 0) = init_auto
;
25496 if (processing_template_decl
)
25497 argvec
= add_to_template_args (current_template_args (), argvec
);
25498 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
25501 /* Hash traits for hashing possibly constrained 'auto'
25502 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25504 struct auto_hash
: default_hash_traits
<tree
>
25506 static inline hashval_t
hash (tree
);
25507 static inline bool equal (tree
, tree
);
25510 /* Hash the 'auto' T. */
25513 auto_hash::hash (tree t
)
25515 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
25516 /* Matching constrained-type-specifiers denote the same template
25517 parameter, so hash the constraint. */
25518 return hash_placeholder_constraint (c
);
25520 /* But unconstrained autos are all separate, so just hash the pointer. */
25521 return iterative_hash_object (t
, 0);
25524 /* Compare two 'auto's. */
25527 auto_hash::equal (tree t1
, tree t2
)
25532 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
25533 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
25535 /* Two unconstrained autos are distinct. */
25539 return equivalent_placeholder_constraints (c1
, c2
);
25542 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25543 constrained) auto, add it to the vector. */
25546 extract_autos_r (tree t
, void *data
)
25548 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
25551 /* All the autos were built with index 0; fix that up now. */
25552 tree
*p
= hash
.find_slot (t
, INSERT
);
25555 /* If this is a repeated constrained-type-specifier, use the index we
25557 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
25560 /* Otherwise this is new, so use the current count. */
25562 idx
= hash
.elements () - 1;
25564 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
25567 /* Always keep walking. */
25571 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25572 says they can appear anywhere in the type. */
25575 extract_autos (tree type
)
25577 hash_set
<tree
> visited
;
25578 hash_table
<auto_hash
> hash (2);
25580 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
25582 tree tree_vec
= make_tree_vec (hash
.elements());
25583 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
25584 iter
!= hash
.end(); ++iter
)
25587 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
25588 TREE_VEC_ELT (tree_vec
, i
)
25589 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
25595 /* The stem for deduction guide names. */
25596 const char *const dguide_base
= "__dguide_";
25598 /* Return the name for a deduction guide for class template TMPL. */
25601 dguide_name (tree tmpl
)
25603 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
25604 tree tname
= TYPE_IDENTIFIER (type
);
25605 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
25606 + IDENTIFIER_LENGTH (tname
));
25607 memcpy (buf
, dguide_base
, strlen (dguide_base
));
25608 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
25609 IDENTIFIER_LENGTH (tname
) + 1);
25610 tree dname
= get_identifier (buf
);
25611 TREE_TYPE (dname
) = type
;
25615 /* True if NAME is the name of a deduction guide. */
25618 dguide_name_p (tree name
)
25620 return (TREE_CODE (name
) == IDENTIFIER_NODE
25621 && TREE_TYPE (name
)
25622 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
25623 strlen (dguide_base
)));
25626 /* True if FN is a deduction guide. */
25629 deduction_guide_p (const_tree fn
)
25632 if (tree name
= DECL_NAME (fn
))
25633 return dguide_name_p (name
);
25637 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25640 copy_guide_p (const_tree fn
)
25642 gcc_assert (deduction_guide_p (fn
));
25643 if (!DECL_ARTIFICIAL (fn
))
25645 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25646 return (TREE_CHAIN (parms
) == void_list_node
25647 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25650 /* True if FN is a guide generated from a constructor template. */
25653 template_guide_p (const_tree fn
)
25655 gcc_assert (deduction_guide_p (fn
));
25656 if (!DECL_ARTIFICIAL (fn
))
25658 tree tmpl
= DECL_TI_TEMPLATE (fn
);
25659 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
25660 return PRIMARY_TEMPLATE_P (org
);
25664 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25665 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25666 template parameter types. Note that the handling of template template
25667 parameters relies on current_template_parms being set appropriately for the
25671 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25672 tree tsubst_args
, tsubst_flags_t complain
)
25674 tree oldidx
= get_template_parm_index (olddecl
);
25677 if (TREE_CODE (olddecl
) == TYPE_DECL
25678 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25680 tree oldtype
= TREE_TYPE (olddecl
);
25681 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25682 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25683 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25684 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25685 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25688 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25689 complain
, NULL_TREE
);
25692 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25693 DECL_NAME (olddecl
), newtype
);
25694 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25697 if (TREE_CODE (olddecl
) == TYPE_DECL
25698 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25700 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25701 = build_template_parm_index (index
, level
, level
,
25703 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25704 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25705 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25706 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25708 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25710 DECL_TEMPLATE_RESULT (newdecl
)
25711 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25712 DECL_NAME (olddecl
), newtype
);
25713 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25714 // First create a copy (ttargs) of tsubst_args with an
25715 // additional level for the template template parameter's own
25716 // template parameters (ttparms).
25717 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25718 (DECL_TEMPLATE_PARMS (olddecl
)));
25719 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25720 tree ttargs
= make_tree_vec (depth
+ 1);
25721 for (int i
= 0; i
< depth
; ++i
)
25722 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25723 TREE_VEC_ELT (ttargs
, depth
)
25724 = template_parms_level_to_args (ttparms
);
25725 // Substitute ttargs into ttparms to fix references to
25726 // other template parameters.
25727 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25728 complain
|tf_partial
);
25729 // Now substitute again with args based on tparms, to reduce
25730 // the level of the ttparms.
25731 ttargs
= current_template_args ();
25732 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25734 // Finally, tack the adjusted parms onto tparms.
25735 ttparms
= tree_cons (size_int (depth
), ttparms
,
25736 current_template_parms
);
25737 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25742 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25744 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25745 TREE_CODE (oldconst
),
25746 DECL_NAME (oldconst
), newtype
);
25747 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25748 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25749 SET_DECL_TEMPLATE_PARM_P (newconst
);
25750 newidx
= build_template_parm_index (index
, level
, level
,
25751 newconst
, newtype
);
25752 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25753 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25754 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25760 /* Returns a C++17 class deduction guide template based on the constructor
25761 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25762 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25765 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25767 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25768 bool memtmpl
= false;
25771 tree fn_tmpl
= NULL_TREE
;
25776 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25779 type
= TREE_TYPE (type
);
25780 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25783 fparms
= void_list_node
;
25785 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25786 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25787 targs
= CLASSTYPE_TI_ARGS (type
);
25790 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25791 explicit_p
= false;
25795 ++processing_template_decl
;
25798 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
25799 : DECL_TI_TEMPLATE (ctor
));
25801 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
25802 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25804 type
= DECL_CONTEXT (ctor
);
25806 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25807 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25808 fully specialized args for the enclosing class. Strip those off, as
25809 the deduction guide won't have those template parameters. */
25810 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25811 TMPL_PARMS_DEPTH (tparms
));
25812 /* Discard the 'this' parameter. */
25813 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25814 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25815 ci
= get_constraints (ctor
);
25816 loc
= DECL_SOURCE_LOCATION (ctor
);
25817 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25819 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25823 /* For a member template constructor, we need to flatten the two
25824 template parameter lists into one, and then adjust the function
25825 signature accordingly. This gets...complicated. */
25826 tree save_parms
= current_template_parms
;
25828 /* For a member template we should have two levels of parms/args, one
25829 for the class and one for the constructor. We stripped
25830 specialized args for further enclosing classes above. */
25831 const int depth
= 2;
25832 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25834 /* Template args for translating references to the two-level template
25835 parameters into references to the one-level template parameters we
25837 tree tsubst_args
= copy_node (targs
);
25838 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25839 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25841 /* Template parms for the constructor template. */
25842 tree ftparms
= TREE_VALUE (tparms
);
25843 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25844 /* Template parms for the class template. */
25845 tparms
= TREE_CHAIN (tparms
);
25846 tree ctparms
= TREE_VALUE (tparms
);
25847 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25848 /* Template parms for the deduction guide start as a copy of the
25849 template parms for the class. We set current_template_parms for
25850 lookup_template_class_1. */
25851 current_template_parms
= tparms
= copy_node (tparms
);
25852 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25853 for (unsigned i
= 0; i
< clen
; ++i
)
25854 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25856 /* Now we need to rewrite the constructor parms to append them to the
25858 for (unsigned i
= 0; i
< flen
; ++i
)
25860 unsigned index
= i
+ clen
;
25861 unsigned level
= 1;
25862 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25863 tree olddecl
= TREE_VALUE (oldelt
);
25864 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25865 tsubst_args
, complain
);
25866 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25867 tsubst_args
, complain
, ctor
);
25868 tree list
= build_tree_list (newdef
, newdecl
);
25869 TEMPLATE_PARM_CONSTRAINTS (list
)
25870 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25871 tsubst_args
, complain
, ctor
);
25872 TREE_VEC_ELT (new_vec
, index
) = list
;
25873 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25876 /* Now we have a final set of template parms to substitute into the
25877 function signature. */
25878 targs
= template_parms_to_args (tparms
);
25879 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25881 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25883 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25885 current_template_parms
= save_parms
;
25887 --processing_template_decl
;
25892 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25893 tparms
= copy_node (tparms
);
25894 INNERMOST_TEMPLATE_PARMS (tparms
)
25895 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25898 tree fntype
= build_function_type (type
, fparms
);
25899 tree ded_fn
= build_lang_decl_loc (loc
,
25901 dguide_name (type
), fntype
);
25902 DECL_ARGUMENTS (ded_fn
) = fargs
;
25903 DECL_ARTIFICIAL (ded_fn
) = true;
25904 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25905 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25906 DECL_ARTIFICIAL (ded_tmpl
) = true;
25907 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25908 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25909 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25910 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25912 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
25914 set_constraints (ded_tmpl
, ci
);
25919 /* Deduce template arguments for the class template placeholder PTYPE for
25920 template TMPL based on the initializer INIT, and return the resulting
25924 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25925 tsubst_flags_t complain
)
25927 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25929 /* We should have handled this in the caller. */
25930 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25932 if (complain
& tf_error
)
25933 error ("non-class template %qT used without template arguments", tmpl
);
25934 return error_mark_node
;
25937 tree type
= TREE_TYPE (tmpl
);
25939 bool try_list_ctor
= false;
25941 vec
<tree
,va_gc
> *args
;
25942 if (init
== NULL_TREE
25943 || TREE_CODE (init
) == TREE_LIST
)
25944 args
= make_tree_vector_from_list (init
);
25945 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25947 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
25948 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
25950 /* As an exception, the first phase in 16.3.1.7 (considering the
25951 initializer list as a single argument) is omitted if the
25952 initializer list consists of a single expression of type cv U,
25953 where U is a specialization of C or a class derived from a
25954 specialization of C. */
25955 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
25956 tree etype
= TREE_TYPE (elt
);
25958 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
25959 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25960 int err
= unify (tparms
, targs
, type
, etype
,
25961 UNIFY_ALLOW_DERIVED
, /*explain*/false);
25963 try_list_ctor
= false;
25966 if (try_list_ctor
|| is_std_init_list (type
))
25967 args
= make_tree_vector_single (init
);
25969 args
= make_tree_vector_from_ctor (init
);
25972 args
= make_tree_vector_single (init
);
25974 tree dname
= dguide_name (tmpl
);
25975 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25976 /*type*/false, /*complain*/false,
25978 bool elided
= false;
25979 if (cands
== error_mark_node
)
25982 /* Prune explicit deduction guides in copy-initialization context. */
25983 if (flags
& LOOKUP_ONLYCONVERTING
)
25985 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25986 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25991 /* Found a nonconverting guide, prune the candidates. */
25992 tree pruned
= NULL_TREE
;
25993 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25994 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25995 pruned
= lookup_add (*iter
, pruned
);
26001 tree outer_args
= NULL_TREE
;
26002 if (DECL_CLASS_SCOPE_P (tmpl
)
26003 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
26005 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
26006 type
= TREE_TYPE (most_general_template (tmpl
));
26009 bool saw_ctor
= false;
26010 // FIXME cache artificial deduction guides
26011 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
26013 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
26014 if ((flags
& LOOKUP_ONLYCONVERTING
)
26015 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
26018 cands
= lookup_add (guide
, cands
);
26023 tree call
= error_mark_node
;
26025 /* If this is list-initialization and the class has a list constructor, first
26026 try deducing from the list as a single argument, as [over.match.list]. */
26027 tree list_cands
= NULL_TREE
;
26028 if (try_list_ctor
&& cands
)
26029 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26032 if (is_list_ctor (dg
))
26033 list_cands
= lookup_add (dg
, list_cands
);
26037 ++cp_unevaluated_operand
;
26038 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
26039 --cp_unevaluated_operand
;
26041 if (call
== error_mark_node
)
26043 /* That didn't work, now try treating the list as a sequence of
26045 release_tree_vector (args
);
26046 args
= make_tree_vector_from_ctor (init
);
26050 /* Maybe generate an implicit deduction guide. */
26051 if (call
== error_mark_node
&& args
->length () < 2)
26053 tree gtype
= NULL_TREE
;
26055 if (args
->length () == 1)
26056 /* Generate a copy guide. */
26057 gtype
= build_reference_type (type
);
26058 else if (!saw_ctor
)
26059 /* Generate a default guide. */
26064 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
26065 cands
= lookup_add (guide
, cands
);
26069 if (elided
&& !cands
)
26071 error ("cannot deduce template arguments for copy-initialization"
26072 " of %qT, as it has no non-explicit deduction guides or "
26073 "user-declared constructors", type
);
26074 return error_mark_node
;
26076 else if (!cands
&& call
== error_mark_node
)
26078 error ("cannot deduce template arguments of %qT, as it has no viable "
26079 "deduction guides", type
);
26080 return error_mark_node
;
26083 if (call
== error_mark_node
)
26085 ++cp_unevaluated_operand
;
26086 call
= build_new_function_call (cands
, &args
, tf_decltype
);
26087 --cp_unevaluated_operand
;
26090 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
26092 error ("class template argument deduction failed:");
26094 ++cp_unevaluated_operand
;
26095 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
26096 --cp_unevaluated_operand
;
26099 inform (input_location
, "explicit deduction guides not considered "
26100 "for copy-initialization");
26103 release_tree_vector (args
);
26105 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
26108 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26109 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26110 The CONTEXT determines the context in which auto deduction is performed
26111 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26112 OUTER_TARGS are used during template argument deduction
26113 (context == adc_unify) to properly substitute the result, and is ignored
26116 For partial-concept-ids, extra args may be appended to the list of deduced
26117 template arguments prior to determining constraint satisfaction. */
26120 do_auto_deduction (tree type
, tree init
, tree auto_node
,
26121 tsubst_flags_t complain
, auto_deduction_context context
,
26122 tree outer_targs
, int flags
)
26126 if (init
== error_mark_node
)
26127 return error_mark_node
;
26129 if (init
&& type_dependent_expression_p (init
)
26130 && context
!= adc_unify
)
26131 /* Defining a subset of type-dependent expressions that we can deduce
26132 from ahead of time isn't worth the trouble. */
26135 /* Similarly, we can't deduce from another undeduced decl. */
26136 if (init
&& undeduced_auto_decl (init
))
26139 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
26140 /* C++17 class template argument deduction. */
26141 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
26143 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
26144 /* Nothing we can do with this, even in deduction context. */
26147 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26148 with either a new invented type template parameter U or, if the
26149 initializer is a braced-init-list (8.5.4), with
26150 std::initializer_list<U>. */
26151 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26153 if (!DIRECT_LIST_INIT_P (init
))
26154 type
= listify_autos (type
, auto_node
);
26155 else if (CONSTRUCTOR_NELTS (init
) == 1)
26156 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26159 if (complain
& tf_warning_or_error
)
26161 if (permerror (input_location
, "direct-list-initialization of "
26162 "%<auto%> requires exactly one element"))
26163 inform (input_location
,
26164 "for deduction to %<std::initializer_list%>, use copy-"
26165 "list-initialization (i.e. add %<=%> before the %<{%>)");
26167 type
= listify_autos (type
, auto_node
);
26171 if (type
== error_mark_node
)
26172 return error_mark_node
;
26174 init
= resolve_nondeduced_context (init
, complain
);
26176 if (context
== adc_decomp_type
26177 && auto_node
== type
26178 && init
!= error_mark_node
26179 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26180 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26181 and initializer has array type, deduce cv-qualified array type. */
26182 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26184 else if (AUTO_IS_DECLTYPE (auto_node
))
26186 bool id
= (DECL_P (init
)
26187 || ((TREE_CODE (init
) == COMPONENT_REF
26188 || TREE_CODE (init
) == SCOPE_REF
)
26189 && !REF_PARENTHESIZED_P (init
)));
26190 targs
= make_tree_vec (1);
26191 TREE_VEC_ELT (targs
, 0)
26192 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26193 if (type
!= auto_node
)
26195 if (complain
& tf_error
)
26196 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26197 return error_mark_node
;
26202 tree parms
= build_tree_list (NULL_TREE
, type
);
26206 tparms
= extract_autos (type
);
26209 tparms
= make_tree_vec (1);
26210 TREE_VEC_ELT (tparms
, 0)
26211 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26214 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26215 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26216 DEDUCE_CALL
, LOOKUP_NORMAL
,
26217 NULL
, /*explain_p=*/false);
26220 if (processing_template_decl
)
26221 /* Try again at instantiation time. */
26223 if (type
&& type
!= error_mark_node
26224 && (complain
& tf_error
))
26225 /* If type is error_mark_node a diagnostic must have been
26226 emitted by now. Also, having a mention to '<type error>'
26227 in the diagnostic is not really useful to the user. */
26229 if (cfun
&& auto_node
== current_function_auto_return_pattern
26230 && LAMBDA_FUNCTION_P (current_function_decl
))
26231 error ("unable to deduce lambda return type from %qE", init
);
26233 error ("unable to deduce %qT from %qE", type
, init
);
26234 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26235 DEDUCE_CALL
, LOOKUP_NORMAL
,
26236 NULL
, /*explain_p=*/true);
26238 return error_mark_node
;
26242 /* Check any placeholder constraints against the deduced type. */
26243 if (flag_concepts
&& !processing_template_decl
)
26244 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26246 /* Use the deduced type to check the associated constraints. If we
26247 have a partial-concept-id, rebuild the argument list so that
26248 we check using the extra arguments. */
26249 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26250 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26251 if (TREE_VEC_LENGTH (cargs
) > 1)
26253 cargs
= copy_node (cargs
);
26254 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26258 if (!constraints_satisfied_p (constr
, cargs
))
26260 if (complain
& tf_warning_or_error
)
26264 case adc_unspecified
:
26266 error("placeholder constraints not satisfied");
26268 case adc_variable_type
:
26269 case adc_decomp_type
:
26270 error ("deduced initializer does not satisfy "
26271 "placeholder constraints");
26273 case adc_return_type
:
26274 error ("deduced return type does not satisfy "
26275 "placeholder constraints");
26277 case adc_requirement
:
26278 error ("deduced expression type does not satisfy "
26279 "placeholder constraints");
26282 diagnose_constraints (input_location
, constr
, targs
);
26284 return error_mark_node
;
26288 if (processing_template_decl
&& context
!= adc_unify
)
26289 outer_targs
= current_template_args ();
26290 targs
= add_to_template_args (outer_targs
, targs
);
26291 return tsubst (type
, targs
, complain
, NULL_TREE
);
26294 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26298 splice_late_return_type (tree type
, tree late_return_type
)
26300 if (is_auto (type
))
26302 if (late_return_type
)
26303 return late_return_type
;
26305 tree idx
= get_template_parm_index (type
);
26306 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26307 /* In an abbreviated function template we didn't know we were dealing
26308 with a function template when we saw the auto return type, so update
26309 it to have the correct level. */
26310 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26315 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26316 'decltype(auto)' or a deduced class template. */
26319 is_auto (const_tree type
)
26321 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26322 && (TYPE_IDENTIFIER (type
) == auto_identifier
26323 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26324 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26330 /* for_each_template_parm callback for type_uses_auto. */
26333 is_auto_r (tree tp
, void */
*data*/
)
26335 return is_auto (tp
);
26338 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26339 a use of `auto'. Returns NULL_TREE otherwise. */
26342 type_uses_auto (tree type
)
26344 if (type
== NULL_TREE
)
26346 else if (flag_concepts
)
26348 /* The Concepts TS allows multiple autos in one type-specifier; just
26349 return the first one we find, do_auto_deduction will collect all of
26351 if (uses_template_parms (type
))
26352 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26353 /*visited*/NULL
, /*nondeduced*/true);
26358 return find_type_usage (type
, is_auto
);
26361 /* For a given template T, return the vector of typedefs referenced
26362 in T for which access check is needed at T instantiation time.
26363 T is either a FUNCTION_DECL or a RECORD_TYPE.
26364 Those typedefs were added to T by the function
26365 append_type_to_template_for_access_check. */
26367 vec
<qualified_typedef_usage_t
, va_gc
> *
26368 get_types_needing_access_check (tree t
)
26371 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
26373 if (!t
|| t
== error_mark_node
)
26376 if (!(ti
= get_template_info (t
)))
26379 if (CLASS_TYPE_P (t
)
26380 || TREE_CODE (t
) == FUNCTION_DECL
)
26382 if (!TI_TEMPLATE (ti
))
26385 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
26391 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26392 tied to T. That list of typedefs will be access checked at
26393 T instantiation time.
26394 T is either a FUNCTION_DECL or a RECORD_TYPE.
26395 TYPE_DECL is a TYPE_DECL node representing a typedef.
26396 SCOPE is the scope through which TYPE_DECL is accessed.
26397 LOCATION is the location of the usage point of TYPE_DECL.
26399 This function is a subroutine of
26400 append_type_to_template_for_access_check. */
26403 append_type_to_template_for_access_check_1 (tree t
,
26406 location_t location
)
26408 qualified_typedef_usage_t typedef_usage
;
26411 if (!t
|| t
== error_mark_node
)
26414 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
26415 || CLASS_TYPE_P (t
))
26417 && TREE_CODE (type_decl
) == TYPE_DECL
26420 if (!(ti
= get_template_info (t
)))
26423 gcc_assert (TI_TEMPLATE (ti
));
26425 typedef_usage
.typedef_decl
= type_decl
;
26426 typedef_usage
.context
= scope
;
26427 typedef_usage
.locus
= location
;
26429 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
26432 /* Append TYPE_DECL to the template TEMPL.
26433 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26434 At TEMPL instanciation time, TYPE_DECL will be checked to see
26435 if it can be accessed through SCOPE.
26436 LOCATION is the location of the usage point of TYPE_DECL.
26438 e.g. consider the following code snippet:
26445 template<class U> struct S
26447 C::myint mi; // <-- usage point of the typedef C::myint
26452 At S<char> instantiation time, we need to check the access of C::myint
26453 In other words, we need to check the access of the myint typedef through
26454 the C scope. For that purpose, this function will add the myint typedef
26455 and the scope C through which its being accessed to a list of typedefs
26456 tied to the template S. That list will be walked at template instantiation
26457 time and access check performed on each typedefs it contains.
26458 Note that this particular code snippet should yield an error because
26459 myint is private to C. */
26462 append_type_to_template_for_access_check (tree templ
,
26465 location_t location
)
26467 qualified_typedef_usage_t
*iter
;
26470 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
26472 /* Make sure we don't append the type to the template twice. */
26473 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
26474 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
26477 append_type_to_template_for_access_check_1 (templ
, type_decl
,
26481 /* Convert the generic type parameters in PARM that match the types given in the
26482 range [START_IDX, END_IDX) from the current_template_parms into generic type
26486 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
26488 tree current
= current_template_parms
;
26489 int depth
= TMPL_PARMS_DEPTH (current
);
26490 current
= INNERMOST_TEMPLATE_PARMS (current
);
26491 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
26493 for (int i
= 0; i
< start_idx
; ++i
)
26494 TREE_VEC_ELT (replacement
, i
)
26495 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26497 for (int i
= start_idx
; i
< end_idx
; ++i
)
26499 /* Create a distinct parameter pack type from the current parm and add it
26500 to the replacement args to tsubst below into the generic function
26503 tree o
= TREE_TYPE (TREE_VALUE
26504 (TREE_VEC_ELT (current
, i
)));
26505 tree t
= copy_type (o
);
26506 TEMPLATE_TYPE_PARM_INDEX (t
)
26507 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
26509 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
26510 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
26511 TYPE_MAIN_VARIANT (t
) = t
;
26512 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
26513 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26514 TREE_VEC_ELT (replacement
, i
) = t
;
26515 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
26518 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
26519 TREE_VEC_ELT (replacement
, i
)
26520 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
26522 /* If there are more levels then build up the replacement with the outer
26525 replacement
= add_to_template_args (template_parms_to_args
26526 (TREE_CHAIN (current_template_parms
)),
26529 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
26532 /* Entries in the decl_constraint hash table. */
26533 struct GTY((for_user
)) constr_entry
26539 /* Hashing function and equality for constraint entries. */
26540 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
26542 static hashval_t
hash (constr_entry
*e
)
26544 return (hashval_t
)DECL_UID (e
->decl
);
26547 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
26549 return e1
->decl
== e2
->decl
;
26553 /* A mapping from declarations to constraint information. Note that
26554 both templates and their underlying declarations are mapped to the
26555 same constraint information.
26557 FIXME: This is defined in pt.c because garbage collection
26558 code is not being generated for constraint.cc. */
26560 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
26562 /* Returns the template constraints of declaration T. If T is not
26563 constrained, return NULL_TREE. Note that T must be non-null. */
26566 get_constraints (tree t
)
26568 if (!flag_concepts
)
26571 gcc_assert (DECL_P (t
));
26572 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26573 t
= DECL_TEMPLATE_RESULT (t
);
26574 constr_entry elt
= { t
, NULL_TREE
};
26575 constr_entry
* found
= decl_constraints
->find (&elt
);
26582 /* Associate the given constraint information CI with the declaration
26583 T. If T is a template, then the constraints are associated with
26584 its underlying declaration. Don't build associations if CI is
26588 set_constraints (tree t
, tree ci
)
26592 gcc_assert (t
&& flag_concepts
);
26593 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26594 t
= DECL_TEMPLATE_RESULT (t
);
26595 gcc_assert (!get_constraints (t
));
26596 constr_entry elt
= {t
, ci
};
26597 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
26598 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
26603 /* Remove the associated constraints of the declaration T. */
26606 remove_constraints (tree t
)
26608 gcc_assert (DECL_P (t
));
26609 if (TREE_CODE (t
) == TEMPLATE_DECL
)
26610 t
= DECL_TEMPLATE_RESULT (t
);
26612 constr_entry elt
= {t
, NULL_TREE
};
26613 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
26615 decl_constraints
->clear_slot (slot
);
26618 /* Memoized satisfaction results for declarations. This
26619 maps the pair (constraint_info, arguments) to the result computed
26620 by constraints_satisfied_p. */
26622 struct GTY((for_user
)) constraint_sat_entry
26629 /* Hashing function and equality for constraint entries. */
26631 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
26633 static hashval_t
hash (constraint_sat_entry
*e
)
26635 hashval_t val
= iterative_hash_object(e
->ci
, 0);
26636 return iterative_hash_template_arg (e
->args
, val
);
26639 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
26641 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
26645 /* Memoized satisfaction results for concept checks. */
26647 struct GTY((for_user
)) concept_spec_entry
26654 /* Hashing function and equality for constraint entries. */
26656 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
26658 static hashval_t
hash (concept_spec_entry
*e
)
26660 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
26663 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
26665 ++comparing_specializations
;
26666 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
26667 --comparing_specializations
;
26672 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
26673 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
26675 /* Search for a memoized satisfaction result. Returns one of the
26676 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26679 lookup_constraint_satisfaction (tree ci
, tree args
)
26681 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
26682 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
26684 return found
->result
;
26689 /* Memoize the result of a satisfication test. Returns the saved result. */
26692 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26694 constraint_sat_entry elt
= {ci
, args
, result
};
26695 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26696 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26702 /* Search for a memoized satisfaction result for a concept. */
26705 lookup_concept_satisfaction (tree tmpl
, tree args
)
26707 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26708 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26710 return found
->result
;
26715 /* Memoize the result of a concept check. Returns the saved result. */
26718 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26720 concept_spec_entry elt
= {tmpl
, args
, result
};
26721 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26722 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26728 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26730 /* Returns a prior concept specialization. This returns the substituted
26731 and normalized constraints defined by the concept. */
26734 get_concept_expansion (tree tmpl
, tree args
)
26736 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26737 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26739 return found
->result
;
26744 /* Save a concept expansion for later. */
26747 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26749 concept_spec_entry elt
= {tmpl
, args
, def
};
26750 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26751 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26758 hash_subsumption_args (tree t1
, tree t2
)
26760 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26761 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26763 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26764 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26765 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26766 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26770 /* Compare the constraints of two subsumption entries. The LEFT1 and
26771 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26772 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26775 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26777 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26778 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26779 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26780 CHECK_CONSTR_ARGS (right1
)))
26781 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26782 CHECK_CONSTR_ARGS (right2
));
26786 /* Key/value pair for learning and memoizing subsumption results. This
26787 associates a pair of check constraints (including arguments) with
26788 a boolean value indicating the result. */
26790 struct GTY((for_user
)) subsumption_entry
26797 /* Hashing function and equality for constraint entries. */
26799 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26801 static hashval_t
hash (subsumption_entry
*e
)
26803 return hash_subsumption_args (e
->t1
, e
->t2
);
26806 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26808 ++comparing_specializations
;
26809 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26810 --comparing_specializations
;
26815 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26817 /* Search for a previously cached subsumption result. */
26820 lookup_subsumption_result (tree t1
, tree t2
)
26822 subsumption_entry elt
= { t1
, t2
, false };
26823 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26825 return &found
->result
;
26830 /* Save a subsumption result. */
26833 save_subsumption_result (tree t1
, tree t2
, bool result
)
26835 subsumption_entry elt
= {t1
, t2
, result
};
26836 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26837 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26843 /* Set up the hash table for constraint association. */
26846 init_constraint_processing (void)
26848 if (!flag_concepts
)
26851 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26852 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26853 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26854 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26855 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26858 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26862 declare_integer_pack (void)
26864 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26865 build_function_type_list (integer_type_node
,
26868 NULL_TREE
, ECF_CONST
);
26869 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26870 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26873 /* Set up the hash tables for template instantiations. */
26876 init_template_processing (void)
26878 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26879 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26881 if (cxx_dialect
>= cxx11
)
26882 declare_integer_pack ();
26885 /* Print stats about the template hash tables for -fstats. */
26888 print_template_statistics (void)
26890 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26891 "%f collisions\n", (long) decl_specializations
->size (),
26892 (long) decl_specializations
->elements (),
26893 decl_specializations
->collisions ());
26894 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26895 "%f collisions\n", (long) type_specializations
->size (),
26896 (long) type_specializations
->elements (),
26897 type_specializations
->collisions ());
26902 namespace selftest
{
26904 /* Verify that build_non_dependent_expr () works, for various expressions,
26905 and that location wrappers don't affect the results. */
26908 test_build_non_dependent_expr ()
26910 location_t loc
= BUILTINS_LOCATION
;
26912 /* Verify constants, without and with location wrappers. */
26913 tree int_cst
= build_int_cst (integer_type_node
, 42);
26914 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
26916 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
26917 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
26918 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
26920 tree string_lit
= build_string (4, "foo");
26921 TREE_TYPE (string_lit
) = char_array_type_node
;
26922 string_lit
= fix_string_type (string_lit
);
26923 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
26925 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
26926 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
26927 ASSERT_EQ (wrapped_string_lit
,
26928 build_non_dependent_expr (wrapped_string_lit
));
26931 /* Verify that type_dependent_expression_p () works correctly, even
26932 in the presence of location wrapper nodes. */
26935 test_type_dependent_expression_p ()
26937 location_t loc
= BUILTINS_LOCATION
;
26939 tree name
= get_identifier ("foo");
26941 /* If no templates are involved, nothing is type-dependent. */
26942 gcc_assert (!processing_template_decl
);
26943 ASSERT_FALSE (type_dependent_expression_p (name
));
26945 ++processing_template_decl
;
26947 /* Within a template, an unresolved name is always type-dependent. */
26948 ASSERT_TRUE (type_dependent_expression_p (name
));
26950 /* Ensure it copes with NULL_TREE and errors. */
26951 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
26952 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
26954 /* A USING_DECL in a template should be type-dependent, even if wrapped
26955 with a location wrapper (PR c++/83799). */
26956 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
26957 TREE_TYPE (using_decl
) = integer_type_node
;
26958 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
26959 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
26960 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
26961 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
26963 --processing_template_decl
;
26966 /* Run all of the selftests within this file. */
26971 test_build_non_dependent_expr ();
26972 test_type_dependent_expression_p ();
26975 } // namespace selftest
26977 #endif /* #if CHECKING_P */
26979 #include "gt-cp-pt.h"