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
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) tree saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
80 // Implementation of the RAII helper for creating new local
82 local_specialization_stack::local_specialization_stack (lss_policy policy
)
83 : saved (local_specializations
)
85 if (policy
== lss_blank
|| !saved
)
86 local_specializations
= new hash_map
<tree
, tree
>;
88 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
91 local_specialization_stack::~local_specialization_stack ()
93 delete local_specializations
;
94 local_specializations
= saved
;
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth
;
100 struct GTY((for_user
)) spec_entry
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
115 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132 enum template_base_result
{
134 tbr_ambiguous_baseclass
,
138 static void push_access_scope (tree
);
139 static void pop_access_scope (tree
);
140 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
141 unification_kind_t
, int,
143 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
144 unification_kind_t
, int, bool, bool);
145 static int unify (tree
, tree
, tree
, tree
, int, bool);
146 static void add_pending_template (tree
);
147 static tree
reopen_tinst_level (struct tinst_level
*);
148 static tree
tsubst_initializer_list (tree
, tree
);
149 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
150 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
154 static void tsubst_enum (tree
, tree
, tree
);
155 static tree
add_to_template_args (tree
, tree
);
156 static tree
add_outermost_template_args (tree
, tree
);
157 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
158 static int check_non_deducible_conversion (tree
, tree
, int, int,
159 struct conversion
**, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
162 static int type_unification_real (tree
, tree
, tree
, const tree
*,
163 unsigned int, int, unification_kind_t
,
164 vec
<deferred_access_check
, va_gc
> **,
166 static void note_template_header (int);
167 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
168 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
169 static tree
convert_template_argument (tree
, tree
, tree
,
170 tsubst_flags_t
, int, tree
);
171 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
172 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
173 static tree
expand_template_argument_pack (tree
);
174 static tree
build_template_parm_index (int, int, int, tree
, tree
);
175 static bool inline_needs_template_parms (tree
, bool);
176 static void push_inline_template_parms_recursive (tree
, int);
177 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
178 static int mark_template_parm (tree
, void *);
179 static int template_parm_this_level_p (tree
, void *);
180 static tree
tsubst_friend_function (tree
, tree
);
181 static tree
tsubst_friend_class (tree
, tree
);
182 static int can_complete_type_without_circularity (tree
);
183 static tree
get_bindings (tree
, tree
, tree
, bool);
184 static int template_decl_level (tree
);
185 static int check_cv_quals_for_unify (int, tree
, tree
);
186 static void template_parm_level_and_index (tree
, int*, int*);
187 static int unify_pack_expansion (tree
, tree
, tree
,
188 tree
, unification_kind_t
, bool, bool);
189 static tree
copy_template_args (tree
);
190 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
192 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
193 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
194 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
195 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
197 static bool check_specialization_scope (void);
198 static tree
process_partial_specialization (tree
);
199 static void set_current_access_from_decl (tree
);
200 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
202 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
203 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
205 static bool template_template_parm_bindings_ok_p (tree
, tree
);
206 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
207 static tree
for_each_template_parm_r (tree
*, int *, void *);
208 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
209 static void copy_default_args_to_explicit_spec (tree
);
210 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
211 static bool dependent_template_arg_p (tree
);
212 static bool any_template_arguments_need_structural_equality_p (tree
);
213 static bool dependent_type_p_r (tree
);
214 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
215 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
216 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
217 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
225 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
226 static tree
make_argument_pack (tree
);
227 static void register_parameter_specializations (tree
, tree
);
228 static tree
enclosing_instantiation_of (tree tctx
);
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
236 push_access_scope (tree t
)
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
239 || TREE_CODE (t
) == TYPE_DECL
);
241 if (DECL_FRIEND_CONTEXT (t
))
242 push_nested_class (DECL_FRIEND_CONTEXT (t
));
243 else if (DECL_CLASS_SCOPE_P (t
))
244 push_nested_class (DECL_CONTEXT (t
));
246 push_to_top_level ();
248 if (TREE_CODE (t
) == FUNCTION_DECL
)
250 saved_access_scope
= tree_cons
251 (NULL_TREE
, current_function_decl
, saved_access_scope
);
252 current_function_decl
= t
;
256 /* Restore the scope set up by push_access_scope. T is the node we
260 pop_access_scope (tree t
)
262 if (TREE_CODE (t
) == FUNCTION_DECL
)
264 current_function_decl
= TREE_VALUE (saved_access_scope
);
265 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
268 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
271 pop_from_top_level ();
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
280 finish_member_template_decl (tree decl
)
282 if (decl
== error_mark_node
)
283 return error_mark_node
;
285 gcc_assert (DECL_P (decl
));
287 if (TREE_CODE (decl
) == TYPE_DECL
)
291 type
= TREE_TYPE (decl
);
292 if (type
== error_mark_node
)
293 return error_mark_node
;
294 if (MAYBE_CLASS_TYPE_P (type
)
295 && CLASSTYPE_TEMPLATE_INFO (type
)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
298 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
299 check_member_template (tmpl
);
304 else if (TREE_CODE (decl
) == FIELD_DECL
)
305 error ("data member %qD cannot be a member template", decl
);
306 else if (DECL_TEMPLATE_INFO (decl
))
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
310 check_member_template (DECL_TI_TEMPLATE (decl
));
311 return DECL_TI_TEMPLATE (decl
);
317 error ("invalid member template declaration %qD", decl
);
319 return error_mark_node
;
322 /* Create a template info node. */
325 build_template_info (tree template_decl
, tree template_args
)
327 tree result
= make_node (TEMPLATE_INFO
);
328 TI_TEMPLATE (result
) = template_decl
;
329 TI_ARGS (result
) = template_args
;
333 /* Return the template info node corresponding to T, whatever T is. */
336 get_template_info (const_tree t
)
338 tree tinfo
= NULL_TREE
;
340 if (!t
|| t
== error_mark_node
)
343 if (TREE_CODE (t
) == NAMESPACE_DECL
344 || TREE_CODE (t
) == PARM_DECL
)
347 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
348 tinfo
= DECL_TEMPLATE_INFO (t
);
350 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
353 if (OVERLOAD_TYPE_P (t
))
354 tinfo
= TYPE_TEMPLATE_INFO (t
);
355 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
356 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
361 /* Returns the template nesting level of the indicated class TYPE.
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
380 template_class_depth (tree type
)
384 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
386 tree tinfo
= get_template_info (type
);
388 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
393 type
= CP_DECL_CONTEXT (type
);
394 else if (LAMBDA_TYPE_P (type
))
395 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
397 type
= CP_TYPE_CONTEXT (type
);
403 /* Subroutine of maybe_begin_member_template_processing.
404 Returns true if processing DECL needs us to push template parms. */
407 inline_needs_template_parms (tree decl
, bool nsdmi
)
409 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
412 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
413 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
416 /* Subroutine of maybe_begin_member_template_processing.
417 Push the template parms in PARMS, starting from LEVELS steps into the
418 chain, and ending at the beginning, since template parms are listed
422 push_inline_template_parms_recursive (tree parmlist
, int levels
)
424 tree parms
= TREE_VALUE (parmlist
);
428 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
430 ++processing_template_decl
;
431 current_template_parms
432 = tree_cons (size_int (processing_template_decl
),
433 parms
, current_template_parms
);
434 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
436 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
438 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
440 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
442 if (error_operand_p (parm
))
445 gcc_assert (DECL_P (parm
));
447 switch (TREE_CODE (parm
))
455 /* Push the CONST_DECL. */
456 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
465 /* Restore the template parameter context for a member template, a
466 friend template defined in a class definition, or a non-template
467 member of template class. */
470 maybe_begin_member_template_processing (tree decl
)
474 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
478 tree ctx
= DECL_CONTEXT (decl
);
479 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
480 /* Disregard full specializations (c++/60999). */
481 && uses_template_parms (ctx
)
482 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
485 if (inline_needs_template_parms (decl
, nsdmi
))
487 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
488 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
490 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
493 parms
= TREE_CHAIN (parms
);
496 push_inline_template_parms_recursive (parms
, levels
);
499 /* Remember how many levels of template parameters we pushed so that
500 we can pop them later. */
501 inline_parm_levels
.safe_push (levels
);
504 /* Undo the effects of maybe_begin_member_template_processing. */
507 maybe_end_member_template_processing (void)
512 if (inline_parm_levels
.length () == 0)
515 last
= inline_parm_levels
.pop ();
516 for (i
= 0; i
< last
; ++i
)
518 --processing_template_decl
;
519 current_template_parms
= TREE_CHAIN (current_template_parms
);
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. */
528 add_to_template_args (tree args
, tree extra_args
)
535 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
538 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
539 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
541 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
544 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
545 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
550 /* Like add_to_template_args, but only the outermost ARGS are added to
551 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
552 (EXTRA_ARGS) levels are added. This function is used to combine
553 the template arguments from a partial instantiation with the
554 template arguments used to attain the full instantiation from the
555 partial instantiation. */
558 add_outermost_template_args (tree args
, tree extra_args
)
562 /* If there are more levels of EXTRA_ARGS than there are ARGS,
563 something very fishy is going on. */
564 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
566 /* If *all* the new arguments will be the EXTRA_ARGS, just return
568 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
571 /* For the moment, we make ARGS look like it contains fewer levels. */
572 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
574 new_args
= add_to_template_args (args
, extra_args
);
576 /* Now, we restore ARGS to its full dimensions. */
577 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
582 /* Return the N levels of innermost template arguments from the ARGS. */
585 get_innermost_template_args (tree args
, int n
)
593 /* If N is 1, just return the innermost set of template arguments. */
595 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
597 /* If we're not removing anything, just return the arguments we were
599 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
600 gcc_assert (extra_levels
>= 0);
601 if (extra_levels
== 0)
604 /* Make a new set of arguments, not containing the outer arguments. */
605 new_args
= make_tree_vec (n
);
606 for (i
= 1; i
<= n
; ++i
)
607 SET_TMPL_ARGS_LEVEL (new_args
, i
,
608 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
613 /* The inverse of get_innermost_template_args: Return all but the innermost
614 EXTRA_LEVELS levels of template arguments from the ARGS. */
617 strip_innermost_template_args (tree args
, int extra_levels
)
620 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
625 /* If N is 1, just return the outermost set of template arguments. */
627 return TMPL_ARGS_LEVEL (args
, 1);
629 /* If we're not removing anything, just return the arguments we were
631 gcc_assert (extra_levels
>= 0);
632 if (extra_levels
== 0)
635 /* Make a new set of arguments, not containing the inner arguments. */
636 new_args
= make_tree_vec (n
);
637 for (i
= 1; i
<= n
; ++i
)
638 SET_TMPL_ARGS_LEVEL (new_args
, i
,
639 TMPL_ARGS_LEVEL (args
, i
));
644 /* We've got a template header coming up; push to a new level for storing
648 begin_template_parm_list (void)
650 /* We use a non-tag-transparent scope here, which causes pushtag to
651 put tags in this scope, rather than in the enclosing class or
652 namespace scope. This is the right thing, since we want
653 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
654 global template class, push_template_decl handles putting the
655 TEMPLATE_DECL into top-level scope. For a nested template class,
658 template <class T> struct S1 {
659 template <class T> struct S2 {};
662 pushtag contains special code to insert the TEMPLATE_DECL for S2
663 at the right scope. */
664 begin_scope (sk_template_parms
, NULL
);
665 ++processing_template_decl
;
666 ++processing_template_parmlist
;
667 note_template_header (0);
669 /* Add a dummy parameter level while we process the parameter list. */
670 current_template_parms
671 = tree_cons (size_int (processing_template_decl
),
673 current_template_parms
);
676 /* This routine is called when a specialization is declared. If it is
677 invalid to declare a specialization here, an error is reported and
678 false is returned, otherwise this routine will return true. */
681 check_specialization_scope (void)
683 tree scope
= current_scope ();
687 An explicit specialization shall be declared in the namespace of
688 which the template is a member, or, for member templates, in the
689 namespace of which the enclosing class or enclosing class
690 template is a member. An explicit specialization of a member
691 function, member class or static data member of a class template
692 shall be declared in the namespace of which the class template
694 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
696 error ("explicit specialization in non-namespace scope %qD", scope
);
702 In an explicit specialization declaration for a member of a class
703 template or a member template that appears in namespace scope,
704 the member template and some of its enclosing class templates may
705 remain unspecialized, except that the declaration shall not
706 explicitly specialize a class member template if its enclosing
707 class templates are not explicitly specialized as well. */
708 if (current_template_parms
)
710 error ("enclosing class templates are not explicitly specialized");
717 /* We've just seen template <>. */
720 begin_specialization (void)
722 begin_scope (sk_template_spec
, NULL
);
723 note_template_header (1);
724 return check_specialization_scope ();
727 /* Called at then end of processing a declaration preceded by
731 end_specialization (void)
734 reset_specialization ();
737 /* Any template <>'s that we have seen thus far are not referring to a
738 function specialization. */
741 reset_specialization (void)
743 processing_specialization
= 0;
744 template_header_count
= 0;
747 /* We've just seen a template header. If SPECIALIZATION is nonzero,
748 it was of the form template <>. */
751 note_template_header (int specialization
)
753 processing_specialization
= specialization
;
754 template_header_count
++;
757 /* We're beginning an explicit instantiation. */
760 begin_explicit_instantiation (void)
762 gcc_assert (!processing_explicit_instantiation
);
763 processing_explicit_instantiation
= true;
768 end_explicit_instantiation (void)
770 gcc_assert (processing_explicit_instantiation
);
771 processing_explicit_instantiation
= false;
774 /* An explicit specialization or partial specialization of TMPL is being
775 declared. Check that the namespace in which the specialization is
776 occurring is permissible. Returns false iff it is invalid to
777 specialize TMPL in the current namespace. */
780 check_specialization_namespace (tree tmpl
)
782 tree tpl_ns
= decl_namespace_context (tmpl
);
786 An explicit specialization shall be declared in a namespace enclosing the
787 specialized template. An explicit specialization whose declarator-id is
788 not qualified shall be declared in the nearest enclosing namespace of the
789 template, or, if the namespace is inline (7.3.1), any namespace from its
790 enclosing namespace set. */
791 if (current_scope() != DECL_CONTEXT (tmpl
)
792 && !at_namespace_scope_p ())
794 error ("specialization of %qD must appear at namespace scope", tmpl
);
798 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
799 /* Same or enclosing namespace. */
803 auto_diagnostic_group d
;
804 if (permerror (input_location
,
805 "specialization of %qD in different namespace", tmpl
))
806 inform (DECL_SOURCE_LOCATION (tmpl
),
807 " from definition of %q#D", tmpl
);
812 /* SPEC is an explicit instantiation. Check that it is valid to
813 perform this explicit instantiation in the current namespace. */
816 check_explicit_instantiation_namespace (tree spec
)
820 /* DR 275: An explicit instantiation shall appear in an enclosing
821 namespace of its template. */
822 ns
= decl_namespace_context (spec
);
823 if (!is_nested_namespace (current_namespace
, ns
))
824 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
825 "(which does not enclose namespace %qD)",
826 spec
, current_namespace
, ns
);
829 // Returns the type of a template specialization only if that
830 // specialization needs to be defined. Otherwise (e.g., if the type has
831 // already been defined), the function returns NULL_TREE.
833 maybe_new_partial_specialization (tree type
)
835 // An implicit instantiation of an incomplete type implies
836 // the definition of a new class template.
838 // template<typename T>
841 // template<typename T>
844 // Here, S<T*> is an implicit instantiation of S whose type
846 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
849 // It can also be the case that TYPE is a completed specialization.
850 // Continuing the previous example, suppose we also declare:
852 // template<typename T>
853 // requires Integral<T>
856 // Here, S<T*> refers to the specialization S<T*> defined
857 // above. However, we need to differentiate definitions because
858 // we intend to define a new partial specialization. In this case,
859 // we rely on the fact that the constraints are different for
860 // this declaration than that above.
862 // Note that we also get here for injected class names and
863 // late-parsed template definitions. We must ensure that we
864 // do not create new type declarations for those cases.
865 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
867 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
868 tree args
= CLASSTYPE_TI_ARGS (type
);
870 // If there are no template parameters, this cannot be a new
871 // partial template specializtion?
872 if (!current_template_parms
)
875 // The injected-class-name is not a new partial specialization.
876 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
879 // If the constraints are not the same as those of the primary
880 // then, we can probably create a new specialization.
881 tree type_constr
= current_template_constraints ();
883 if (type
== TREE_TYPE (tmpl
))
885 tree main_constr
= get_constraints (tmpl
);
886 if (equivalent_constraints (type_constr
, main_constr
))
890 // Also, if there's a pre-existing specialization with matching
891 // constraints, then this also isn't new.
892 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
895 tree spec_tmpl
= TREE_VALUE (specs
);
896 tree spec_args
= TREE_PURPOSE (specs
);
897 tree spec_constr
= get_constraints (spec_tmpl
);
898 if (comp_template_args (args
, spec_args
)
899 && equivalent_constraints (type_constr
, spec_constr
))
901 specs
= TREE_CHAIN (specs
);
904 // Create a new type node (and corresponding type decl)
905 // for the newly declared specialization.
906 tree t
= make_class_type (TREE_CODE (type
));
907 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
908 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
910 /* We only need a separate type node for storing the definition of this
911 partial specialization; uses of S<T*> are unconstrained, so all are
912 equivalent. So keep TYPE_CANONICAL the same. */
913 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
915 // Build the corresponding type decl.
916 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
917 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
918 DECL_SOURCE_LOCATION (d
) = input_location
;
926 /* The TYPE is being declared. If it is a template type, that means it
927 is a partial specialization. Do appropriate error-checking. */
930 maybe_process_partial_specialization (tree type
)
934 if (type
== error_mark_node
)
935 return error_mark_node
;
937 /* A lambda that appears in specialization context is not itself a
939 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
942 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
944 error ("name of class shadows template template parameter %qD",
946 return error_mark_node
;
949 context
= TYPE_CONTEXT (type
);
951 if (TYPE_ALIAS_P (type
))
953 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
955 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
956 error ("specialization of alias template %qD",
957 TI_TEMPLATE (tinfo
));
959 error ("explicit specialization of non-template %qT", type
);
960 return error_mark_node
;
962 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
964 /* This is for ordinary explicit specialization and partial
965 specialization of a template class such as:
967 template <> class C<int>;
971 template <class T> class C<T*>;
973 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
975 if (tree t
= maybe_new_partial_specialization (type
))
977 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
978 && !at_namespace_scope_p ())
979 return error_mark_node
;
980 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
981 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
982 if (processing_template_decl
)
984 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
985 if (decl
== error_mark_node
)
986 return error_mark_node
;
987 return TREE_TYPE (decl
);
990 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
991 error ("specialization of %qT after instantiation", type
);
992 else if (errorcount
&& !processing_specialization
993 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
994 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
995 /* Trying to define a specialization either without a template<> header
996 or in an inappropriate place. We've already given an error, so just
997 bail now so we don't actually define the specialization. */
998 return error_mark_node
;
1000 else if (CLASS_TYPE_P (type
)
1001 && !CLASSTYPE_USE_TEMPLATE (type
)
1002 && CLASSTYPE_TEMPLATE_INFO (type
)
1003 && context
&& CLASS_TYPE_P (context
)
1004 && CLASSTYPE_TEMPLATE_INFO (context
))
1006 /* This is for an explicit specialization of member class
1007 template according to [temp.expl.spec/18]:
1009 template <> template <class U> class C<int>::D;
1011 The context `C<int>' must be an implicit instantiation.
1012 Otherwise this is just a member class template declared
1015 template <> class C<int> { template <class U> class D; };
1016 template <> template <class U> class C<int>::D;
1018 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1019 while in the second case, `C<int>::D' is a primary template
1020 and `C<T>::D' may not exist. */
1022 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1023 && !COMPLETE_TYPE_P (type
))
1026 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1028 if (current_namespace
1029 != decl_namespace_context (tmpl
))
1031 permerror (input_location
,
1032 "specializing %q#T in different namespace", type
);
1033 permerror (DECL_SOURCE_LOCATION (tmpl
),
1034 " from definition of %q#D", tmpl
);
1037 /* Check for invalid specialization after instantiation:
1039 template <> template <> class C<int>::D<int>;
1040 template <> template <class U> class C<int>::D; */
1042 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1043 t
; t
= TREE_CHAIN (t
))
1045 tree inst
= TREE_VALUE (t
);
1046 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1047 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1049 /* We already have a full specialization of this partial
1050 instantiation, or a full specialization has been
1051 looked up but not instantiated. Reassign it to the
1052 new member specialization template. */
1056 elt
.tmpl
= most_general_template (tmpl
);
1057 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1060 type_specializations
->remove_elt (&elt
);
1063 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1066 = type_specializations
->find_slot (&elt
, INSERT
);
1067 entry
= ggc_alloc
<spec_entry
> ();
1072 /* But if we've had an implicit instantiation, that's a
1073 problem ([temp.expl.spec]/6). */
1074 error ("specialization %qT after instantiation %qT",
1078 /* Mark TYPE as a specialization. And as a result, we only
1079 have one level of template argument for the innermost
1081 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1082 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1083 CLASSTYPE_TI_ARGS (type
)
1084 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1087 else if (processing_specialization
)
1089 /* Someday C++0x may allow for enum template specialization. */
1090 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1091 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1092 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1093 "of %qD not allowed by ISO C++", type
);
1096 error ("explicit specialization of non-template %qT", type
);
1097 return error_mark_node
;
1104 /* Returns nonzero if we can optimize the retrieval of specializations
1105 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1106 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1109 optimize_specialization_lookup_p (tree tmpl
)
1111 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1112 && DECL_CLASS_SCOPE_P (tmpl
)
1113 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1115 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1116 /* The optimized lookup depends on the fact that the
1117 template arguments for the member function template apply
1118 purely to the containing class, which is not true if the
1119 containing class is an explicit or partial
1121 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1122 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1123 && !DECL_CONV_FN_P (tmpl
)
1124 /* It is possible to have a template that is not a member
1125 template and is not a member of a template class:
1127 template <typename T>
1128 struct S { friend A::f(); };
1130 Here, the friend function is a template, but the context does
1131 not have template information. The optimized lookup relies
1132 on having ARGS be the template arguments for both the class
1133 and the function template. */
1134 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1137 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1138 gone through coerce_template_parms by now. */
1141 verify_unstripped_args_1 (tree inner
)
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 (ARGUMENT_PACK_P (arg
))
1151 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1152 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1153 /* Allow typedefs on the type of a non-type argument, since a
1154 parameter can have them. */;
1156 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1161 verify_unstripped_args (tree args
)
1163 ++processing_template_decl
;
1164 if (!any_dependent_template_arguments_p (args
))
1165 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1166 --processing_template_decl
;
1169 /* Retrieve the specialization (in the sense of [temp.spec] - a
1170 specialization is either an instantiation or an explicit
1171 specialization) of TMPL for the given template ARGS. If there is
1172 no such specialization, return NULL_TREE. The ARGS are a vector of
1173 arguments, or a vector of vectors of arguments, in the case of
1174 templates with more than one level of parameters.
1176 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1177 then we search for a partial specialization matching ARGS. This
1178 parameter is ignored if TMPL is not a class template.
1180 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1181 result is a NONTYPE_ARGUMENT_PACK. */
1184 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1186 if (tmpl
== NULL_TREE
)
1189 if (args
== error_mark_node
)
1192 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1193 || TREE_CODE (tmpl
) == FIELD_DECL
);
1195 /* There should be as many levels of arguments as there are
1196 levels of parameters. */
1197 gcc_assert (TMPL_ARGS_DEPTH (args
)
1198 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1199 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1200 : template_class_depth (DECL_CONTEXT (tmpl
))));
1203 verify_unstripped_args (args
);
1205 /* Lambda functions in templates aren't instantiated normally, but through
1206 tsubst_lambda_expr. */
1207 if (lambda_fn_in_template_p (tmpl
))
1210 if (optimize_specialization_lookup_p (tmpl
))
1212 /* The template arguments actually apply to the containing
1213 class. Find the class specialization with those
1215 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1216 tree class_specialization
1217 = retrieve_specialization (class_template
, args
, 0);
1218 if (!class_specialization
)
1221 /* Find the instance of TMPL. */
1222 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1223 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1226 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1227 /* using-declarations can add base methods to the method vec,
1228 and we don't want those here. */
1229 && DECL_CONTEXT (fn
) == class_specialization
)
1238 hash_table
<spec_hasher
> *specializations
;
1242 elt
.spec
= NULL_TREE
;
1244 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1245 specializations
= type_specializations
;
1247 specializations
= decl_specializations
;
1250 hash
= spec_hasher::hash (&elt
);
1251 found
= specializations
->find_with_hash (&elt
, hash
);
1259 /* Like retrieve_specialization, but for local declarations. */
1262 retrieve_local_specialization (tree tmpl
)
1264 if (local_specializations
== NULL
)
1267 tree
*slot
= local_specializations
->get (tmpl
);
1268 return slot
? *slot
: NULL_TREE
;
1271 /* Returns nonzero iff DECL is a specialization of TMPL. */
1274 is_specialization_of (tree decl
, tree tmpl
)
1278 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1282 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1288 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1290 for (t
= TREE_TYPE (decl
);
1292 t
= CLASSTYPE_USE_TEMPLATE (t
)
1293 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1294 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1301 /* Returns nonzero iff DECL is a specialization of friend declaration
1302 FRIEND_DECL according to [temp.friend]. */
1305 is_specialization_of_friend (tree decl
, tree friend_decl
)
1307 bool need_template
= true;
1310 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1311 || TREE_CODE (decl
) == TYPE_DECL
);
1313 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1314 of a template class, we want to check if DECL is a specialization
1316 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1317 && DECL_TEMPLATE_INFO (friend_decl
)
1318 && !DECL_USE_TEMPLATE (friend_decl
))
1320 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1321 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1322 need_template
= false;
1324 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1325 && !PRIMARY_TEMPLATE_P (friend_decl
))
1326 need_template
= false;
1328 /* There is nothing to do if this is not a template friend. */
1329 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1332 if (is_specialization_of (decl
, friend_decl
))
1336 A member of a class template may be declared to be a friend of a
1337 non-template class. In this case, the corresponding member of
1338 every specialization of the class template is a friend of the
1339 class granting friendship.
1341 For example, given a template friend declaration
1343 template <class T> friend void A<T>::f();
1345 the member function below is considered a friend
1347 template <> struct A<int> {
1351 For this type of template friend, TEMPLATE_DEPTH below will be
1352 nonzero. To determine if DECL is a friend of FRIEND, we first
1353 check if the enclosing class is a specialization of another. */
1355 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1357 && DECL_CLASS_SCOPE_P (decl
)
1358 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1359 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1361 /* Next, we check the members themselves. In order to handle
1362 a few tricky cases, such as when FRIEND_DECL's are
1364 template <class T> friend void A<T>::g(T t);
1365 template <class T> template <T t> friend void A<T>::h();
1369 void A<int>::g(int);
1370 template <int> void A<int>::h();
1372 we need to figure out ARGS, the template arguments from
1373 the context of DECL. This is required for template substitution
1374 of `T' in the function parameter of `g' and template parameter
1375 of `h' in the above examples. Here ARGS corresponds to `int'. */
1377 tree context
= DECL_CONTEXT (decl
);
1378 tree args
= NULL_TREE
;
1379 int current_depth
= 0;
1381 while (current_depth
< template_depth
)
1383 if (CLASSTYPE_TEMPLATE_INFO (context
))
1385 if (current_depth
== 0)
1386 args
= TYPE_TI_ARGS (context
);
1388 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1391 context
= TYPE_CONTEXT (context
);
1394 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1399 tree friend_args_type
;
1400 tree decl_args_type
;
1402 /* Make sure that both DECL and FRIEND_DECL are templates or
1404 is_template
= DECL_TEMPLATE_INFO (decl
)
1405 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1406 if (need_template
^ is_template
)
1408 else if (is_template
)
1410 /* If both are templates, check template parameter list. */
1412 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1414 if (!comp_template_parms
1415 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1419 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1422 decl_type
= TREE_TYPE (decl
);
1424 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1425 tf_none
, NULL_TREE
);
1426 if (friend_type
== error_mark_node
)
1429 /* Check if return types match. */
1430 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1433 /* Check if function parameter types match, ignoring the
1434 `this' parameter. */
1435 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1436 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1437 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1438 friend_args_type
= TREE_CHAIN (friend_args_type
);
1439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1440 decl_args_type
= TREE_CHAIN (decl_args_type
);
1442 return compparms (decl_args_type
, friend_args_type
);
1446 /* DECL is a TYPE_DECL */
1448 tree decl_type
= TREE_TYPE (decl
);
1450 /* Make sure that both DECL and FRIEND_DECL are templates or
1453 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1454 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1456 if (need_template
^ is_template
)
1458 else if (is_template
)
1461 /* If both are templates, check the name of the two
1462 TEMPLATE_DECL's first because is_friend didn't. */
1463 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1464 != DECL_NAME (friend_decl
))
1467 /* Now check template parameter list. */
1469 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1471 return comp_template_parms
1472 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1476 return (DECL_NAME (decl
)
1477 == DECL_NAME (friend_decl
));
1483 /* Register the specialization SPEC as a specialization of TMPL with
1484 the indicated ARGS. IS_FRIEND indicates whether the specialization
1485 is actually just a friend declaration. ATTRLIST is the list of
1486 attributes that the specialization is declared with or NULL when
1487 it isn't. Returns SPEC, or an equivalent prior declaration, if
1490 We also store instantiations of field packs in the hash table, even
1491 though they are not themselves templates, to make lookup easier. */
1494 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1498 spec_entry
**slot
= NULL
;
1501 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1502 || (TREE_CODE (tmpl
) == FIELD_DECL
1503 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1505 if (TREE_CODE (spec
) == FUNCTION_DECL
1506 && uses_template_parms (DECL_TI_ARGS (spec
)))
1507 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1508 register it; we want the corresponding TEMPLATE_DECL instead.
1509 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1510 the more obvious `uses_template_parms (spec)' to avoid problems
1511 with default function arguments. In particular, given
1512 something like this:
1514 template <class T> void f(T t1, T t = T())
1516 the default argument expression is not substituted for in an
1517 instantiation unless and until it is actually needed. */
1520 if (optimize_specialization_lookup_p (tmpl
))
1521 /* We don't put these specializations in the hash table, but we might
1522 want to give an error about a mismatch. */
1523 fn
= retrieve_specialization (tmpl
, args
, 0);
1531 hash
= spec_hasher::hash (&elt
);
1534 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1536 fn
= ((spec_entry
*) *slot
)->spec
;
1541 /* We can sometimes try to re-register a specialization that we've
1542 already got. In particular, regenerate_decl_from_template calls
1543 duplicate_decls which will update the specialization list. But,
1544 we'll still get called again here anyhow. It's more convenient
1545 to simply allow this than to try to prevent it. */
1548 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1550 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1552 if (DECL_ODR_USED (fn
)
1553 || DECL_EXPLICIT_INSTANTIATION (fn
))
1555 error ("specialization of %qD after instantiation",
1557 return error_mark_node
;
1562 /* This situation should occur only if the first
1563 specialization is an implicit instantiation, the
1564 second is an explicit specialization, and the
1565 implicit instantiation has not yet been used. That
1566 situation can occur if we have implicitly
1567 instantiated a member function and then specialized
1570 We can also wind up here if a friend declaration that
1571 looked like an instantiation turns out to be a
1574 template <class T> void foo(T);
1575 class S { friend void foo<>(int) };
1576 template <> void foo(int);
1578 We transform the existing DECL in place so that any
1579 pointers to it become pointers to the updated
1582 If there was a definition for the template, but not
1583 for the specialization, we want this to look as if
1584 there were no definition, and vice versa. */
1585 DECL_INITIAL (fn
) = NULL_TREE
;
1586 duplicate_decls (spec
, fn
, is_friend
);
1587 /* The call to duplicate_decls will have applied
1590 An explicit specialization of a function template
1591 is inline only if it is explicitly declared to be,
1592 and independently of whether its function template
1595 to the primary function; now copy the inline bits to
1596 the various clones. */
1597 FOR_EACH_CLONE (clone
, fn
)
1599 DECL_DECLARED_INLINE_P (clone
)
1600 = DECL_DECLARED_INLINE_P (fn
);
1601 DECL_SOURCE_LOCATION (clone
)
1602 = DECL_SOURCE_LOCATION (fn
);
1603 DECL_DELETED_FN (clone
)
1604 = DECL_DELETED_FN (fn
);
1606 check_specialization_namespace (tmpl
);
1611 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1613 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1614 if (dd
== error_mark_node
)
1615 /* We've already complained in duplicate_decls. */
1616 return error_mark_node
;
1618 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1619 /* Dup decl failed, but this is a new definition. Set the
1620 line number so any errors match this new
1622 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1628 return duplicate_decls (spec
, fn
, is_friend
);
1630 /* A specialization must be declared in the same namespace as the
1631 template it is specializing. */
1632 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1633 && !check_specialization_namespace (tmpl
))
1634 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1636 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1638 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1639 gcc_assert (tmpl
&& args
&& spec
);
1642 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1643 && PRIMARY_TEMPLATE_P (tmpl
)
1644 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1645 || variable_template_p (tmpl
))
1646 /* If TMPL is a forward declaration of a template function, keep a list
1647 of all specializations in case we need to reassign them to a friend
1648 template later in tsubst_friend_function.
1650 Also keep a list of all variable template instantiations so that
1651 process_partial_specialization can check whether a later partial
1652 specialization would have used it. */
1653 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1654 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1660 /* Returns true iff two spec_entry nodes are equivalent. */
1662 int comparing_specializations
;
1665 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1669 ++comparing_specializations
;
1670 equal
= (e1
->tmpl
== e2
->tmpl
1671 && comp_template_args (e1
->args
, e2
->args
));
1672 if (equal
&& flag_concepts
1673 /* tmpl could be a FIELD_DECL for a capture pack. */
1674 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1675 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1676 && uses_template_parms (e1
->args
))
1678 /* Partial specializations of a variable template can be distinguished by
1680 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1681 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1682 equal
= equivalent_constraints (c1
, c2
);
1684 --comparing_specializations
;
1689 /* Returns a hash for a template TMPL and template arguments ARGS. */
1692 hash_tmpl_and_args (tree tmpl
, tree args
)
1694 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1695 return iterative_hash_template_arg (args
, val
);
1698 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1702 spec_hasher::hash (spec_entry
*e
)
1704 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1707 /* Recursively calculate a hash value for a template argument ARG, for use
1708 in the hash tables of template specializations. */
1711 iterative_hash_template_arg (tree arg
, hashval_t val
)
1713 unsigned HOST_WIDE_INT i
;
1714 enum tree_code code
;
1717 if (arg
== NULL_TREE
)
1718 return iterative_hash_object (arg
, val
);
1723 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1726 code
= TREE_CODE (arg
);
1727 tclass
= TREE_CODE_CLASS (code
);
1729 val
= iterative_hash_object (code
, val
);
1736 case IDENTIFIER_NODE
:
1737 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1741 int i
, len
= TREE_VEC_LENGTH (arg
);
1742 for (i
= 0; i
< len
; ++i
)
1743 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1747 case TYPE_PACK_EXPANSION
:
1748 case EXPR_PACK_EXPANSION
:
1749 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1750 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1752 case TYPE_ARGUMENT_PACK
:
1753 case NONTYPE_ARGUMENT_PACK
:
1754 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1757 for (; arg
; arg
= TREE_CHAIN (arg
))
1758 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1762 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1763 val
= iterative_hash_template_arg (*iter
, val
);
1769 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1770 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1772 val
= iterative_hash_template_arg (field
, val
);
1773 val
= iterative_hash_template_arg (value
, val
);
1779 if (!DECL_ARTIFICIAL (arg
))
1781 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1782 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1784 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1787 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1790 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1791 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1793 case TEMPLATE_PARM_INDEX
:
1794 val
= iterative_hash_template_arg
1795 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1796 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1797 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1800 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1801 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1802 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1805 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1807 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1811 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1812 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1813 val
= iterative_hash_object (code
, val
);
1814 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1817 /* A lambda can't appear in a template arg, but don't crash on
1819 gcc_assert (seen_error ());
1823 case IMPLICIT_CONV_EXPR
:
1824 case STATIC_CAST_EXPR
:
1825 case REINTERPRET_CAST_EXPR
:
1826 case CONST_CAST_EXPR
:
1827 case DYNAMIC_CAST_EXPR
:
1829 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1830 /* Now hash operands as usual. */
1840 if (alias_template_specialization_p (arg
))
1842 // We want an alias specialization that survived strip_typedefs
1843 // to hash differently from its TYPE_CANONICAL, to avoid hash
1844 // collisions that compare as different in template_args_equal.
1845 // These could be dependent specializations that strip_typedefs
1846 // left alone, or untouched specializations because
1847 // coerce_template_parms returns the unconverted template
1848 // arguments if it sees incomplete argument packs.
1849 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1850 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1852 if (TYPE_CANONICAL (arg
))
1853 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1855 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1856 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1857 /* Otherwise just compare the types during lookup. */
1860 case tcc_declaration
:
1862 return iterative_hash_expr (arg
, val
);
1865 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1867 unsigned n
= cp_tree_operand_length (arg
);
1868 for (i
= 0; i
< n
; ++i
)
1869 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1877 /* Unregister the specialization SPEC as a specialization of TMPL.
1878 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1879 if the SPEC was listed as a specialization of TMPL.
1881 Note that SPEC has been ggc_freed, so we can't look inside it. */
1884 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1889 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1890 elt
.args
= TI_ARGS (tinfo
);
1891 elt
.spec
= NULL_TREE
;
1893 entry
= decl_specializations
->find (&elt
);
1896 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1897 gcc_assert (new_spec
!= NULL_TREE
);
1898 entry
->spec
= new_spec
;
1905 /* Like register_specialization, but for local declarations. We are
1906 registering SPEC, an instantiation of TMPL. */
1909 register_local_specialization (tree spec
, tree tmpl
)
1911 gcc_assert (tmpl
!= spec
);
1912 local_specializations
->put (tmpl
, spec
);
1915 /* TYPE is a class type. Returns true if TYPE is an explicitly
1916 specialized class. */
1919 explicit_class_specialization_p (tree type
)
1921 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1923 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1926 /* Print the list of functions at FNS, going through all the overloads
1927 for each element of the list. Alternatively, FNS can not be a
1928 TREE_LIST, in which case it will be printed together with all the
1931 MORE and *STR should respectively be FALSE and NULL when the function
1932 is called from the outside. They are used internally on recursive
1933 calls. print_candidates manages the two parameters and leaves NULL
1934 in *STR when it ends. */
1937 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1939 if (TREE_CODE (fns
) == TREE_LIST
)
1940 for (; fns
; fns
= TREE_CHAIN (fns
))
1941 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1943 for (lkp_iterator
iter (fns
); iter
;)
1948 const char *pfx
= *str
;
1952 pfx
= _("candidates are:");
1954 pfx
= _("candidate is:");
1955 *str
= get_spaces (pfx
);
1957 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1961 /* Print the list of candidate FNS in an error message. FNS can also
1962 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1965 print_candidates (tree fns
)
1968 print_candidates_1 (fns
, &str
);
1972 /* Get a (possibly) constrained template declaration for the
1973 purpose of ordering candidates. */
1975 get_template_for_ordering (tree list
)
1977 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1978 tree f
= TREE_VALUE (list
);
1979 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1980 return TI_TEMPLATE (ti
);
1984 /* Among candidates having the same signature, return the
1985 most constrained or NULL_TREE if there is no best candidate.
1986 If the signatures of candidates vary (e.g., template
1987 specialization vs. member function), then there can be no
1990 Note that we don't compare constraints on the functions
1991 themselves, but rather those of their templates. */
1993 most_constrained_function (tree candidates
)
1995 // Try to find the best candidate in a first pass.
1996 tree champ
= candidates
;
1997 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1999 int winner
= more_constrained (get_template_for_ordering (champ
),
2000 get_template_for_ordering (c
));
2002 champ
= c
; // The candidate is more constrained
2003 else if (winner
== 0)
2004 return NULL_TREE
; // Neither is more constrained
2007 // Verify that the champ is better than previous candidates.
2008 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2009 if (!more_constrained (get_template_for_ordering (champ
),
2010 get_template_for_ordering (c
)))
2018 /* Returns the template (one of the functions given by TEMPLATE_ID)
2019 which can be specialized to match the indicated DECL with the
2020 explicit template args given in TEMPLATE_ID. The DECL may be
2021 NULL_TREE if none is available. In that case, the functions in
2022 TEMPLATE_ID are non-members.
2024 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2025 specialization of a member template.
2027 The TEMPLATE_COUNT is the number of references to qualifying
2028 template classes that appeared in the name of the function. See
2029 check_explicit_specialization for a more accurate description.
2031 TSK indicates what kind of template declaration (if any) is being
2032 declared. TSK_TEMPLATE indicates that the declaration given by
2033 DECL, though a FUNCTION_DECL, has template parameters, and is
2034 therefore a template function.
2036 The template args (those explicitly specified and those deduced)
2037 are output in a newly created vector *TARGS_OUT.
2039 If it is impossible to determine the result, an error message is
2040 issued. The error_mark_node is returned to indicate failure. */
2043 determine_specialization (tree template_id
,
2046 int need_member_template
,
2052 tree explicit_targs
;
2053 tree candidates
= NULL_TREE
;
2055 /* A TREE_LIST of templates of which DECL may be a specialization.
2056 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2057 corresponding TREE_PURPOSE is the set of template arguments that,
2058 when used to instantiate the template, would produce a function
2059 with the signature of DECL. */
2060 tree templates
= NULL_TREE
;
2062 cp_binding_level
*b
;
2064 *targs_out
= NULL_TREE
;
2066 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2067 return error_mark_node
;
2069 /* We shouldn't be specializing a member template of an
2070 unspecialized class template; we already gave an error in
2071 check_specialization_scope, now avoid crashing. */
2073 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2074 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2076 gcc_assert (errorcount
);
2077 return error_mark_node
;
2080 fns
= TREE_OPERAND (template_id
, 0);
2081 explicit_targs
= TREE_OPERAND (template_id
, 1);
2083 if (fns
== error_mark_node
)
2084 return error_mark_node
;
2086 /* Check for baselinks. */
2087 if (BASELINK_P (fns
))
2088 fns
= BASELINK_FUNCTIONS (fns
);
2090 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2092 error ("%qD is not a function template", fns
);
2093 return error_mark_node
;
2095 else if (VAR_P (decl
) && !variable_template_p (fns
))
2097 error ("%qD is not a variable template", fns
);
2098 return error_mark_node
;
2101 /* Count the number of template headers specified for this
2104 for (b
= current_binding_level
;
2105 b
->kind
== sk_template_parms
;
2109 tree orig_fns
= fns
;
2111 if (variable_template_p (fns
))
2113 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2114 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2115 tf_warning_or_error
,
2116 /*req_all*/true, /*use_defarg*/true);
2117 if (targs
!= error_mark_node
)
2118 templates
= tree_cons (targs
, fns
, templates
);
2120 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2124 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2126 tree decl_arg_types
;
2130 /* In case of explicit specialization, we need to check if
2131 the number of template headers appearing in the specialization
2132 is correct. This is usually done in check_explicit_specialization,
2133 but the check done there cannot be exhaustive when specializing
2134 member functions. Consider the following code:
2136 template <> void A<int>::f(int);
2137 template <> template <> void A<int>::f(int);
2139 Assuming that A<int> is not itself an explicit specialization
2140 already, the first line specializes "f" which is a non-template
2141 member function, whilst the second line specializes "f" which
2142 is a template member function. So both lines are syntactically
2143 correct, and check_explicit_specialization does not reject
2146 Here, we can do better, as we are matching the specialization
2147 against the declarations. We count the number of template
2148 headers, and we check if they match TEMPLATE_COUNT + 1
2149 (TEMPLATE_COUNT is the number of qualifying template classes,
2150 plus there must be another header for the member template
2153 Notice that if header_count is zero, this is not a
2154 specialization but rather a template instantiation, so there
2155 is no check we can perform here. */
2156 if (header_count
&& header_count
!= template_count
+ 1)
2159 /* Check that the number of template arguments at the
2160 innermost level for DECL is the same as for FN. */
2161 if (current_binding_level
->kind
== sk_template_parms
2162 && !current_binding_level
->explicit_spec_p
2163 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2164 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2165 (current_template_parms
))))
2168 /* DECL might be a specialization of FN. */
2169 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2170 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2172 /* For a non-static member function, we need to make sure
2173 that the const qualification is the same. Since
2174 get_bindings does not try to merge the "this" parameter,
2175 we must do the comparison explicitly. */
2176 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2178 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2179 TREE_VALUE (decl_arg_types
)))
2182 /* And the ref-qualification. */
2183 if (type_memfn_rqual (TREE_TYPE (decl
))
2184 != type_memfn_rqual (TREE_TYPE (fn
)))
2188 /* Skip the "this" parameter and, for constructors of
2189 classes with virtual bases, the VTT parameter. A
2190 full specialization of a constructor will have a VTT
2191 parameter, but a template never will. */
2193 = skip_artificial_parms_for (decl
, decl_arg_types
);
2195 = skip_artificial_parms_for (fn
, fn_arg_types
);
2197 /* Function templates cannot be specializations; there are
2198 no partial specializations of functions. Therefore, if
2199 the type of DECL does not match FN, there is no
2202 Note that it should never be the case that we have both
2203 candidates added here, and for regular member functions
2205 if (tsk
== tsk_template
)
2207 if (compparms (fn_arg_types
, decl_arg_types
))
2208 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2212 /* See whether this function might be a specialization of this
2213 template. Suppress access control because we might be trying
2214 to make this specialization a friend, and we have already done
2215 access control for the declaration of the specialization. */
2216 push_deferring_access_checks (dk_no_check
);
2217 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2218 pop_deferring_access_checks ();
2221 /* We cannot deduce template arguments that when used to
2222 specialize TMPL will produce DECL. */
2225 if (uses_template_parms (targs
))
2226 /* We deduced something involving 'auto', which isn't a valid
2227 template argument. */
2230 /* Remove, from the set of candidates, all those functions
2231 whose constraints are not satisfied. */
2232 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2235 // Then, try to form the new function type.
2236 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2237 if (insttype
== error_mark_node
)
2240 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2241 if (!compparms (fn_arg_types
, decl_arg_types
))
2244 /* Save this template, and the arguments deduced. */
2245 templates
= tree_cons (targs
, fn
, templates
);
2247 else if (need_member_template
)
2248 /* FN is an ordinary member function, and we need a
2249 specialization of a member template. */
2251 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2252 /* We can get IDENTIFIER_NODEs here in certain erroneous
2255 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2256 /* This is just an ordinary non-member function. Nothing can
2257 be a specialization of that. */
2259 else if (DECL_ARTIFICIAL (fn
))
2260 /* Cannot specialize functions that are created implicitly. */
2264 tree decl_arg_types
;
2266 /* This is an ordinary member function. However, since
2267 we're here, we can assume its enclosing class is a
2268 template class. For example,
2270 template <typename T> struct S { void f(); };
2271 template <> void S<int>::f() {}
2273 Here, S<int>::f is a non-template, but S<int> is a
2274 template class. If FN has the same type as DECL, we
2275 might be in business. */
2277 if (!DECL_TEMPLATE_INFO (fn
))
2278 /* Its enclosing class is an explicit specialization
2279 of a template class. This is not a candidate. */
2282 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2283 TREE_TYPE (TREE_TYPE (fn
))))
2284 /* The return types differ. */
2287 /* Adjust the type of DECL in case FN is a static member. */
2288 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2289 if (DECL_STATIC_FUNCTION_P (fn
)
2290 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2291 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2293 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2297 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2298 && (type_memfn_rqual (TREE_TYPE (decl
))
2299 != type_memfn_rqual (TREE_TYPE (fn
))))
2302 // If the deduced arguments do not satisfy the constraints,
2303 // this is not a candidate.
2304 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2307 // Add the candidate.
2308 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2312 if (templates
&& TREE_CHAIN (templates
))
2318 It is possible for a specialization with a given function
2319 signature to be instantiated from more than one function
2320 template. In such cases, explicit specification of the
2321 template arguments must be used to uniquely identify the
2322 function template specialization being specialized.
2324 Note that here, there's no suggestion that we're supposed to
2325 determine which of the candidate templates is most
2326 specialized. However, we, also have:
2330 Partial ordering of overloaded function template
2331 declarations is used in the following contexts to select
2332 the function template to which a function template
2333 specialization refers:
2335 -- when an explicit specialization refers to a function
2338 So, we do use the partial ordering rules, at least for now.
2339 This extension can only serve to make invalid programs valid,
2340 so it's safe. And, there is strong anecdotal evidence that
2341 the committee intended the partial ordering rules to apply;
2342 the EDG front end has that behavior, and John Spicer claims
2343 that the committee simply forgot to delete the wording in
2344 [temp.expl.spec]. */
2345 tree tmpl
= most_specialized_instantiation (templates
);
2346 if (tmpl
!= error_mark_node
)
2349 TREE_CHAIN (templates
) = NULL_TREE
;
2353 // Concepts allows multiple declarations of member functions
2354 // with the same signature. Like above, we need to rely on
2355 // on the partial ordering of those candidates to determine which
2357 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2359 if (tree cand
= most_constrained_function (candidates
))
2362 TREE_CHAIN (cand
) = NULL_TREE
;
2366 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2368 error ("template-id %qD for %q+D does not match any template "
2369 "declaration", template_id
, decl
);
2370 if (header_count
&& header_count
!= template_count
+ 1)
2371 inform (input_location
, "saw %d %<template<>%>, need %d for "
2372 "specializing a member function template",
2373 header_count
, template_count
+ 1);
2375 print_candidates (orig_fns
);
2376 return error_mark_node
;
2378 else if ((templates
&& TREE_CHAIN (templates
))
2379 || (candidates
&& TREE_CHAIN (candidates
))
2380 || (templates
&& candidates
))
2382 error ("ambiguous template specialization %qD for %q+D",
2384 candidates
= chainon (candidates
, templates
);
2385 print_candidates (candidates
);
2386 return error_mark_node
;
2389 /* We have one, and exactly one, match. */
2392 tree fn
= TREE_VALUE (candidates
);
2393 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2395 // Propagate the candidate's constraints to the declaration.
2396 set_constraints (decl
, get_constraints (fn
));
2398 /* DECL is a re-declaration or partial instantiation of a template
2400 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2402 /* It was a specialization of an ordinary member function in a
2404 return DECL_TI_TEMPLATE (fn
);
2407 /* It was a specialization of a template. */
2408 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2409 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2411 *targs_out
= copy_node (targs
);
2412 SET_TMPL_ARGS_LEVEL (*targs_out
,
2413 TMPL_ARGS_DEPTH (*targs_out
),
2414 TREE_PURPOSE (templates
));
2417 *targs_out
= TREE_PURPOSE (templates
);
2418 return TREE_VALUE (templates
);
2421 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2422 but with the default argument values filled in from those in the
2426 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2429 tree new_spec_types
;
2434 if (spec_types
== void_list_node
)
2435 return void_list_node
;
2437 /* Substitute into the rest of the list. */
2439 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2440 TREE_CHAIN (tmpl_types
));
2442 /* Add the default argument for this parameter. */
2443 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2444 TREE_VALUE (spec_types
),
2448 /* DECL is an explicit specialization. Replicate default arguments
2449 from the template it specializes. (That way, code like:
2451 template <class T> void f(T = 3);
2452 template <> void f(double);
2455 works, as required.) An alternative approach would be to look up
2456 the correct default arguments at the call-site, but this approach
2457 is consistent with how implicit instantiations are handled. */
2460 copy_default_args_to_explicit_spec (tree decl
)
2465 tree new_spec_types
;
2469 tree object_type
= NULL_TREE
;
2470 tree in_charge
= NULL_TREE
;
2471 tree vtt
= NULL_TREE
;
2473 /* See if there's anything we need to do. */
2474 tmpl
= DECL_TI_TEMPLATE (decl
);
2475 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2476 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2477 if (TREE_PURPOSE (t
))
2482 old_type
= TREE_TYPE (decl
);
2483 spec_types
= TYPE_ARG_TYPES (old_type
);
2485 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2487 /* Remove the this pointer, but remember the object's type for
2489 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2490 spec_types
= TREE_CHAIN (spec_types
);
2491 tmpl_types
= TREE_CHAIN (tmpl_types
);
2493 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2495 /* DECL may contain more parameters than TMPL due to the extra
2496 in-charge parameter in constructors and destructors. */
2497 in_charge
= spec_types
;
2498 spec_types
= TREE_CHAIN (spec_types
);
2500 if (DECL_HAS_VTT_PARM_P (decl
))
2503 spec_types
= TREE_CHAIN (spec_types
);
2507 /* Compute the merged default arguments. */
2509 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2511 /* Compute the new FUNCTION_TYPE. */
2515 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2520 /* Put the in-charge parameter back. */
2521 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2522 TREE_VALUE (in_charge
),
2525 new_type
= build_method_type_directly (object_type
,
2526 TREE_TYPE (old_type
),
2530 new_type
= build_function_type (TREE_TYPE (old_type
),
2532 new_type
= cp_build_type_attribute_variant (new_type
,
2533 TYPE_ATTRIBUTES (old_type
));
2534 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2536 TREE_TYPE (decl
) = new_type
;
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2543 num_template_headers_for_class (tree ctype
)
2545 int num_templates
= 0;
2547 while (ctype
&& CLASS_TYPE_P (ctype
))
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2564 if (explicit_class_specialization_p (ctype
))
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2569 ctype
= TYPE_CONTEXT (ctype
);
2572 return num_templates
;
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2579 check_template_variable (tree decl
)
2581 tree ctx
= CP_DECL_CONTEXT (decl
);
2582 int wanted
= num_template_headers_for_class (ctx
);
2583 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2586 if (cxx_dialect
< cxx14
)
2587 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2591 // Namespace-scope variable templates should have a template header.
2594 if (template_header_count
> wanted
)
2596 auto_diagnostic_group d
;
2597 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2598 "too many template headers for %qD "
2601 if (warned
&& CLASS_TYPE_P (ctx
)
2602 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2603 inform (DECL_SOURCE_LOCATION (decl
),
2604 "members of an explicitly specialized class are defined "
2605 "without a template header");
2609 /* An explicit specialization whose declarator-id or class-head-name is not
2610 qualified shall be declared in the nearest enclosing namespace of the
2611 template, or, if the namespace is inline (7.3.1), any namespace from its
2612 enclosing namespace set.
2614 If the name declared in the explicit instantiation is an unqualified name,
2615 the explicit instantiation shall appear in the namespace where its template
2616 is declared or, if that namespace is inline (7.3.1), any namespace from its
2617 enclosing namespace set. */
2620 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2622 tree tmpl
= most_general_template (t
);
2623 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2624 && !is_nested_namespace (current_namespace
,
2625 CP_DECL_CONTEXT (tmpl
), true))
2627 if (processing_specialization
)
2628 permerror (loc
, "explicit specialization of %qD outside its "
2629 "namespace must use a nested-name-specifier", tmpl
);
2630 else if (processing_explicit_instantiation
2631 && cxx_dialect
>= cxx11
)
2632 /* This was allowed in C++98, so only pedwarn. */
2633 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2634 "outside its namespace must use a nested-name-"
2639 /* Warn for a template specialization SPEC that is missing some of a set
2640 of function or type attributes that the template TEMPL is declared with.
2641 ATTRLIST is a list of additional attributes that SPEC should be taken
2642 to ultimately be declared with. */
2645 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2647 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2648 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2650 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2653 /* Avoid warning if either declaration or its type is deprecated. */
2654 if (TREE_DEPRECATED (tmpl
)
2655 || TREE_DEPRECATED (spec
))
2658 tree tmpl_type
= TREE_TYPE (tmpl
);
2659 tree spec_type
= TREE_TYPE (spec
);
2661 if (TREE_DEPRECATED (tmpl_type
)
2662 || TREE_DEPRECATED (spec_type
)
2663 || TREE_DEPRECATED (TREE_TYPE (tmpl_type
))
2664 || TREE_DEPRECATED (TREE_TYPE (spec_type
)))
2667 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpl_type
) };
2668 tree spec_attrs
[] = { DECL_ATTRIBUTES (spec
), TYPE_ATTRIBUTES (spec_type
) };
2671 spec_attrs
[0] = attrlist
;
2672 else if (!spec_attrs
[1])
2673 spec_attrs
[1] = attrlist
;
2675 /* Avoid warning if the primary has no attributes. */
2676 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2679 /* Avoid warning if either declaration contains an attribute on
2680 the white list below. */
2681 const char* const whitelist
[] = {
2685 for (unsigned i
= 0; i
!= 2; ++i
)
2686 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
2687 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2688 || lookup_attribute (whitelist
[j
], spec_attrs
[i
]))
2691 /* Avoid warning if the difference between the primary and
2692 the specialization is not in one of the attributes below. */
2693 const char* const blacklist
[] = {
2694 "alloc_align", "alloc_size", "assume_aligned", "format",
2695 "format_arg", "malloc", "nonnull"
2698 /* Put together a list of the black listed attributes that the primary
2699 template is declared with that the specialization is not, in case
2700 it's not apparent from the most recent declaration of the primary. */
2701 unsigned nattrs
= 0;
2704 for (unsigned i
= 0; i
!= sizeof blacklist
/ sizeof *blacklist
; ++i
)
2706 for (unsigned j
= 0; j
!= 2; ++j
)
2708 if (!lookup_attribute (blacklist
[i
], tmpl_attrs
[j
]))
2711 for (unsigned k
= 0; k
!= 1 + !!spec_attrs
[1]; ++k
)
2713 if (lookup_attribute (blacklist
[i
], spec_attrs
[k
]))
2717 pp_string (&str
, ", ");
2718 pp_begin_quote (&str
, pp_show_color (global_dc
->printer
));
2719 pp_string (&str
, blacklist
[i
]);
2720 pp_end_quote (&str
, pp_show_color (global_dc
->printer
));
2729 auto_diagnostic_group d
;
2730 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2731 "explicit specialization %q#D may be missing attributes",
2733 inform (DECL_SOURCE_LOCATION (tmpl
),
2735 ? G_("missing primary template attributes %s")
2736 : G_("missing primary template attribute %s"),
2737 pp_formatted_text (&str
));
2740 /* Check to see if the function just declared, as indicated in
2741 DECLARATOR, and in DECL, is a specialization of a function
2742 template. We may also discover that the declaration is an explicit
2743 instantiation at this point.
2745 Returns DECL, or an equivalent declaration that should be used
2746 instead if all goes well. Issues an error message if something is
2747 amiss. Returns error_mark_node if the error is not easily
2750 FLAGS is a bitmask consisting of the following flags:
2752 2: The function has a definition.
2753 4: The function is a friend.
2755 The TEMPLATE_COUNT is the number of references to qualifying
2756 template classes that appeared in the name of the function. For
2759 template <class T> struct S { void f(); };
2762 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2763 classes are not counted in the TEMPLATE_COUNT, so that in
2765 template <class T> struct S {};
2766 template <> struct S<int> { void f(); }
2767 template <> void S<int>::f();
2769 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2770 invalid; there should be no template <>.)
2772 If the function is a specialization, it is marked as such via
2773 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2774 is set up correctly, and it is added to the list of specializations
2775 for that template. */
2778 check_explicit_specialization (tree declarator
,
2784 int have_def
= flags
& 2;
2785 int is_friend
= flags
& 4;
2786 bool is_concept
= flags
& 8;
2787 int specialization
= 0;
2788 int explicit_instantiation
= 0;
2789 int member_specialization
= 0;
2790 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2791 tree dname
= DECL_NAME (decl
);
2796 if (!processing_specialization
)
2799 tsk
= tsk_excessive_parms
;
2802 tsk
= current_tmpl_spec_kind (template_count
);
2807 if (processing_specialization
&& !VAR_P (decl
))
2810 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2812 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2815 /* This could be something like:
2817 template <class T> void f(T);
2818 class S { friend void f<>(int); } */
2822 /* This case handles bogus declarations like template <>
2823 template <class T> void f<int>(); */
2825 error ("template-id %qD in declaration of primary template",
2832 case tsk_invalid_member_spec
:
2833 /* The error has already been reported in
2834 check_specialization_scope. */
2835 return error_mark_node
;
2837 case tsk_invalid_expl_inst
:
2838 error ("template parameter list used in explicit instantiation");
2844 error ("definition provided for explicit instantiation");
2846 explicit_instantiation
= 1;
2849 case tsk_excessive_parms
:
2850 case tsk_insufficient_parms
:
2851 if (tsk
== tsk_excessive_parms
)
2852 error ("too many template parameter lists in declaration of %qD",
2854 else if (template_header_count
)
2855 error("too few template parameter lists in declaration of %qD", decl
);
2857 error("explicit specialization of %qD must be introduced by "
2858 "%<template <>%>", decl
);
2863 error ("explicit specialization declared %<concept%>");
2865 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2866 /* In cases like template<> constexpr bool v = true;
2867 We'll give an error in check_template_variable. */
2870 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2872 member_specialization
= 1;
2878 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2880 /* This case handles bogus declarations like template <>
2881 template <class T> void f<int>(); */
2883 if (!uses_template_parms (declarator
))
2884 error ("template-id %qD in declaration of primary template",
2886 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2888 /* Partial specialization of variable template. */
2889 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2893 else if (cxx_dialect
< cxx14
)
2894 error ("non-type partial specialization %qD "
2895 "is not allowed", declarator
);
2897 error ("non-class, non-variable partial specialization %qD "
2898 "is not allowed", declarator
);
2903 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2904 /* This is a specialization of a member template, without
2905 specialization the containing class. Something like:
2907 template <class T> struct S {
2908 template <class U> void f (U);
2910 template <> template <class U> void S<int>::f(U) {}
2912 That's a specialization -- but of the entire template. */
2920 if ((specialization
|| member_specialization
)
2921 /* This doesn't apply to variable templates. */
2922 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2923 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2925 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2926 for (; t
; t
= TREE_CHAIN (t
))
2927 if (TREE_PURPOSE (t
))
2929 permerror (input_location
,
2930 "default argument specified in explicit specialization");
2935 if (specialization
|| member_specialization
|| explicit_instantiation
)
2937 tree tmpl
= NULL_TREE
;
2938 tree targs
= NULL_TREE
;
2939 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2941 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2942 if (!was_template_id
)
2946 gcc_assert (identifier_p (declarator
));
2951 /* If there is no class context, the explicit instantiation
2952 must be at namespace scope. */
2953 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2955 /* Find the namespace binding, using the declaration
2957 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2959 if (fns
== error_mark_node
)
2960 /* If lookup fails, look for a friend declaration so we can
2961 give a better diagnostic. */
2962 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2963 /*type*/false, /*complain*/true,
2966 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2968 error ("%qD is not a template function", dname
);
2969 fns
= error_mark_node
;
2973 declarator
= lookup_template_function (fns
, NULL_TREE
);
2976 if (declarator
== error_mark_node
)
2977 return error_mark_node
;
2979 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2981 if (!explicit_instantiation
)
2982 /* A specialization in class scope. This is invalid,
2983 but the error will already have been flagged by
2984 check_specialization_scope. */
2985 return error_mark_node
;
2988 /* It's not valid to write an explicit instantiation in
2991 class C { template void f(); }
2993 This case is caught by the parser. However, on
2996 template class C { void f(); };
2998 (which is invalid) we can get here. The error will be
3005 else if (ctype
!= NULL_TREE
3006 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3008 // We'll match variable templates in start_decl.
3012 /* Find the list of functions in ctype that have the same
3013 name as the declared function. */
3014 tree name
= TREE_OPERAND (declarator
, 0);
3016 if (constructor_name_p (name
, ctype
))
3018 if (DECL_CONSTRUCTOR_P (decl
)
3019 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3020 : !CLASSTYPE_DESTRUCTOR (ctype
))
3022 /* From [temp.expl.spec]:
3024 If such an explicit specialization for the member
3025 of a class template names an implicitly-declared
3026 special member function (clause _special_), the
3027 program is ill-formed.
3029 Similar language is found in [temp.explicit]. */
3030 error ("specialization of implicitly-declared special member function");
3031 return error_mark_node
;
3034 name
= DECL_NAME (decl
);
3037 /* For a type-conversion operator, We might be looking for
3038 `operator int' which will be a specialization of
3039 `operator T'. Grab all the conversion operators, and
3040 then select from them. */
3041 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3042 ? conv_op_identifier
: name
);
3044 if (fns
== NULL_TREE
)
3046 error ("no member function %qD declared in %qT", name
, ctype
);
3047 return error_mark_node
;
3050 TREE_OPERAND (declarator
, 0) = fns
;
3053 /* Figure out what exactly is being specialized at this point.
3054 Note that for an explicit instantiation, even one for a
3055 member function, we cannot tell a priori whether the
3056 instantiation is for a member template, or just a member
3057 function of a template class. Even if a member template is
3058 being instantiated, the member template arguments may be
3059 elided if they can be deduced from the rest of the
3061 tmpl
= determine_specialization (declarator
, decl
,
3063 member_specialization
,
3067 if (!tmpl
|| tmpl
== error_mark_node
)
3068 /* We couldn't figure out what this declaration was
3070 return error_mark_node
;
3073 if (TREE_CODE (decl
) == FUNCTION_DECL
3074 && DECL_HIDDEN_FRIEND_P (tmpl
))
3076 auto_diagnostic_group d
;
3077 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3078 "friend declaration %qD is not visible to "
3079 "explicit specialization", tmpl
))
3080 inform (DECL_SOURCE_LOCATION (tmpl
),
3081 "friend declaration here");
3083 else if (!ctype
&& !is_friend
3084 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3085 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3087 tree gen_tmpl
= most_general_template (tmpl
);
3089 if (explicit_instantiation
)
3091 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3092 is done by do_decl_instantiation later. */
3094 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3095 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3097 if (arg_depth
> parm_depth
)
3099 /* If TMPL is not the most general template (for
3100 example, if TMPL is a friend template that is
3101 injected into namespace scope), then there will
3102 be too many levels of TARGS. Remove some of them
3107 new_targs
= make_tree_vec (parm_depth
);
3108 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3109 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3110 = TREE_VEC_ELT (targs
, i
);
3114 return instantiate_template (tmpl
, targs
, tf_error
);
3117 /* If we thought that the DECL was a member function, but it
3118 turns out to be specializing a static member function,
3119 make DECL a static member function as well. */
3120 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3121 && DECL_STATIC_FUNCTION_P (tmpl
)
3122 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3123 revert_static_member_fn (decl
);
3125 /* If this is a specialization of a member template of a
3126 template class, we want to return the TEMPLATE_DECL, not
3127 the specialization of it. */
3128 if (tsk
== tsk_template
&& !was_template_id
)
3130 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3131 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3132 DECL_INITIAL (result
) = NULL_TREE
;
3136 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3137 DECL_SOURCE_LOCATION (result
)
3138 = DECL_SOURCE_LOCATION (decl
);
3139 /* We want to use the argument list specified in the
3140 definition, not in the original declaration. */
3141 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3142 for (parm
= DECL_ARGUMENTS (result
); parm
;
3143 parm
= DECL_CHAIN (parm
))
3144 DECL_CONTEXT (parm
) = result
;
3146 return register_specialization (tmpl
, gen_tmpl
, targs
,
3150 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3151 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3153 if (was_template_id
)
3154 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3156 /* Inherit default function arguments from the template
3157 DECL is specializing. */
3158 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3159 copy_default_args_to_explicit_spec (decl
);
3161 /* This specialization has the same protection as the
3162 template it specializes. */
3163 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3164 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3166 /* 7.1.1-1 [dcl.stc]
3168 A storage-class-specifier shall not be specified in an
3169 explicit specialization...
3171 The parser rejects these, so unless action is taken here,
3172 explicit function specializations will always appear with
3175 The action recommended by the C++ CWG in response to C++
3176 defect report 605 is to make the storage class and linkage
3177 of the explicit specialization match the templated function:
3179 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3181 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3183 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3184 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3186 /* A concept cannot be specialized. */
3187 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3189 error ("explicit specialization of function concept %qD",
3191 return error_mark_node
;
3194 /* This specialization has the same linkage and visibility as
3195 the function template it specializes. */
3196 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3197 if (! TREE_PUBLIC (decl
))
3199 DECL_INTERFACE_KNOWN (decl
) = 1;
3200 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3202 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3203 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3205 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3206 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3210 /* If DECL is a friend declaration, declared using an
3211 unqualified name, the namespace associated with DECL may
3212 have been set incorrectly. For example, in:
3214 template <typename T> void f(T);
3216 struct S { friend void f<int>(int); }
3219 we will have set the DECL_CONTEXT for the friend
3220 declaration to N, rather than to the global namespace. */
3221 if (DECL_NAMESPACE_SCOPE_P (decl
))
3222 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3224 if (is_friend
&& !have_def
)
3225 /* This is not really a declaration of a specialization.
3226 It's just the name of an instantiation. But, it's not
3227 a request for an instantiation, either. */
3228 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3229 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3230 /* A specialization is not necessarily COMDAT. */
3231 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3232 && DECL_DECLARED_INLINE_P (decl
));
3233 else if (VAR_P (decl
))
3234 DECL_COMDAT (decl
) = false;
3236 /* If this is a full specialization, register it so that we can find
3237 it again. Partial specializations will be registered in
3238 process_partial_specialization. */
3239 if (!processing_template_decl
)
3241 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3243 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3248 /* A 'structor should already have clones. */
3249 gcc_assert (decl
== error_mark_node
3250 || variable_template_p (tmpl
)
3251 || !(DECL_CONSTRUCTOR_P (decl
)
3252 || DECL_DESTRUCTOR_P (decl
))
3253 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3260 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3261 parameters. These are represented in the same format used for
3262 DECL_TEMPLATE_PARMS. */
3265 comp_template_parms (const_tree parms1
, const_tree parms2
)
3270 if (parms1
== parms2
)
3273 for (p1
= parms1
, p2
= parms2
;
3274 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3275 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3277 tree t1
= TREE_VALUE (p1
);
3278 tree t2
= TREE_VALUE (p2
);
3281 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3282 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3284 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3287 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3289 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3290 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3292 /* If either of the template parameters are invalid, assume
3293 they match for the sake of error recovery. */
3294 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3297 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3300 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3301 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3302 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3304 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3309 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3310 /* One set of parameters has more parameters lists than the
3317 /* Determine whether PARM is a parameter pack. */
3320 template_parameter_pack_p (const_tree parm
)
3322 /* Determine if we have a non-type template parameter pack. */
3323 if (TREE_CODE (parm
) == PARM_DECL
)
3324 return (DECL_TEMPLATE_PARM_P (parm
)
3325 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3326 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3327 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3329 /* If this is a list of template parameters, we could get a
3330 TYPE_DECL or a TEMPLATE_DECL. */
3331 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3332 parm
= TREE_TYPE (parm
);
3334 /* Otherwise it must be a type template parameter. */
3335 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3336 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3337 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3340 /* Determine if T is a function parameter pack. */
3343 function_parameter_pack_p (const_tree t
)
3345 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3346 return DECL_PACK_P (t
);
3350 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3351 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3354 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3356 if (! primary_func_tmpl_inst
3357 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3358 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3361 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3364 /* Return true iff the function parameter PARAM_DECL was expanded
3365 from the function parameter pack PACK. */
3368 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3370 if (DECL_ARTIFICIAL (param_decl
)
3371 || !function_parameter_pack_p (pack
))
3374 /* The parameter pack and its pack arguments have the same
3376 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3379 /* Determine whether ARGS describes a variadic template args list,
3380 i.e., one that is terminated by a template argument pack. */
3383 template_args_variadic_p (tree args
)
3388 if (args
== NULL_TREE
)
3391 args
= INNERMOST_TEMPLATE_ARGS (args
);
3392 nargs
= TREE_VEC_LENGTH (args
);
3397 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3399 return ARGUMENT_PACK_P (last_parm
);
3402 /* Generate a new name for the parameter pack name NAME (an
3403 IDENTIFIER_NODE) that incorporates its */
3406 make_ith_pack_parameter_name (tree name
, int i
)
3408 /* Munge the name to include the parameter index. */
3409 #define NUMBUF_LEN 128
3410 char numbuf
[NUMBUF_LEN
];
3414 if (name
== NULL_TREE
)
3416 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3417 newname_len
= IDENTIFIER_LENGTH (name
)
3418 + strlen (numbuf
) + 2;
3419 newname
= (char*)alloca (newname_len
);
3420 snprintf (newname
, newname_len
,
3421 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3422 return get_identifier (newname
);
3425 /* Return true if T is a primary function, class or alias template
3426 specialization, not including the template pattern. */
3429 primary_template_specialization_p (const_tree t
)
3434 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3435 return (DECL_LANG_SPECIFIC (t
)
3436 && DECL_USE_TEMPLATE (t
)
3437 && DECL_TEMPLATE_INFO (t
)
3438 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3439 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3440 return (CLASSTYPE_TEMPLATE_INFO (t
)
3441 && CLASSTYPE_USE_TEMPLATE (t
)
3442 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3443 else if (alias_template_specialization_p (t
))
3448 /* Return true if PARM is a template template parameter. */
3451 template_template_parameter_p (const_tree parm
)
3453 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3456 /* Return true iff PARM is a DECL representing a type template
3460 template_type_parameter_p (const_tree parm
)
3463 && (TREE_CODE (parm
) == TYPE_DECL
3464 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3465 && DECL_TEMPLATE_PARM_P (parm
));
3468 /* Return the template parameters of T if T is a
3469 primary template instantiation, NULL otherwise. */
3472 get_primary_template_innermost_parameters (const_tree t
)
3474 tree parms
= NULL
, template_info
= NULL
;
3476 if ((template_info
= get_template_info (t
))
3477 && primary_template_specialization_p (t
))
3478 parms
= INNERMOST_TEMPLATE_PARMS
3479 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3484 /* Return the template parameters of the LEVELth level from the full list
3485 of template parameters PARMS. */
3488 get_template_parms_at_level (tree parms
, int level
)
3492 || TREE_CODE (parms
) != TREE_LIST
3493 || level
> TMPL_PARMS_DEPTH (parms
))
3496 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3497 if (TMPL_PARMS_DEPTH (p
) == level
)
3503 /* Returns the template arguments of T if T is a template instantiation,
3507 get_template_innermost_arguments (const_tree t
)
3509 tree args
= NULL
, template_info
= NULL
;
3511 if ((template_info
= get_template_info (t
))
3512 && TI_ARGS (template_info
))
3513 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3518 /* Return the argument pack elements of T if T is a template argument pack,
3522 get_template_argument_pack_elems (const_tree t
)
3524 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3525 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3528 return ARGUMENT_PACK_ARGS (t
);
3531 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3532 ARGUMENT_PACK_SELECT represents. */
3535 argument_pack_select_arg (tree t
)
3537 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3538 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3540 /* If the selected argument is an expansion E, that most likely means we were
3541 called from gen_elem_of_pack_expansion_instantiation during the
3542 substituting of an argument pack (of which the Ith element is a pack
3543 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3544 In this case, the Ith element resulting from this substituting is going to
3545 be a pack expansion, which pattern is the pattern of E. Let's return the
3546 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3547 resulting pack expansion from it. */
3548 if (PACK_EXPANSION_P (arg
))
3550 /* Make sure we aren't throwing away arg info. */
3551 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3552 arg
= PACK_EXPANSION_PATTERN (arg
);
3559 /* True iff FN is a function representing a built-in variadic parameter
3563 builtin_pack_fn_p (tree fn
)
3566 || TREE_CODE (fn
) != FUNCTION_DECL
3567 || !DECL_IS_BUILTIN (fn
))
3570 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3576 /* True iff CALL is a call to a function representing a built-in variadic
3580 builtin_pack_call_p (tree call
)
3582 if (TREE_CODE (call
) != CALL_EXPR
)
3584 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3587 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3590 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3593 tree ohi
= CALL_EXPR_ARG (call
, 0);
3594 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3595 false/*fn*/, true/*int_cst*/);
3597 if (value_dependent_expression_p (hi
))
3601 call
= copy_node (call
);
3602 CALL_EXPR_ARG (call
, 0) = hi
;
3604 tree ex
= make_pack_expansion (call
, complain
);
3605 tree vec
= make_tree_vec (1);
3606 TREE_VEC_ELT (vec
, 0) = ex
;
3611 hi
= cxx_constant_value (hi
);
3612 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3614 /* Calculate the largest value of len that won't make the size of the vec
3615 overflow an int. The compiler will exceed resource limits long before
3616 this, but it seems a decent place to diagnose. */
3617 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3619 if (len
< 0 || len
> max
)
3621 if ((complain
& tf_error
)
3622 && hi
!= error_mark_node
)
3623 error ("argument to __integer_pack must be between 0 and %d", max
);
3624 return error_mark_node
;
3627 tree vec
= make_tree_vec (len
);
3629 for (int i
= 0; i
< len
; ++i
)
3630 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3636 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3640 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3643 if (!builtin_pack_call_p (call
))
3646 tree fn
= CALL_EXPR_FN (call
);
3648 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3649 return expand_integer_pack (call
, args
, complain
, in_decl
);
3654 /* Structure used to track the progress of find_parameter_packs_r. */
3655 struct find_parameter_pack_data
3657 /* TREE_LIST that will contain all of the parameter packs found by
3659 tree
* parameter_packs
;
3661 /* Set of AST nodes that have been visited by the traversal. */
3662 hash_set
<tree
> *visited
;
3664 /* True iff we're making a type pack expansion. */
3665 bool type_pack_expansion_p
;
3668 /* Identifies all of the argument packs that occur in a template
3669 argument and appends them to the TREE_LIST inside DATA, which is a
3670 find_parameter_pack_data structure. This is a subroutine of
3671 make_pack_expansion and uses_parameter_packs. */
3673 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3676 struct find_parameter_pack_data
* ppd
=
3677 (struct find_parameter_pack_data
*)data
;
3678 bool parameter_pack_p
= false;
3680 /* Handle type aliases/typedefs. */
3681 if (TYPE_ALIAS_P (t
))
3683 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3684 cp_walk_tree (&TI_ARGS (tinfo
),
3685 &find_parameter_packs_r
,
3691 /* Identify whether this is a parameter pack or not. */
3692 switch (TREE_CODE (t
))
3694 case TEMPLATE_PARM_INDEX
:
3695 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3696 parameter_pack_p
= true;
3699 case TEMPLATE_TYPE_PARM
:
3700 t
= TYPE_MAIN_VARIANT (t
);
3702 case TEMPLATE_TEMPLATE_PARM
:
3703 /* If the placeholder appears in the decl-specifier-seq of a function
3704 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3705 is a pack expansion, the invented template parameter is a template
3707 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3708 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3709 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3710 parameter_pack_p
= true;
3715 if (DECL_PACK_P (t
))
3717 /* We don't want to walk into the type of a PARM_DECL,
3718 because we don't want to see the type parameter pack. */
3720 parameter_pack_p
= true;
3725 if (DECL_PACK_P (t
))
3727 /* We don't want to walk into the type of a variadic capture proxy,
3728 because we don't want to see the type parameter pack. */
3730 parameter_pack_p
= true;
3732 else if (variable_template_specialization_p (t
))
3734 cp_walk_tree (&DECL_TI_ARGS (t
),
3735 find_parameter_packs_r
,
3742 if (builtin_pack_call_p (t
))
3743 parameter_pack_p
= true;
3747 parameter_pack_p
= true;
3750 /* Not a parameter pack. */
3754 if (parameter_pack_p
)
3756 /* Add this parameter pack to the list. */
3757 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3761 cp_walk_tree (&TYPE_CONTEXT (t
),
3762 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3764 /* This switch statement will return immediately if we don't find a
3766 switch (TREE_CODE (t
))
3768 case TEMPLATE_PARM_INDEX
:
3771 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3772 /* Check the template itself. */
3773 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3774 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3775 /* Check the template arguments. */
3776 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3781 case TEMPLATE_TYPE_PARM
:
3782 case TEMPLATE_TEMPLATE_PARM
:
3789 /* Ignore the declaration of a capture proxy for a parameter pack. */
3790 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3795 if (TYPE_PTRMEMFUNC_P (t
))
3801 if (TYPE_TEMPLATE_INFO (t
))
3802 cp_walk_tree (&TYPE_TI_ARGS (t
),
3803 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3809 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3814 cp_walk_tree (&TREE_TYPE (t
),
3815 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3819 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3824 case TYPE_PACK_EXPANSION
:
3825 case EXPR_PACK_EXPANSION
:
3830 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3835 case IDENTIFIER_NODE
:
3836 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3843 /* Look at explicit captures. */
3844 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3845 cap
; cap
= TREE_CHAIN (cap
))
3846 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3848 /* Since we defer implicit capture, look in the parms and body. */
3849 tree fn
= lambda_function (t
);
3850 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3852 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3860 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3861 type_pack_expansion_p to false so that any placeholders
3862 within the expression don't get marked as parameter packs. */
3863 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3864 ppd
->type_pack_expansion_p
= false;
3865 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3867 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3873 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3875 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3877 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
3879 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3890 /* Determines if the expression or type T uses any parameter packs. */
3892 uses_parameter_packs (tree t
)
3894 tree parameter_packs
= NULL_TREE
;
3895 struct find_parameter_pack_data ppd
;
3896 ppd
.parameter_packs
= ¶meter_packs
;
3897 ppd
.visited
= new hash_set
<tree
>;
3898 ppd
.type_pack_expansion_p
= false;
3899 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3901 return parameter_packs
!= NULL_TREE
;
3904 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3905 representation a base-class initializer into a parameter pack
3906 expansion. If all goes well, the resulting node will be an
3907 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3910 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3913 tree parameter_packs
= NULL_TREE
;
3914 bool for_types
= false;
3915 struct find_parameter_pack_data ppd
;
3917 if (!arg
|| arg
== error_mark_node
)
3920 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3922 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3923 class initializer. In this case, the TREE_PURPOSE will be a
3924 _TYPE node (representing the base class expansion we're
3925 initializing) and the TREE_VALUE will be a TREE_LIST
3926 containing the initialization arguments.
3928 The resulting expansion looks somewhat different from most
3929 expansions. Rather than returning just one _EXPANSION, we
3930 return a TREE_LIST whose TREE_PURPOSE is a
3931 TYPE_PACK_EXPANSION containing the bases that will be
3932 initialized. The TREE_VALUE will be identical to the
3933 original TREE_VALUE, which is a list of arguments that will
3934 be passed to each base. We do not introduce any new pack
3935 expansion nodes into the TREE_VALUE (although it is possible
3936 that some already exist), because the TREE_PURPOSE and
3937 TREE_VALUE all need to be expanded together with the same
3938 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3939 resulting TREE_PURPOSE will mention the parameter packs in
3940 both the bases and the arguments to the bases. */
3943 tree parameter_packs
= NULL_TREE
;
3945 /* Determine which parameter packs will be used by the base
3947 ppd
.visited
= new hash_set
<tree
>;
3948 ppd
.parameter_packs
= ¶meter_packs
;
3949 ppd
.type_pack_expansion_p
= true;
3950 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3951 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3954 if (parameter_packs
== NULL_TREE
)
3956 if (complain
& tf_error
)
3957 error ("base initializer expansion %qT contains no parameter packs",
3960 return error_mark_node
;
3963 if (TREE_VALUE (arg
) != void_type_node
)
3965 /* Collect the sets of parameter packs used in each of the
3966 initialization arguments. */
3967 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3969 /* Determine which parameter packs will be expanded in this
3971 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3978 /* Create the pack expansion type for the base type. */
3979 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3980 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3981 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3982 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3984 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3985 they will rarely be compared to anything. */
3986 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3988 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3991 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3994 /* Build the PACK_EXPANSION_* node. */
3996 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3997 : make_node (EXPR_PACK_EXPANSION
);
3998 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3999 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
4001 /* Propagate type and const-expression information. */
4002 TREE_TYPE (result
) = TREE_TYPE (arg
);
4003 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4004 /* Mark this read now, since the expansion might be length 0. */
4005 mark_exp_read (arg
);
4008 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4009 they will rarely be compared to anything. */
4010 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4012 /* Determine which parameter packs will be expanded. */
4013 ppd
.parameter_packs
= ¶meter_packs
;
4014 ppd
.visited
= new hash_set
<tree
>;
4015 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4016 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4019 /* Make sure we found some parameter packs. */
4020 if (parameter_packs
== NULL_TREE
)
4022 if (complain
& tf_error
)
4025 error ("expansion pattern %qT contains no parameter packs", arg
);
4027 error ("expansion pattern %qE contains no parameter packs", arg
);
4029 return error_mark_node
;
4031 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4033 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4038 /* Checks T for any "bare" parameter packs, which have not yet been
4039 expanded, and issues an error if any are found. This operation can
4040 only be done on full expressions or types (e.g., an expression
4041 statement, "if" condition, etc.), because we could have expressions like:
4043 foo(f(g(h(args)))...)
4045 where "args" is a parameter pack. check_for_bare_parameter_packs
4046 should not be called for the subexpressions args, h(args),
4047 g(h(args)), or f(g(h(args))), because we would produce erroneous
4050 Returns TRUE and emits an error if there were bare parameter packs,
4051 returns FALSE otherwise. */
4053 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4055 tree parameter_packs
= NULL_TREE
;
4056 struct find_parameter_pack_data ppd
;
4058 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4061 /* A lambda might use a parameter pack from the containing context. */
4062 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4063 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4066 if (TREE_CODE (t
) == TYPE_DECL
)
4069 ppd
.parameter_packs
= ¶meter_packs
;
4070 ppd
.visited
= new hash_set
<tree
>;
4071 ppd
.type_pack_expansion_p
= false;
4072 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4075 if (parameter_packs
)
4077 if (loc
== UNKNOWN_LOCATION
)
4078 loc
= cp_expr_loc_or_loc (t
, input_location
);
4079 error_at (loc
, "parameter packs not expanded with %<...%>:");
4080 while (parameter_packs
)
4082 tree pack
= TREE_VALUE (parameter_packs
);
4083 tree name
= NULL_TREE
;
4085 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4086 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4087 name
= TYPE_NAME (pack
);
4088 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4089 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4090 else if (TREE_CODE (pack
) == CALL_EXPR
)
4091 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4093 name
= DECL_NAME (pack
);
4096 inform (loc
, " %qD", name
);
4098 inform (loc
, " <anonymous>");
4100 parameter_packs
= TREE_CHAIN (parameter_packs
);
4109 /* Expand any parameter packs that occur in the template arguments in
4112 expand_template_argument_pack (tree args
)
4114 if (args
== error_mark_node
)
4115 return error_mark_node
;
4117 tree result_args
= NULL_TREE
;
4118 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4119 int num_result_args
= -1;
4120 int non_default_args_count
= -1;
4122 /* First, determine if we need to expand anything, and the number of
4123 slots we'll need. */
4124 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4126 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4127 if (arg
== NULL_TREE
)
4129 if (ARGUMENT_PACK_P (arg
))
4131 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4132 if (num_result_args
< 0)
4133 num_result_args
= in_arg
+ num_packed
;
4135 num_result_args
+= num_packed
;
4139 if (num_result_args
>= 0)
4144 /* If no expansion is necessary, we're done. */
4145 if (num_result_args
< 0)
4148 /* Expand arguments. */
4149 result_args
= make_tree_vec (num_result_args
);
4150 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4151 non_default_args_count
=
4152 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4153 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4155 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4156 if (ARGUMENT_PACK_P (arg
))
4158 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4159 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4160 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4161 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4162 if (non_default_args_count
> 0)
4163 non_default_args_count
+= num_packed
- 1;
4167 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4171 if (non_default_args_count
>= 0)
4172 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4176 /* Checks if DECL shadows a template parameter.
4178 [temp.local]: A template-parameter shall not be redeclared within its
4179 scope (including nested scopes).
4181 Emits an error and returns TRUE if the DECL shadows a parameter,
4182 returns FALSE otherwise. */
4185 check_template_shadow (tree decl
)
4189 /* If we're not in a template, we can't possibly shadow a template
4191 if (!current_template_parms
)
4194 /* Figure out what we're shadowing. */
4195 decl
= OVL_FIRST (decl
);
4196 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4198 /* If there's no previous binding for this name, we're not shadowing
4199 anything, let alone a template parameter. */
4203 /* If we're not shadowing a template parameter, we're done. Note
4204 that OLDDECL might be an OVERLOAD (or perhaps even an
4205 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4207 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4210 /* We check for decl != olddecl to avoid bogus errors for using a
4211 name inside a class. We check TPFI to avoid duplicate errors for
4212 inline member templates. */
4214 || (DECL_TEMPLATE_PARM_P (decl
)
4215 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4218 /* Don't complain about the injected class name, as we've already
4219 complained about the class itself. */
4220 if (DECL_SELF_REFERENCE_P (decl
))
4223 if (DECL_TEMPLATE_PARM_P (decl
))
4224 error ("declaration of template parameter %q+D shadows "
4225 "template parameter", decl
);
4227 error ("declaration of %q+#D shadows template parameter", decl
);
4228 inform (DECL_SOURCE_LOCATION (olddecl
),
4229 "template parameter %qD declared here", olddecl
);
4233 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4234 ORIG_LEVEL, DECL, and TYPE. */
4237 build_template_parm_index (int index
,
4243 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4244 TEMPLATE_PARM_IDX (t
) = index
;
4245 TEMPLATE_PARM_LEVEL (t
) = level
;
4246 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4247 TEMPLATE_PARM_DECL (t
) = decl
;
4248 TREE_TYPE (t
) = type
;
4249 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4250 TREE_READONLY (t
) = TREE_READONLY (decl
);
4255 /* Find the canonical type parameter for the given template type
4256 parameter. Returns the canonical type parameter, which may be TYPE
4257 if no such parameter existed. */
4260 canonical_type_parameter (tree type
)
4263 int idx
= TEMPLATE_TYPE_IDX (type
);
4264 if (!canonical_template_parms
)
4265 vec_alloc (canonical_template_parms
, idx
+ 1);
4267 if (canonical_template_parms
->length () <= (unsigned) idx
)
4268 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4270 list
= (*canonical_template_parms
)[idx
];
4271 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4272 list
= TREE_CHAIN (list
);
4275 return TREE_VALUE (list
);
4278 (*canonical_template_parms
)[idx
]
4279 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4284 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4285 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4286 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4287 new one is created. */
4290 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4291 tsubst_flags_t complain
)
4293 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4294 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4295 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4296 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4298 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4301 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4302 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4303 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4304 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4305 DECL_ARTIFICIAL (decl
) = 1;
4306 SET_DECL_TEMPLATE_PARM_P (decl
);
4308 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4309 TEMPLATE_PARM_LEVEL (index
) - levels
,
4310 TEMPLATE_PARM_ORIG_LEVEL (index
),
4312 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4313 TEMPLATE_PARM_PARAMETER_PACK (t
)
4314 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4316 /* Template template parameters need this. */
4317 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4319 DECL_TEMPLATE_RESULT (decl
)
4320 = build_decl (DECL_SOURCE_LOCATION (decl
),
4321 TYPE_DECL
, DECL_NAME (decl
), type
);
4322 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4323 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4324 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4328 return TEMPLATE_PARM_DESCENDANTS (index
);
4331 /* Process information from new template parameter PARM and append it
4332 to the LIST being built. This new parameter is a non-type
4333 parameter iff IS_NON_TYPE is true. This new parameter is a
4334 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4338 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4339 bool is_non_type
, bool is_parameter_pack
)
4344 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4345 tree defval
= TREE_PURPOSE (parm
);
4346 tree constr
= TREE_TYPE (parm
);
4350 tree p
= tree_last (list
);
4352 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4355 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4356 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4358 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4366 parm
= TREE_VALUE (parm
);
4368 SET_DECL_TEMPLATE_PARM_P (parm
);
4370 if (TREE_TYPE (parm
) != error_mark_node
)
4374 The top-level cv-qualifiers on the template-parameter are
4375 ignored when determining its type. */
4376 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4377 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4378 TREE_TYPE (parm
) = error_mark_node
;
4379 else if (uses_parameter_packs (TREE_TYPE (parm
))
4380 && !is_parameter_pack
4381 /* If we're in a nested template parameter list, the template
4382 template parameter could be a parameter pack. */
4383 && processing_template_parmlist
== 1)
4385 /* This template parameter is not a parameter pack, but it
4386 should be. Complain about "bare" parameter packs. */
4387 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4389 /* Recover by calling this a parameter pack. */
4390 is_parameter_pack
= true;
4394 /* A template parameter is not modifiable. */
4395 TREE_CONSTANT (parm
) = 1;
4396 TREE_READONLY (parm
) = 1;
4397 decl
= build_decl (parm_loc
,
4398 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4399 TREE_CONSTANT (decl
) = 1;
4400 TREE_READONLY (decl
) = 1;
4401 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4402 = build_template_parm_index (idx
, processing_template_decl
,
4403 processing_template_decl
,
4404 decl
, TREE_TYPE (parm
));
4406 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4407 = is_parameter_pack
;
4412 parm
= TREE_VALUE (TREE_VALUE (parm
));
4414 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4416 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4417 /* This is for distinguishing between real templates and template
4418 template parameters */
4419 TREE_TYPE (parm
) = t
;
4420 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4425 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4426 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4427 decl
= build_decl (parm_loc
,
4428 TYPE_DECL
, parm
, t
);
4431 TYPE_NAME (t
) = decl
;
4432 TYPE_STUB_DECL (t
) = decl
;
4434 TEMPLATE_TYPE_PARM_INDEX (t
)
4435 = build_template_parm_index (idx
, processing_template_decl
,
4436 processing_template_decl
,
4437 decl
, TREE_TYPE (parm
));
4438 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4439 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4441 DECL_ARTIFICIAL (decl
) = 1;
4442 SET_DECL_TEMPLATE_PARM_P (decl
);
4444 /* Build requirements for the type/template parameter.
4445 This must be done after SET_DECL_TEMPLATE_PARM_P or
4446 process_template_parm could fail. */
4447 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4451 /* Build the parameter node linking the parameter declaration,
4452 its default argument (if any), and its constraints (if any). */
4453 parm
= build_tree_list (defval
, parm
);
4454 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4456 return chainon (list
, parm
);
4459 /* The end of a template parameter list has been reached. Process the
4460 tree list into a parameter vector, converting each parameter into a more
4461 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4465 end_template_parm_list (tree parms
)
4469 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4471 /* Pop the dummy parameter level and add the real one. */
4472 current_template_parms
= TREE_CHAIN (current_template_parms
);
4474 current_template_parms
4475 = tree_cons (size_int (processing_template_decl
),
4476 saved_parmlist
, current_template_parms
);
4478 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4480 next
= TREE_CHAIN (parm
);
4481 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4482 TREE_CHAIN (parm
) = NULL_TREE
;
4485 --processing_template_parmlist
;
4487 return saved_parmlist
;
4490 // Explicitly indicate the end of the template parameter list. We assume
4491 // that the current template parameters have been constructed and/or
4492 // managed explicitly, as when creating new template template parameters
4493 // from a shorthand constraint.
4495 end_template_parm_list ()
4497 --processing_template_parmlist
;
4500 /* end_template_decl is called after a template declaration is seen. */
4503 end_template_decl (void)
4505 reset_specialization ();
4507 if (! processing_template_decl
)
4510 /* This matches the pushlevel in begin_template_parm_list. */
4513 --processing_template_decl
;
4514 current_template_parms
= TREE_CHAIN (current_template_parms
);
4517 /* Takes a TREE_LIST representing a template parameter and convert it
4518 into an argument suitable to be passed to the type substitution
4519 functions. Note that If the TREE_LIST contains an error_mark
4520 node, the returned argument is error_mark_node. */
4523 template_parm_to_arg (tree t
)
4527 || TREE_CODE (t
) != TREE_LIST
)
4530 if (error_operand_p (TREE_VALUE (t
)))
4531 return error_mark_node
;
4535 if (TREE_CODE (t
) == TYPE_DECL
4536 || TREE_CODE (t
) == TEMPLATE_DECL
)
4540 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4542 /* Turn this argument into a TYPE_ARGUMENT_PACK
4543 with a single element, which expands T. */
4544 tree vec
= make_tree_vec (1);
4546 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4548 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4550 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4551 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4556 t
= DECL_INITIAL (t
);
4558 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4560 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4561 with a single element, which expands T. */
4562 tree vec
= make_tree_vec (1);
4564 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4566 t
= convert_from_reference (t
);
4567 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4569 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4570 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4573 t
= convert_from_reference (t
);
4578 /* Given a single level of template parameters (a TREE_VEC), return it
4579 as a set of template arguments. */
4582 template_parms_level_to_args (tree parms
)
4584 tree a
= copy_node (parms
);
4585 TREE_TYPE (a
) = NULL_TREE
;
4586 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4587 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4590 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4595 /* Given a set of template parameters, return them as a set of template
4596 arguments. The template parameters are represented as a TREE_VEC, in
4597 the form documented in cp-tree.h for template arguments. */
4600 template_parms_to_args (tree parms
)
4603 tree args
= NULL_TREE
;
4604 int length
= TMPL_PARMS_DEPTH (parms
);
4607 /* If there is only one level of template parameters, we do not
4608 create a TREE_VEC of TREE_VECs. Instead, we return a single
4609 TREE_VEC containing the arguments. */
4611 args
= make_tree_vec (length
);
4613 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4615 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4618 TREE_VEC_ELT (args
, --l
) = a
;
4626 /* Within the declaration of a template, return the currently active
4627 template parameters as an argument TREE_VEC. */
4630 current_template_args (void)
4632 return template_parms_to_args (current_template_parms
);
4635 /* Update the declared TYPE by doing any lookups which were thought to be
4636 dependent, but are not now that we know the SCOPE of the declarator. */
4639 maybe_update_decl_type (tree orig_type
, tree scope
)
4641 tree type
= orig_type
;
4643 if (type
== NULL_TREE
)
4646 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4647 type
= TREE_TYPE (type
);
4649 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4650 && dependent_type_p (type
)
4651 /* Don't bother building up the args in this case. */
4652 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4654 /* tsubst in the args corresponding to the template parameters,
4655 including auto if present. Most things will be unchanged, but
4656 make_typename_type and tsubst_qualified_id will resolve
4657 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4658 tree args
= current_template_args ();
4659 tree auto_node
= type_uses_auto (type
);
4663 tree auto_vec
= make_tree_vec (1);
4664 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4665 args
= add_to_template_args (args
, auto_vec
);
4667 pushed
= push_scope (scope
);
4668 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4673 if (type
== error_mark_node
)
4676 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4678 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4681 type
= TYPE_NAME (type
);
4686 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4687 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4688 the new template is a member template. */
4691 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4693 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4694 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4695 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4696 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4697 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4698 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4703 struct template_parm_data
4705 /* The level of the template parameters we are currently
4709 /* The index of the specialization argument we are currently
4713 /* An array whose size is the number of template parameters. The
4714 elements are nonzero if the parameter has been used in any one
4715 of the arguments processed so far. */
4718 /* An array whose size is the number of template arguments. The
4719 elements are nonzero if the argument makes use of template
4720 parameters of this level. */
4721 int* arg_uses_template_parms
;
4724 /* Subroutine of push_template_decl used to see if each template
4725 parameter in a partial specialization is used in the explicit
4726 argument list. If T is of the LEVEL given in DATA (which is
4727 treated as a template_parm_data*), then DATA->PARMS is marked
4731 mark_template_parm (tree t
, void* data
)
4735 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4737 template_parm_level_and_index (t
, &level
, &idx
);
4739 if (level
== tpd
->level
)
4741 tpd
->parms
[idx
] = 1;
4742 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4745 /* In C++17 the type of a non-type argument is a deduced context. */
4746 if (cxx_dialect
>= cxx17
4747 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4748 for_each_template_parm (TREE_TYPE (t
),
4749 &mark_template_parm
,
4752 /*include_nondeduced_p=*/false);
4754 /* Return zero so that for_each_template_parm will continue the
4755 traversal of the tree; we want to mark *every* template parm. */
4759 /* Process the partial specialization DECL. */
4762 process_partial_specialization (tree decl
)
4764 tree type
= TREE_TYPE (decl
);
4765 tree tinfo
= get_template_info (decl
);
4766 tree maintmpl
= TI_TEMPLATE (tinfo
);
4767 tree specargs
= TI_ARGS (tinfo
);
4768 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4769 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4772 int nargs
= TREE_VEC_LENGTH (inner_args
);
4775 bool did_error_intro
= false;
4776 struct template_parm_data tpd
;
4777 struct template_parm_data tpd2
;
4779 gcc_assert (current_template_parms
);
4781 /* A concept cannot be specialized. */
4782 if (flag_concepts
&& variable_concept_p (maintmpl
))
4784 error ("specialization of variable concept %q#D", maintmpl
);
4785 return error_mark_node
;
4788 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4789 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4791 /* We check that each of the template parameters given in the
4792 partial specialization is used in the argument list to the
4793 specialization. For example:
4795 template <class T> struct S;
4796 template <class T> struct S<T*>;
4798 The second declaration is OK because `T*' uses the template
4799 parameter T, whereas
4801 template <class T> struct S<int>;
4803 is no good. Even trickier is:
4814 The S2<T> declaration is actually invalid; it is a
4815 full-specialization. Of course,
4818 struct S2<T (*)(U)>;
4820 or some such would have been OK. */
4821 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4822 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4823 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4825 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4826 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4827 for (i
= 0; i
< nargs
; ++i
)
4829 tpd
.current_arg
= i
;
4830 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4831 &mark_template_parm
,
4834 /*include_nondeduced_p=*/false);
4836 for (i
= 0; i
< ntparms
; ++i
)
4837 if (tpd
.parms
[i
] == 0)
4839 /* One of the template parms was not used in a deduced context in the
4841 if (!did_error_intro
)
4843 error ("template parameters not deducible in "
4844 "partial specialization:");
4845 did_error_intro
= true;
4848 inform (input_location
, " %qD",
4849 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4852 if (did_error_intro
)
4853 return error_mark_node
;
4855 /* [temp.class.spec]
4857 The argument list of the specialization shall not be identical to
4858 the implicit argument list of the primary template. */
4860 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4861 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4863 || !strictly_subsumes (current_template_constraints (),
4864 get_constraints (maintmpl
))))
4867 error ("partial specialization %q+D does not specialize "
4868 "any template arguments; to define the primary template, "
4869 "remove the template argument list", decl
);
4871 error ("partial specialization %q+D does not specialize any "
4872 "template arguments and is not more constrained than "
4873 "the primary template; to define the primary template, "
4874 "remove the template argument list", decl
);
4875 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4878 /* A partial specialization that replaces multiple parameters of the
4879 primary template with a pack expansion is less specialized for those
4881 if (nargs
< DECL_NTPARMS (maintmpl
))
4883 error ("partial specialization is not more specialized than the "
4884 "primary template because it replaces multiple parameters "
4885 "with a pack expansion");
4886 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4887 /* Avoid crash in process_partial_specialization. */
4891 /* If we aren't in a dependent class, we can actually try deduction. */
4892 else if (tpd
.level
== 1
4893 /* FIXME we should be able to handle a partial specialization of a
4894 partial instantiation, but currently we can't (c++/41727). */
4895 && TMPL_ARGS_DEPTH (specargs
) == 1
4896 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4898 auto_diagnostic_group d
;
4899 if (permerror (input_location
, "partial specialization %qD is not "
4900 "more specialized than", decl
))
4901 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4905 /* [temp.class.spec]
4907 A partially specialized non-type argument expression shall not
4908 involve template parameters of the partial specialization except
4909 when the argument expression is a simple identifier.
4911 The type of a template parameter corresponding to a specialized
4912 non-type argument shall not be dependent on a parameter of the
4915 Also, we verify that pack expansions only occur at the
4916 end of the argument list. */
4917 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4919 for (i
= 0; i
< nargs
; ++i
)
4921 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4922 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4923 tree packed_args
= NULL_TREE
;
4926 if (ARGUMENT_PACK_P (arg
))
4928 /* Extract the arguments from the argument pack. We'll be
4929 iterating over these in the following loop. */
4930 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4931 len
= TREE_VEC_LENGTH (packed_args
);
4934 for (j
= 0; j
< len
; j
++)
4937 /* Get the Jth argument in the parameter pack. */
4938 arg
= TREE_VEC_ELT (packed_args
, j
);
4940 if (PACK_EXPANSION_P (arg
))
4942 /* Pack expansions must come at the end of the
4944 if ((packed_args
&& j
< len
- 1)
4945 || (!packed_args
&& i
< nargs
- 1))
4947 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4948 error ("parameter pack argument %qE must be at the "
4949 "end of the template argument list", arg
);
4951 error ("parameter pack argument %qT must be at the "
4952 "end of the template argument list", arg
);
4956 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4957 /* We only care about the pattern. */
4958 arg
= PACK_EXPANSION_PATTERN (arg
);
4960 if (/* These first two lines are the `non-type' bit. */
4962 && TREE_CODE (arg
) != TEMPLATE_DECL
4963 /* This next two lines are the `argument expression is not just a
4964 simple identifier' condition and also the `specialized
4965 non-type argument' bit. */
4966 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4967 && !(REFERENCE_REF_P (arg
)
4968 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4970 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4971 || (packed_args
&& uses_template_parms (arg
)))
4972 error ("template argument %qE involves template parameter(s)",
4976 /* Look at the corresponding template parameter,
4977 marking which template parameters its type depends
4979 tree type
= TREE_TYPE (parm
);
4983 /* We haven't yet initialized TPD2. Do so now. */
4984 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4985 /* The number of parameters here is the number in the
4986 main template, which, as checked in the assertion
4988 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4990 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4993 /* Mark the template parameters. But this time, we're
4994 looking for the template parameters of the main
4995 template, not in the specialization. */
4996 tpd2
.current_arg
= i
;
4997 tpd2
.arg_uses_template_parms
[i
] = 0;
4998 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4999 for_each_template_parm (type
,
5000 &mark_template_parm
,
5003 /*include_nondeduced_p=*/false);
5005 if (tpd2
.arg_uses_template_parms
[i
])
5007 /* The type depended on some template parameters.
5008 If they are fully specialized in the
5009 specialization, that's OK. */
5012 for (j
= 0; j
< nargs
; ++j
)
5013 if (tpd2
.parms
[j
] != 0
5014 && tpd
.arg_uses_template_parms
[j
])
5017 error_n (input_location
, count
,
5018 "type %qT of template argument %qE depends "
5019 "on a template parameter",
5020 "type %qT of template argument %qE depends "
5021 "on template parameters",
5030 /* We should only get here once. */
5031 if (TREE_CODE (decl
) == TYPE_DECL
)
5032 gcc_assert (!COMPLETE_TYPE_P (type
));
5034 // Build the template decl.
5035 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5036 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5037 TREE_TYPE (tmpl
) = type
;
5038 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5039 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5040 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5041 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5043 /* Give template template parms a DECL_CONTEXT of the template
5044 for which they are a parameter. */
5045 for (i
= 0; i
< ntparms
; ++i
)
5047 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5048 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5049 DECL_CONTEXT (parm
) = tmpl
;
5053 /* We didn't register this in check_explicit_specialization so we could
5054 wait until the constraints were set. */
5055 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5057 associate_classtype_constraints (type
);
5059 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5060 = tree_cons (specargs
, tmpl
,
5061 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5062 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5064 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5065 inst
= TREE_CHAIN (inst
))
5067 tree instance
= TREE_VALUE (inst
);
5068 if (TYPE_P (instance
)
5069 ? (COMPLETE_TYPE_P (instance
)
5070 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5071 : DECL_TEMPLATE_INSTANTIATION (instance
))
5073 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5074 tree inst_decl
= (DECL_P (instance
)
5075 ? instance
: TYPE_NAME (instance
));
5078 else if (spec
== error_mark_node
)
5079 permerror (input_location
,
5080 "declaration of %qD ambiguates earlier template "
5081 "instantiation for %qD", decl
, inst_decl
);
5082 else if (TREE_VALUE (spec
) == tmpl
)
5083 permerror (input_location
,
5084 "partial specialization of %qD after instantiation "
5085 "of %qD", decl
, inst_decl
);
5092 /* PARM is a template parameter of some form; return the corresponding
5093 TEMPLATE_PARM_INDEX. */
5096 get_template_parm_index (tree parm
)
5098 if (TREE_CODE (parm
) == PARM_DECL
5099 || TREE_CODE (parm
) == CONST_DECL
)
5100 parm
= DECL_INITIAL (parm
);
5101 else if (TREE_CODE (parm
) == TYPE_DECL
5102 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5103 parm
= TREE_TYPE (parm
);
5104 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5105 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5106 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5107 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5108 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5112 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5113 parameter packs used by the template parameter PARM. */
5116 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5118 /* A type parm can't refer to another parm. */
5119 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5121 else if (TREE_CODE (parm
) == PARM_DECL
)
5123 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5128 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5130 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5131 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5132 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5135 /* PARM is a template parameter pack. Return any parameter packs used in
5136 its type or the type of any of its template parameters. If there are
5137 any such packs, it will be instantiated into a fixed template parameter
5138 list by partial instantiation rather than be fully deduced. */
5141 fixed_parameter_pack_p (tree parm
)
5143 /* This can only be true in a member template. */
5144 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5146 /* This can only be true for a parameter pack. */
5147 if (!template_parameter_pack_p (parm
))
5149 /* A type parm can't refer to another parm. */
5150 if (TREE_CODE (parm
) == TYPE_DECL
)
5153 tree parameter_packs
= NULL_TREE
;
5154 struct find_parameter_pack_data ppd
;
5155 ppd
.parameter_packs
= ¶meter_packs
;
5156 ppd
.visited
= new hash_set
<tree
>;
5157 ppd
.type_pack_expansion_p
= false;
5159 fixed_parameter_pack_p_1 (parm
, &ppd
);
5162 return parameter_packs
;
5165 /* Check that a template declaration's use of default arguments and
5166 parameter packs is not invalid. Here, PARMS are the template
5167 parameters. IS_PRIMARY is true if DECL is the thing declared by
5168 a primary template. IS_PARTIAL is true if DECL is a partial
5171 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5172 function template declaration or a friend class template
5173 declaration. In the function case, 1 indicates a declaration, 2
5174 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5175 emitted for extraneous default arguments.
5177 Returns TRUE if there were no errors found, FALSE otherwise. */
5180 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5181 bool is_partial
, int is_friend_decl
)
5184 int last_level_to_check
;
5186 bool no_errors
= true;
5190 A default template-argument shall not be specified in a
5191 function template declaration or a function template definition, nor
5192 in the template-parameter-list of the definition of a member of a
5195 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5196 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5197 /* You can't have a function template declaration in a local
5198 scope, nor you can you define a member of a class template in a
5202 if ((TREE_CODE (decl
) == TYPE_DECL
5204 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5205 || (TREE_CODE (decl
) == FUNCTION_DECL
5206 && LAMBDA_FUNCTION_P (decl
)))
5207 /* A lambda doesn't have an explicit declaration; don't complain
5208 about the parms of the enclosing class. */
5211 if (current_class_type
5212 && !TYPE_BEING_DEFINED (current_class_type
)
5213 && DECL_LANG_SPECIFIC (decl
)
5214 && DECL_DECLARES_FUNCTION_P (decl
)
5215 /* If this is either a friend defined in the scope of the class
5216 or a member function. */
5217 && (DECL_FUNCTION_MEMBER_P (decl
)
5218 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5219 : DECL_FRIEND_CONTEXT (decl
)
5220 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5222 /* And, if it was a member function, it really was defined in
5223 the scope of the class. */
5224 && (!DECL_FUNCTION_MEMBER_P (decl
)
5225 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5226 /* We already checked these parameters when the template was
5227 declared, so there's no need to do it again now. This function
5228 was defined in class scope, but we're processing its body now
5229 that the class is complete. */
5232 /* Core issue 226 (C++0x only): the following only applies to class
5235 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5239 If a template-parameter has a default template-argument, all
5240 subsequent template-parameters shall have a default
5241 template-argument supplied. */
5242 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5244 tree inner_parms
= TREE_VALUE (parm_level
);
5245 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5246 int seen_def_arg_p
= 0;
5249 for (i
= 0; i
< ntparms
; ++i
)
5251 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5253 if (parm
== error_mark_node
)
5256 if (TREE_PURPOSE (parm
))
5258 else if (seen_def_arg_p
5259 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5261 error ("no default argument for %qD", TREE_VALUE (parm
));
5262 /* For better subsequent error-recovery, we indicate that
5263 there should have been a default argument. */
5264 TREE_PURPOSE (parm
) = error_mark_node
;
5267 else if (!is_partial
5269 /* Don't complain about an enclosing partial
5271 && parm_level
== parms
5272 && TREE_CODE (decl
) == TYPE_DECL
5274 && template_parameter_pack_p (TREE_VALUE (parm
))
5275 /* A fixed parameter pack will be partially
5276 instantiated into a fixed length list. */
5277 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5279 /* A primary class template can only have one
5280 parameter pack, at the end of the template
5283 error ("parameter pack %q+D must be at the end of the"
5284 " template parameter list", TREE_VALUE (parm
));
5286 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5294 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5298 /* For an ordinary class template, default template arguments are
5299 allowed at the innermost level, e.g.:
5300 template <class T = int>
5302 but, in a partial specialization, they're not allowed even
5303 there, as we have in [temp.class.spec]:
5305 The template parameter list of a specialization shall not
5306 contain default template argument values.
5308 So, for a partial specialization, or for a function template
5309 (in C++98/C++03), we look at all of them. */
5312 /* But, for a primary class template that is not a partial
5313 specialization we look at all template parameters except the
5315 parms
= TREE_CHAIN (parms
);
5317 /* Figure out what error message to issue. */
5318 if (is_friend_decl
== 2)
5319 msg
= G_("default template arguments may not be used in function template "
5320 "friend re-declaration");
5321 else if (is_friend_decl
)
5322 msg
= G_("default template arguments may not be used in template "
5323 "friend declarations");
5324 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5325 msg
= G_("default template arguments may not be used in function templates "
5326 "without -std=c++11 or -std=gnu++11");
5327 else if (is_partial
)
5328 msg
= G_("default template arguments may not be used in "
5329 "partial specializations");
5330 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5331 msg
= G_("default argument for template parameter for class enclosing %qD");
5333 /* Per [temp.param]/9, "A default template-argument shall not be
5334 specified in the template-parameter-lists of the definition of
5335 a member of a class template that appears outside of the member's
5336 class.", thus if we aren't handling a member of a class template
5337 there is no need to examine the parameters. */
5340 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5341 /* If we're inside a class definition, there's no need to
5342 examine the parameters to the class itself. On the one
5343 hand, they will be checked when the class is defined, and,
5344 on the other, default arguments are valid in things like:
5345 template <class T = double>
5346 struct S { template <class U> void f(U); };
5347 Here the default argument for `S' has no bearing on the
5348 declaration of `f'. */
5349 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5351 /* Check everything. */
5352 last_level_to_check
= 0;
5354 for (parm_level
= parms
;
5355 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5356 parm_level
= TREE_CHAIN (parm_level
))
5358 tree inner_parms
= TREE_VALUE (parm_level
);
5362 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5363 for (i
= 0; i
< ntparms
; ++i
)
5365 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5368 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5373 if (is_friend_decl
== 2)
5380 /* Clear out the default argument so that we are not
5382 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5386 /* At this point, if we're still interested in issuing messages,
5387 they must apply to classes surrounding the object declared. */
5389 msg
= G_("default argument for template parameter for class "
5396 /* Worker for push_template_decl_real, called via
5397 for_each_template_parm. DATA is really an int, indicating the
5398 level of the parameters we are interested in. If T is a template
5399 parameter of that level, return nonzero. */
5402 template_parm_this_level_p (tree t
, void* data
)
5404 int this_level
= *(int *)data
;
5407 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5408 level
= TEMPLATE_PARM_LEVEL (t
);
5410 level
= TEMPLATE_TYPE_LEVEL (t
);
5411 return level
== this_level
;
5414 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5415 DATA is really an int, indicating the innermost outer level of parameters.
5416 If T is a template parameter of that level or further out, return
5420 template_parm_outer_level (tree t
, void *data
)
5422 int this_level
= *(int *)data
;
5425 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5426 level
= TEMPLATE_PARM_LEVEL (t
);
5428 level
= TEMPLATE_TYPE_LEVEL (t
);
5429 return level
<= this_level
;
5432 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5433 parameters given by current_template_args, or reuses a
5434 previously existing one, if appropriate. Returns the DECL, or an
5435 equivalent one, if it is replaced via a call to duplicate_decls.
5437 If IS_FRIEND is true, DECL is a friend declaration. */
5440 push_template_decl_real (tree decl
, bool is_friend
)
5448 int new_template_p
= 0;
5449 /* True if the template is a member template, in the sense of
5451 bool member_template_p
= false;
5453 if (decl
== error_mark_node
|| !current_template_parms
)
5454 return error_mark_node
;
5456 /* See if this is a partial specialization. */
5457 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5458 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5459 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5461 && DECL_LANG_SPECIFIC (decl
)
5462 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5463 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5465 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5469 /* For a friend, we want the context of the friend, not
5470 the type of which it is a friend. */
5471 ctx
= CP_DECL_CONTEXT (decl
);
5472 else if (CP_DECL_CONTEXT (decl
)
5473 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5474 /* In the case of a virtual function, we want the class in which
5476 ctx
= CP_DECL_CONTEXT (decl
);
5478 /* Otherwise, if we're currently defining some class, the DECL
5479 is assumed to be a member of the class. */
5480 ctx
= current_scope ();
5482 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5485 if (!DECL_CONTEXT (decl
))
5486 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5488 /* See if this is a primary template. */
5489 if (is_friend
&& ctx
5490 && uses_template_parms_level (ctx
, processing_template_decl
))
5491 /* A friend template that specifies a class context, i.e.
5492 template <typename T> friend void A<T>::f();
5495 else if (TREE_CODE (decl
) == TYPE_DECL
5496 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5499 is_primary
= template_parm_scope_p ();
5503 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5505 if (DECL_CLASS_SCOPE_P (decl
))
5506 member_template_p
= true;
5507 if (TREE_CODE (decl
) == TYPE_DECL
5508 && anon_aggrname_p (DECL_NAME (decl
)))
5510 error ("template class without a name");
5511 return error_mark_node
;
5513 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5515 if (member_template_p
)
5517 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5518 error ("member template %qD may not have virt-specifiers", decl
);
5520 if (DECL_DESTRUCTOR_P (decl
))
5524 A destructor shall not be a member template. */
5525 error ("destructor %qD declared as member template", decl
);
5526 return error_mark_node
;
5528 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5529 && (!prototype_p (TREE_TYPE (decl
))
5530 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5531 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5532 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5533 == void_list_node
)))
5535 /* [basic.stc.dynamic.allocation]
5537 An allocation function can be a function
5538 template. ... Template allocation functions shall
5539 have two or more parameters. */
5540 error ("invalid template declaration of %qD", decl
);
5541 return error_mark_node
;
5544 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5545 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5547 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5548 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5549 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5551 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5552 if (TREE_CODE (t
) == TYPE_DECL
)
5554 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5555 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5558 else if (TREE_CODE (decl
) == TYPE_DECL
5559 && TYPE_DECL_ALIAS_P (decl
))
5560 /* alias-declaration */
5561 gcc_assert (!DECL_ARTIFICIAL (decl
));
5562 else if (VAR_P (decl
))
5563 /* C++14 variable template. */;
5566 error ("template declaration of %q#D", decl
);
5567 return error_mark_node
;
5571 /* Check to see that the rules regarding the use of default
5572 arguments are not being violated. We check args for a friend
5573 functions when we know whether it's a definition, introducing
5574 declaration or re-declaration. */
5575 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5576 check_default_tmpl_args (decl
, current_template_parms
,
5577 is_primary
, is_partial
, is_friend
);
5579 /* Ensure that there are no parameter packs in the type of this
5580 declaration that have not been expanded. */
5581 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5583 /* Check each of the arguments individually to see if there are
5584 any bare parameter packs. */
5585 tree type
= TREE_TYPE (decl
);
5586 tree arg
= DECL_ARGUMENTS (decl
);
5587 tree argtype
= TYPE_ARG_TYPES (type
);
5589 while (arg
&& argtype
)
5591 if (!DECL_PACK_P (arg
)
5592 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5594 /* This is a PARM_DECL that contains unexpanded parameter
5595 packs. We have already complained about this in the
5596 check_for_bare_parameter_packs call, so just replace
5597 these types with ERROR_MARK_NODE. */
5598 TREE_TYPE (arg
) = error_mark_node
;
5599 TREE_VALUE (argtype
) = error_mark_node
;
5602 arg
= DECL_CHAIN (arg
);
5603 argtype
= TREE_CHAIN (argtype
);
5606 /* Check for bare parameter packs in the return type and the
5607 exception specifiers. */
5608 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5609 /* Errors were already issued, set return type to int
5610 as the frontend doesn't expect error_mark_node as
5612 TREE_TYPE (type
) = integer_type_node
;
5613 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5614 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5616 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5617 && TYPE_DECL_ALIAS_P (decl
))
5618 ? DECL_ORIGINAL_TYPE (decl
)
5619 : TREE_TYPE (decl
)))
5621 TREE_TYPE (decl
) = error_mark_node
;
5622 return error_mark_node
;
5626 return process_partial_specialization (decl
);
5628 args
= current_template_args ();
5631 || TREE_CODE (ctx
) == FUNCTION_DECL
5632 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5633 || (TREE_CODE (decl
) == TYPE_DECL
5634 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5635 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5637 if (DECL_LANG_SPECIFIC (decl
)
5638 && DECL_TEMPLATE_INFO (decl
)
5639 && DECL_TI_TEMPLATE (decl
))
5640 tmpl
= DECL_TI_TEMPLATE (decl
);
5641 /* If DECL is a TYPE_DECL for a class-template, then there won't
5642 be DECL_LANG_SPECIFIC. The information equivalent to
5643 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5644 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5645 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5646 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5648 /* Since a template declaration already existed for this
5649 class-type, we must be redeclaring it here. Make sure
5650 that the redeclaration is valid. */
5651 redeclare_class_template (TREE_TYPE (decl
),
5652 current_template_parms
,
5653 current_template_constraints ());
5654 /* We don't need to create a new TEMPLATE_DECL; just use the
5655 one we already had. */
5656 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5660 tmpl
= build_template_decl (decl
, current_template_parms
,
5664 if (DECL_LANG_SPECIFIC (decl
)
5665 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5667 /* A specialization of a member template of a template
5669 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5670 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5671 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5677 tree a
, t
, current
, parms
;
5679 tree tinfo
= get_template_info (decl
);
5683 error ("template definition of non-template %q#D", decl
);
5684 return error_mark_node
;
5687 tmpl
= TI_TEMPLATE (tinfo
);
5689 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5690 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5691 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5692 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5696 /* The declaration is a specialization of a member
5697 template, declared outside the class. Therefore, the
5698 innermost template arguments will be NULL, so we
5699 replace them with the arguments determined by the
5700 earlier call to check_explicit_specialization. */
5701 args
= DECL_TI_ARGS (decl
);
5704 = build_template_decl (decl
, current_template_parms
,
5706 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5707 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5708 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5709 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5710 DECL_TEMPLATE_INFO (new_tmpl
)
5711 = build_template_info (tmpl
, args
);
5713 register_specialization (new_tmpl
,
5714 most_general_template (tmpl
),
5720 /* Make sure the template headers we got make sense. */
5722 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5723 i
= TMPL_PARMS_DEPTH (parms
);
5724 if (TMPL_ARGS_DEPTH (args
) != i
)
5726 error ("expected %d levels of template parms for %q#D, got %d",
5727 i
, decl
, TMPL_ARGS_DEPTH (args
));
5728 DECL_INTERFACE_KNOWN (decl
) = 1;
5729 return error_mark_node
;
5732 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5734 a
= TMPL_ARGS_LEVEL (args
, i
);
5735 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5737 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5739 if (current
== decl
)
5740 error ("got %d template parameters for %q#D",
5741 TREE_VEC_LENGTH (a
), decl
);
5743 error ("got %d template parameters for %q#T",
5744 TREE_VEC_LENGTH (a
), current
);
5745 error (" but %d required", TREE_VEC_LENGTH (t
));
5746 /* Avoid crash in import_export_decl. */
5747 DECL_INTERFACE_KNOWN (decl
) = 1;
5748 return error_mark_node
;
5751 if (current
== decl
)
5753 else if (current
== NULL_TREE
)
5754 /* Can happen in erroneous input. */
5757 current
= get_containing_scope (current
);
5760 /* Check that the parms are used in the appropriate qualifying scopes
5761 in the declarator. */
5762 if (!comp_template_args
5764 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5766 error ("template arguments to %qD do not match original "
5767 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5768 if (!uses_template_parms (TI_ARGS (tinfo
)))
5769 inform (input_location
, "use %<template<>%> for"
5770 " an explicit specialization");
5771 /* Avoid crash in import_export_decl. */
5772 DECL_INTERFACE_KNOWN (decl
) = 1;
5773 return error_mark_node
;
5777 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5778 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5780 /* Push template declarations for global functions and types. Note
5781 that we do not try to push a global template friend declared in a
5782 template class; such a thing may well depend on the template
5783 parameters of the class. */
5784 if (new_template_p
&& !ctx
5785 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5787 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5788 if (tmpl
== error_mark_node
)
5789 return error_mark_node
;
5791 /* Hide template friend classes that haven't been declared yet. */
5792 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5794 DECL_ANTICIPATED (tmpl
) = 1;
5795 DECL_FRIEND_P (tmpl
) = 1;
5801 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5803 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5805 /* Give template template parms a DECL_CONTEXT of the template
5806 for which they are a parameter. */
5807 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5808 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5810 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5811 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5812 DECL_CONTEXT (parm
) = tmpl
;
5815 if (TREE_CODE (decl
) == TYPE_DECL
5816 && TYPE_DECL_ALIAS_P (decl
)
5817 && complex_alias_template_p (tmpl
))
5818 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5821 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5822 back to its most general template. If TMPL is a specialization,
5823 ARGS may only have the innermost set of arguments. Add the missing
5824 argument levels if necessary. */
5825 if (DECL_TEMPLATE_INFO (tmpl
))
5826 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5828 info
= build_template_info (tmpl
, args
);
5830 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5831 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5835 retrofit_lang_decl (decl
);
5836 if (DECL_LANG_SPECIFIC (decl
))
5837 DECL_TEMPLATE_INFO (decl
) = info
;
5840 if (flag_implicit_templates
5842 && TREE_PUBLIC (decl
)
5843 && VAR_OR_FUNCTION_DECL_P (decl
))
5844 /* Set DECL_COMDAT on template instantiations; if we force
5845 them to be emitted by explicit instantiation or -frepo,
5846 mark_needed will tell cgraph to do the right thing. */
5847 DECL_COMDAT (decl
) = true;
5849 return DECL_TEMPLATE_RESULT (tmpl
);
5853 push_template_decl (tree decl
)
5855 return push_template_decl_real (decl
, false);
5858 /* FN is an inheriting constructor that inherits from the constructor
5859 template INHERITED; turn FN into a constructor template with a matching
5863 add_inherited_template_parms (tree fn
, tree inherited
)
5866 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5867 inner_parms
= copy_node (inner_parms
);
5869 = tree_cons (size_int (processing_template_decl
+ 1),
5870 inner_parms
, current_template_parms
);
5871 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5872 tree args
= template_parms_to_args (parms
);
5873 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5874 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5875 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5876 DECL_ARTIFICIAL (tmpl
) = true;
5877 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5881 /* Called when a class template TYPE is redeclared with the indicated
5882 template PARMS, e.g.:
5884 template <class T> struct S;
5885 template <class T> struct S {}; */
5888 redeclare_class_template (tree type
, tree parms
, tree cons
)
5894 if (!TYPE_TEMPLATE_INFO (type
))
5896 error ("%qT is not a template type", type
);
5900 tmpl
= TYPE_TI_TEMPLATE (type
);
5901 if (!PRIMARY_TEMPLATE_P (tmpl
))
5902 /* The type is nested in some template class. Nothing to worry
5903 about here; there are no new template parameters for the nested
5909 error ("template specifiers not specified in declaration of %qD",
5914 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5915 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5917 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5919 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5920 "redeclared with %d template parameter",
5921 "redeclared with %d template parameters",
5922 TREE_VEC_LENGTH (parms
));
5923 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5924 "previous declaration %qD used %d template parameter",
5925 "previous declaration %qD used %d template parameters",
5926 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5930 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5937 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5938 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5941 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5942 if (error_operand_p (tmpl_parm
))
5945 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5946 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5947 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5949 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5951 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5952 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5953 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5954 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5955 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5956 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5957 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5958 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5959 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5961 error ("template parameter %q+#D", tmpl_parm
);
5962 error ("redeclared here as %q#D", parm
);
5966 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5968 /* We have in [temp.param]:
5970 A template-parameter may not be given default arguments
5971 by two different declarations in the same scope. */
5972 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5973 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5974 "original definition appeared here");
5978 if (parm_default
!= NULL_TREE
)
5979 /* Update the previous template parameters (which are the ones
5980 that will really count) with the new default value. */
5981 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5982 else if (tmpl_default
!= NULL_TREE
)
5983 /* Update the new parameters, too; they'll be used as the
5984 parameters for any members. */
5985 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5987 /* Give each template template parm in this redeclaration a
5988 DECL_CONTEXT of the template for which they are a parameter. */
5989 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5991 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5992 DECL_CONTEXT (parm
) = tmpl
;
5995 if (TREE_CODE (parm
) == TYPE_DECL
)
5996 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5999 // Cannot redeclare a class template with a different set of constraints.
6000 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
6002 error_at (input_location
, "redeclaration %q#D with different "
6003 "constraints", tmpl
);
6004 inform (DECL_SOURCE_LOCATION (tmpl
),
6005 "original declaration appeared here");
6011 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6012 to be used when the caller has already checked
6013 (processing_template_decl
6014 && !instantiation_dependent_expression_p (expr)
6015 && potential_constant_expression (expr))
6016 and cleared processing_template_decl. */
6019 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6021 return tsubst_copy_and_build (expr
,
6024 /*in_decl=*/NULL_TREE
,
6025 /*function_p=*/false,
6026 /*integral_constant_expression_p=*/true);
6029 /* Simplify EXPR if it is a non-dependent expression. Returns the
6030 (possibly simplified) expression. */
6033 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6035 if (expr
== NULL_TREE
)
6038 /* If we're in a template, but EXPR isn't value dependent, simplify
6039 it. We're supposed to treat:
6041 template <typename T> void f(T[1 + 1]);
6042 template <typename T> void f(T[2]);
6044 as two declarations of the same function, for example. */
6045 if (processing_template_decl
6046 && is_nondependent_constant_expression (expr
))
6048 processing_template_decl_sentinel s
;
6049 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6055 instantiate_non_dependent_expr (tree expr
)
6057 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6060 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6061 an uninstantiated expression. */
6064 instantiate_non_dependent_or_null (tree expr
)
6066 if (expr
== NULL_TREE
)
6068 if (processing_template_decl
)
6070 if (!is_nondependent_constant_expression (expr
))
6074 processing_template_decl_sentinel s
;
6075 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6081 /* True iff T is a specialization of a variable template. */
6084 variable_template_specialization_p (tree t
)
6086 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6088 tree tmpl
= DECL_TI_TEMPLATE (t
);
6089 return variable_template_p (tmpl
);
6092 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6093 template declaration, or a TYPE_DECL for an alias declaration. */
6096 alias_type_or_template_p (tree t
)
6100 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6103 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6104 || DECL_ALIAS_TEMPLATE_P (t
));
6107 /* Return TRUE iff T is a specialization of an alias template. */
6110 alias_template_specialization_p (const_tree t
)
6112 /* It's an alias template specialization if it's an alias and its
6113 TYPE_NAME is a specialization of a primary template. */
6114 if (TYPE_ALIAS_P (t
))
6115 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6116 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6121 /* An alias template is complex from a SFINAE perspective if a template-id
6122 using that alias can be ill-formed when the expansion is not, as with
6123 the void_t template. We determine this by checking whether the
6124 expansion for the alias template uses all its template parameters. */
6126 struct uses_all_template_parms_data
6133 uses_all_template_parms_r (tree t
, void *data_
)
6135 struct uses_all_template_parms_data
&data
6136 = *(struct uses_all_template_parms_data
*)data_
;
6137 tree idx
= get_template_parm_index (t
);
6139 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6140 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6145 complex_alias_template_p (const_tree tmpl
)
6147 struct uses_all_template_parms_data data
;
6148 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6149 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6150 data
.level
= TMPL_PARMS_DEPTH (parms
);
6151 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6152 data
.seen
= XALLOCAVEC (bool, len
);
6153 for (int i
= 0; i
< len
; ++i
)
6154 data
.seen
[i
] = false;
6156 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6157 for (int i
= 0; i
< len
; ++i
)
6163 /* Return TRUE iff T is a specialization of a complex alias template with
6164 dependent template-arguments. */
6167 dependent_alias_template_spec_p (const_tree t
)
6169 if (!alias_template_specialization_p (t
))
6172 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6173 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6176 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6177 if (!any_dependent_template_arguments_p (args
))
6183 /* Return the number of innermost template parameters in TMPL. */
6186 num_innermost_template_parms (tree tmpl
)
6188 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6189 return TREE_VEC_LENGTH (parms
);
6192 /* Return either TMPL or another template that it is equivalent to under DR
6193 1286: An alias that just changes the name of a template is equivalent to
6194 the other template. */
6197 get_underlying_template (tree tmpl
)
6199 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6200 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6202 /* Determine if the alias is equivalent to an underlying template. */
6203 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6204 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6208 tree underlying
= TI_TEMPLATE (tinfo
);
6209 if (!PRIMARY_TEMPLATE_P (underlying
)
6210 || (num_innermost_template_parms (tmpl
)
6211 != num_innermost_template_parms (underlying
)))
6214 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6215 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6216 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6219 /* Alias is equivalent. Strip it and repeat. */
6226 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6227 must be a reference-to-function or a pointer-to-function type, as specified
6228 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6229 and check that the resulting function has external linkage. */
6232 convert_nontype_argument_function (tree type
, tree expr
,
6233 tsubst_flags_t complain
)
6237 linkage_kind linkage
;
6239 fn
= instantiate_type (type
, fns
, tf_none
);
6240 if (fn
== error_mark_node
)
6241 return error_mark_node
;
6243 if (value_dependent_expression_p (fn
))
6246 fn_no_ptr
= strip_fnptr_conv (fn
);
6247 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6248 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6249 if (BASELINK_P (fn_no_ptr
))
6250 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6252 /* [temp.arg.nontype]/1
6254 A template-argument for a non-type, non-template template-parameter
6257 -- the address of an object or function with external [C++11: or
6258 internal] linkage. */
6260 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6262 if (complain
& tf_error
)
6264 error ("%qE is not a valid template argument for type %qT",
6266 if (TYPE_PTR_P (type
))
6267 inform (input_location
, "it must be the address of a function "
6268 "with external linkage");
6270 inform (input_location
, "it must be the name of a function with "
6271 "external linkage");
6276 linkage
= decl_linkage (fn_no_ptr
);
6277 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6279 if (complain
& tf_error
)
6281 if (cxx_dialect
>= cxx11
)
6282 error ("%qE is not a valid template argument for type %qT "
6283 "because %qD has no linkage",
6284 expr
, type
, fn_no_ptr
);
6286 error ("%qE is not a valid template argument for type %qT "
6287 "because %qD does not have external linkage",
6288 expr
, type
, fn_no_ptr
);
6294 if (TYPE_REF_P (type
))
6296 if (REFERENCE_REF_P (fn
))
6297 fn
= TREE_OPERAND (fn
, 0);
6299 fn
= build_address (fn
);
6301 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6302 fn
= build_nop (type
, fn
);
6307 /* Subroutine of convert_nontype_argument.
6308 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6309 Emit an error otherwise. */
6312 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6313 tsubst_flags_t complain
)
6315 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6316 tree orig_expr
= expr
;
6318 if (null_ptr_cst_p (expr
))
6320 if (TREE_CODE (expr
) == PTRMEM_CST
6321 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6322 PTRMEM_CST_CLASS (expr
)))
6324 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6326 if (processing_template_decl
6327 && TREE_CODE (expr
) == ADDR_EXPR
6328 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6330 if (complain
& tf_error
)
6332 error_at (loc
, "%qE is not a valid template argument for type %qT",
6334 if (TREE_CODE (expr
) != PTRMEM_CST
)
6335 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6337 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6342 /* Returns TRUE iff the address of OP is value-dependent.
6344 14.6.2.4 [temp.dep.temp]:
6345 A non-integral non-type template-argument is dependent if its type is
6346 dependent or it has either of the following forms
6349 and contains a nested-name-specifier which specifies a class-name that
6350 names a dependent type.
6352 We generalize this to just say that the address of a member of a
6353 dependent class is value-dependent; the above doesn't cover the
6354 address of a static data member named with an unqualified-id. */
6357 has_value_dependent_address (tree op
)
6359 /* We could use get_inner_reference here, but there's no need;
6360 this is only relevant for template non-type arguments, which
6361 can only be expressed as &id-expression. */
6364 tree ctx
= CP_DECL_CONTEXT (op
);
6365 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6372 /* The next set of functions are used for providing helpful explanatory
6373 diagnostics for failed overload resolution. Their messages should be
6374 indented by two spaces for consistency with the messages in
6378 unify_success (bool /*explain_p*/)
6383 /* Other failure functions should call this one, to provide a single function
6384 for setting a breakpoint on. */
6387 unify_invalid (bool /*explain_p*/)
6393 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6396 inform (input_location
,
6397 " couldn't deduce template parameter %qD", parm
);
6398 return unify_invalid (explain_p
);
6402 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6405 inform (input_location
,
6406 " types %qT and %qT have incompatible cv-qualifiers",
6408 return unify_invalid (explain_p
);
6412 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6415 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6416 return unify_invalid (explain_p
);
6420 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6423 inform (input_location
,
6424 " template parameter %qD is not a parameter pack, but "
6427 return unify_invalid (explain_p
);
6431 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6434 inform (input_location
,
6435 " template argument %qE does not match "
6436 "pointer-to-member constant %qE",
6438 return unify_invalid (explain_p
);
6442 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6445 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6446 return unify_invalid (explain_p
);
6450 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6453 inform (input_location
,
6454 " inconsistent parameter pack deduction with %qT and %qT",
6456 return unify_invalid (explain_p
);
6460 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6465 inform (input_location
,
6466 " deduced conflicting types for parameter %qT (%qT and %qT)",
6467 parm
, first
, second
);
6469 inform (input_location
,
6470 " deduced conflicting values for non-type parameter "
6471 "%qE (%qE and %qE)", parm
, first
, second
);
6473 return unify_invalid (explain_p
);
6477 unify_vla_arg (bool explain_p
, tree arg
)
6480 inform (input_location
,
6481 " variable-sized array type %qT is not "
6482 "a valid template argument",
6484 return unify_invalid (explain_p
);
6488 unify_method_type_error (bool explain_p
, tree arg
)
6491 inform (input_location
,
6492 " member function type %qT is not a valid template argument",
6494 return unify_invalid (explain_p
);
6498 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6503 inform_n (input_location
, wanted
,
6504 " candidate expects at least %d argument, %d provided",
6505 " candidate expects at least %d arguments, %d provided",
6508 inform_n (input_location
, wanted
,
6509 " candidate expects %d argument, %d provided",
6510 " candidate expects %d arguments, %d provided",
6513 return unify_invalid (explain_p
);
6517 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6519 return unify_arity (explain_p
, have
, wanted
);
6523 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6524 bool least_p
= false)
6526 return unify_arity (explain_p
, have
, wanted
, least_p
);
6530 unify_arg_conversion (bool explain_p
, tree to_type
,
6531 tree from_type
, tree arg
)
6534 inform (cp_expr_loc_or_loc (arg
, input_location
),
6535 " cannot convert %qE (type %qT) to type %qT",
6536 arg
, from_type
, to_type
);
6537 return unify_invalid (explain_p
);
6541 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6542 tree parm
, tree arg
)
6547 case tbr_ambiguous_baseclass
:
6548 inform (input_location
, " %qT is an ambiguous base class of %qT",
6552 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6555 return unify_invalid (explain_p
);
6559 unify_inconsistent_template_template_parameters (bool explain_p
)
6562 inform (input_location
,
6563 " template parameters of a template template argument are "
6564 "inconsistent with other deduced template arguments");
6565 return unify_invalid (explain_p
);
6569 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6572 inform (input_location
,
6573 " can't deduce a template for %qT from non-template type %qT",
6575 return unify_invalid (explain_p
);
6579 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6582 inform (input_location
,
6583 " template argument %qE does not match %qE", arg
, parm
);
6584 return unify_invalid (explain_p
);
6587 /* Attempt to convert the non-type template parameter EXPR to the
6588 indicated TYPE. If the conversion is successful, return the
6589 converted value. If the conversion is unsuccessful, return
6590 NULL_TREE if we issued an error message, or error_mark_node if we
6591 did not. We issue error messages for out-and-out bad template
6592 parameters, but not simply because the conversion failed, since we
6593 might be just trying to do argument deduction. Both TYPE and EXPR
6594 must be non-dependent.
6596 The conversion follows the special rules described in
6597 [temp.arg.nontype], and it is much more strict than an implicit
6600 This function is called twice for each template argument (see
6601 lookup_template_class for a more accurate description of this
6602 problem). This means that we need to handle expressions which
6603 are not valid in a C++ source, but can be created from the
6604 first call (for instance, casts to perform conversions). These
6605 hacks can go away after we fix the double coercion problem. */
6608 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6611 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6612 tree orig_expr
= expr
;
6614 /* Detect immediately string literals as invalid non-type argument.
6615 This special-case is not needed for correctness (we would easily
6616 catch this later), but only to provide better diagnostic for this
6617 common user mistake. As suggested by DR 100, we do not mention
6618 linkage issues in the diagnostic as this is not the point. */
6619 /* FIXME we're making this OK. */
6620 if (TREE_CODE (expr
) == STRING_CST
)
6622 if (complain
& tf_error
)
6623 error ("%qE is not a valid template argument for type %qT "
6624 "because string literals can never be used in this context",
6629 /* Add the ADDR_EXPR now for the benefit of
6630 value_dependent_expression_p. */
6631 if (TYPE_PTROBV_P (type
)
6632 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6634 expr
= decay_conversion (expr
, complain
);
6635 if (expr
== error_mark_node
)
6636 return error_mark_node
;
6639 /* If we are in a template, EXPR may be non-dependent, but still
6640 have a syntactic, rather than semantic, form. For example, EXPR
6641 might be a SCOPE_REF, rather than the VAR_DECL to which the
6642 SCOPE_REF refers. Preserving the qualifying scope is necessary
6643 so that access checking can be performed when the template is
6644 instantiated -- but here we need the resolved form so that we can
6645 convert the argument. */
6646 bool non_dep
= false;
6647 if (TYPE_REF_OBJ_P (type
)
6648 && has_value_dependent_address (expr
))
6649 /* If we want the address and it's value-dependent, don't fold. */;
6650 else if (processing_template_decl
6651 && is_nondependent_constant_expression (expr
))
6653 if (error_operand_p (expr
))
6654 return error_mark_node
;
6655 expr_type
= TREE_TYPE (expr
);
6657 /* If the argument is non-dependent, perform any conversions in
6658 non-dependent context as well. */
6659 processing_template_decl_sentinel
s (non_dep
);
6661 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6663 if (value_dependent_expression_p (expr
))
6664 expr
= canonicalize_expr_argument (expr
, complain
);
6666 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6667 to a non-type argument of "nullptr". */
6668 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6669 expr
= fold_simple (convert (type
, expr
));
6671 /* In C++11, integral or enumeration non-type template arguments can be
6672 arbitrary constant expressions. Pointer and pointer to
6673 member arguments can be general constant expressions that evaluate
6674 to a null value, but otherwise still need to be of a specific form. */
6675 if (cxx_dialect
>= cxx11
)
6677 if (TREE_CODE (expr
) == PTRMEM_CST
)
6678 /* A PTRMEM_CST is already constant, and a valid template
6679 argument for a parameter of pointer to member type, we just want
6680 to leave it in that form rather than lower it to a
6682 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6683 || cxx_dialect
>= cxx17
)
6685 /* C++17: A template-argument for a non-type template-parameter shall
6686 be a converted constant expression (8.20) of the type of the
6687 template-parameter. */
6688 expr
= build_converted_constant_expr (type
, expr
, complain
);
6689 if (expr
== error_mark_node
)
6690 /* Make sure we return NULL_TREE only if we have really issued
6691 an error, as described above. */
6692 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
6693 expr
= maybe_constant_value (expr
);
6694 expr
= convert_from_reference (expr
);
6696 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6698 tree folded
= maybe_constant_value (expr
);
6699 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6700 : null_member_pointer_value_p (folded
))
6705 if (TYPE_REF_P (type
))
6706 expr
= mark_lvalue_use (expr
);
6708 expr
= mark_rvalue_use (expr
);
6710 /* HACK: Due to double coercion, we can get a
6711 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6712 which is the tree that we built on the first call (see
6713 below when coercing to reference to object or to reference to
6714 function). We just strip everything and get to the arg.
6715 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6717 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6719 tree probe_type
, probe
= expr
;
6720 if (REFERENCE_REF_P (probe
))
6721 probe
= TREE_OPERAND (probe
, 0);
6722 probe_type
= TREE_TYPE (probe
);
6723 if (TREE_CODE (probe
) == NOP_EXPR
)
6725 /* ??? Maybe we could use convert_from_reference here, but we
6726 would need to relax its constraints because the NOP_EXPR
6727 could actually change the type to something more cv-qualified,
6728 and this is not folded by convert_from_reference. */
6729 tree addr
= TREE_OPERAND (probe
, 0);
6730 if (TYPE_REF_P (probe_type
)
6731 && TREE_CODE (addr
) == ADDR_EXPR
6732 && TYPE_PTR_P (TREE_TYPE (addr
))
6733 && (same_type_ignoring_top_level_qualifiers_p
6734 (TREE_TYPE (probe_type
),
6735 TREE_TYPE (TREE_TYPE (addr
)))))
6737 expr
= TREE_OPERAND (addr
, 0);
6738 expr_type
= TREE_TYPE (probe_type
);
6743 /* [temp.arg.nontype]/5, bullet 1
6745 For a non-type template-parameter of integral or enumeration type,
6746 integral promotions (_conv.prom_) and integral conversions
6747 (_conv.integral_) are applied. */
6748 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6750 if (cxx_dialect
< cxx11
)
6752 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6753 t
= maybe_constant_value (t
);
6754 if (t
!= error_mark_node
)
6758 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6759 return error_mark_node
;
6761 /* Notice that there are constant expressions like '4 % 0' which
6762 do not fold into integer constants. */
6763 if (TREE_CODE (expr
) != INTEGER_CST
6764 && !value_dependent_expression_p (expr
))
6766 if (complain
& tf_error
)
6768 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6769 if (!require_potential_constant_expression (expr
))
6770 expr
= error_mark_node
;
6772 expr
= cxx_constant_value (expr
);
6773 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6774 inform (loc
, "in template argument for type %qT", type
);
6775 if (expr
== error_mark_node
)
6777 /* else cxx_constant_value complained but gave us
6778 a real constant, so go ahead. */
6779 if (TREE_CODE (expr
) != INTEGER_CST
)
6781 /* Some assemble time constant expressions like
6782 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6783 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6784 as we can emit them into .rodata initializers of
6785 variables, yet they can't fold into an INTEGER_CST at
6786 compile time. Refuse them here. */
6787 gcc_checking_assert (reduced_constant_expression_p (expr
));
6788 error_at (loc
, "template argument %qE for type %qT not "
6789 "a constant integer", expr
, type
);
6797 /* Avoid typedef problems. */
6798 if (TREE_TYPE (expr
) != type
)
6799 expr
= fold_convert (type
, expr
);
6801 /* [temp.arg.nontype]/5, bullet 2
6803 For a non-type template-parameter of type pointer to object,
6804 qualification conversions (_conv.qual_) and the array-to-pointer
6805 conversion (_conv.array_) are applied. */
6806 else if (TYPE_PTROBV_P (type
))
6808 tree decayed
= expr
;
6810 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6811 decay_conversion or an explicit cast. If it's a problematic cast,
6812 we'll complain about it below. */
6813 if (TREE_CODE (expr
) == NOP_EXPR
)
6817 if (TREE_CODE (probe
) == ADDR_EXPR
6818 && TYPE_PTR_P (TREE_TYPE (probe
)))
6821 expr_type
= TREE_TYPE (expr
);
6825 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6827 A template-argument for a non-type, non-template template-parameter
6828 shall be one of: [...]
6830 -- the name of a non-type template-parameter;
6831 -- the address of an object or function with external linkage, [...]
6832 expressed as "& id-expression" where the & is optional if the name
6833 refers to a function or array, or if the corresponding
6834 template-parameter is a reference.
6836 Here, we do not care about functions, as they are invalid anyway
6837 for a parameter of type pointer-to-object. */
6839 if (value_dependent_expression_p (expr
))
6840 /* Non-type template parameters are OK. */
6842 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6843 /* Null pointer values are OK in C++11. */;
6844 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6848 if (complain
& tf_error
)
6849 error ("%qD is not a valid template argument "
6850 "because %qD is a variable, not the address of "
6851 "a variable", orig_expr
, expr
);
6854 if (INDIRECT_TYPE_P (expr_type
))
6856 if (complain
& tf_error
)
6857 error ("%qE is not a valid template argument for %qT "
6858 "because it is not the address of a variable",
6862 /* Other values, like integer constants, might be valid
6863 non-type arguments of some other type. */
6864 return error_mark_node
;
6868 tree decl
= TREE_OPERAND (expr
, 0);
6872 if (complain
& tf_error
)
6873 error ("%qE is not a valid template argument of type %qT "
6874 "because %qE is not a variable", orig_expr
, type
, decl
);
6877 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6879 if (complain
& tf_error
)
6880 error ("%qE is not a valid template argument of type %qT "
6881 "because %qD does not have external linkage",
6882 orig_expr
, type
, decl
);
6885 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6886 && decl_linkage (decl
) == lk_none
)
6888 if (complain
& tf_error
)
6889 error ("%qE is not a valid template argument of type %qT "
6890 "because %qD has no linkage", orig_expr
, type
, decl
);
6893 /* C++17: For a non-type template-parameter of reference or pointer
6894 type, the value of the constant expression shall not refer to (or
6895 for a pointer type, shall not be the address of):
6896 * a subobject (4.5),
6897 * a temporary object (15.2),
6898 * a string literal (5.13.5),
6899 * the result of a typeid expression (8.2.8), or
6900 * a predefined __func__ variable (11.4.1). */
6901 else if (DECL_ARTIFICIAL (decl
))
6903 if (complain
& tf_error
)
6904 error ("the address of %qD is not a valid template argument",
6908 else if (!same_type_ignoring_top_level_qualifiers_p
6909 (strip_array_types (TREE_TYPE (type
)),
6910 strip_array_types (TREE_TYPE (decl
))))
6912 if (complain
& tf_error
)
6913 error ("the address of the %qT subobject of %qD is not a "
6914 "valid template argument", TREE_TYPE (type
), decl
);
6917 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6919 if (complain
& tf_error
)
6920 error ("the address of %qD is not a valid template argument "
6921 "because it does not have static storage duration",
6929 expr
= perform_qualification_conversions (type
, expr
);
6930 if (expr
== error_mark_node
)
6931 return error_mark_node
;
6933 /* [temp.arg.nontype]/5, bullet 3
6935 For a non-type template-parameter of type reference to object, no
6936 conversions apply. The type referred to by the reference may be more
6937 cv-qualified than the (otherwise identical) type of the
6938 template-argument. The template-parameter is bound directly to the
6939 template-argument, which must be an lvalue. */
6940 else if (TYPE_REF_OBJ_P (type
))
6942 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6944 return error_mark_node
;
6946 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6948 if (complain
& tf_error
)
6949 error ("%qE is not a valid template argument for type %qT "
6950 "because of conflicts in cv-qualification", expr
, type
);
6954 if (!lvalue_p (expr
))
6956 if (complain
& tf_error
)
6957 error ("%qE is not a valid template argument for type %qT "
6958 "because it is not an lvalue", expr
, type
);
6962 /* [temp.arg.nontype]/1
6964 A template-argument for a non-type, non-template template-parameter
6965 shall be one of: [...]
6967 -- the address of an object or function with external linkage. */
6968 if (INDIRECT_REF_P (expr
)
6969 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6971 expr
= TREE_OPERAND (expr
, 0);
6974 if (complain
& tf_error
)
6975 error ("%q#D is not a valid template argument for type %qT "
6976 "because a reference variable does not have a constant "
6977 "address", expr
, type
);
6982 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6983 && value_dependent_expression_p (expr
))
6984 /* OK, dependent reference. We don't want to ask whether a DECL is
6985 itself value-dependent, since what we want here is its address. */;
6990 if (complain
& tf_error
)
6991 error ("%qE is not a valid template argument for type %qT "
6992 "because it is not an object with linkage",
6997 /* DR 1155 allows internal linkage in C++11 and up. */
6998 linkage_kind linkage
= decl_linkage (expr
);
6999 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
7001 if (complain
& tf_error
)
7002 error ("%qE is not a valid template argument for type %qT "
7003 "because object %qD does not have linkage",
7008 expr
= build_address (expr
);
7011 if (!same_type_p (type
, TREE_TYPE (expr
)))
7012 expr
= build_nop (type
, expr
);
7014 /* [temp.arg.nontype]/5, bullet 4
7016 For a non-type template-parameter of type pointer to function, only
7017 the function-to-pointer conversion (_conv.func_) is applied. If the
7018 template-argument represents a set of overloaded functions (or a
7019 pointer to such), the matching function is selected from the set
7021 else if (TYPE_PTRFN_P (type
))
7023 /* If the argument is a template-id, we might not have enough
7024 context information to decay the pointer. */
7025 if (!type_unknown_p (expr_type
))
7027 expr
= decay_conversion (expr
, complain
);
7028 if (expr
== error_mark_node
)
7029 return error_mark_node
;
7032 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7033 /* Null pointer values are OK in C++11. */
7034 return perform_qualification_conversions (type
, expr
);
7036 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7037 if (!expr
|| expr
== error_mark_node
)
7040 /* [temp.arg.nontype]/5, bullet 5
7042 For a non-type template-parameter of type reference to function, no
7043 conversions apply. If the template-argument represents a set of
7044 overloaded functions, the matching function is selected from the set
7046 else if (TYPE_REFFN_P (type
))
7048 if (TREE_CODE (expr
) == ADDR_EXPR
)
7050 if (complain
& tf_error
)
7052 error ("%qE is not a valid template argument for type %qT "
7053 "because it is a pointer", expr
, type
);
7054 inform (input_location
, "try using %qE instead",
7055 TREE_OPERAND (expr
, 0));
7060 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7061 if (!expr
|| expr
== error_mark_node
)
7064 /* [temp.arg.nontype]/5, bullet 6
7066 For a non-type template-parameter of type pointer to member function,
7067 no conversions apply. If the template-argument represents a set of
7068 overloaded member functions, the matching member function is selected
7069 from the set (_over.over_). */
7070 else if (TYPE_PTRMEMFUNC_P (type
))
7072 expr
= instantiate_type (type
, expr
, tf_none
);
7073 if (expr
== error_mark_node
)
7074 return error_mark_node
;
7076 /* [temp.arg.nontype] bullet 1 says the pointer to member
7077 expression must be a pointer-to-member constant. */
7078 if (!value_dependent_expression_p (expr
)
7079 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7082 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7083 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7084 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7085 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7087 /* [temp.arg.nontype]/5, bullet 7
7089 For a non-type template-parameter of type pointer to data member,
7090 qualification conversions (_conv.qual_) are applied. */
7091 else if (TYPE_PTRDATAMEM_P (type
))
7093 /* [temp.arg.nontype] bullet 1 says the pointer to member
7094 expression must be a pointer-to-member constant. */
7095 if (!value_dependent_expression_p (expr
)
7096 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7099 expr
= perform_qualification_conversions (type
, expr
);
7100 if (expr
== error_mark_node
)
7103 else if (NULLPTR_TYPE_P (type
))
7105 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7107 if (complain
& tf_error
)
7108 error ("%qE is not a valid template argument for type %qT "
7109 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7114 /* A template non-type parameter must be one of the above. */
7118 /* Sanity check: did we actually convert the argument to the
7120 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7121 (type
, TREE_TYPE (expr
)));
7122 return convert_from_reference (expr
);
7125 /* Subroutine of coerce_template_template_parms, which returns 1 if
7126 PARM_PARM and ARG_PARM match using the rule for the template
7127 parameters of template template parameters. Both PARM and ARG are
7128 template parameters; the rest of the arguments are the same as for
7129 coerce_template_template_parms.
7132 coerce_template_template_parm (tree parm
,
7134 tsubst_flags_t complain
,
7138 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7139 || parm
== NULL_TREE
|| error_operand_p (parm
))
7142 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7145 switch (TREE_CODE (parm
))
7148 /* We encounter instantiations of templates like
7149 template <template <template <class> class> class TT>
7152 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7153 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7155 if (!coerce_template_template_parms
7156 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7162 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7163 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7164 /* Argument is a parameter pack but parameter is not. */
7169 /* The tsubst call is used to handle cases such as
7171 template <int> class C {};
7172 template <class T, template <T> class TT> class D {};
7175 i.e. the parameter list of TT depends on earlier parameters. */
7176 if (!uses_template_parms (TREE_TYPE (arg
)))
7178 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7179 if (!uses_template_parms (t
)
7180 && !same_type_p (t
, TREE_TYPE (arg
)))
7184 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7185 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7186 /* Argument is a parameter pack but parameter is not. */
7198 /* Coerce template argument list ARGLIST for use with template
7199 template-parameter TEMPL. */
7202 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7203 tsubst_flags_t complain
)
7205 /* Consider an example where a template template parameter declared as
7207 template <class T, class U = std::allocator<T> > class TT
7209 The template parameter level of T and U are one level larger than
7210 of TT. To proper process the default argument of U, say when an
7211 instantiation `TT<int>' is seen, we need to build the full
7212 arguments containing {int} as the innermost level. Outer levels,
7213 available when not appearing as default template argument, can be
7214 obtained from the arguments of the enclosing template.
7216 Suppose that TT is later substituted with std::vector. The above
7217 instantiation is `TT<int, std::allocator<T> >' with TT at
7218 level 1, and T at level 2, while the template arguments at level 1
7219 becomes {std::vector} and the inner level 2 is {int}. */
7221 tree outer
= DECL_CONTEXT (templ
);
7224 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7225 /* We want arguments for the partial specialization, not arguments for
7226 the primary template. */
7227 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7229 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7231 else if (current_template_parms
)
7233 /* This is an argument of the current template, so we haven't set
7234 DECL_CONTEXT yet. */
7235 tree relevant_template_parms
;
7237 /* Parameter levels that are greater than the level of the given
7238 template template parm are irrelevant. */
7239 relevant_template_parms
= current_template_parms
;
7240 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7241 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7242 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7244 outer
= template_parms_to_args (relevant_template_parms
);
7248 arglist
= add_to_template_args (outer
, arglist
);
7250 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7251 return coerce_template_parms (parmlist
, arglist
, templ
,
7253 /*require_all_args=*/true,
7254 /*use_default_args=*/true);
7257 /* A cache of template template parameters with match-all default
7259 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7261 store_defaulted_ttp (tree v
, tree t
)
7263 if (!defaulted_ttp_cache
)
7264 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7265 defaulted_ttp_cache
->put (v
, t
);
7268 lookup_defaulted_ttp (tree v
)
7270 if (defaulted_ttp_cache
)
7271 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7276 /* T is a bound template template-parameter. Copy its arguments into default
7277 arguments of the template template-parameter's template parameters. */
7280 add_defaults_to_ttp (tree otmpl
)
7282 if (tree c
= lookup_defaulted_ttp (otmpl
))
7285 tree ntmpl
= copy_node (otmpl
);
7287 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7288 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7289 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7290 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7291 TYPE_NAME (ntype
) = ntmpl
;
7292 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7294 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7295 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7296 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7297 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7299 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7300 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7301 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7302 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7303 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7305 tree o
= TREE_VEC_ELT (vec
, i
);
7306 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7308 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7309 TREE_PURPOSE (n
) = any_targ_node
;
7313 store_defaulted_ttp (otmpl
, ntmpl
);
7317 /* ARG is a bound potential template template-argument, and PARGS is a list
7318 of arguments for the corresponding template template-parameter. Adjust
7319 PARGS as appropriate for application to ARG's template, and if ARG is a
7320 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7321 arguments to the template template parameter. */
7324 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7326 ++processing_template_decl
;
7327 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7328 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7330 /* When comparing two template template-parameters in partial ordering,
7331 rewrite the one currently being used as an argument to have default
7332 arguments for all parameters. */
7333 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7334 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7335 if (pargs
!= error_mark_node
)
7336 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7337 TYPE_TI_ARGS (arg
));
7342 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7343 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7344 /*require_all*/true,
7345 /*use_default*/true);
7347 --processing_template_decl
;
7351 /* Subroutine of unify for the case when PARM is a
7352 BOUND_TEMPLATE_TEMPLATE_PARM. */
7355 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7358 tree parmvec
= TYPE_TI_ARGS (parm
);
7359 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7361 /* The template template parm might be variadic and the argument
7362 not, so flatten both argument lists. */
7363 parmvec
= expand_template_argument_pack (parmvec
);
7364 argvec
= expand_template_argument_pack (argvec
);
7368 /* In keeping with P0522R0, adjust P's template arguments
7369 to apply to A's template; then flatten it again. */
7370 tree nparmvec
= parmvec
;
7371 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7372 nparmvec
= expand_template_argument_pack (nparmvec
);
7374 if (unify (tparms
, targs
, nparmvec
, argvec
,
7375 UNIFY_ALLOW_NONE
, explain_p
))
7378 /* If the P0522 adjustment eliminated a pack expansion, deduce
7381 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7382 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7383 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7388 /* Deduce arguments T, i from TT<T> or TT<i>.
7389 We check each element of PARMVEC and ARGVEC individually
7390 rather than the whole TREE_VEC since they can have
7391 different number of elements, which is allowed under N2555. */
7393 int len
= TREE_VEC_LENGTH (parmvec
);
7395 /* Check if the parameters end in a pack, making them
7397 int parm_variadic_p
= 0;
7399 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7400 parm_variadic_p
= 1;
7402 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7403 /* If the template argument list of P contains a pack
7404 expansion that is not the last template argument, the
7405 entire template argument list is a non-deduced
7407 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7408 return unify_success (explain_p
);
7410 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7411 return unify_too_few_arguments (explain_p
,
7412 TREE_VEC_LENGTH (argvec
), len
);
7414 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7415 if (unify (tparms
, targs
,
7416 TREE_VEC_ELT (parmvec
, i
),
7417 TREE_VEC_ELT (argvec
, i
),
7418 UNIFY_ALLOW_NONE
, explain_p
))
7422 && unify_pack_expansion (tparms
, targs
,
7425 /*subr=*/true, explain_p
))
7432 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7433 template template parameters. Both PARM_PARMS and ARG_PARMS are
7434 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7437 Consider the example:
7438 template <class T> class A;
7439 template<template <class U> class TT> class B;
7441 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7442 the parameters to A, and OUTER_ARGS contains A. */
7445 coerce_template_template_parms (tree parm_parms
,
7447 tsubst_flags_t complain
,
7451 int nparms
, nargs
, i
;
7455 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7456 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7458 nparms
= TREE_VEC_LENGTH (parm_parms
);
7459 nargs
= TREE_VEC_LENGTH (arg_parms
);
7463 /* P0522R0: A template template-parameter P is at least as specialized as
7464 a template template-argument A if, given the following rewrite to two
7465 function templates, the function template corresponding to P is at
7466 least as specialized as the function template corresponding to A
7467 according to the partial ordering rules for function templates
7468 ([temp.func.order]). Given an invented class template X with the
7469 template parameter list of A (including default arguments):
7471 * Each of the two function templates has the same template parameters,
7472 respectively, as P or A.
7474 * Each function template has a single function parameter whose type is
7475 a specialization of X with template arguments corresponding to the
7476 template parameters from the respective function template where, for
7477 each template parameter PP in the template parameter list of the
7478 function template, a corresponding template argument AA is formed. If
7479 PP declares a parameter pack, then AA is the pack expansion
7480 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7482 If the rewrite produces an invalid type, then P is not at least as
7483 specialized as A. */
7485 /* So coerce P's args to apply to A's parms, and then deduce between A's
7486 args and the converted args. If that succeeds, A is at least as
7487 specialized as P, so they match.*/
7488 tree pargs
= template_parms_level_to_args (parm_parms
);
7489 ++processing_template_decl
;
7490 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7491 /*require_all*/true, /*use_default*/true);
7492 --processing_template_decl
;
7493 if (pargs
!= error_mark_node
)
7495 tree targs
= make_tree_vec (nargs
);
7496 tree aargs
= template_parms_level_to_args (arg_parms
);
7497 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7503 /* Determine whether we have a parameter pack at the end of the
7504 template template parameter's template parameter list. */
7505 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7507 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7509 if (error_operand_p (parm
))
7512 switch (TREE_CODE (parm
))
7516 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7521 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7531 && !(variadic_p
&& nargs
>= nparms
- 1))
7534 /* Check all of the template parameters except the parameter pack at
7535 the end (if any). */
7536 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7538 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7539 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7542 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7543 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7545 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7553 /* Check each of the template parameters in the template
7554 argument against the template parameter pack at the end of
7555 the template template parameter. */
7556 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7559 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7561 for (; i
< nargs
; ++i
)
7563 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7566 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7568 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7577 /* Verifies that the deduced template arguments (in TARGS) for the
7578 template template parameters (in TPARMS) represent valid bindings,
7579 by comparing the template parameter list of each template argument
7580 to the template parameter list of its corresponding template
7581 template parameter, in accordance with DR150. This
7582 routine can only be called after all template arguments have been
7583 deduced. It will return TRUE if all of the template template
7584 parameter bindings are okay, FALSE otherwise. */
7586 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7588 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7591 /* We're dealing with template parms in this process. */
7592 ++processing_template_decl
;
7594 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7596 for (i
= 0; i
< ntparms
; ++i
)
7598 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7599 tree targ
= TREE_VEC_ELT (targs
, i
);
7601 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7603 tree packed_args
= NULL_TREE
;
7606 if (ARGUMENT_PACK_P (targ
))
7608 /* Look inside the argument pack. */
7609 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7610 len
= TREE_VEC_LENGTH (packed_args
);
7613 for (idx
= 0; idx
< len
; ++idx
)
7615 tree targ_parms
= NULL_TREE
;
7618 /* Extract the next argument from the argument
7620 targ
= TREE_VEC_ELT (packed_args
, idx
);
7622 if (PACK_EXPANSION_P (targ
))
7623 /* Look at the pattern of the pack expansion. */
7624 targ
= PACK_EXPANSION_PATTERN (targ
);
7626 /* Extract the template parameters from the template
7628 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7629 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7630 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7631 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7633 /* Verify that we can coerce the template template
7634 parameters from the template argument to the template
7635 parameter. This requires an exact match. */
7637 && !coerce_template_template_parms
7638 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7653 --processing_template_decl
;
7657 /* Since type attributes aren't mangled, we need to strip them from
7658 template type arguments. */
7661 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7663 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7665 bool removed_attributes
= false;
7666 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7667 if (removed_attributes
7668 && (complain
& tf_warning
))
7669 warning (OPT_Wignored_attributes
,
7670 "ignoring attributes on template argument %qT", arg
);
7674 /* And from inside dependent non-type arguments like sizeof(Type). */
7677 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7679 if (!arg
|| arg
== error_mark_node
)
7681 bool removed_attributes
= false;
7682 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7683 if (removed_attributes
7684 && (complain
& tf_warning
))
7685 warning (OPT_Wignored_attributes
,
7686 "ignoring attributes in template argument %qE", arg
);
7690 // A template declaration can be substituted for a constrained
7691 // template template parameter only when the argument is more
7692 // constrained than the parameter.
7694 is_compatible_template_arg (tree parm
, tree arg
)
7696 tree parm_cons
= get_constraints (parm
);
7698 /* For now, allow constrained template template arguments
7699 and unconstrained template template parameters. */
7700 if (parm_cons
== NULL_TREE
)
7703 tree arg_cons
= get_constraints (arg
);
7705 // If the template parameter is constrained, we need to rewrite its
7706 // constraints in terms of the ARG's template parameters. This ensures
7707 // that all of the template parameter types will have the same depth.
7709 // Note that this is only valid when coerce_template_template_parm is
7710 // true for the innermost template parameters of PARM and ARG. In other
7711 // words, because coercion is successful, this conversion will be valid.
7714 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7715 parm_cons
= tsubst_constraint_info (parm_cons
,
7716 INNERMOST_TEMPLATE_ARGS (args
),
7717 tf_none
, NULL_TREE
);
7718 if (parm_cons
== error_mark_node
)
7722 return subsumes (parm_cons
, arg_cons
);
7725 // Convert a placeholder argument into a binding to the original
7726 // parameter. The original parameter is saved as the TREE_TYPE of
7729 convert_wildcard_argument (tree parm
, tree arg
)
7731 TREE_TYPE (arg
) = parm
;
7735 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7736 because one of them is dependent. But we need to represent the
7737 conversion for the benefit of cp_tree_equal. */
7740 maybe_convert_nontype_argument (tree type
, tree arg
)
7742 /* Auto parms get no conversion. */
7743 if (type_uses_auto (type
))
7745 /* We don't need or want to add this conversion now if we're going to use the
7746 argument for deduction. */
7747 if (value_dependent_expression_p (arg
))
7750 type
= cv_unqualified (type
);
7751 tree argtype
= TREE_TYPE (arg
);
7752 if (same_type_p (type
, argtype
))
7755 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7756 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7760 /* Convert the indicated template ARG as necessary to match the
7761 indicated template PARM. Returns the converted ARG, or
7762 error_mark_node if the conversion was unsuccessful. Error and
7763 warning messages are issued under control of COMPLAIN. This
7764 conversion is for the Ith parameter in the parameter list. ARGS is
7765 the full set of template arguments deduced so far. */
7768 convert_template_argument (tree parm
,
7771 tsubst_flags_t complain
,
7777 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7779 if (parm
== error_mark_node
)
7780 return error_mark_node
;
7782 /* Trivially convert placeholders. */
7783 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7784 return convert_wildcard_argument (parm
, arg
);
7786 if (arg
== any_targ_node
)
7789 if (TREE_CODE (arg
) == TREE_LIST
7790 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7792 /* The template argument was the name of some
7793 member function. That's usually
7794 invalid, but static members are OK. In any
7795 case, grab the underlying fields/functions
7796 and issue an error later if required. */
7797 orig_arg
= TREE_VALUE (arg
);
7798 TREE_TYPE (arg
) = unknown_type_node
;
7803 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7804 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7805 || requires_tmpl_type
);
7807 /* When determining whether an argument pack expansion is a template,
7808 look at the pattern. */
7809 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7810 arg
= PACK_EXPANSION_PATTERN (arg
);
7812 /* Deal with an injected-class-name used as a template template arg. */
7813 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7815 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7816 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7818 if (cxx_dialect
>= cxx11
)
7819 /* OK under DR 1004. */;
7820 else if (complain
& tf_warning_or_error
)
7821 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7822 " used as template template argument", TYPE_NAME (arg
));
7823 else if (flag_pedantic_errors
)
7831 ((TREE_CODE (arg
) == TEMPLATE_DECL
7832 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7833 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7834 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7835 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7838 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7839 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7840 arg
= TYPE_STUB_DECL (arg
);
7842 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7844 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7845 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7847 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7849 if (complain
& tf_error
)
7850 error ("invalid use of destructor %qE as a type", orig_arg
);
7851 return error_mark_node
;
7854 permerror (input_location
,
7855 "to refer to a type member of a template parameter, "
7856 "use %<typename %E%>", orig_arg
);
7858 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7859 TREE_OPERAND (arg
, 1),
7865 if (is_type
!= requires_type
)
7869 if (complain
& tf_error
)
7871 error ("type/value mismatch at argument %d in template "
7872 "parameter list for %qD",
7875 inform (input_location
,
7876 " expected a constant of type %qT, got %qT",
7878 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7879 else if (requires_tmpl_type
)
7880 inform (input_location
,
7881 " expected a class template, got %qE", orig_arg
);
7883 inform (input_location
,
7884 " expected a type, got %qE", orig_arg
);
7887 return error_mark_node
;
7889 if (is_tmpl_type
^ requires_tmpl_type
)
7891 if (in_decl
&& (complain
& tf_error
))
7893 error ("type/value mismatch at argument %d in template "
7894 "parameter list for %qD",
7897 inform (input_location
,
7898 " expected a type, got %qT", DECL_NAME (arg
));
7900 inform (input_location
,
7901 " expected a class template, got %qT", orig_arg
);
7903 return error_mark_node
;
7906 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7907 /* We already did the appropriate conversion when packing args. */
7911 if (requires_tmpl_type
)
7913 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7914 /* The number of argument required is not known yet.
7915 Just accept it for now. */
7919 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7922 /* Strip alias templates that are equivalent to another
7924 arg
= get_underlying_template (arg
);
7925 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7927 if (coerce_template_template_parms (parmparm
, argparm
,
7933 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7935 if (val
!= error_mark_node
)
7937 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7938 val
= TREE_TYPE (val
);
7939 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7940 val
= make_pack_expansion (val
, complain
);
7945 if (in_decl
&& (complain
& tf_error
))
7947 error ("type/value mismatch at argument %d in "
7948 "template parameter list for %qD",
7950 inform (input_location
,
7951 " expected a template of type %qD, got %qT",
7955 val
= error_mark_node
;
7958 // Check that the constraints are compatible before allowing the
7960 if (val
!= error_mark_node
)
7961 if (!is_compatible_template_arg (parm
, arg
))
7963 if (in_decl
&& (complain
& tf_error
))
7965 error ("constraint mismatch at argument %d in "
7966 "template parameter list for %qD",
7968 inform (input_location
, " expected %qD but got %qD",
7971 val
= error_mark_node
;
7977 /* We only form one instance of each template specialization.
7978 Therefore, if we use a non-canonical variant (i.e., a
7979 typedef), any future messages referring to the type will use
7980 the typedef, which is confusing if those future uses do not
7981 themselves also use the typedef. */
7983 val
= canonicalize_type_argument (val
, complain
);
7987 tree t
= TREE_TYPE (parm
);
7989 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
7990 > TMPL_ARGS_DEPTH (args
))
7991 /* We don't have enough levels of args to do any substitution. This
7992 can happen in the context of -fnew-ttp-matching. */;
7993 else if (tree a
= type_uses_auto (t
))
7995 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7996 if (t
== error_mark_node
)
7997 return error_mark_node
;
8000 t
= tsubst (t
, args
, complain
, in_decl
);
8002 if (invalid_nontype_parm_type_p (t
, complain
))
8003 return error_mark_node
;
8005 if (!type_dependent_expression_p (orig_arg
)
8006 && !uses_template_parms (t
))
8007 /* We used to call digest_init here. However, digest_init
8008 will report errors, which we don't want when complain
8009 is zero. More importantly, digest_init will try too
8010 hard to convert things: for example, `0' should not be
8011 converted to pointer type at this point according to
8012 the standard. Accepting this is not merely an
8013 extension, since deciding whether or not these
8014 conversions can occur is part of determining which
8015 function template to call, or whether a given explicit
8016 argument specification is valid. */
8017 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8020 val
= canonicalize_expr_argument (orig_arg
, complain
);
8021 val
= maybe_convert_nontype_argument (t
, val
);
8025 if (val
== NULL_TREE
)
8026 val
= error_mark_node
;
8027 else if (val
== error_mark_node
&& (complain
& tf_error
))
8028 error ("could not convert template argument %qE from %qT to %qT",
8029 orig_arg
, TREE_TYPE (orig_arg
), t
);
8031 if (INDIRECT_REF_P (val
))
8033 /* Reject template arguments that are references to built-in
8034 functions with no library fallbacks. */
8035 const_tree inner
= TREE_OPERAND (val
, 0);
8036 const_tree innertype
= TREE_TYPE (inner
);
8038 && TYPE_REF_P (innertype
)
8039 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8040 && TREE_OPERAND_LENGTH (inner
) > 0
8041 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8042 return error_mark_node
;
8045 if (TREE_CODE (val
) == SCOPE_REF
)
8047 /* Strip typedefs from the SCOPE_REF. */
8048 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8049 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8051 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8052 QUALIFIED_NAME_IS_TEMPLATE (val
));
8059 /* Coerces the remaining template arguments in INNER_ARGS (from
8060 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8061 Returns the coerced argument pack. PARM_IDX is the position of this
8062 parameter in the template parameter list. ARGS is the original
8063 template argument list. */
8065 coerce_template_parameter_pack (tree parms
,
8073 tsubst_flags_t complain
)
8075 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8076 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8079 tree packed_parms
= NULL_TREE
;
8081 if (arg_idx
> nargs
)
8084 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8086 /* When the template parameter is a non-type template parameter pack
8087 or template template parameter pack whose type or template
8088 parameters use parameter packs, we know exactly how many arguments
8089 we are looking for. Build a vector of the instantiated decls for
8090 these template parameters in PACKED_PARMS. */
8091 /* We can't use make_pack_expansion here because it would interpret a
8092 _DECL as a use rather than a declaration. */
8093 tree decl
= TREE_VALUE (parm
);
8094 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8095 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8096 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8097 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8099 TREE_VEC_LENGTH (args
)--;
8100 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8101 TREE_VEC_LENGTH (args
)++;
8103 if (packed_parms
== error_mark_node
)
8104 return error_mark_node
;
8106 /* If we're doing a partial instantiation of a member template,
8107 verify that all of the types used for the non-type
8108 template parameter pack are, in fact, valid for non-type
8109 template parameters. */
8111 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8113 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8114 for (j
= 0; j
< len
; ++j
)
8116 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8117 if (invalid_nontype_parm_type_p (t
, complain
))
8118 return error_mark_node
;
8120 /* We don't know how many args we have yet, just
8121 use the unconverted ones for now. */
8125 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8127 /* Check if we have a placeholder pack, which indicates we're
8128 in the context of a introduction list. In that case we want
8129 to match this pack to the single placeholder. */
8130 else if (arg_idx
< nargs
8131 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8132 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8134 nargs
= arg_idx
+ 1;
8135 packed_args
= make_tree_vec (1);
8138 packed_args
= make_tree_vec (nargs
- arg_idx
);
8140 /* Convert the remaining arguments, which will be a part of the
8141 parameter pack "parm". */
8142 int first_pack_arg
= arg_idx
;
8143 for (; arg_idx
< nargs
; ++arg_idx
)
8145 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8146 tree actual_parm
= TREE_VALUE (parm
);
8147 int pack_idx
= arg_idx
- first_pack_arg
;
8151 /* Once we've packed as many args as we have types, stop. */
8152 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8154 else if (PACK_EXPANSION_P (arg
))
8155 /* We don't know how many args we have yet, just
8156 use the unconverted ones for now. */
8159 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8162 if (arg
== error_mark_node
)
8164 if (complain
& tf_error
)
8165 error ("template argument %d is invalid", arg_idx
+ 1);
8168 arg
= convert_template_argument (actual_parm
,
8169 arg
, new_args
, complain
, parm_idx
,
8171 if (arg
== error_mark_node
)
8173 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8176 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8177 && TREE_VEC_LENGTH (packed_args
) > 0)
8179 if (complain
& tf_error
)
8180 error ("wrong number of template arguments (%d, should be %d)",
8181 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8182 return error_mark_node
;
8185 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8186 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8187 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8190 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8191 TREE_CONSTANT (argument_pack
) = 1;
8194 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8196 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8197 TREE_VEC_LENGTH (packed_args
));
8198 return argument_pack
;
8201 /* Returns the number of pack expansions in the template argument vector
8205 pack_expansion_args_count (tree args
)
8210 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8212 tree elt
= TREE_VEC_ELT (args
, i
);
8213 if (elt
&& PACK_EXPANSION_P (elt
))
8219 /* Convert all template arguments to their appropriate types, and
8220 return a vector containing the innermost resulting template
8221 arguments. If any error occurs, return error_mark_node. Error and
8222 warning messages are issued under control of COMPLAIN.
8224 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8225 for arguments not specified in ARGS. Otherwise, if
8226 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8227 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8228 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8232 coerce_template_parms (tree parms
,
8235 tsubst_flags_t complain
,
8236 bool require_all_args
,
8237 bool use_default_args
)
8239 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8240 tree orig_inner_args
;
8243 tree new_inner_args
;
8244 int saved_unevaluated_operand
;
8245 int saved_inhibit_evaluation_warnings
;
8247 /* When used as a boolean value, indicates whether this is a
8248 variadic template parameter list. Since it's an int, we can also
8249 subtract it from nparms to get the number of non-variadic
8252 int variadic_args_p
= 0;
8253 int post_variadic_parms
= 0;
8255 /* Adjustment to nparms for fixed parameter packs. */
8256 int fixed_pack_adjust
= 0;
8257 int fixed_packs
= 0;
8260 /* Likewise for parameters with default arguments. */
8263 if (args
== error_mark_node
)
8264 return error_mark_node
;
8266 nparms
= TREE_VEC_LENGTH (parms
);
8268 /* Determine if there are any parameter packs or default arguments. */
8269 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8271 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8273 ++post_variadic_parms
;
8274 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8276 if (TREE_PURPOSE (parm
))
8280 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8281 /* If there are no parameters that follow a parameter pack, we need to
8282 expand any argument packs so that we can deduce a parameter pack from
8283 some non-packed args followed by an argument pack, as in variadic85.C.
8284 If there are such parameters, we need to leave argument packs intact
8285 so the arguments are assigned properly. This can happen when dealing
8286 with a nested class inside a partial specialization of a class
8287 template, as in variadic92.C, or when deducing a template parameter pack
8288 from a sub-declarator, as in variadic114.C. */
8289 if (!post_variadic_parms
)
8290 inner_args
= expand_template_argument_pack (inner_args
);
8292 /* Count any pack expansion args. */
8293 variadic_args_p
= pack_expansion_args_count (inner_args
);
8295 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8296 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8297 || (nargs
< nparms
- variadic_p
8300 && (!use_default_args
8301 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8302 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8305 if (complain
& tf_error
)
8307 if (variadic_p
|| default_p
)
8309 nparms
-= variadic_p
+ default_p
;
8310 error ("wrong number of template arguments "
8311 "(%d, should be at least %d)", nargs
, nparms
);
8314 error ("wrong number of template arguments "
8315 "(%d, should be %d)", nargs
, nparms
);
8318 inform (DECL_SOURCE_LOCATION (in_decl
),
8319 "provided for %qD", in_decl
);
8322 return error_mark_node
;
8324 /* We can't pass a pack expansion to a non-pack parameter of an alias
8325 template (DR 1430). */
8327 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8328 || concept_template_p (in_decl
))
8330 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8332 if (complain
& tf_error
)
8334 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8336 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8337 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8339 if (PACK_EXPANSION_P (arg
)
8340 && !template_parameter_pack_p (parm
))
8342 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8343 error_at (location_of (arg
),
8344 "pack expansion argument for non-pack parameter "
8345 "%qD of alias template %qD", parm
, in_decl
);
8347 error_at (location_of (arg
),
8348 "pack expansion argument for non-pack parameter "
8349 "%qD of concept %qD", parm
, in_decl
);
8350 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8357 return error_mark_node
;
8360 /* We need to evaluate the template arguments, even though this
8361 template-id may be nested within a "sizeof". */
8362 saved_unevaluated_operand
= cp_unevaluated_operand
;
8363 cp_unevaluated_operand
= 0;
8364 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8365 c_inhibit_evaluation_warnings
= 0;
8366 new_inner_args
= make_tree_vec (nparms
);
8367 new_args
= add_outermost_template_args (args
, new_inner_args
);
8368 int pack_adjust
= 0;
8369 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8374 /* Get the Ith template parameter. */
8375 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8377 if (parm
== error_mark_node
)
8379 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8383 /* Calculate the next argument. */
8384 if (arg_idx
< nargs
)
8385 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8389 if (template_parameter_pack_p (TREE_VALUE (parm
))
8390 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8392 /* Some arguments will be placed in the
8393 template parameter pack PARM. */
8394 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8395 inner_args
, arg_idx
,
8399 if (arg
== NULL_TREE
)
8401 /* We don't know how many args we have yet, just use the
8402 unconverted (and still packed) ones for now. */
8403 new_inner_args
= orig_inner_args
;
8408 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8410 /* Store this argument. */
8411 if (arg
== error_mark_node
)
8414 /* We are done with all of the arguments. */
8420 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8421 arg_idx
+= pack_adjust
;
8422 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8425 fixed_pack_adjust
+= pack_adjust
;
8433 if (PACK_EXPANSION_P (arg
))
8435 /* "If every valid specialization of a variadic template
8436 requires an empty template parameter pack, the template is
8437 ill-formed, no diagnostic required." So check that the
8438 pattern works with this parameter. */
8439 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8440 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8444 if (conv
== error_mark_node
)
8446 if (complain
& tf_error
)
8447 inform (input_location
, "so any instantiation with a "
8448 "non-empty parameter pack would be ill-formed");
8451 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8452 /* Recover from missing typename. */
8453 TREE_VEC_ELT (inner_args
, arg_idx
)
8454 = make_pack_expansion (conv
, complain
);
8456 /* We don't know how many args we have yet, just
8457 use the unconverted ones for now. */
8458 new_inner_args
= inner_args
;
8463 else if (require_all_args
)
8465 /* There must be a default arg in this case. */
8466 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8468 /* The position of the first default template argument,
8469 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8471 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8472 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8473 arg_idx
- pack_adjust
);
8478 if (arg
== error_mark_node
)
8480 if (complain
& tf_error
)
8481 error ("template argument %d is invalid", arg_idx
+ 1);
8485 /* This can occur if there was an error in the template
8486 parameter list itself (which we would already have
8487 reported) that we are trying to recover from, e.g., a class
8488 template with a parameter list such as
8489 template<typename..., typename> (cpp0x/variadic150.C). */
8492 /* This can also happen with a fixed parameter pack (71834). */
8493 if (arg_idx
>= nargs
)
8497 arg
= convert_template_argument (TREE_VALUE (parm
),
8498 arg
, new_args
, complain
,
8501 if (arg
== error_mark_node
)
8503 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8505 cp_unevaluated_operand
= saved_unevaluated_operand
;
8506 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8508 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8510 /* If we had fixed parameter packs, we didn't know how many arguments we
8511 actually needed earlier; now we do. */
8512 nparms
+= fixed_pack_adjust
;
8513 variadic_p
-= fixed_packs
;
8517 if (arg_idx
< nargs
)
8519 /* We had some pack expansion arguments that will only work if the packs
8520 are empty, but wait until instantiation time to complain.
8521 See variadic-ttp3.C. */
8522 int len
= nparms
+ (nargs
- arg_idx
);
8523 tree args
= make_tree_vec (len
);
8525 for (; i
< nparms
; ++i
)
8526 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8527 for (; i
< len
; ++i
, ++arg_idx
)
8528 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8529 arg_idx
- pack_adjust
);
8530 new_inner_args
= args
;
8535 gcc_assert (!(complain
& tf_error
) || seen_error ());
8536 return error_mark_node
;
8539 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8541 TREE_VEC_LENGTH (new_inner_args
));
8543 return new_inner_args
;
8546 /* Convert all template arguments to their appropriate types, and
8547 return a vector containing the innermost resulting template
8548 arguments. If any error occurs, return error_mark_node. Error and
8549 warning messages are not issued.
8551 Note that no function argument deduction is performed, and default
8552 arguments are used to fill in unspecified arguments. */
8554 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8556 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8559 /* Convert all template arguments to their appropriate type, and
8560 instantiate default arguments as needed. This returns a vector
8561 containing the innermost resulting template arguments, or
8562 error_mark_node if unsuccessful. */
8564 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8565 tsubst_flags_t complain
)
8567 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8570 /* Like coerce_template_parms. If PARMS represents all template
8571 parameters levels, this function returns a vector of vectors
8572 representing all the resulting argument levels. Note that in this
8573 case, only the innermost arguments are coerced because the
8574 outermost ones are supposed to have been coerced already.
8576 Otherwise, if PARMS represents only (the innermost) vector of
8577 parameters, this function returns a vector containing just the
8578 innermost resulting arguments. */
8581 coerce_innermost_template_parms (tree parms
,
8584 tsubst_flags_t complain
,
8585 bool require_all_args
,
8586 bool use_default_args
)
8588 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8589 int args_depth
= TMPL_ARGS_DEPTH (args
);
8592 if (parms_depth
> 1)
8594 coerced_args
= make_tree_vec (parms_depth
);
8598 for (level
= parms
, cur_depth
= parms_depth
;
8599 parms_depth
> 0 && level
!= NULL_TREE
;
8600 level
= TREE_CHAIN (level
), --cur_depth
)
8603 if (cur_depth
== args_depth
)
8604 l
= coerce_template_parms (TREE_VALUE (level
),
8605 args
, in_decl
, complain
,
8609 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8611 if (l
== error_mark_node
)
8612 return error_mark_node
;
8614 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8618 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8619 args
, in_decl
, complain
,
8622 return coerced_args
;
8625 /* Returns 1 if template args OT and NT are equivalent. */
8628 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8632 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8634 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8637 if (TREE_CODE (nt
) == TREE_VEC
)
8638 /* For member templates */
8639 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8640 else if (PACK_EXPANSION_P (ot
))
8641 return (PACK_EXPANSION_P (nt
)
8642 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8643 PACK_EXPANSION_PATTERN (nt
))
8644 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8645 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8646 else if (ARGUMENT_PACK_P (ot
))
8651 if (!ARGUMENT_PACK_P (nt
))
8654 opack
= ARGUMENT_PACK_ARGS (ot
);
8655 npack
= ARGUMENT_PACK_ARGS (nt
);
8656 len
= TREE_VEC_LENGTH (opack
);
8657 if (TREE_VEC_LENGTH (npack
) != len
)
8659 for (i
= 0; i
< len
; ++i
)
8660 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8661 TREE_VEC_ELT (npack
, i
)))
8665 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8667 else if (TYPE_P (nt
))
8671 /* Don't treat an alias template specialization with dependent
8672 arguments as equivalent to its underlying type when used as a
8673 template argument; we need them to be distinct so that we
8674 substitute into the specialization arguments at instantiation
8675 time. And aliases can't be equivalent without being ==, so
8676 we don't need to look any deeper.
8678 During partial ordering, however, we need to treat them normally so
8679 that we can order uses of the same alias with different
8680 cv-qualification (79960). */
8682 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8685 return same_type_p (ot
, nt
);
8687 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8691 /* Try to treat a template non-type argument that has been converted
8692 to the parameter type as equivalent to one that hasn't yet. */
8693 for (enum tree_code code1
= TREE_CODE (ot
);
8694 CONVERT_EXPR_CODE_P (code1
)
8695 || code1
== NON_LVALUE_EXPR
;
8696 code1
= TREE_CODE (ot
))
8697 ot
= TREE_OPERAND (ot
, 0);
8698 for (enum tree_code code2
= TREE_CODE (nt
);
8699 CONVERT_EXPR_CODE_P (code2
)
8700 || code2
== NON_LVALUE_EXPR
;
8701 code2
= TREE_CODE (nt
))
8702 nt
= TREE_OPERAND (nt
, 0);
8704 return cp_tree_equal (ot
, nt
);
8708 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8709 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8710 NEWARG_PTR with the offending arguments if they are non-NULL. */
8713 comp_template_args (tree oldargs
, tree newargs
,
8714 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8719 if (oldargs
== newargs
)
8722 if (!oldargs
|| !newargs
)
8725 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8728 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8730 tree nt
= TREE_VEC_ELT (newargs
, i
);
8731 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8733 if (! template_args_equal (ot
, nt
, partial_order
))
8735 if (oldarg_ptr
!= NULL
)
8737 if (newarg_ptr
!= NULL
)
8746 comp_template_args_porder (tree oargs
, tree nargs
)
8748 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8751 /* Implement a freelist interface for objects of type T.
8753 Head is a separate object, rather than a regular member, so that we
8754 can define it as a GTY deletable pointer, which is highly
8755 desirable. A data member could be declared that way, but then the
8756 containing object would implicitly get GTY((user)), which would
8757 prevent us from instantiating freelists as global objects.
8758 Although this way we can create freelist global objects, they're
8759 such thin wrappers that instantiating temporaries at every use
8760 loses nothing and saves permanent storage for the freelist object.
8762 Member functions next, anew, poison and reinit have default
8763 implementations that work for most of the types we're interested
8764 in, but if they don't work for some type, they should be explicitly
8765 specialized. See the comments before them for requirements, and
8766 the example specializations for the tree_list_freelist. */
8767 template <typename T
>
8770 /* Return the next object in a chain. We could just do type
8771 punning, but if we access the object with its underlying type, we
8772 avoid strict-aliasing trouble. This needs only work between
8773 poison and reinit. */
8774 static T
*&next (T
*obj
) { return obj
->next
; }
8776 /* Return a newly allocated, uninitialized or minimally-initialized
8777 object of type T. Any initialization performed by anew should
8778 either remain across the life of the object and the execution of
8779 poison, or be redone by reinit. */
8780 static T
*anew () { return ggc_alloc
<T
> (); }
8782 /* Optionally scribble all over the bits holding the object, so that
8783 they become (mostly?) uninitialized memory. This is called while
8784 preparing to make the object part of the free list. */
8785 static void poison (T
*obj
) {
8786 T
*p ATTRIBUTE_UNUSED
= obj
;
8787 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8789 #ifdef ENABLE_GC_CHECKING
8790 /* Poison the data, to indicate the data is garbage. */
8791 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
8792 memset (p
, 0xa5, sizeof (*p
));
8794 /* Let valgrind know the object is free. */
8795 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
8797 /* Let valgrind know the next portion of the object is available,
8798 but uninitialized. */
8799 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8802 /* Bring an object that underwent at least one lifecycle after anew
8803 and before the most recent free and poison, back to a usable
8804 state, reinitializing whatever is needed for it to be
8805 functionally equivalent to an object just allocated and returned
8806 by anew. This may poison or clear the next field, used by
8807 freelist housekeeping after poison was called. */
8808 static void reinit (T
*obj
) {
8809 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8811 #ifdef ENABLE_GC_CHECKING
8812 memset (q
, 0xa5, sizeof (*q
));
8814 /* Let valgrind know the entire object is available, but
8816 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
8819 /* Reference a GTY-deletable pointer that points to the first object
8820 in the free list proper. */
8823 /* Construct a freelist object chaining objects off of HEAD. */
8824 freelist (T
*&head
) : head(head
) {}
8826 /* Add OBJ to the free object list. The former head becomes OBJ's
8835 /* Take an object from the free list, if one is available, or
8836 allocate a new one. Objects taken from the free list should be
8837 regarded as filled with garbage, except for bits that are
8838 configured to be preserved across free and alloc. */
8853 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8854 want to allocate a TREE_LIST using the usual interface, and ensure
8855 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8856 build_tree_list logic in reinit, so this could go out of sync. */
8859 freelist
<tree_node
>::next (tree obj
)
8861 return TREE_CHAIN (obj
);
8865 freelist
<tree_node
>::anew ()
8867 return build_tree_list (NULL
, NULL
);
8871 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
8873 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
8874 tree p ATTRIBUTE_UNUSED
= obj
;
8875 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8876 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
8878 #ifdef ENABLE_GC_CHECKING
8879 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8881 /* Poison the data, to indicate the data is garbage. */
8882 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
8883 memset (p
, 0xa5, size
);
8885 /* Let valgrind know the object is free. */
8886 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
8887 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8888 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8889 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8891 #ifdef ENABLE_GC_CHECKING
8892 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
8893 /* Keep TREE_CHAIN functional. */
8894 TREE_SET_CODE (obj
, TREE_LIST
);
8896 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8901 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
8903 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8905 #ifdef ENABLE_GC_CHECKING
8906 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8907 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8908 memset (obj
, 0, sizeof (tree_list
));
8911 /* Let valgrind know the entire object is available, but
8913 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8915 #ifdef ENABLE_GC_CHECKING
8916 TREE_SET_CODE (obj
, TREE_LIST
);
8918 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8922 /* Point to the first object in the TREE_LIST freelist. */
8923 static GTY((deletable
)) tree tree_list_freelist_head
;
8924 /* Return the/an actual TREE_LIST freelist. */
8925 static inline freelist
<tree_node
>
8926 tree_list_freelist ()
8928 return tree_list_freelist_head
;
8931 /* Point to the first object in the tinst_level freelist. */
8932 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
8933 /* Return the/an actual tinst_level freelist. */
8934 static inline freelist
<tinst_level
>
8935 tinst_level_freelist ()
8937 return tinst_level_freelist_head
;
8940 /* Point to the first object in the pending_template freelist. */
8941 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
8942 /* Return the/an actual pending_template freelist. */
8943 static inline freelist
<pending_template
>
8944 pending_template_freelist ()
8946 return pending_template_freelist_head
;
8949 /* Build the TREE_LIST object out of a split list, store it
8950 permanently, and return it. */
8952 tinst_level::to_list ()
8954 gcc_assert (split_list_p ());
8955 tree ret
= tree_list_freelist ().alloc ();
8956 TREE_PURPOSE (ret
) = tldcl
;
8957 TREE_VALUE (ret
) = targs
;
8960 gcc_assert (tree_list_p ());
8964 const unsigned short tinst_level::refcount_infinity
;
8966 /* Increment OBJ's refcount unless it is already infinite. */
8967 static tinst_level
*
8968 inc_refcount_use (tinst_level
*obj
)
8970 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
8975 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8977 tinst_level::free (tinst_level
*obj
)
8979 if (obj
->tree_list_p ())
8980 tree_list_freelist ().free (obj
->get_node ());
8981 tinst_level_freelist ().free (obj
);
8984 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
8985 OBJ's DECL and OBJ, and start over with the tinst_level object that
8986 used to be referenced by OBJ's NEXT. */
8988 dec_refcount_use (tinst_level
*obj
)
8991 && obj
->refcount
!= tinst_level::refcount_infinity
8992 && !--obj
->refcount
)
8994 tinst_level
*next
= obj
->next
;
8995 tinst_level::free (obj
);
9000 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9001 and of the former PTR. Omitting the second argument is equivalent
9002 to passing (T*)NULL; this is allowed because passing the
9003 zero-valued integral constant NULL confuses type deduction and/or
9004 overload resolution. */
9005 template <typename T
>
9007 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9010 ptr
= inc_refcount_use (obj
);
9011 dec_refcount_use (save
);
9015 add_pending_template (tree d
)
9017 tree ti
= (TYPE_P (d
)
9018 ? CLASSTYPE_TEMPLATE_INFO (d
)
9019 : DECL_TEMPLATE_INFO (d
));
9020 struct pending_template
*pt
;
9023 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9026 /* We are called both from instantiate_decl, where we've already had a
9027 tinst_level pushed, and instantiate_template, where we haven't.
9029 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9030 level
= !current_tinst_level
9031 || current_tinst_level
->maybe_get_node () != d
;
9034 push_tinst_level (d
);
9036 pt
= pending_template_freelist ().alloc ();
9039 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9040 if (last_pending_template
)
9041 last_pending_template
->next
= pt
;
9043 pending_templates
= pt
;
9045 last_pending_template
= pt
;
9047 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9054 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9055 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9056 documentation for TEMPLATE_ID_EXPR. */
9059 lookup_template_function (tree fns
, tree arglist
)
9063 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9064 return error_mark_node
;
9066 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9068 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9070 error ("%q#D is not a function template", fns
);
9071 return error_mark_node
;
9074 if (BASELINK_P (fns
))
9076 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9078 BASELINK_FUNCTIONS (fns
),
9083 type
= TREE_TYPE (fns
);
9084 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
9085 type
= unknown_type_node
;
9087 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
9090 /* Within the scope of a template class S<T>, the name S gets bound
9091 (in build_self_reference) to a TYPE_DECL for the class, not a
9092 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9093 or one of its enclosing classes, and that type is a template,
9094 return the associated TEMPLATE_DECL. Otherwise, the original
9097 Also handle the case when DECL is a TREE_LIST of ambiguous
9098 injected-class-names from different bases. */
9101 maybe_get_template_decl_from_type_decl (tree decl
)
9103 if (decl
== NULL_TREE
)
9106 /* DR 176: A lookup that finds an injected-class-name (10.2
9107 [class.member.lookup]) can result in an ambiguity in certain cases
9108 (for example, if it is found in more than one base class). If all of
9109 the injected-class-names that are found refer to specializations of
9110 the same class template, and if the name is followed by a
9111 template-argument-list, the reference refers to the class template
9112 itself and not a specialization thereof, and is not ambiguous. */
9113 if (TREE_CODE (decl
) == TREE_LIST
)
9115 tree t
, tmpl
= NULL_TREE
;
9116 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9118 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9121 else if (tmpl
!= elt
)
9124 if (tmpl
&& t
== NULL_TREE
)
9130 return (decl
!= NULL_TREE
9131 && DECL_SELF_REFERENCE_P (decl
)
9132 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9133 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9136 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9137 parameters, find the desired type.
9139 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9141 IN_DECL, if non-NULL, is the template declaration we are trying to
9144 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9145 the class we are looking up.
9147 Issue error and warning messages under control of COMPLAIN.
9149 If the template class is really a local class in a template
9150 function, then the FUNCTION_CONTEXT is the function in which it is
9153 ??? Note that this function is currently called *twice* for each
9154 template-id: the first time from the parser, while creating the
9155 incomplete type (finish_template_type), and the second type during the
9156 real instantiation (instantiate_template_class). This is surely something
9157 that we want to avoid. It also causes some problems with argument
9158 coercion (see convert_nontype_argument for more information on this). */
9161 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9162 int entering_scope
, tsubst_flags_t complain
)
9164 tree templ
= NULL_TREE
, parmlist
;
9171 if (identifier_p (d1
))
9173 tree value
= innermost_non_namespace_value (d1
);
9174 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9179 push_decl_namespace (context
);
9180 templ
= lookup_name (d1
);
9181 templ
= maybe_get_template_decl_from_type_decl (templ
);
9183 pop_decl_namespace ();
9186 context
= DECL_CONTEXT (templ
);
9188 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9190 tree type
= TREE_TYPE (d1
);
9192 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9193 an implicit typename for the second A. Deal with it. */
9194 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9195 type
= TREE_TYPE (type
);
9197 if (CLASSTYPE_TEMPLATE_INFO (type
))
9199 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9200 d1
= DECL_NAME (templ
);
9203 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9204 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9206 templ
= TYPE_TI_TEMPLATE (d1
);
9207 d1
= DECL_NAME (templ
);
9209 else if (DECL_TYPE_TEMPLATE_P (d1
))
9212 d1
= DECL_NAME (templ
);
9213 context
= DECL_CONTEXT (templ
);
9215 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9218 d1
= DECL_NAME (templ
);
9221 /* Issue an error message if we didn't find a template. */
9224 if (complain
& tf_error
)
9225 error ("%qT is not a template", d1
);
9226 return error_mark_node
;
9229 if (TREE_CODE (templ
) != TEMPLATE_DECL
9230 /* Make sure it's a user visible template, if it was named by
9232 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9233 && !PRIMARY_TEMPLATE_P (templ
)))
9235 if (complain
& tf_error
)
9237 error ("non-template type %qT used as a template", d1
);
9239 error ("for template declaration %q+D", in_decl
);
9241 return error_mark_node
;
9244 complain
&= ~tf_user
;
9246 /* An alias that just changes the name of a template is equivalent to the
9247 other template, so if any of the arguments are pack expansions, strip
9248 the alias to avoid problems with a pack expansion passed to a non-pack
9249 alias template parameter (DR 1430). */
9250 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9251 templ
= get_underlying_template (templ
);
9253 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9256 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9257 if (arglist2
== error_mark_node
9258 || (!uses_template_parms (arglist2
)
9259 && check_instantiated_args (templ
, arglist2
, complain
)))
9260 return error_mark_node
;
9262 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9267 tree template_type
= TREE_TYPE (templ
);
9270 tree found
= NULL_TREE
;
9273 int is_dependent_type
;
9274 int use_partial_inst_tmpl
= false;
9276 if (template_type
== error_mark_node
)
9277 /* An error occurred while building the template TEMPL, and a
9278 diagnostic has most certainly been emitted for that
9279 already. Let's propagate that error. */
9280 return error_mark_node
;
9282 gen_tmpl
= most_general_template (templ
);
9283 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9284 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9285 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9287 if (arg_depth
== 1 && parm_depth
> 1)
9289 /* We've been given an incomplete set of template arguments.
9292 template <class T> struct S1 {
9293 template <class U> struct S2 {};
9294 template <class U> struct S2<U*> {};
9297 we will be called with an ARGLIST of `U*', but the
9298 TEMPLATE will be `template <class T> template
9299 <class U> struct S1<T>::S2'. We must fill in the missing
9301 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9302 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9303 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9306 /* Now we should have enough arguments. */
9307 gcc_assert (parm_depth
== arg_depth
);
9309 /* From here on, we're only interested in the most general
9312 /* Calculate the BOUND_ARGS. These will be the args that are
9313 actually tsubst'd into the definition to create the
9315 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9317 /*require_all_args=*/true,
9318 /*use_default_args=*/true);
9320 if (arglist
== error_mark_node
)
9321 /* We were unable to bind the arguments. */
9322 return error_mark_node
;
9324 /* In the scope of a template class, explicit references to the
9325 template class refer to the type of the template, not any
9326 instantiation of it. For example, in:
9328 template <class T> class C { void f(C<T>); }
9330 the `C<T>' is just the same as `C'. Outside of the
9331 class, however, such a reference is an instantiation. */
9333 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9334 || currently_open_class (template_type
))
9336 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9338 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9339 return template_type
;
9342 /* If we already have this specialization, return it. */
9343 elt
.tmpl
= gen_tmpl
;
9345 elt
.spec
= NULL_TREE
;
9346 hash
= spec_hasher::hash (&elt
);
9347 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9352 /* If the the template's constraints are not satisfied,
9353 then we cannot form a valid type.
9355 Note that the check is deferred until after the hash
9356 lookup. This prevents redundant checks on previously
9357 instantiated specializations. */
9358 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9360 if (complain
& tf_error
)
9362 auto_diagnostic_group d
;
9363 error ("template constraint failure");
9364 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9366 return error_mark_node
;
9369 is_dependent_type
= uses_template_parms (arglist
);
9371 /* If the deduced arguments are invalid, then the binding
9373 if (!is_dependent_type
9374 && check_instantiated_args (gen_tmpl
,
9375 INNERMOST_TEMPLATE_ARGS (arglist
),
9377 return error_mark_node
;
9379 if (!is_dependent_type
9380 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9381 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9382 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9384 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9385 DECL_NAME (gen_tmpl
),
9386 /*tag_scope=*/ts_global
);
9390 context
= DECL_CONTEXT (gen_tmpl
);
9391 if (context
&& TYPE_P (context
))
9393 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9394 context
= complete_type (context
);
9397 context
= tsubst (context
, arglist
, complain
, in_decl
);
9399 if (context
== error_mark_node
)
9400 return error_mark_node
;
9403 context
= global_namespace
;
9405 /* Create the type. */
9406 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9408 /* The user referred to a specialization of an alias
9409 template represented by GEN_TMPL.
9411 [temp.alias]/2 says:
9413 When a template-id refers to the specialization of an
9414 alias template, it is equivalent to the associated
9415 type obtained by substitution of its
9416 template-arguments for the template-parameters in the
9417 type-id of the alias template. */
9419 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9420 /* Note that the call above (by indirectly calling
9421 register_specialization in tsubst_decl) registers the
9422 TYPE_DECL representing the specialization of the alias
9423 template. So next time someone substitutes ARGLIST for
9424 the template parms into the alias template (GEN_TMPL),
9425 she'll get that TYPE_DECL back. */
9427 if (t
== error_mark_node
)
9430 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9432 if (!is_dependent_type
)
9434 set_current_access_from_decl (TYPE_NAME (template_type
));
9435 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9436 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9437 arglist
, complain
, in_decl
),
9438 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9439 arglist
, complain
, in_decl
),
9440 SCOPED_ENUM_P (template_type
), NULL
);
9442 if (t
== error_mark_node
)
9447 /* We don't want to call start_enum for this type, since
9448 the values for the enumeration constants may involve
9449 template parameters. And, no one should be interested
9450 in the enumeration constants for such a type. */
9451 t
= cxx_make_type (ENUMERAL_TYPE
);
9452 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9454 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9455 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9456 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9458 else if (CLASS_TYPE_P (template_type
))
9460 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9461 instantiated here. */
9462 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9464 t
= make_class_type (TREE_CODE (template_type
));
9465 CLASSTYPE_DECLARED_CLASS (t
)
9466 = CLASSTYPE_DECLARED_CLASS (template_type
);
9467 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9469 /* A local class. Make sure the decl gets registered properly. */
9470 if (context
== current_function_decl
)
9471 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9473 return error_mark_node
;
9475 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9476 /* This instantiation is another name for the primary
9477 template type. Set the TYPE_CANONICAL field
9479 TYPE_CANONICAL (t
) = template_type
;
9480 else if (any_template_arguments_need_structural_equality_p (arglist
))
9481 /* Some of the template arguments require structural
9482 equality testing, so this template class requires
9483 structural equality testing. */
9484 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9489 /* If we called start_enum or pushtag above, this information
9490 will already be set up. */
9493 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9495 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9496 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9497 DECL_SOURCE_LOCATION (type_decl
)
9498 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9501 type_decl
= TYPE_NAME (t
);
9503 if (CLASS_TYPE_P (template_type
))
9505 TREE_PRIVATE (type_decl
)
9506 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9507 TREE_PROTECTED (type_decl
)
9508 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9509 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9511 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9512 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9516 if (OVERLOAD_TYPE_P (t
)
9517 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9519 static const char *tags
[] = {"abi_tag", "may_alias"};
9521 for (unsigned ix
= 0; ix
!= 2; ix
++)
9524 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9528 = tree_cons (TREE_PURPOSE (attributes
),
9529 TREE_VALUE (attributes
),
9530 TYPE_ATTRIBUTES (t
));
9534 /* Let's consider the explicit specialization of a member
9535 of a class template specialization that is implicitly instantiated,
9540 template<class U> struct M {}; //#0
9545 struct S<int>::M<char> //#1
9549 [temp.expl.spec]/4 says this is valid.
9551 In this case, when we write:
9554 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9557 When we encounter #1, we want to store the partial instantiation
9558 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9560 For all cases other than this "explicit specialization of member of a
9561 class template", we just want to store the most general template into
9562 the CLASSTYPE_TI_TEMPLATE of M.
9564 This case of "explicit specialization of member of a class template"
9566 1/ the enclosing class is an instantiation of, and therefore not
9567 the same as, the context of the most general template, and
9568 2/ we aren't looking at the partial instantiation itself, i.e.
9569 the innermost arguments are not the same as the innermost parms of
9570 the most general template.
9572 So it's only when 1/ and 2/ happens that we want to use the partial
9573 instantiation of the member template in lieu of its most general
9576 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9577 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9578 /* the enclosing class must be an instantiation... */
9579 && CLASS_TYPE_P (context
)
9580 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9582 TREE_VEC_LENGTH (arglist
)--;
9583 ++processing_template_decl
;
9584 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9585 tree partial_inst_args
=
9586 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9587 arglist
, complain
, NULL_TREE
);
9588 --processing_template_decl
;
9589 TREE_VEC_LENGTH (arglist
)++;
9590 if (partial_inst_args
== error_mark_node
)
9591 return error_mark_node
;
9592 use_partial_inst_tmpl
=
9593 /*...and we must not be looking at the partial instantiation
9595 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9599 if (!use_partial_inst_tmpl
)
9600 /* This case is easy; there are no member templates involved. */
9604 /* This is a full instantiation of a member template. Find
9605 the partial instantiation of which this is an instance. */
9607 /* Temporarily reduce by one the number of levels in the ARGLIST
9608 so as to avoid comparing the last set of arguments. */
9609 TREE_VEC_LENGTH (arglist
)--;
9610 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9611 TREE_VEC_LENGTH (arglist
)++;
9612 /* FOUND is either a proper class type, or an alias
9613 template specialization. In the later case, it's a
9614 TYPE_DECL, resulting from the substituting of arguments
9615 for parameters in the TYPE_DECL of the alias template
9616 done earlier. So be careful while getting the template
9618 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9620 : (TREE_CODE (found
) == TYPE_DECL
9621 ? DECL_TI_TEMPLATE (found
)
9622 : CLASSTYPE_TI_TEMPLATE (found
)));
9625 // Build template info for the new specialization.
9626 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9629 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9630 entry
= ggc_alloc
<spec_entry
> ();
9634 /* Note this use of the partial instantiation so we can check it
9635 later in maybe_process_partial_specialization. */
9636 DECL_TEMPLATE_INSTANTIATIONS (found
)
9637 = tree_cons (arglist
, t
,
9638 DECL_TEMPLATE_INSTANTIATIONS (found
));
9640 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9641 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9642 /* Now that the type has been registered on the instantiations
9643 list, we set up the enumerators. Because the enumeration
9644 constants may involve the enumeration type itself, we make
9645 sure to register the type first, and then create the
9646 constants. That way, doing tsubst_expr for the enumeration
9647 constants won't result in recursive calls here; we'll find
9648 the instantiation and exit above. */
9649 tsubst_enum (template_type
, t
, arglist
);
9651 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9652 /* If the type makes use of template parameters, the
9653 code that generates debugging information will crash. */
9654 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9656 /* Possibly limit visibility based on template args. */
9657 TREE_PUBLIC (type_decl
) = 1;
9658 determine_visibility (type_decl
);
9660 inherit_targ_abi_tags (t
);
9666 /* Wrapper for lookup_template_class_1. */
9669 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9670 int entering_scope
, tsubst_flags_t complain
)
9673 timevar_push (TV_TEMPLATE_INST
);
9674 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9675 entering_scope
, complain
);
9676 timevar_pop (TV_TEMPLATE_INST
);
9680 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9683 lookup_template_variable (tree templ
, tree arglist
)
9685 /* The type of the expression is NULL_TREE since the template-id could refer
9686 to an explicit or partial specialization. */
9687 tree type
= NULL_TREE
;
9688 if (flag_concepts
&& variable_concept_p (templ
))
9689 /* Except that concepts are always bool. */
9690 type
= boolean_type_node
;
9691 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9694 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9697 finish_template_variable (tree var
, tsubst_flags_t complain
)
9699 tree templ
= TREE_OPERAND (var
, 0);
9700 tree arglist
= TREE_OPERAND (var
, 1);
9702 /* We never want to return a VAR_DECL for a variable concept, since they
9703 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9704 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9705 if (concept_p
&& processing_template_decl
)
9708 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9709 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9711 templ
= most_general_template (templ
);
9712 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9713 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9715 /*use_default*/true);
9717 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9719 if (complain
& tf_error
)
9721 auto_diagnostic_group d
;
9722 error ("use of invalid variable template %qE", var
);
9723 diagnose_constraints (location_of (var
), templ
, arglist
);
9725 return error_mark_node
;
9728 /* If a template-id refers to a specialization of a variable
9729 concept, then the expression is true if and only if the
9730 concept's constraints are satisfied by the given template
9733 NOTE: This is an extension of Concepts Lite TS that
9734 allows constraints to be used in expressions. */
9737 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9738 return evaluate_variable_concept (decl
, arglist
);
9741 return instantiate_template (templ
, arglist
, complain
);
9744 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9745 TARGS template args, and instantiate it if it's not dependent. */
9748 lookup_and_finish_template_variable (tree templ
, tree targs
,
9749 tsubst_flags_t complain
)
9751 templ
= lookup_template_variable (templ
, targs
);
9752 if (!any_dependent_template_arguments_p (targs
))
9754 templ
= finish_template_variable (templ
, complain
);
9758 return convert_from_reference (templ
);
9767 /* True when we should also visit template parameters that occur in
9768 non-deduced contexts. */
9769 bool include_nondeduced_p
;
9770 hash_set
<tree
> *visited
;
9773 /* Called from for_each_template_parm via walk_tree. */
9776 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9779 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9780 tree_fn_t fn
= pfd
->fn
;
9781 void *data
= pfd
->data
;
9782 tree result
= NULL_TREE
;
9784 #define WALK_SUBTREE(NODE) \
9787 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9788 pfd->include_nondeduced_p, \
9790 if (result) goto out; \
9794 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9798 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9799 WALK_SUBTREE (TYPE_CONTEXT (t
));
9801 switch (TREE_CODE (t
))
9804 if (TYPE_PTRMEMFUNC_P (t
))
9810 if (!TYPE_TEMPLATE_INFO (t
))
9813 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9817 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9818 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9822 /* Since we're not going to walk subtrees, we have to do this
9824 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9828 /* Check the return type. */
9829 WALK_SUBTREE (TREE_TYPE (t
));
9831 /* Check the parameter types. Since default arguments are not
9832 instantiated until they are needed, the TYPE_ARG_TYPES may
9833 contain expressions that involve template parameters. But,
9834 no-one should be looking at them yet. And, once they're
9835 instantiated, they don't contain template parameters, so
9836 there's no point in looking at them then, either. */
9840 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9841 WALK_SUBTREE (TREE_VALUE (parm
));
9843 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9844 want walk_tree walking into them itself. */
9848 if (flag_noexcept_type
)
9850 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9852 WALK_SUBTREE (TREE_PURPOSE (spec
));
9858 case UNDERLYING_TYPE
:
9859 if (pfd
->include_nondeduced_p
9860 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9862 pfd
->include_nondeduced_p
,
9864 return error_mark_node
;
9865 *walk_subtrees
= false;
9870 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9871 WALK_SUBTREE (DECL_TI_ARGS (t
));
9876 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9877 WALK_SUBTREE (DECL_INITIAL (t
));
9878 if (DECL_CONTEXT (t
)
9879 && pfd
->include_nondeduced_p
)
9880 WALK_SUBTREE (DECL_CONTEXT (t
));
9883 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9884 /* Record template parameters such as `T' inside `TT<T>'. */
9885 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9888 case TEMPLATE_TEMPLATE_PARM
:
9889 case TEMPLATE_TYPE_PARM
:
9890 case TEMPLATE_PARM_INDEX
:
9891 if (fn
&& (*fn
)(t
, data
))
9898 /* A template template parameter is encountered. */
9899 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9900 WALK_SUBTREE (TREE_TYPE (t
));
9902 /* Already substituted template template parameter */
9907 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9908 partial instantiation. */
9909 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9913 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9914 && pfd
->include_nondeduced_p
)
9915 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9920 /* If there's no type, then this thing must be some expression
9921 involving template parameters. */
9922 if (!fn
&& !TREE_TYPE (t
))
9923 return error_mark_node
;
9928 case IMPLICIT_CONV_EXPR
:
9929 case REINTERPRET_CAST_EXPR
:
9930 case CONST_CAST_EXPR
:
9931 case STATIC_CAST_EXPR
:
9932 case DYNAMIC_CAST_EXPR
:
9936 case PSEUDO_DTOR_EXPR
:
9938 return error_mark_node
;
9947 /* We didn't find any template parameters we liked. */
9952 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9953 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9954 call FN with the parameter and the DATA.
9955 If FN returns nonzero, the iteration is terminated, and
9956 for_each_template_parm returns 1. Otherwise, the iteration
9957 continues. If FN never returns a nonzero value, the value
9958 returned by for_each_template_parm is 0. If FN is NULL, it is
9959 considered to be the function which always returns 1.
9961 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9962 parameters that occur in non-deduced contexts. When false, only
9963 visits those template parameters that can be deduced. */
9966 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9967 hash_set
<tree
> *visited
,
9968 bool include_nondeduced_p
,
9971 struct pair_fn_data pfd
;
9976 pfd
.any_fn
= any_fn
;
9978 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9980 /* Walk the tree. (Conceptually, we would like to walk without
9981 duplicates, but for_each_template_parm_r recursively calls
9982 for_each_template_parm, so we would need to reorganize a fair
9983 bit to use walk_tree_without_duplicates, so we keep our own
9986 pfd
.visited
= visited
;
9988 pfd
.visited
= new hash_set
<tree
>;
9989 result
= cp_walk_tree (&t
,
9990 for_each_template_parm_r
,
10004 /* Returns true if T depends on any template parameter. */
10007 uses_template_parms (tree t
)
10009 if (t
== NULL_TREE
)
10013 int saved_processing_template_decl
;
10015 saved_processing_template_decl
= processing_template_decl
;
10016 if (!saved_processing_template_decl
)
10017 processing_template_decl
= 1;
10019 dependent_p
= dependent_type_p (t
);
10020 else if (TREE_CODE (t
) == TREE_VEC
)
10021 dependent_p
= any_dependent_template_arguments_p (t
);
10022 else if (TREE_CODE (t
) == TREE_LIST
)
10023 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10024 || uses_template_parms (TREE_CHAIN (t
)));
10025 else if (TREE_CODE (t
) == TYPE_DECL
)
10026 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10027 else if (DECL_P (t
)
10029 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10030 || TREE_CODE (t
) == OVERLOAD
10032 || identifier_p (t
)
10033 || TREE_CODE (t
) == TRAIT_EXPR
10034 || TREE_CODE (t
) == CONSTRUCTOR
10035 || CONSTANT_CLASS_P (t
))
10036 dependent_p
= (type_dependent_expression_p (t
)
10037 || value_dependent_expression_p (t
));
10040 gcc_assert (t
== error_mark_node
);
10041 dependent_p
= false;
10044 processing_template_decl
= saved_processing_template_decl
;
10046 return dependent_p
;
10049 /* Returns true iff current_function_decl is an incompletely instantiated
10050 template. Useful instead of processing_template_decl because the latter
10051 is set to 0 during instantiate_non_dependent_expr. */
10054 in_template_function (void)
10056 tree fn
= current_function_decl
;
10058 ++processing_template_decl
;
10059 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10060 && DECL_TEMPLATE_INFO (fn
)
10061 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10062 --processing_template_decl
;
10066 /* Returns true if T depends on any template parameter with level LEVEL. */
10069 uses_template_parms_level (tree t
, int level
)
10071 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10072 /*include_nondeduced_p=*/true);
10075 /* Returns true if the signature of DECL depends on any template parameter from
10076 its enclosing class. */
10079 uses_outer_template_parms (tree decl
)
10081 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10084 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10085 &depth
, NULL
, /*include_nondeduced_p=*/true))
10087 if (PRIMARY_TEMPLATE_P (decl
)
10088 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10089 (DECL_TEMPLATE_PARMS (decl
)),
10090 template_parm_outer_level
,
10091 &depth
, NULL
, /*include_nondeduced_p=*/true))
10093 tree ci
= get_constraints (decl
);
10095 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10096 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10097 &depth
, NULL
, /*nondeduced*/true))
10102 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10103 ill-formed translation unit, i.e. a variable or function that isn't
10104 usable in a constant expression. */
10107 neglectable_inst_p (tree d
)
10109 return (d
&& DECL_P (d
)
10110 && !undeduced_auto_decl (d
)
10111 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10112 : decl_maybe_constant_var_p (d
)));
10115 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10116 neglectable and instantiated from within an erroneous instantiation. */
10119 limit_bad_template_recursion (tree decl
)
10121 struct tinst_level
*lev
= current_tinst_level
;
10122 int errs
= errorcount
+ sorrycount
;
10123 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10126 for (; lev
; lev
= lev
->next
)
10127 if (neglectable_inst_p (lev
->maybe_get_node ()))
10130 return (lev
&& errs
> lev
->errors
);
10133 static int tinst_depth
;
10134 extern int max_tinst_depth
;
10137 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10139 /* We're starting to instantiate D; record the template instantiation context
10140 at LOC for diagnostics and to restore it later. */
10143 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10145 struct tinst_level
*new_level
;
10147 if (tinst_depth
>= max_tinst_depth
)
10149 /* Tell error.c not to try to instantiate any templates. */
10151 fatal_error (input_location
,
10152 "template instantiation depth exceeds maximum of %d"
10153 " (use -ftemplate-depth= to increase the maximum)",
10158 /* If the current instantiation caused problems, don't let it instantiate
10159 anything else. Do allow deduction substitution and decls usable in
10160 constant expressions. */
10161 if (!targs
&& limit_bad_template_recursion (tldcl
))
10164 /* When not -quiet, dump template instantiations other than functions, since
10165 announce_function will take care of those. */
10166 if (!quiet_flag
&& !targs
10167 && TREE_CODE (tldcl
) != TREE_LIST
10168 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10169 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10171 new_level
= tinst_level_freelist ().alloc ();
10172 new_level
->tldcl
= tldcl
;
10173 new_level
->targs
= targs
;
10174 new_level
->locus
= loc
;
10175 new_level
->errors
= errorcount
+ sorrycount
;
10176 new_level
->next
= NULL
;
10177 new_level
->refcount
= 0;
10178 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10179 set_refcount_ptr (current_tinst_level
, new_level
);
10182 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10183 depth_reached
= tinst_depth
;
10188 /* We're starting substitution of TMPL<ARGS>; record the template
10189 substitution context for diagnostics and to restore it later. */
10192 push_tinst_level (tree tmpl
, tree args
)
10194 return push_tinst_level_loc (tmpl
, args
, input_location
);
10197 /* We're starting to instantiate D; record INPUT_LOCATION and the
10198 template instantiation context for diagnostics and to restore it
10202 push_tinst_level (tree d
)
10204 return push_tinst_level_loc (d
, input_location
);
10207 /* Likewise, but record LOC as the program location. */
10210 push_tinst_level_loc (tree d
, location_t loc
)
10212 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10213 return push_tinst_level_loc (d
, NULL
, loc
);
10216 /* We're done instantiating this template; return to the instantiation
10220 pop_tinst_level (void)
10222 /* Restore the filename and line number stashed away when we started
10223 this instantiation. */
10224 input_location
= current_tinst_level
->locus
;
10225 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10229 /* We're instantiating a deferred template; restore the template
10230 instantiation context in which the instantiation was requested, which
10231 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10234 reopen_tinst_level (struct tinst_level
*level
)
10236 struct tinst_level
*t
;
10239 for (t
= level
; t
; t
= t
->next
)
10242 set_refcount_ptr (current_tinst_level
, level
);
10243 pop_tinst_level ();
10244 if (current_tinst_level
)
10245 current_tinst_level
->errors
= errorcount
+sorrycount
;
10246 return level
->maybe_get_node ();
10249 /* Returns the TINST_LEVEL which gives the original instantiation
10252 struct tinst_level
*
10253 outermost_tinst_level (void)
10255 struct tinst_level
*level
= current_tinst_level
;
10257 while (level
->next
)
10258 level
= level
->next
;
10262 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10263 vector of template arguments, as for tsubst.
10265 Returns an appropriate tsubst'd friend declaration. */
10268 tsubst_friend_function (tree decl
, tree args
)
10272 if (TREE_CODE (decl
) == FUNCTION_DECL
10273 && DECL_TEMPLATE_INSTANTIATION (decl
)
10274 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10275 /* This was a friend declared with an explicit template
10276 argument list, e.g.:
10278 friend void f<>(T);
10280 to indicate that f was a template instantiation, not a new
10281 function declaration. Now, we have to figure out what
10282 instantiation of what template. */
10284 tree template_id
, arglist
, fns
;
10287 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10289 /* Friend functions are looked up in the containing namespace scope.
10290 We must enter that scope, to avoid finding member functions of the
10291 current class with same name. */
10292 push_nested_namespace (ns
);
10293 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10294 tf_warning_or_error
, NULL_TREE
,
10295 /*integral_constant_expression_p=*/false);
10296 pop_nested_namespace (ns
);
10297 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10298 tf_warning_or_error
, NULL_TREE
);
10299 template_id
= lookup_template_function (fns
, arglist
);
10301 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10302 tmpl
= determine_specialization (template_id
, new_friend
,
10304 /*need_member_template=*/0,
10305 TREE_VEC_LENGTH (args
),
10307 return instantiate_template (tmpl
, new_args
, tf_error
);
10310 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10312 /* The NEW_FRIEND will look like an instantiation, to the
10313 compiler, but is not an instantiation from the point of view of
10314 the language. For example, we might have had:
10316 template <class T> struct S {
10317 template <class U> friend void f(T, U);
10320 Then, in S<int>, template <class U> void f(int, U) is not an
10321 instantiation of anything. */
10322 if (new_friend
== error_mark_node
)
10323 return error_mark_node
;
10325 DECL_USE_TEMPLATE (new_friend
) = 0;
10326 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10328 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10329 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10330 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10333 /* The mangled name for the NEW_FRIEND is incorrect. The function
10334 is not a template instantiation and should not be mangled like
10335 one. Therefore, we forget the mangling here; we'll recompute it
10336 later if we need it. */
10337 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10339 SET_DECL_RTL (new_friend
, NULL
);
10340 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10343 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10346 tree new_friend_template_info
;
10347 tree new_friend_result_template_info
;
10349 int new_friend_is_defn
;
10351 /* We must save some information from NEW_FRIEND before calling
10352 duplicate decls since that function will free NEW_FRIEND if
10354 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10355 new_friend_is_defn
=
10356 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10357 (template_for_substitution (new_friend
)))
10359 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10361 /* This declaration is a `primary' template. */
10362 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10364 new_friend_result_template_info
10365 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10368 new_friend_result_template_info
= NULL_TREE
;
10370 /* Inside pushdecl_namespace_level, we will push into the
10371 current namespace. However, the friend function should go
10372 into the namespace of the template. */
10373 ns
= decl_namespace_context (new_friend
);
10374 push_nested_namespace (ns
);
10375 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10376 pop_nested_namespace (ns
);
10378 if (old_decl
== error_mark_node
)
10379 return error_mark_node
;
10381 if (old_decl
!= new_friend
)
10383 /* This new friend declaration matched an existing
10384 declaration. For example, given:
10386 template <class T> void f(T);
10387 template <class U> class C {
10388 template <class T> friend void f(T) {}
10391 the friend declaration actually provides the definition
10392 of `f', once C has been instantiated for some type. So,
10393 old_decl will be the out-of-class template declaration,
10394 while new_friend is the in-class definition.
10396 But, if `f' was called before this point, the
10397 instantiation of `f' will have DECL_TI_ARGS corresponding
10398 to `T' but not to `U', references to which might appear
10399 in the definition of `f'. Previously, the most general
10400 template for an instantiation of `f' was the out-of-class
10401 version; now it is the in-class version. Therefore, we
10402 run through all specialization of `f', adding to their
10403 DECL_TI_ARGS appropriately. In particular, they need a
10404 new set of outer arguments, corresponding to the
10405 arguments for this class instantiation.
10407 The same situation can arise with something like this:
10409 friend void f(int);
10410 template <class T> class C {
10411 friend void f(T) {}
10414 when `C<int>' is instantiated. Now, `f(int)' is defined
10417 if (!new_friend_is_defn
)
10418 /* On the other hand, if the in-class declaration does
10419 *not* provide a definition, then we don't want to alter
10420 existing definitions. We can just leave everything
10425 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10426 tree new_args
= TI_ARGS (new_friend_template_info
);
10428 /* Overwrite whatever template info was there before, if
10429 any, with the new template information pertaining to
10430 the declaration. */
10431 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10433 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10435 /* We should have called reregister_specialization in
10436 duplicate_decls. */
10437 gcc_assert (retrieve_specialization (new_template
,
10441 /* Instantiate it if the global has already been used. */
10442 if (DECL_ODR_USED (old_decl
))
10443 instantiate_decl (old_decl
, /*defer_ok=*/true,
10444 /*expl_inst_class_mem_p=*/false);
10450 /* Indicate that the old function template is a partial
10452 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10453 = new_friend_result_template_info
;
10455 gcc_assert (new_template
10456 == most_general_template (new_template
));
10457 gcc_assert (new_template
!= old_decl
);
10459 /* Reassign any specializations already in the hash table
10460 to the new more general template, and add the
10461 additional template args. */
10462 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10464 t
= TREE_CHAIN (t
))
10466 tree spec
= TREE_VALUE (t
);
10469 elt
.tmpl
= old_decl
;
10470 elt
.args
= DECL_TI_ARGS (spec
);
10471 elt
.spec
= NULL_TREE
;
10473 decl_specializations
->remove_elt (&elt
);
10475 DECL_TI_ARGS (spec
)
10476 = add_outermost_template_args (new_args
,
10477 DECL_TI_ARGS (spec
));
10479 register_specialization
10480 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10483 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10487 /* The information from NEW_FRIEND has been merged into OLD_DECL
10488 by duplicate_decls. */
10489 new_friend
= old_decl
;
10494 tree context
= DECL_CONTEXT (new_friend
);
10498 template <class T> class C {
10499 template <class U> friend void C1<U>::f (); // case 1
10500 friend void C2<T>::f (); // case 2
10502 we only need to make sure CONTEXT is a complete type for
10503 case 2. To distinguish between the two cases, we note that
10504 CONTEXT of case 1 remains dependent type after tsubst while
10505 this isn't true for case 2. */
10506 ++processing_template_decl
;
10507 dependent_p
= dependent_type_p (context
);
10508 --processing_template_decl
;
10511 && !complete_type_or_else (context
, NULL_TREE
))
10512 return error_mark_node
;
10514 if (COMPLETE_TYPE_P (context
))
10516 tree fn
= new_friend
;
10517 /* do_friend adds the TEMPLATE_DECL for any member friend
10518 template even if it isn't a member template, i.e.
10519 template <class T> friend A<T>::f();
10520 Look through it in that case. */
10521 if (TREE_CODE (fn
) == TEMPLATE_DECL
10522 && !PRIMARY_TEMPLATE_P (fn
))
10523 fn
= DECL_TEMPLATE_RESULT (fn
);
10524 /* Check to see that the declaration is really present, and,
10525 possibly obtain an improved declaration. */
10526 fn
= check_classfn (context
, fn
, NULL_TREE
);
10536 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10537 template arguments, as for tsubst.
10539 Returns an appropriate tsubst'd friend type or error_mark_node on
10543 tsubst_friend_class (tree friend_tmpl
, tree args
)
10547 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10549 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10550 return TREE_TYPE (tmpl
);
10553 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10554 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10555 push_nested_namespace (context
);
10557 push_nested_class (context
);
10559 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10560 /*non_class=*/false, /*block_p=*/false,
10561 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10563 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10565 /* The friend template has already been declared. Just
10566 check to see that the declarations match, and install any new
10567 default parameters. We must tsubst the default parameters,
10568 of course. We only need the innermost template parameters
10569 because that is all that redeclare_class_template will look
10571 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10572 > TMPL_ARGS_DEPTH (args
))
10574 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10575 args
, tf_warning_or_error
);
10576 location_t saved_input_location
= input_location
;
10577 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10578 tree cons
= get_constraints (tmpl
);
10579 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10580 input_location
= saved_input_location
;
10585 /* The friend template has not already been declared. In this
10586 case, the instantiation of the template class will cause the
10587 injection of this template into the namespace scope. */
10588 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10590 if (tmpl
!= error_mark_node
)
10592 /* The new TMPL is not an instantiation of anything, so we
10593 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10594 for the new type because that is supposed to be the
10595 corresponding template decl, i.e., TMPL. */
10596 DECL_USE_TEMPLATE (tmpl
) = 0;
10597 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10598 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10599 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10600 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10602 /* It is hidden. */
10603 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10604 DECL_ANTICIPATED (tmpl
)
10605 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10607 /* Inject this template into the enclosing namspace scope. */
10608 tmpl
= pushdecl_namespace_level (tmpl
, true);
10612 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10613 pop_nested_namespace (context
);
10615 pop_nested_class ();
10617 return TREE_TYPE (tmpl
);
10620 /* Returns zero if TYPE cannot be completed later due to circularity.
10621 Otherwise returns one. */
10624 can_complete_type_without_circularity (tree type
)
10626 if (type
== NULL_TREE
|| type
== error_mark_node
)
10628 else if (COMPLETE_TYPE_P (type
))
10630 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10631 return can_complete_type_without_circularity (TREE_TYPE (type
));
10632 else if (CLASS_TYPE_P (type
)
10633 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10639 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10640 tsubst_flags_t
, tree
);
10642 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10643 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10646 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10647 tsubst_flags_t complain
, tree in_decl
)
10649 gcc_assert (ATTR_IS_DEPENDENT (t
));
10651 tree val
= TREE_VALUE (t
);
10652 if (val
== NULL_TREE
)
10653 /* Nothing to do. */;
10654 else if ((flag_openmp
|| flag_openmp_simd
)
10655 && is_attribute_p ("omp declare simd",
10656 get_attribute_name (t
)))
10658 tree clauses
= TREE_VALUE (val
);
10659 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10660 complain
, in_decl
);
10661 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10662 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10663 tree parms
= DECL_ARGUMENTS (*decl_p
);
10665 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10667 val
= build_tree_list (NULL_TREE
, clauses
);
10671 /* If the first attribute argument is an identifier, don't
10672 pass it through tsubst. Attributes like mode, format,
10673 cleanup and several target specific attributes expect it
10675 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10678 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10679 /*integral_constant_expression_p=*/false);
10680 if (chain
!= TREE_CHAIN (val
))
10681 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10683 else if (PACK_EXPANSION_P (val
))
10685 /* An attribute pack expansion. */
10686 tree purp
= TREE_PURPOSE (t
);
10687 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10688 if (pack
== error_mark_node
)
10689 return error_mark_node
;
10690 int len
= TREE_VEC_LENGTH (pack
);
10691 tree list
= NULL_TREE
;
10693 for (int i
= 0; i
< len
; ++i
)
10695 tree elt
= TREE_VEC_ELT (pack
, i
);
10696 *q
= build_tree_list (purp
, elt
);
10697 q
= &TREE_CHAIN (*q
);
10702 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10703 /*integral_constant_expression_p=*/false);
10705 if (val
!= TREE_VALUE (t
))
10706 return build_tree_list (TREE_PURPOSE (t
), val
);
10710 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10711 unchanged or a new TREE_LIST chain. */
10714 tsubst_attributes (tree attributes
, tree args
,
10715 tsubst_flags_t complain
, tree in_decl
)
10717 tree last_dep
= NULL_TREE
;
10719 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10720 if (ATTR_IS_DEPENDENT (t
))
10723 attributes
= copy_list (attributes
);
10728 for (tree
*p
= &attributes
; *p
; )
10731 if (ATTR_IS_DEPENDENT (t
))
10733 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10738 p
= &TREE_CHAIN (*p
);
10739 *p
= TREE_CHAIN (t
);
10743 p
= &TREE_CHAIN (*p
);
10749 /* Apply any attributes which had to be deferred until instantiation
10750 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10751 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10754 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10755 tree args
, tsubst_flags_t complain
, tree in_decl
)
10757 tree last_dep
= NULL_TREE
;
10761 if (attributes
== NULL_TREE
)
10764 if (DECL_P (*decl_p
))
10766 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10768 p
= &DECL_ATTRIBUTES (*decl_p
);
10769 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10770 to our attributes parameter. */
10771 gcc_assert (*p
== attributes
);
10775 p
= &TYPE_ATTRIBUTES (*decl_p
);
10776 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10777 lookup_template_class_1, and should be preserved. */
10778 gcc_assert (*p
!= attributes
);
10780 p
= &TREE_CHAIN (*p
);
10783 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10784 if (ATTR_IS_DEPENDENT (t
))
10787 attributes
= copy_list (attributes
);
10794 tree late_attrs
= NULL_TREE
;
10795 tree
*q
= &late_attrs
;
10800 if (ATTR_IS_DEPENDENT (t
))
10802 *p
= TREE_CHAIN (t
);
10803 TREE_CHAIN (t
) = NULL_TREE
;
10804 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10806 q
= &TREE_CHAIN (*q
);
10809 p
= &TREE_CHAIN (t
);
10812 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10816 /* Perform (or defer) access check for typedefs that were referenced
10817 from within the template TMPL code.
10818 This is a subroutine of instantiate_decl and instantiate_class_template.
10819 TMPL is the template to consider and TARGS is the list of arguments of
10823 perform_typedefs_access_check (tree tmpl
, tree targs
)
10825 location_t saved_location
;
10827 qualified_typedef_usage_t
*iter
;
10830 || (!CLASS_TYPE_P (tmpl
)
10831 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10834 saved_location
= input_location
;
10835 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10837 tree type_decl
= iter
->typedef_decl
;
10838 tree type_scope
= iter
->context
;
10840 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10843 if (uses_template_parms (type_decl
))
10844 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10845 if (uses_template_parms (type_scope
))
10846 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10848 /* Make access check error messages point to the location
10849 of the use of the typedef. */
10850 input_location
= iter
->locus
;
10851 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10852 type_decl
, type_decl
,
10853 tf_warning_or_error
);
10855 input_location
= saved_location
;
10859 instantiate_class_template_1 (tree type
)
10861 tree templ
, args
, pattern
, t
, member
;
10865 unsigned int saved_maximum_field_alignment
;
10868 if (type
== error_mark_node
)
10869 return error_mark_node
;
10871 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10872 || uses_template_parms (type
))
10875 /* Figure out which template is being instantiated. */
10876 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10877 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10879 /* Mark the type as in the process of being defined. */
10880 TYPE_BEING_DEFINED (type
) = 1;
10882 /* We may be in the middle of deferred access check. Disable
10884 deferring_access_check_sentinel
acs (dk_no_deferred
);
10886 /* Determine what specialization of the original template to
10888 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10889 if (t
== error_mark_node
)
10890 return error_mark_node
;
10893 /* This TYPE is actually an instantiation of a partial
10894 specialization. We replace the innermost set of ARGS with
10895 the arguments appropriate for substitution. For example,
10898 template <class T> struct S {};
10899 template <class T> struct S<T*> {};
10901 and supposing that we are instantiating S<int*>, ARGS will
10902 presently be {int*} -- but we need {int}. */
10903 pattern
= TREE_TYPE (t
);
10904 args
= TREE_PURPOSE (t
);
10908 pattern
= TREE_TYPE (templ
);
10909 args
= CLASSTYPE_TI_ARGS (type
);
10912 /* If the template we're instantiating is incomplete, then clearly
10913 there's nothing we can do. */
10914 if (!COMPLETE_TYPE_P (pattern
))
10916 /* We can try again later. */
10917 TYPE_BEING_DEFINED (type
) = 0;
10921 /* If we've recursively instantiated too many templates, stop. */
10922 if (! push_tinst_level (type
))
10925 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10926 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10928 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10929 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10930 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10931 fn_context
= error_mark_node
;
10933 push_to_top_level ();
10936 cp_unevaluated_operand
= 0;
10937 c_inhibit_evaluation_warnings
= 0;
10939 /* Use #pragma pack from the template context. */
10940 saved_maximum_field_alignment
= maximum_field_alignment
;
10941 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10943 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10945 /* Set the input location to the most specialized template definition.
10946 This is needed if tsubsting causes an error. */
10947 typedecl
= TYPE_MAIN_DECL (pattern
);
10948 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10949 DECL_SOURCE_LOCATION (typedecl
);
10951 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10952 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10953 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10954 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10955 if (ANON_AGGR_TYPE_P (pattern
))
10956 SET_ANON_AGGR_TYPE_P (type
);
10957 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10959 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10960 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10961 /* Adjust visibility for template arguments. */
10962 determine_visibility (TYPE_MAIN_DECL (type
));
10964 if (CLASS_TYPE_P (type
))
10965 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10967 pbinfo
= TYPE_BINFO (pattern
);
10969 /* We should never instantiate a nested class before its enclosing
10970 class; we need to look up the nested class by name before we can
10971 instantiate it, and that lookup should instantiate the enclosing
10973 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10974 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10976 base_list
= NULL_TREE
;
10977 if (BINFO_N_BASE_BINFOS (pbinfo
))
10983 /* We must enter the scope containing the type, as that is where
10984 the accessibility of types named in dependent bases are
10986 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10988 /* Substitute into each of the bases to determine the actual
10990 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10993 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10994 tree expanded_bases
= NULL_TREE
;
10997 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11000 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11001 args
, tf_error
, NULL_TREE
);
11002 if (expanded_bases
== error_mark_node
)
11005 len
= TREE_VEC_LENGTH (expanded_bases
);
11008 for (idx
= 0; idx
< len
; idx
++)
11010 if (expanded_bases
)
11011 /* Extract the already-expanded base class. */
11012 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11014 /* Substitute to figure out the base class. */
11015 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11018 if (base
== error_mark_node
)
11021 base_list
= tree_cons (access
, base
, base_list
);
11022 if (BINFO_VIRTUAL_P (pbase_binfo
))
11023 TREE_TYPE (base_list
) = integer_type_node
;
11027 /* The list is now in reverse order; correct that. */
11028 base_list
= nreverse (base_list
);
11031 pop_scope (pushed_scope
);
11033 /* Now call xref_basetypes to set up all the base-class
11035 xref_basetypes (type
, base_list
);
11037 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11038 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11039 args
, tf_error
, NULL_TREE
);
11040 fixup_attribute_variants (type
);
11042 /* Now that our base classes are set up, enter the scope of the
11043 class, so that name lookups into base classes, etc. will work
11044 correctly. This is precisely analogous to what we do in
11045 begin_class_definition when defining an ordinary non-template
11046 class, except we also need to push the enclosing classes. */
11047 push_nested_class (type
);
11049 /* Now members are processed in the order of declaration. */
11050 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11051 member
; member
= TREE_CHAIN (member
))
11053 tree t
= TREE_VALUE (member
);
11055 if (TREE_PURPOSE (member
))
11059 if (LAMBDA_TYPE_P (t
))
11060 /* A closure type for a lambda in an NSDMI or default argument.
11061 Ignore it; it will be regenerated when needed. */
11064 /* Build new CLASSTYPE_NESTED_UTDS. */
11067 bool class_template_p
;
11069 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11070 && TYPE_LANG_SPECIFIC (t
)
11071 && CLASSTYPE_IS_TEMPLATE (t
));
11072 /* If the member is a class template, then -- even after
11073 substitution -- there may be dependent types in the
11074 template argument list for the class. We increment
11075 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11076 that function will assume that no types are dependent
11077 when outside of a template. */
11078 if (class_template_p
)
11079 ++processing_template_decl
;
11080 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11081 if (class_template_p
)
11082 --processing_template_decl
;
11083 if (newtag
== error_mark_node
)
11086 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11088 tree name
= TYPE_IDENTIFIER (t
);
11090 if (class_template_p
)
11091 /* Unfortunately, lookup_template_class sets
11092 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11093 instantiation (i.e., for the type of a member
11094 template class nested within a template class.)
11095 This behavior is required for
11096 maybe_process_partial_specialization to work
11097 correctly, but is not accurate in this case;
11098 the TAG is not an instantiation of anything.
11099 (The corresponding TEMPLATE_DECL is an
11100 instantiation, but the TYPE is not.) */
11101 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11103 /* Now, we call pushtag to put this NEWTAG into the scope of
11104 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11105 pushtag calling push_template_decl. We don't have to do
11106 this for enums because it will already have been done in
11109 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11110 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11113 else if (DECL_DECLARES_FUNCTION_P (t
))
11117 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11118 ++processing_template_decl
;
11119 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11120 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11121 --processing_template_decl
;
11122 set_current_access_from_decl (r
);
11123 finish_member_declaration (r
);
11124 /* Instantiate members marked with attribute used. */
11125 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11127 if (TREE_CODE (r
) == FUNCTION_DECL
11128 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11129 cp_check_omp_declare_reduction (r
);
11131 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11132 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11133 /* A closure type for a lambda in an NSDMI or default argument.
11134 Ignore it; it will be regenerated when needed. */;
11137 /* Build new TYPE_FIELDS. */
11138 if (TREE_CODE (t
) == STATIC_ASSERT
)
11142 ++c_inhibit_evaluation_warnings
;
11144 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11145 tf_warning_or_error
, NULL_TREE
,
11146 /*integral_constant_expression_p=*/true);
11147 --c_inhibit_evaluation_warnings
;
11149 finish_static_assert (condition
,
11150 STATIC_ASSERT_MESSAGE (t
),
11151 STATIC_ASSERT_SOURCE_LOCATION (t
),
11152 /*member_p=*/true);
11154 else if (TREE_CODE (t
) != CONST_DECL
)
11157 tree vec
= NULL_TREE
;
11160 /* The file and line for this declaration, to
11161 assist in error message reporting. Since we
11162 called push_tinst_level above, we don't need to
11164 input_location
= DECL_SOURCE_LOCATION (t
);
11166 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11167 ++processing_template_decl
;
11168 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11169 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11170 --processing_template_decl
;
11172 if (TREE_CODE (r
) == TREE_VEC
)
11174 /* A capture pack became multiple fields. */
11176 len
= TREE_VEC_LENGTH (vec
);
11179 for (int i
= 0; i
< len
; ++i
)
11182 r
= TREE_VEC_ELT (vec
, i
);
11187 [t]he initialization (and any associated
11188 side-effects) of a static data member does
11189 not occur unless the static data member is
11190 itself used in a way that requires the
11191 definition of the static data member to
11194 Therefore, we do not substitute into the
11195 initialized for the static data member here. */
11196 finish_static_data_member_decl
11198 /*init=*/NULL_TREE
,
11199 /*init_const_expr_p=*/false,
11200 /*asmspec_tree=*/NULL_TREE
,
11202 /* Instantiate members marked with attribute used. */
11203 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11206 else if (TREE_CODE (r
) == FIELD_DECL
)
11208 /* Determine whether R has a valid type and can be
11209 completed later. If R is invalid, then its type
11210 is replaced by error_mark_node. */
11211 tree rtype
= TREE_TYPE (r
);
11212 if (can_complete_type_without_circularity (rtype
))
11213 complete_type (rtype
);
11215 if (!complete_or_array_type_p (rtype
))
11217 /* If R's type couldn't be completed and
11218 it isn't a flexible array member (whose
11219 type is incomplete by definition) give
11221 cxx_incomplete_type_error (r
, rtype
);
11222 TREE_TYPE (r
) = error_mark_node
;
11224 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11225 && TYPE_DOMAIN (rtype
) == NULL_TREE
11226 && (TREE_CODE (type
) == UNION_TYPE
11227 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11229 error ("flexible array member %qD in union", r
);
11230 TREE_TYPE (r
) = error_mark_node
;
11234 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11235 such a thing will already have been added to the field
11236 list by tsubst_enum in finish_member_declaration in the
11237 CLASSTYPE_NESTED_UTDS case above. */
11238 if (!(TREE_CODE (r
) == TYPE_DECL
11239 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11240 && DECL_ARTIFICIAL (r
)))
11242 set_current_access_from_decl (r
);
11243 finish_member_declaration (r
);
11251 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11252 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11254 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11256 tree friend_type
= t
;
11257 bool adjust_processing_template_decl
= false;
11259 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11261 /* template <class T> friend class C; */
11262 friend_type
= tsubst_friend_class (friend_type
, args
);
11263 adjust_processing_template_decl
= true;
11265 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11267 /* template <class T> friend class C::D; */
11268 friend_type
= tsubst (friend_type
, args
,
11269 tf_warning_or_error
, NULL_TREE
);
11270 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11271 friend_type
= TREE_TYPE (friend_type
);
11272 adjust_processing_template_decl
= true;
11274 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11275 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11277 /* This could be either
11281 when dependent_type_p is false or
11283 template <class U> friend class T::C;
11286 /* Bump processing_template_decl in case this is something like
11287 template <class T> friend struct A<T>::B. */
11288 ++processing_template_decl
;
11289 friend_type
= tsubst (friend_type
, args
,
11290 tf_warning_or_error
, NULL_TREE
);
11291 if (dependent_type_p (friend_type
))
11292 adjust_processing_template_decl
= true;
11293 --processing_template_decl
;
11295 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11296 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11297 && TYPE_HIDDEN_P (friend_type
))
11301 where C hasn't been declared yet. Let's lookup name
11302 from namespace scope directly, bypassing any name that
11303 come from dependent base class. */
11304 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11306 /* The call to xref_tag_from_type does injection for friend
11308 push_nested_namespace (ns
);
11310 xref_tag_from_type (friend_type
, NULL_TREE
,
11311 /*tag_scope=*/ts_current
);
11312 pop_nested_namespace (ns
);
11314 else if (uses_template_parms (friend_type
))
11315 /* friend class C<T>; */
11316 friend_type
= tsubst (friend_type
, args
,
11317 tf_warning_or_error
, NULL_TREE
);
11322 where C is already declared or
11324 friend class C<int>;
11326 We don't have to do anything in these cases. */
11328 if (adjust_processing_template_decl
)
11329 /* Trick make_friend_class into realizing that the friend
11330 we're adding is a template, not an ordinary class. It's
11331 important that we use make_friend_class since it will
11332 perform some error-checking and output cross-reference
11334 ++processing_template_decl
;
11336 if (friend_type
!= error_mark_node
)
11337 make_friend_class (type
, friend_type
, /*complain=*/false);
11339 if (adjust_processing_template_decl
)
11340 --processing_template_decl
;
11344 /* Build new DECL_FRIENDLIST. */
11347 /* The file and line for this declaration, to
11348 assist in error message reporting. Since we
11349 called push_tinst_level above, we don't need to
11351 input_location
= DECL_SOURCE_LOCATION (t
);
11353 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11355 ++processing_template_decl
;
11356 push_deferring_access_checks (dk_no_check
);
11359 r
= tsubst_friend_function (t
, args
);
11360 add_friend (type
, r
, /*complain=*/false);
11361 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11363 pop_deferring_access_checks ();
11364 --processing_template_decl
;
11372 /* Restore these before substituting into the lambda capture
11374 cp_unevaluated_operand
= saved_unevaluated_operand
;
11375 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11378 /* Set the file and line number information to whatever is given for
11379 the class itself. This puts error messages involving generated
11380 implicit functions at a predictable point, and the same point
11381 that would be used for non-template classes. */
11382 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11384 unreverse_member_declarations (type
);
11385 finish_struct_1 (type
);
11386 TYPE_BEING_DEFINED (type
) = 0;
11388 /* We don't instantiate default arguments for member functions. 14.7.1:
11390 The implicit instantiation of a class template specialization causes
11391 the implicit instantiation of the declarations, but not of the
11392 definitions or default arguments, of the class member functions,
11393 member classes, static data members and member templates.... */
11395 /* Some typedefs referenced from within the template code need to be access
11396 checked at template instantiation time, i.e now. These types were
11397 added to the template at parsing time. Let's get those and perform
11398 the access checks then. */
11399 perform_typedefs_access_check (pattern
, args
);
11400 perform_deferred_access_checks (tf_warning_or_error
);
11401 pop_nested_class ();
11402 maximum_field_alignment
= saved_maximum_field_alignment
;
11404 pop_from_top_level ();
11405 pop_tinst_level ();
11407 /* The vtable for a template class can be emitted in any translation
11408 unit in which the class is instantiated. When there is no key
11409 method, however, finish_struct_1 will already have added TYPE to
11410 the keyed_classes. */
11411 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11412 vec_safe_push (keyed_classes
, type
);
11417 /* Wrapper for instantiate_class_template_1. */
11420 instantiate_class_template (tree type
)
11423 timevar_push (TV_TEMPLATE_INST
);
11424 ret
= instantiate_class_template_1 (type
);
11425 timevar_pop (TV_TEMPLATE_INST
);
11430 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11436 else if (TYPE_P (t
))
11437 r
= tsubst (t
, args
, complain
, in_decl
);
11440 if (!(complain
& tf_warning
))
11441 ++c_inhibit_evaluation_warnings
;
11442 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11443 /*integral_constant_expression_p=*/true);
11444 if (!(complain
& tf_warning
))
11445 --c_inhibit_evaluation_warnings
;
11450 /* Given a function parameter pack TMPL_PARM and some function parameters
11451 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11452 and set *SPEC_P to point at the next point in the list. */
11455 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11457 /* Collect all of the extra "packed" parameters into an
11460 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11461 tree spec_parm
= *spec_p
;
11464 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11466 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11469 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11470 parmvec
= make_tree_vec (len
);
11471 spec_parm
= *spec_p
;
11472 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11474 tree elt
= spec_parm
;
11475 if (DECL_PACK_P (elt
))
11476 elt
= make_pack_expansion (elt
);
11477 TREE_VEC_ELT (parmvec
, i
) = elt
;
11480 /* Build the argument packs. */
11481 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11482 *spec_p
= spec_parm
;
11487 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11488 NONTYPE_ARGUMENT_PACK. */
11491 make_fnparm_pack (tree spec_parm
)
11493 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11496 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11497 pack expansion with no extra args, 2 if it has extra args, or 0
11498 if it is not a pack expansion. */
11501 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11503 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11504 if (i
>= TREE_VEC_LENGTH (vec
))
11506 tree elt
= TREE_VEC_ELT (vec
, i
);
11508 /* A decl pack is itself an expansion. */
11509 elt
= TREE_TYPE (elt
);
11510 if (!PACK_EXPANSION_P (elt
))
11512 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11518 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11521 make_argument_pack_select (tree arg_pack
, unsigned index
)
11523 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11525 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11526 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11531 /* This is a subroutine of tsubst_pack_expansion.
11533 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11534 mechanism to store the (non complete list of) arguments of the
11535 substitution and return a non substituted pack expansion, in order
11536 to wait for when we have enough arguments to really perform the
11540 use_pack_expansion_extra_args_p (tree parm_packs
,
11542 bool has_empty_arg
)
11544 /* If one pack has an expansion and another pack has a normal
11545 argument or if one pack has an empty argument and an another
11546 one hasn't then tsubst_pack_expansion cannot perform the
11547 substitution and need to fall back on the
11548 PACK_EXPANSION_EXTRA mechanism. */
11549 if (parm_packs
== NULL_TREE
)
11551 else if (has_empty_arg
)
11554 bool has_expansion_arg
= false;
11555 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11557 bool has_non_expansion_arg
= false;
11558 for (tree parm_pack
= parm_packs
;
11560 parm_pack
= TREE_CHAIN (parm_pack
))
11562 tree arg
= TREE_VALUE (parm_pack
);
11564 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11566 /* We can't substitute a pack expansion with extra args into
11570 has_expansion_arg
= true;
11572 has_non_expansion_arg
= true;
11575 if (has_expansion_arg
&& has_non_expansion_arg
)
11581 /* [temp.variadic]/6 says that:
11583 The instantiation of a pack expansion [...]
11584 produces a list E1,E2, ..., En, where N is the number of elements
11585 in the pack expansion parameters.
11587 This subroutine of tsubst_pack_expansion produces one of these Ei.
11589 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11590 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11591 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11592 INDEX is the index 'i' of the element Ei to produce. ARGS,
11593 COMPLAIN, and IN_DECL are the same parameters as for the
11594 tsubst_pack_expansion function.
11596 The function returns the resulting Ei upon successful completion,
11597 or error_mark_node.
11599 Note that this function possibly modifies the ARGS parameter, so
11600 it's the responsibility of the caller to restore it. */
11603 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11606 tree args
/* This parm gets
11608 tsubst_flags_t complain
,
11612 bool ith_elem_is_expansion
= false;
11614 /* For each parameter pack, change the substitution of the parameter
11615 pack to the ith argument in its argument pack, then expand the
11617 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11619 tree parm
= TREE_PURPOSE (pack
);
11620 tree arg_pack
= TREE_VALUE (pack
);
11621 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11623 ith_elem_is_expansion
|=
11624 argument_pack_element_is_expansion_p (arg_pack
, index
);
11626 /* Select the Ith argument from the pack. */
11627 if (TREE_CODE (parm
) == PARM_DECL
11629 || TREE_CODE (parm
) == FIELD_DECL
)
11633 aps
= make_argument_pack_select (arg_pack
, index
);
11634 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11635 return error_mark_node
;
11636 register_local_specialization (aps
, parm
);
11639 aps
= retrieve_local_specialization (parm
);
11644 template_parm_level_and_index (parm
, &level
, &idx
);
11648 aps
= make_argument_pack_select (arg_pack
, index
);
11649 /* Update the corresponding argument. */
11650 TMPL_ARG (args
, level
, idx
) = aps
;
11653 /* Re-use the ARGUMENT_PACK_SELECT. */
11654 aps
= TMPL_ARG (args
, level
, idx
);
11656 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11659 /* Substitute into the PATTERN with the (possibly altered)
11661 if (pattern
== in_decl
)
11662 /* Expanding a fixed parameter pack from
11663 coerce_template_parameter_pack. */
11664 t
= tsubst_decl (pattern
, args
, complain
);
11665 else if (pattern
== error_mark_node
)
11666 t
= error_mark_node
;
11667 else if (constraint_p (pattern
))
11669 if (processing_template_decl
)
11670 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11672 t
= (constraints_satisfied_p (pattern
, args
)
11673 ? boolean_true_node
: boolean_false_node
);
11675 else if (!TYPE_P (pattern
))
11676 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11677 /*integral_constant_expression_p=*/false);
11679 t
= tsubst (pattern
, args
, complain
, in_decl
);
11681 /* If the Ith argument pack element is a pack expansion, then
11682 the Ith element resulting from the substituting is going to
11683 be a pack expansion as well. */
11684 if (ith_elem_is_expansion
)
11685 t
= make_pack_expansion (t
, complain
);
11690 /* When the unexpanded parameter pack in a fold expression expands to an empty
11691 sequence, the value of the expression is as follows; the program is
11692 ill-formed if the operator is not listed in this table.
11699 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11701 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11702 if (!FOLD_EXPR_MODIFY_P (t
))
11705 case TRUTH_ANDIF_EXPR
:
11706 return boolean_true_node
;
11707 case TRUTH_ORIF_EXPR
:
11708 return boolean_false_node
;
11709 case COMPOUND_EXPR
:
11715 if (complain
& tf_error
)
11716 error_at (location_of (t
),
11717 "fold of empty expansion over %O", code
);
11718 return error_mark_node
;
11721 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11722 form an expression that combines the two terms using the
11726 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11728 tree op
= FOLD_EXPR_OP (t
);
11729 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11731 // Handle compound assignment operators.
11732 if (FOLD_EXPR_MODIFY_P (t
))
11733 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11737 case COMPOUND_EXPR
:
11738 return build_x_compound_expr (input_location
, left
, right
, complain
);
11740 return build_m_component_ref (left
, right
, complain
);
11742 return build_x_binary_op (input_location
, code
,
11743 left
, TREE_CODE (left
),
11744 right
, TREE_CODE (right
),
11750 /* Substitute ARGS into the pack of a fold expression T. */
11753 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11755 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11758 /* Substitute ARGS into the pack of a fold expression T. */
11761 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11763 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11766 /* Expand a PACK of arguments into a grouped as left fold.
11767 Given a pack containing elements A0, A1, ..., An and an
11768 operator @, this builds the expression:
11770 ((A0 @ A1) @ A2) ... @ An
11772 Note that PACK must not be empty.
11774 The operator is defined by the original fold expression T. */
11777 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11779 tree left
= TREE_VEC_ELT (pack
, 0);
11780 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11782 tree right
= TREE_VEC_ELT (pack
, i
);
11783 left
= fold_expression (t
, left
, right
, complain
);
11788 /* Substitute into a unary left fold expression. */
11791 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11794 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11795 if (pack
== error_mark_node
)
11796 return error_mark_node
;
11797 if (PACK_EXPANSION_P (pack
))
11799 tree r
= copy_node (t
);
11800 FOLD_EXPR_PACK (r
) = pack
;
11803 if (TREE_VEC_LENGTH (pack
) == 0)
11804 return expand_empty_fold (t
, complain
);
11806 return expand_left_fold (t
, pack
, complain
);
11809 /* Substitute into a binary left fold expression.
11811 Do ths by building a single (non-empty) vector of argumnts and
11812 building the expression from those elements. */
11815 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11818 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11819 if (pack
== error_mark_node
)
11820 return error_mark_node
;
11821 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11822 if (init
== error_mark_node
)
11823 return error_mark_node
;
11825 if (PACK_EXPANSION_P (pack
))
11827 tree r
= copy_node (t
);
11828 FOLD_EXPR_PACK (r
) = pack
;
11829 FOLD_EXPR_INIT (r
) = init
;
11833 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11834 TREE_VEC_ELT (vec
, 0) = init
;
11835 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11836 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11838 return expand_left_fold (t
, vec
, complain
);
11841 /* Expand a PACK of arguments into a grouped as right fold.
11842 Given a pack containing elementns A0, A1, ..., and an
11843 operator @, this builds the expression:
11845 A0@ ... (An-2 @ (An-1 @ An))
11847 Note that PACK must not be empty.
11849 The operator is defined by the original fold expression T. */
11852 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11854 // Build the expression.
11855 int n
= TREE_VEC_LENGTH (pack
);
11856 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11857 for (--n
; n
!= 0; --n
)
11859 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11860 right
= fold_expression (t
, left
, right
, complain
);
11865 /* Substitute into a unary right fold expression. */
11868 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11871 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11872 if (pack
== error_mark_node
)
11873 return error_mark_node
;
11874 if (PACK_EXPANSION_P (pack
))
11876 tree r
= copy_node (t
);
11877 FOLD_EXPR_PACK (r
) = pack
;
11880 if (TREE_VEC_LENGTH (pack
) == 0)
11881 return expand_empty_fold (t
, complain
);
11883 return expand_right_fold (t
, pack
, complain
);
11886 /* Substitute into a binary right fold expression.
11888 Do ths by building a single (non-empty) vector of arguments and
11889 building the expression from those elements. */
11892 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11895 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11896 if (pack
== error_mark_node
)
11897 return error_mark_node
;
11898 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11899 if (init
== error_mark_node
)
11900 return error_mark_node
;
11902 if (PACK_EXPANSION_P (pack
))
11904 tree r
= copy_node (t
);
11905 FOLD_EXPR_PACK (r
) = pack
;
11906 FOLD_EXPR_INIT (r
) = init
;
11910 int n
= TREE_VEC_LENGTH (pack
);
11911 tree vec
= make_tree_vec (n
+ 1);
11912 for (int i
= 0; i
< n
; ++i
)
11913 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11914 TREE_VEC_ELT (vec
, n
) = init
;
11916 return expand_right_fold (t
, vec
, complain
);
11919 /* Walk through the pattern of a pack expansion, adding everything in
11920 local_specializations to a list. */
11924 hash_set
<tree
> internal
;
11926 tsubst_flags_t complain
;
11928 el_data (tsubst_flags_t c
)
11929 : extra (NULL_TREE
), complain (c
) {}
11932 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11934 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11935 tree
*extra
= &data
.extra
;
11936 tsubst_flags_t complain
= data
.complain
;
11938 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
11939 /* Remember local typedefs (85214). */
11940 tp
= &TYPE_NAME (*tp
);
11942 if (TREE_CODE (*tp
) == DECL_EXPR
)
11943 data
.internal
.add (DECL_EXPR_DECL (*tp
));
11944 else if (tree spec
= retrieve_local_specialization (*tp
))
11946 if (data
.internal
.contains (*tp
))
11947 /* Don't mess with variables declared within the pattern. */
11949 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11951 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11952 tree args
= ARGUMENT_PACK_ARGS (spec
);
11953 if (TREE_VEC_LENGTH (args
) == 1)
11955 tree elt
= TREE_VEC_ELT (args
, 0);
11956 if (PACK_EXPANSION_P (elt
))
11957 elt
= PACK_EXPANSION_PATTERN (elt
);
11958 if (DECL_PACK_P (elt
))
11961 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11963 /* Handle lambda capture here, since we aren't doing any
11964 substitution now, and so tsubst_copy won't call
11965 process_outer_var_ref. */
11966 tree args
= ARGUMENT_PACK_ARGS (spec
);
11967 int len
= TREE_VEC_LENGTH (args
);
11968 for (int i
= 0; i
< len
; ++i
)
11970 tree arg
= TREE_VEC_ELT (args
, i
);
11972 if (outer_automatic_var_p (arg
))
11973 carg
= process_outer_var_ref (arg
, complain
);
11976 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11980 spec
= copy_node (spec
);
11981 args
= copy_node (args
);
11982 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11983 register_local_specialization (spec
, *tp
);
11985 TREE_VEC_ELT (args
, i
) = carg
;
11990 if (outer_automatic_var_p (spec
))
11991 spec
= process_outer_var_ref (spec
, complain
);
11992 *extra
= tree_cons (*tp
, spec
, *extra
);
11997 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11999 el_data
data (complain
);
12000 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12004 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12005 for use in PACK_EXPANSION_EXTRA_ARGS. */
12008 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12011 if (local_specializations
)
12012 if (tree locals
= extract_local_specs (pattern
, complain
))
12013 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12017 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12018 normal template args to ARGS. */
12021 add_extra_args (tree extra
, tree args
)
12023 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12025 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12027 /* The partial instantiation involved local declarations collected in
12028 extract_local_specs; map from the general template to our local
12030 tree gen
= TREE_PURPOSE (elt
);
12031 tree inst
= TREE_VALUE (elt
);
12033 if (tree local
= retrieve_local_specialization (inst
))
12035 /* else inst is already a full instantiation of the pack. */
12036 register_local_specialization (inst
, gen
);
12038 gcc_assert (!TREE_PURPOSE (extra
));
12039 extra
= TREE_VALUE (extra
);
12041 return add_to_template_args (extra
, args
);
12044 /* Substitute ARGS into T, which is an pack expansion
12045 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12046 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12047 (if only a partial substitution could be performed) or
12048 ERROR_MARK_NODE if there was an error. */
12050 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12054 tree pack
, packs
= NULL_TREE
;
12055 bool unsubstituted_packs
= false;
12056 bool unsubstituted_fn_pack
= false;
12059 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12060 bool need_local_specializations
= false;
12063 gcc_assert (PACK_EXPANSION_P (t
));
12064 pattern
= PACK_EXPANSION_PATTERN (t
);
12066 /* Add in any args remembered from an earlier partial instantiation. */
12067 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12069 levels
= TMPL_ARGS_DEPTH (args
);
12071 /* Determine the argument packs that will instantiate the parameter
12072 packs used in the expansion expression. While we're at it,
12073 compute the number of arguments to be expanded and make sure it
12075 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12076 pack
= TREE_CHAIN (pack
))
12078 tree parm_pack
= TREE_VALUE (pack
);
12079 tree arg_pack
= NULL_TREE
;
12080 tree orig_arg
= NULL_TREE
;
12083 if (TREE_CODE (parm_pack
) == BASES
)
12085 gcc_assert (parm_pack
== pattern
);
12086 if (BASES_DIRECT (parm_pack
))
12087 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12092 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12093 args
, complain
, in_decl
,
12096 else if (builtin_pack_call_p (parm_pack
))
12098 if (parm_pack
!= pattern
)
12100 if (complain
& tf_error
)
12101 sorry ("%qE is not the entire pattern of the pack expansion",
12103 return error_mark_node
;
12105 return expand_builtin_pack_call (parm_pack
, args
,
12106 complain
, in_decl
);
12108 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12110 /* We know we have correct local_specializations if this
12111 expansion is at function scope, or if we're dealing with a
12112 local parameter in a requires expression; for the latter,
12113 tsubst_requires_expr set it up appropriately. */
12114 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12115 arg_pack
= retrieve_local_specialization (parm_pack
);
12117 /* We can't rely on local_specializations for a parameter
12118 name used later in a function declaration (such as in a
12119 late-specified return type). Even if it exists, it might
12120 have the wrong value for a recursive call. */
12121 need_local_specializations
= true;
12125 /* This parameter pack was used in an unevaluated context. Just
12126 make a dummy decl, since it's only used for its type. */
12127 ++cp_unevaluated_operand
;
12128 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12129 --cp_unevaluated_operand
;
12130 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12131 /* Partial instantiation of the parm_pack, we can't build
12132 up an argument pack yet. */
12133 arg_pack
= NULL_TREE
;
12135 arg_pack
= make_fnparm_pack (arg_pack
);
12137 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12138 /* This argument pack isn't fully instantiated yet. We set this
12139 flag rather than clear arg_pack because we do want to do the
12140 optimization below, and we don't want to substitute directly
12141 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12142 where it isn't expected). */
12143 unsubstituted_fn_pack
= true;
12145 else if (is_normal_capture_proxy (parm_pack
))
12147 arg_pack
= retrieve_local_specialization (parm_pack
);
12148 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12149 unsubstituted_fn_pack
= true;
12154 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12156 if (level
<= levels
)
12157 arg_pack
= TMPL_ARG (args
, level
, idx
);
12160 orig_arg
= arg_pack
;
12161 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12162 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12164 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12165 /* This can only happen if we forget to expand an argument
12166 pack somewhere else. Just return an error, silently. */
12168 result
= make_tree_vec (1);
12169 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12176 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12178 /* Don't bother trying to do a partial substitution with
12179 incomplete packs; we'll try again after deduction. */
12180 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12185 else if (len
!= my_len
12186 && !unsubstituted_fn_pack
)
12188 if (!(complain
& tf_error
))
12189 /* Fail quietly. */;
12190 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12191 error ("mismatched argument pack lengths while expanding %qT",
12194 error ("mismatched argument pack lengths while expanding %qE",
12196 return error_mark_node
;
12199 /* Keep track of the parameter packs and their corresponding
12201 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12202 TREE_TYPE (packs
) = orig_arg
;
12206 /* We can't substitute for this parameter pack. We use a flag as
12207 well as the missing_level counter because function parameter
12208 packs don't have a level. */
12209 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12210 unsubstituted_packs
= true;
12214 /* If the expansion is just T..., return the matching argument pack, unless
12215 we need to call convert_from_reference on all the elements. This is an
12216 important optimization; see c++/68422. */
12217 if (!unsubstituted_packs
12218 && TREE_PURPOSE (packs
) == pattern
)
12220 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12222 /* If the argument pack is a single pack expansion, pull it out. */
12223 if (TREE_VEC_LENGTH (args
) == 1
12224 && pack_expansion_args_count (args
))
12225 return TREE_VEC_ELT (args
, 0);
12227 /* Types need no adjustment, nor does sizeof..., and if we still have
12228 some pack expansion args we won't do anything yet. */
12229 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12230 || PACK_EXPANSION_SIZEOF_P (t
)
12231 || pack_expansion_args_count (args
))
12233 /* Also optimize expression pack expansions if we can tell that the
12234 elements won't have reference type. */
12235 tree type
= TREE_TYPE (pattern
);
12236 if (type
&& !TYPE_REF_P (type
)
12237 && !PACK_EXPANSION_P (type
)
12238 && !WILDCARD_TYPE_P (type
))
12240 /* Otherwise use the normal path so we get convert_from_reference. */
12243 /* We cannot expand this expansion expression, because we don't have
12244 all of the argument packs we need. */
12245 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12247 /* We got some full packs, but we can't substitute them in until we
12248 have values for all the packs. So remember these until then. */
12250 t
= make_pack_expansion (pattern
, complain
);
12251 PACK_EXPANSION_EXTRA_ARGS (t
)
12252 = build_extra_args (pattern
, args
, complain
);
12255 else if (unsubstituted_packs
)
12257 /* There were no real arguments, we're just replacing a parameter
12258 pack with another version of itself. Substitute into the
12259 pattern and return a PACK_EXPANSION_*. The caller will need to
12261 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12262 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12263 /*integral_constant_expression_p=*/false);
12265 t
= tsubst (pattern
, args
, complain
, in_decl
);
12266 t
= make_pack_expansion (t
, complain
);
12270 gcc_assert (len
>= 0);
12272 if (need_local_specializations
)
12274 /* We're in a late-specified return type, so create our own local
12275 specializations map; the current map is either NULL or (in the
12276 case of recursive unification) might have bindings that we don't
12277 want to use or alter. */
12278 saved_local_specializations
= local_specializations
;
12279 local_specializations
= new hash_map
<tree
, tree
>;
12282 /* For each argument in each argument pack, substitute into the
12284 result
= make_tree_vec (len
);
12285 tree elem_args
= copy_template_args (args
);
12286 for (i
= 0; i
< len
; ++i
)
12288 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12290 elem_args
, complain
,
12292 TREE_VEC_ELT (result
, i
) = t
;
12293 if (t
== error_mark_node
)
12295 result
= error_mark_node
;
12300 /* Update ARGS to restore the substitution from parameter packs to
12301 their argument packs. */
12302 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12304 tree parm
= TREE_PURPOSE (pack
);
12306 if (TREE_CODE (parm
) == PARM_DECL
12308 || TREE_CODE (parm
) == FIELD_DECL
)
12309 register_local_specialization (TREE_TYPE (pack
), parm
);
12314 if (TREE_VALUE (pack
) == NULL_TREE
)
12317 template_parm_level_and_index (parm
, &level
, &idx
);
12319 /* Update the corresponding argument. */
12320 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12321 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12324 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12328 if (need_local_specializations
)
12330 delete local_specializations
;
12331 local_specializations
= saved_local_specializations
;
12334 /* If the dependent pack arguments were such that we end up with only a
12335 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12336 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12337 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12338 return TREE_VEC_ELT (result
, 0);
12343 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12344 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12345 parameter packs; all parms generated from a function parameter pack will
12346 have the same DECL_PARM_INDEX. */
12349 get_pattern_parm (tree parm
, tree tmpl
)
12351 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12354 if (DECL_ARTIFICIAL (parm
))
12356 for (patparm
= DECL_ARGUMENTS (pattern
);
12357 patparm
; patparm
= DECL_CHAIN (patparm
))
12358 if (DECL_ARTIFICIAL (patparm
)
12359 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12364 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12365 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12366 gcc_assert (DECL_PARM_INDEX (patparm
)
12367 == DECL_PARM_INDEX (parm
));
12373 /* Make an argument pack out of the TREE_VEC VEC. */
12376 make_argument_pack (tree vec
)
12379 tree elt
= TREE_VEC_ELT (vec
, 0);
12381 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12384 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12385 TREE_CONSTANT (pack
) = 1;
12387 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12391 /* Return an exact copy of template args T that can be modified
12395 copy_template_args (tree t
)
12397 if (t
== error_mark_node
)
12400 int len
= TREE_VEC_LENGTH (t
);
12401 tree new_vec
= make_tree_vec (len
);
12403 for (int i
= 0; i
< len
; ++i
)
12405 tree elt
= TREE_VEC_ELT (t
, i
);
12406 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12407 elt
= copy_template_args (elt
);
12408 TREE_VEC_ELT (new_vec
, i
) = elt
;
12411 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12412 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12417 /* Substitute ARGS into the vector or list of template arguments T. */
12420 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12423 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12426 if (t
== error_mark_node
)
12427 return error_mark_node
;
12429 len
= TREE_VEC_LENGTH (t
);
12430 elts
= XALLOCAVEC (tree
, len
);
12432 for (i
= 0; i
< len
; i
++)
12434 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12437 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12438 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12439 else if (PACK_EXPANSION_P (orig_arg
))
12441 /* Substitute into an expansion expression. */
12442 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12444 if (TREE_CODE (new_arg
) == TREE_VEC
)
12445 /* Add to the expanded length adjustment the number of
12446 expanded arguments. We subtract one from this
12447 measurement, because the argument pack expression
12448 itself is already counted as 1 in
12449 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12450 the argument pack is empty. */
12451 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12453 else if (ARGUMENT_PACK_P (orig_arg
))
12455 /* Substitute into each of the arguments. */
12456 new_arg
= TYPE_P (orig_arg
)
12457 ? cxx_make_type (TREE_CODE (orig_arg
))
12458 : make_node (TREE_CODE (orig_arg
));
12460 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12461 args
, complain
, in_decl
);
12462 if (pack_args
== error_mark_node
)
12463 new_arg
= error_mark_node
;
12465 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12467 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12468 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12471 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12473 if (new_arg
== error_mark_node
)
12474 return error_mark_node
;
12477 if (new_arg
!= orig_arg
)
12484 /* Make space for the expanded arguments coming from template
12486 t
= make_tree_vec (len
+ expanded_len_adjust
);
12487 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12488 arguments for a member template.
12489 In that case each TREE_VEC in ORIG_T represents a level of template
12490 arguments, and ORIG_T won't carry any non defaulted argument count.
12491 It will rather be the nested TREE_VECs that will carry one.
12492 In other words, ORIG_T carries a non defaulted argument count only
12493 if it doesn't contain any nested TREE_VEC. */
12494 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12496 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12497 count
+= expanded_len_adjust
;
12498 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12500 for (i
= 0, out
= 0; i
< len
; i
++)
12502 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12503 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12504 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12508 /* Now expand the template argument pack "in place". */
12509 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12510 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12514 TREE_VEC_ELT (t
, out
) = elts
[i
];
12522 /* Substitute ARGS into one level PARMS of template parameters. */
12525 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12527 if (parms
== error_mark_node
)
12528 return error_mark_node
;
12530 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12532 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12534 tree tuple
= TREE_VEC_ELT (parms
, i
);
12536 if (tuple
== error_mark_node
)
12539 TREE_VEC_ELT (new_vec
, i
) =
12540 tsubst_template_parm (tuple
, args
, complain
);
12546 /* Return the result of substituting ARGS into the template parameters
12547 given by PARMS. If there are m levels of ARGS and m + n levels of
12548 PARMS, then the result will contain n levels of PARMS. For
12549 example, if PARMS is `template <class T> template <class U>
12550 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12551 result will be `template <int*, double, class V>'. */
12554 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12556 tree r
= NULL_TREE
;
12559 /* When substituting into a template, we must set
12560 PROCESSING_TEMPLATE_DECL as the template parameters may be
12561 dependent if they are based on one-another, and the dependency
12562 predicates are short-circuit outside of templates. */
12563 ++processing_template_decl
;
12565 for (new_parms
= &r
;
12566 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12567 new_parms
= &(TREE_CHAIN (*new_parms
)),
12568 parms
= TREE_CHAIN (parms
))
12570 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12573 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12574 - TMPL_ARGS_DEPTH (args
)),
12575 new_vec
, NULL_TREE
);
12578 --processing_template_decl
;
12583 /* Return the result of substituting ARGS into one template parameter
12584 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12585 parameter and which TREE_PURPOSE is the default argument of the
12586 template parameter. */
12589 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12591 tree default_value
, parm_decl
;
12593 if (args
== NULL_TREE
12595 || t
== error_mark_node
)
12598 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12600 default_value
= TREE_PURPOSE (t
);
12601 parm_decl
= TREE_VALUE (t
);
12603 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12604 if (TREE_CODE (parm_decl
) == PARM_DECL
12605 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12606 parm_decl
= error_mark_node
;
12607 default_value
= tsubst_template_arg (default_value
, args
,
12608 complain
, NULL_TREE
);
12610 return build_tree_list (default_value
, parm_decl
);
12613 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12614 type T. If T is not an aggregate or enumeration type, it is
12615 handled as if by tsubst. IN_DECL is as for tsubst. If
12616 ENTERING_SCOPE is nonzero, T is the context for a template which
12617 we are presently tsubst'ing. Return the substituted value. */
12620 tsubst_aggr_type (tree t
,
12622 tsubst_flags_t complain
,
12624 int entering_scope
)
12626 if (t
== NULL_TREE
)
12629 switch (TREE_CODE (t
))
12632 if (TYPE_PTRMEMFUNC_P (t
))
12633 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12635 /* Fall through. */
12636 case ENUMERAL_TYPE
:
12638 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12643 int saved_unevaluated_operand
;
12644 int saved_inhibit_evaluation_warnings
;
12646 /* In "sizeof(X<I>)" we need to evaluate "I". */
12647 saved_unevaluated_operand
= cp_unevaluated_operand
;
12648 cp_unevaluated_operand
= 0;
12649 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12650 c_inhibit_evaluation_warnings
= 0;
12652 /* First, determine the context for the type we are looking
12654 context
= TYPE_CONTEXT (t
);
12655 if (context
&& TYPE_P (context
))
12657 context
= tsubst_aggr_type (context
, args
, complain
,
12658 in_decl
, /*entering_scope=*/1);
12659 /* If context is a nested class inside a class template,
12660 it may still need to be instantiated (c++/33959). */
12661 context
= complete_type (context
);
12664 /* Then, figure out what arguments are appropriate for the
12665 type we are trying to find. For example, given:
12667 template <class T> struct S;
12668 template <class T, class U> void f(T, U) { S<U> su; }
12670 and supposing that we are instantiating f<int, double>,
12671 then our ARGS will be {int, double}, but, when looking up
12672 S we only want {double}. */
12673 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12674 complain
, in_decl
);
12675 if (argvec
== error_mark_node
)
12676 r
= error_mark_node
;
12679 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12680 entering_scope
, complain
);
12681 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12684 cp_unevaluated_operand
= saved_unevaluated_operand
;
12685 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12690 /* This is not a template type, so there's nothing to do. */
12694 return tsubst (t
, args
, complain
, in_decl
);
12698 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12700 /* Substitute into the default argument ARG (a default argument for
12701 FN), which has the indicated TYPE. */
12704 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12705 tsubst_flags_t complain
)
12707 int errs
= errorcount
+ sorrycount
;
12709 /* This can happen in invalid code. */
12710 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12713 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12714 parm
= chain_index (parmnum
, parm
);
12715 tree parmtype
= TREE_TYPE (parm
);
12716 if (DECL_BY_REFERENCE (parm
))
12717 parmtype
= TREE_TYPE (parmtype
);
12718 if (parmtype
== error_mark_node
)
12719 return error_mark_node
;
12721 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12724 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12727 /* This default argument came from a template. Instantiate the
12728 default argument here, not in tsubst. In the case of
12737 we must be careful to do name lookup in the scope of S<T>,
12738 rather than in the current class. */
12739 push_to_top_level ();
12740 push_access_scope (fn
);
12741 start_lambda_scope (parm
);
12743 /* The default argument expression may cause implicitly defined
12744 member functions to be synthesized, which will result in garbage
12745 collection. We must treat this situation as if we were within
12746 the body of function so as to avoid collecting live data on the
12749 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12750 complain
, NULL_TREE
,
12751 /*integral_constant_expression_p=*/false);
12754 finish_lambda_scope ();
12756 if (errorcount
+sorrycount
> errs
12757 && (complain
& tf_warning_or_error
))
12758 inform (input_location
,
12759 " when instantiating default argument for call to %qD", fn
);
12761 /* Make sure the default argument is reasonable. */
12762 arg
= check_default_argument (type
, arg
, complain
);
12764 pop_access_scope (fn
);
12765 pop_from_top_level ();
12767 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12770 defarg_inst
= tree_cache_map::create_ggc (37);
12771 defarg_inst
->put (parm
, arg
);
12777 /* Substitute into all the default arguments for FN. */
12780 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12785 tmpl_args
= DECL_TI_ARGS (fn
);
12787 /* If this function is not yet instantiated, we certainly don't need
12788 its default arguments. */
12789 if (uses_template_parms (tmpl_args
))
12791 /* Don't do this again for clones. */
12792 if (DECL_CLONED_FUNCTION_P (fn
))
12796 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12798 arg
= TREE_CHAIN (arg
), ++i
)
12799 if (TREE_PURPOSE (arg
))
12800 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12802 TREE_PURPOSE (arg
),
12806 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12809 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12810 tree lambda_fntype
)
12812 tree gen_tmpl
, argvec
;
12813 hashval_t hash
= 0;
12816 /* Nobody should be tsubst'ing into non-template functions. */
12817 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12819 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12821 /* If T is not dependent, just return it. */
12822 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12825 /* Calculate the most general template of which R is a
12827 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12829 /* We're substituting a lambda function under tsubst_lambda_expr but not
12830 directly from it; find the matching function we're already inside.
12831 But don't do this if T is a generic lambda with a single level of
12832 template parms, as in that case we're doing a normal instantiation. */
12833 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12834 && (!generic_lambda_fn_p (t
)
12835 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12836 return enclosing_instantiation_of (t
);
12838 /* Calculate the complete set of arguments used to
12840 argvec
= tsubst_template_args (DECL_TI_ARGS
12841 (DECL_TEMPLATE_RESULT
12842 (DECL_TI_TEMPLATE (t
))),
12843 args
, complain
, in_decl
);
12844 if (argvec
== error_mark_node
)
12845 return error_mark_node
;
12847 /* Check to see if we already have this specialization. */
12848 if (!lambda_fntype
)
12850 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12851 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12855 /* We can see more levels of arguments than parameters if
12856 there was a specialization of a member template, like
12859 template <class T> struct S { template <class U> void f(); }
12860 template <> template <class U> void S<int>::f(U);
12862 Here, we'll be substituting into the specialization,
12863 because that's where we can find the code we actually
12864 want to generate, but we'll have enough arguments for
12865 the most general template.
12867 We also deal with the peculiar case:
12869 template <class T> struct S {
12870 template <class U> friend void f();
12872 template <class U> void f() {}
12874 template void f<double>();
12876 Here, the ARGS for the instantiation of will be {int,
12877 double}. But, we only need as many ARGS as there are
12878 levels of template parameters in CODE_PATTERN. We are
12879 careful not to get fooled into reducing the ARGS in
12882 template <class T> struct S { template <class U> void f(U); }
12883 template <class T> template <> void S<T>::f(int) {}
12885 which we can spot because the pattern will be a
12886 specialization in this case. */
12887 int args_depth
= TMPL_ARGS_DEPTH (args
);
12889 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12891 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12892 args
= get_innermost_template_args (args
, parms_depth
);
12896 /* This special case arises when we have something like this:
12898 template <class T> struct S {
12899 friend void f<int>(int, double);
12902 Here, the DECL_TI_TEMPLATE for the friend declaration
12903 will be an IDENTIFIER_NODE. We are being called from
12904 tsubst_friend_function, and we want only to create a
12905 new decl (R) with appropriate types so that we can call
12906 determine_specialization. */
12907 gen_tmpl
= NULL_TREE
;
12908 argvec
= NULL_TREE
;
12911 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12913 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12914 bool member
= ctx
&& TYPE_P (ctx
);
12916 if (member
&& !closure
)
12917 ctx
= tsubst_aggr_type (ctx
, args
,
12918 complain
, t
, /*entering_scope=*/1);
12920 tree type
= (lambda_fntype
? lambda_fntype
12921 : tsubst (TREE_TYPE (t
), args
,
12922 complain
| tf_fndecl_type
, in_decl
));
12923 if (type
== error_mark_node
)
12924 return error_mark_node
;
12926 /* If we hit excessive deduction depth, the type is bogus even if
12927 it isn't error_mark_node, so don't build a decl. */
12928 if (excessive_deduction_depth
)
12929 return error_mark_node
;
12931 /* We do NOT check for matching decls pushed separately at this
12932 point, as they may not represent instantiations of this
12933 template, and in any case are considered separate under the
12935 tree r
= copy_decl (t
);
12936 DECL_USE_TEMPLATE (r
) = 0;
12937 TREE_TYPE (r
) = type
;
12938 /* Clear out the mangled name and RTL for the instantiation. */
12939 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12940 SET_DECL_RTL (r
, NULL
);
12941 /* Leave DECL_INITIAL set on deleted instantiations. */
12942 if (!DECL_DELETED_FN (r
))
12943 DECL_INITIAL (r
) = NULL_TREE
;
12944 DECL_CONTEXT (r
) = ctx
;
12946 /* OpenMP UDRs have the only argument a reference to the declared
12947 type. We want to diagnose if the declared type is a reference,
12948 which is invalid, but as references to references are usually
12949 quietly merged, diagnose it here. */
12950 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12953 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12954 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12955 if (TYPE_REF_P (argtype
))
12956 error_at (DECL_SOURCE_LOCATION (t
),
12957 "reference type %qT in "
12958 "%<#pragma omp declare reduction%>", argtype
);
12959 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12960 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12964 if (member
&& DECL_CONV_FN_P (r
))
12965 /* Type-conversion operator. Reconstruct the name, in
12966 case it's the name of one of the template's parameters. */
12967 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12969 tree parms
= DECL_ARGUMENTS (t
);
12971 parms
= DECL_CHAIN (parms
);
12972 parms
= tsubst (parms
, args
, complain
, t
);
12973 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12974 DECL_CONTEXT (parm
) = r
;
12977 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12978 DECL_CHAIN (tparm
) = parms
;
12981 DECL_ARGUMENTS (r
) = parms
;
12982 DECL_RESULT (r
) = NULL_TREE
;
12984 TREE_STATIC (r
) = 0;
12985 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12986 DECL_EXTERNAL (r
) = 1;
12987 /* If this is an instantiation of a function with internal
12988 linkage, we already know what object file linkage will be
12989 assigned to the instantiation. */
12990 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12991 DECL_DEFER_OUTPUT (r
) = 0;
12992 DECL_CHAIN (r
) = NULL_TREE
;
12993 DECL_PENDING_INLINE_INFO (r
) = 0;
12994 DECL_PENDING_INLINE_P (r
) = 0;
12995 DECL_SAVED_TREE (r
) = NULL_TREE
;
12996 DECL_STRUCT_FUNCTION (r
) = NULL
;
12998 /* We'll re-clone as appropriate in instantiate_template. */
12999 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13001 /* If we aren't complaining now, return on error before we register
13002 the specialization so that we'll complain eventually. */
13003 if ((complain
& tf_error
) == 0
13004 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13005 && !grok_op_properties (r
, /*complain=*/false))
13006 return error_mark_node
;
13008 /* When instantiating a constrained member, substitute
13009 into the constraints to create a new constraint. */
13010 if (tree ci
= get_constraints (t
))
13013 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
13014 set_constraints (r
, ci
);
13017 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13018 this in the special friend case mentioned above where
13019 GEN_TMPL is NULL. */
13020 if (gen_tmpl
&& !closure
)
13022 DECL_TEMPLATE_INFO (r
)
13023 = build_template_info (gen_tmpl
, argvec
);
13024 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13027 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13029 /* We instantiated this while substituting into
13030 the type earlier (template/friend54.C). */
13033 /* We're not supposed to instantiate default arguments
13034 until they are called, for a template. But, for a
13037 template <class T> void f ()
13038 { extern void g(int i = T()); }
13040 we should do the substitution when the template is
13041 instantiated. We handle the member function case in
13042 instantiate_class_template since the default arguments
13043 might refer to other members of the class. */
13045 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13046 && !uses_template_parms (argvec
))
13047 tsubst_default_arguments (r
, complain
);
13050 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13052 /* Copy the list of befriending classes. */
13053 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13055 friends
= &TREE_CHAIN (*friends
))
13057 *friends
= copy_node (*friends
);
13058 TREE_VALUE (*friends
)
13059 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13062 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13064 maybe_retrofit_in_chrg (r
);
13065 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13066 return error_mark_node
;
13067 /* If this is an instantiation of a member template, clone it.
13068 If it isn't, that'll be handled by
13069 clone_constructors_and_destructors. */
13070 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13071 clone_function_decl (r
, /*update_methods=*/false);
13073 else if ((complain
& tf_error
) != 0
13074 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13075 && !grok_op_properties (r
, /*complain=*/true))
13076 return error_mark_node
;
13078 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13079 SET_DECL_FRIEND_CONTEXT (r
,
13080 tsubst (DECL_FRIEND_CONTEXT (t
),
13081 args
, complain
, in_decl
));
13083 /* Possibly limit visibility based on template args. */
13084 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13085 if (DECL_VISIBILITY_SPECIFIED (t
))
13087 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13088 DECL_ATTRIBUTES (r
)
13089 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13091 determine_visibility (r
);
13092 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13093 && !processing_template_decl
)
13094 defaulted_late_check (r
);
13096 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13097 args
, complain
, in_decl
);
13101 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13104 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13105 tree lambda_fntype
)
13107 /* We can get here when processing a member function template,
13108 member class template, or template template parameter. */
13109 tree decl
= DECL_TEMPLATE_RESULT (t
);
13115 hashval_t hash
= 0;
13117 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13119 /* Template template parameter is treated here. */
13120 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13121 if (new_type
== error_mark_node
)
13122 r
= error_mark_node
;
13123 /* If we get a real template back, return it. This can happen in
13124 the context of most_specialized_partial_spec. */
13125 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13128 /* The new TEMPLATE_DECL was built in
13129 reduce_template_parm_level. */
13130 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13134 if (!lambda_fntype
)
13136 /* We might already have an instance of this template.
13137 The ARGS are for the surrounding class type, so the
13138 full args contain the tsubst'd args for the context,
13139 plus the innermost args from the template decl. */
13140 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13141 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13142 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13143 /* Because this is a template, the arguments will still be
13144 dependent, even after substitution. If
13145 PROCESSING_TEMPLATE_DECL is not set, the dependency
13146 predicates will short-circuit. */
13147 ++processing_template_decl
;
13148 full_args
= tsubst_template_args (tmpl_args
, args
,
13149 complain
, in_decl
);
13150 --processing_template_decl
;
13151 if (full_args
== error_mark_node
)
13152 return error_mark_node
;
13154 /* If this is a default template template argument,
13155 tsubst might not have changed anything. */
13156 if (full_args
== tmpl_args
)
13159 hash
= hash_tmpl_and_args (t
, full_args
);
13160 spec
= retrieve_specialization (t
, full_args
, hash
);
13161 if (spec
!= NULL_TREE
)
13165 /* Make a new template decl. It will be similar to the
13166 original, but will record the current template arguments.
13167 We also create a new function declaration, which is just
13168 like the old one, but points to this new template, rather
13169 than the old one. */
13171 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13172 DECL_CHAIN (r
) = NULL_TREE
;
13174 // Build new template info linking to the original template decl.
13175 if (!lambda_fntype
)
13177 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13178 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13181 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13183 /* The template parameters for this new template are all the
13184 template parameters for the old template, except the
13185 outermost level of parameters. */
13186 DECL_TEMPLATE_PARMS (r
)
13187 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13190 if (TREE_CODE (decl
) == TYPE_DECL
13191 && !TYPE_DECL_ALIAS_P (decl
))
13194 ++processing_template_decl
;
13195 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13196 --processing_template_decl
;
13197 if (new_type
== error_mark_node
)
13198 return error_mark_node
;
13200 TREE_TYPE (r
) = new_type
;
13201 /* For a partial specialization, we need to keep pointing to
13202 the primary template. */
13203 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13204 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13205 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13206 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13207 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13212 ++processing_template_decl
;
13213 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13214 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13216 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13217 --processing_template_decl
;
13218 if (new_decl
== error_mark_node
)
13219 return error_mark_node
;
13221 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13222 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13223 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13226 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13227 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13231 DECL_TI_TEMPLATE (new_decl
) = r
;
13232 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13236 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13237 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13239 if (PRIMARY_TEMPLATE_P (t
))
13240 DECL_PRIMARY_TEMPLATE (r
) = r
;
13242 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13244 /* Record this non-type partial instantiation. */
13245 register_specialization (r
, t
,
13246 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13252 /* True if FN is the op() for a lambda in an uninstantiated template. */
13255 lambda_fn_in_template_p (tree fn
)
13257 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13259 tree closure
= DECL_CONTEXT (fn
);
13260 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13263 /* We're instantiating a variable from template function TCTX. Return the
13264 corresponding current enclosing scope. This gets complicated because lambda
13265 functions in templates are regenerated rather than instantiated, but generic
13266 lambda functions are subsequently instantiated. */
13269 enclosing_instantiation_of (tree otctx
)
13272 tree fn
= current_function_decl
;
13273 int lambda_count
= 0;
13275 for (; tctx
&& lambda_fn_in_template_p (tctx
);
13276 tctx
= decl_function_context (tctx
))
13278 for (; fn
; fn
= decl_function_context (fn
))
13281 int flambda_count
= 0;
13282 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
13283 fn
= decl_function_context (fn
))
13285 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13286 ? most_general_template (fn
) != most_general_template (tctx
)
13289 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13290 || DECL_CONV_FN_P (ofn
));
13293 gcc_unreachable ();
13296 /* Substitute the ARGS into the T, which is a _DECL. Return the
13297 result of the substitution. Issue error and warning messages under
13298 control of COMPLAIN. */
13301 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13303 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13304 location_t saved_loc
;
13305 tree r
= NULL_TREE
;
13307 hashval_t hash
= 0;
13309 /* Set the filename and linenumber to improve error-reporting. */
13310 saved_loc
= input_location
;
13311 input_location
= DECL_SOURCE_LOCATION (t
);
13313 switch (TREE_CODE (t
))
13315 case TEMPLATE_DECL
:
13316 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13319 case FUNCTION_DECL
:
13320 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13325 tree type
= NULL_TREE
;
13327 tree expanded_types
= NULL_TREE
;
13328 tree prev_r
= NULL_TREE
;
13329 tree first_r
= NULL_TREE
;
13331 if (DECL_PACK_P (t
))
13333 /* If there is a local specialization that isn't a
13334 parameter pack, it means that we're doing a "simple"
13335 substitution from inside tsubst_pack_expansion. Just
13336 return the local specialization (which will be a single
13338 tree spec
= retrieve_local_specialization (t
);
13340 && TREE_CODE (spec
) == PARM_DECL
13341 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13344 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13345 the parameters in this function parameter pack. */
13346 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13347 complain
, in_decl
);
13348 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13350 len
= TREE_VEC_LENGTH (expanded_types
);
13352 /* Zero-length parameter packs are boring. Just substitute
13355 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13360 /* All we did was update the type. Make a note of that. */
13361 type
= expanded_types
;
13362 expanded_types
= NULL_TREE
;
13366 /* Loop through all of the parameters we'll build. When T is
13367 a function parameter pack, LEN is the number of expanded
13368 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13370 for (i
= 0; i
< len
; ++i
)
13374 if (DECL_TEMPLATE_PARM_P (t
))
13375 SET_DECL_TEMPLATE_PARM_P (r
);
13377 if (expanded_types
)
13378 /* We're on the Ith parameter of the function parameter
13381 /* Get the Ith type. */
13382 type
= TREE_VEC_ELT (expanded_types
, i
);
13384 /* Rename the parameter to include the index. */
13386 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13389 /* We're dealing with a normal parameter. */
13390 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13392 type
= type_decays_to (type
);
13393 TREE_TYPE (r
) = type
;
13394 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13396 if (DECL_INITIAL (r
))
13398 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13399 DECL_INITIAL (r
) = TREE_TYPE (r
);
13401 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13402 complain
, in_decl
);
13405 DECL_CONTEXT (r
) = NULL_TREE
;
13407 if (!DECL_TEMPLATE_PARM_P (r
))
13408 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13410 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13411 args
, complain
, in_decl
);
13413 /* Keep track of the first new parameter we
13414 generate. That's what will be returned to the
13419 /* Build a proper chain of parameters when substituting
13420 into a function parameter pack. */
13422 DECL_CHAIN (prev_r
) = r
;
13425 /* If cp_unevaluated_operand is set, we're just looking for a
13426 single dummy parameter, so don't keep going. */
13427 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13428 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13429 complain
, DECL_CHAIN (t
));
13431 /* FIRST_R contains the start of the chain we've built. */
13438 tree type
= NULL_TREE
;
13439 tree vec
= NULL_TREE
;
13440 tree expanded_types
= NULL_TREE
;
13443 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13445 /* This field is a lambda capture pack. Return a TREE_VEC of
13446 the expanded fields to instantiate_class_template_1. */
13447 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13448 complain
, in_decl
);
13449 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13451 len
= TREE_VEC_LENGTH (expanded_types
);
13452 vec
= make_tree_vec (len
);
13456 /* All we did was update the type. Make a note of that. */
13457 type
= expanded_types
;
13458 expanded_types
= NULL_TREE
;
13462 for (int i
= 0; i
< len
; ++i
)
13465 if (expanded_types
)
13467 type
= TREE_VEC_ELT (expanded_types
, i
);
13469 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13472 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13474 if (type
== error_mark_node
)
13475 RETURN (error_mark_node
);
13476 TREE_TYPE (r
) = type
;
13477 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13479 if (DECL_C_BIT_FIELD (r
))
13480 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13482 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13483 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13485 /*integral_constant_expression_p=*/true);
13486 if (DECL_INITIAL (t
))
13488 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13489 NSDMI in perform_member_init. Still set DECL_INITIAL
13490 so that we know there is one. */
13491 DECL_INITIAL (r
) = void_node
;
13492 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13493 retrofit_lang_decl (r
);
13494 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13496 /* We don't have to set DECL_CONTEXT here; it is set by
13497 finish_member_declaration. */
13498 DECL_CHAIN (r
) = NULL_TREE
;
13500 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13501 args
, complain
, in_decl
);
13504 TREE_VEC_ELT (vec
, i
) = r
;
13513 /* We reach here only for member using decls. We also need to check
13514 uses_template_parms because DECL_DEPENDENT_P is not set for a
13515 using-declaration that designates a member of the current
13516 instantiation (c++/53549). */
13517 if (DECL_DEPENDENT_P (t
)
13518 || uses_template_parms (USING_DECL_SCOPE (t
)))
13520 tree scope
= USING_DECL_SCOPE (t
);
13521 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13522 if (PACK_EXPANSION_P (scope
))
13524 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13525 int len
= TREE_VEC_LENGTH (vec
);
13526 r
= make_tree_vec (len
);
13527 for (int i
= 0; i
< len
; ++i
)
13529 tree escope
= TREE_VEC_ELT (vec
, i
);
13530 tree elt
= do_class_using_decl (escope
, name
);
13533 r
= error_mark_node
;
13538 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13539 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13541 TREE_VEC_ELT (r
, i
) = elt
;
13546 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13547 complain
, in_decl
);
13548 r
= do_class_using_decl (inst_scope
, name
);
13550 r
= error_mark_node
;
13553 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13554 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13561 DECL_CHAIN (r
) = NULL_TREE
;
13568 tree argvec
= NULL_TREE
;
13569 tree gen_tmpl
= NULL_TREE
;
13571 tree tmpl
= NULL_TREE
;
13573 tree type
= NULL_TREE
;
13576 if (TREE_TYPE (t
) == error_mark_node
)
13577 RETURN (error_mark_node
);
13579 if (TREE_CODE (t
) == TYPE_DECL
13580 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13582 /* If this is the canonical decl, we don't have to
13583 mess with instantiations, and often we can't (for
13584 typename, template type parms and such). Note that
13585 TYPE_NAME is not correct for the above test if
13586 we've copied the type for a typedef. */
13587 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13588 if (type
== error_mark_node
)
13589 RETURN (error_mark_node
);
13590 r
= TYPE_NAME (type
);
13594 /* Check to see if we already have the specialization we
13597 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13599 /* T is a static data member or namespace-scope entity.
13600 We have to substitute into namespace-scope variables
13601 (not just variable templates) because of cases like:
13603 template <class T> void f() { extern T t; }
13605 where the entity referenced is not known until
13606 instantiation time. */
13608 ctx
= DECL_CONTEXT (t
);
13609 if (DECL_CLASS_SCOPE_P (t
))
13611 ctx
= tsubst_aggr_type (ctx
, args
,
13613 in_decl
, /*entering_scope=*/1);
13614 /* If CTX is unchanged, then T is in fact the
13615 specialization we want. That situation occurs when
13616 referencing a static data member within in its own
13617 class. We can use pointer equality, rather than
13618 same_type_p, because DECL_CONTEXT is always
13620 if (ctx
== DECL_CONTEXT (t
)
13621 /* ... unless T is a member template; in which
13622 case our caller can be willing to create a
13623 specialization of that template represented
13625 && !(DECL_TI_TEMPLATE (t
)
13626 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13632 tmpl
= DECL_TI_TEMPLATE (t
);
13633 gen_tmpl
= most_general_template (tmpl
);
13634 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13635 if (argvec
!= error_mark_node
)
13636 argvec
= (coerce_innermost_template_parms
13637 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13638 argvec
, t
, complain
,
13639 /*all*/true, /*defarg*/true));
13640 if (argvec
== error_mark_node
)
13641 RETURN (error_mark_node
);
13642 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13643 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13648 /* A local variable. */
13650 /* Subsequent calls to pushdecl will fill this in. */
13652 /* Unless this is a reference to a static variable from an
13653 enclosing function, in which case we need to fill it in now. */
13654 if (TREE_STATIC (t
))
13656 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13657 if (fn
!= current_function_decl
)
13660 spec
= retrieve_local_specialization (t
);
13662 /* If we already have the specialization we need, there is
13663 nothing more to do. */
13670 /* Create a new node for the specialization we need. */
13671 if (type
== NULL_TREE
)
13673 if (is_typedef_decl (t
))
13674 type
= DECL_ORIGINAL_TYPE (t
);
13676 type
= TREE_TYPE (t
);
13678 && VAR_HAD_UNKNOWN_BOUND (t
)
13679 && type
!= error_mark_node
)
13680 type
= strip_array_domain (type
);
13681 tree sub_args
= args
;
13682 if (tree auto_node
= type_uses_auto (type
))
13684 /* Mask off any template args past the variable's context so we
13685 don't replace the auto with an unrelated argument. */
13686 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13687 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13689 /* This should never happen with the new lambda instantiation
13690 model, but keep the handling just in case. */
13691 gcc_assert (!CHECKING_P
),
13692 sub_args
= strip_innermost_template_args (args
, extra
);
13694 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13695 /* Substituting the type might have recursively instantiated this
13696 same alias (c++/86171). */
13697 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
13698 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
13707 DECL_INITIALIZED_P (r
) = 0;
13708 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13709 if (type
== error_mark_node
)
13710 RETURN (error_mark_node
);
13711 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13713 /* It may seem that this case cannot occur, since:
13718 declares a function, not a variable. However:
13721 template <typename T> void g() { T t; }
13722 template void g<f>();
13724 is an attempt to declare a variable with function
13726 error ("variable %qD has function type",
13727 /* R is not yet sufficiently initialized, so we
13728 just use its name. */
13730 RETURN (error_mark_node
);
13732 type
= complete_type (type
);
13733 /* Wait until cp_finish_decl to set this again, to handle
13734 circular dependency (template/instantiate6.C). */
13735 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13736 type
= check_var_type (DECL_NAME (r
), type
);
13738 if (DECL_HAS_VALUE_EXPR_P (t
))
13740 tree ve
= DECL_VALUE_EXPR (t
);
13741 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13742 /*constant_expression_p=*/false);
13743 if (REFERENCE_REF_P (ve
))
13745 gcc_assert (TYPE_REF_P (type
));
13746 ve
= TREE_OPERAND (ve
, 0);
13748 SET_DECL_VALUE_EXPR (r
, ve
);
13750 if (CP_DECL_THREAD_LOCAL_P (r
)
13751 && !processing_template_decl
)
13752 set_decl_tls_model (r
, decl_default_tls_model (r
));
13754 else if (DECL_SELF_REFERENCE_P (t
))
13755 SET_DECL_SELF_REFERENCE_P (r
);
13756 TREE_TYPE (r
) = type
;
13757 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13758 DECL_CONTEXT (r
) = ctx
;
13759 /* Clear out the mangled name and RTL for the instantiation. */
13760 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13761 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13762 SET_DECL_RTL (r
, NULL
);
13763 /* The initializer must not be expanded until it is required;
13764 see [temp.inst]. */
13765 DECL_INITIAL (r
) = NULL_TREE
;
13766 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13769 if (DECL_LANG_SPECIFIC (r
))
13770 SET_DECL_DEPENDENT_INIT_P (r
, false);
13772 SET_DECL_MODE (r
, VOIDmode
);
13774 /* Possibly limit visibility based on template args. */
13775 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13776 if (DECL_VISIBILITY_SPECIFIED (t
))
13778 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13779 DECL_ATTRIBUTES (r
)
13780 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13782 determine_visibility (r
);
13787 /* A static data member declaration is always marked
13788 external when it is declared in-class, even if an
13789 initializer is present. We mimic the non-template
13790 processing here. */
13791 DECL_EXTERNAL (r
) = 1;
13792 if (DECL_NAMESPACE_SCOPE_P (t
))
13793 DECL_NOT_REALLY_EXTERN (r
) = 1;
13795 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13796 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13797 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13801 if (DECL_LANG_SPECIFIC (r
))
13802 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13803 if (!cp_unevaluated_operand
)
13804 register_local_specialization (r
, t
);
13807 DECL_CHAIN (r
) = NULL_TREE
;
13809 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13811 args
, complain
, in_decl
);
13813 /* Preserve a typedef that names a type. */
13814 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13816 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13817 set_underlying_type (r
);
13818 if (TYPE_DECL_ALIAS_P (r
))
13819 /* An alias template specialization can be dependent
13820 even if its underlying type is not. */
13821 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13824 layout_decl (r
, 0);
13829 gcc_unreachable ();
13834 /* Restore the file and line information. */
13835 input_location
= saved_loc
;
13840 /* Substitute into the ARG_TYPES of a function type.
13841 If END is a TREE_CHAIN, leave it and any following types
13845 tsubst_arg_types (tree arg_types
,
13848 tsubst_flags_t complain
,
13851 tree remaining_arg_types
;
13852 tree type
= NULL_TREE
;
13854 tree expanded_args
= NULL_TREE
;
13857 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13860 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13861 args
, end
, complain
, in_decl
);
13862 if (remaining_arg_types
== error_mark_node
)
13863 return error_mark_node
;
13865 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13867 /* For a pack expansion, perform substitution on the
13868 entire expression. Later on, we'll handle the arguments
13870 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13871 args
, complain
, in_decl
);
13873 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13874 /* So that we'll spin through the parameters, one by one. */
13875 i
= TREE_VEC_LENGTH (expanded_args
);
13878 /* We only partially substituted into the parameter
13879 pack. Our type is TYPE_PACK_EXPANSION. */
13880 type
= expanded_args
;
13881 expanded_args
= NULL_TREE
;
13889 type
= TREE_VEC_ELT (expanded_args
, i
);
13891 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13893 if (type
== error_mark_node
)
13894 return error_mark_node
;
13895 if (VOID_TYPE_P (type
))
13897 if (complain
& tf_error
)
13899 error ("invalid parameter type %qT", type
);
13901 error ("in declaration %q+D", in_decl
);
13903 return error_mark_node
;
13906 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13907 return error_mark_node
;
13909 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13910 top-level qualifiers as required. */
13911 type
= cv_unqualified (type_decays_to (type
));
13913 /* We do not substitute into default arguments here. The standard
13914 mandates that they be instantiated only when needed, which is
13915 done in build_over_call. */
13916 default_arg
= TREE_PURPOSE (arg_types
);
13918 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13919 since the new op() won't have any associated template arguments for us
13920 to refer to later. */
13921 if (lambda_fn_in_template_p (in_decl
))
13922 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13923 false/*fn*/, false/*constexpr*/);
13925 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13927 /* We've instantiated a template before its default arguments
13928 have been parsed. This can happen for a nested template
13929 class, and is not an error unless we require the default
13930 argument in a call of this function. */
13931 remaining_arg_types
=
13932 tree_cons (default_arg
, type
, remaining_arg_types
);
13933 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13936 remaining_arg_types
=
13937 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13940 return remaining_arg_types
;
13943 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13944 *not* handle the exception-specification for FNTYPE, because the
13945 initial substitution of explicitly provided template parameters
13946 during argument deduction forbids substitution into the
13947 exception-specification:
13951 All references in the function type of the function template to the
13952 corresponding template parameters are replaced by the specified tem-
13953 plate argument values. If a substitution in a template parameter or
13954 in the function type of the function template results in an invalid
13955 type, type deduction fails. [Note: The equivalent substitution in
13956 exception specifications is done only when the function is instanti-
13957 ated, at which point a program is ill-formed if the substitution
13958 results in an invalid type.] */
13961 tsubst_function_type (tree t
,
13963 tsubst_flags_t complain
,
13967 tree arg_types
= NULL_TREE
;
13970 /* The TYPE_CONTEXT is not used for function/method types. */
13971 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13973 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13975 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13977 if (late_return_type_p
)
13979 /* Substitute the argument types. */
13980 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13981 complain
, in_decl
);
13982 if (arg_types
== error_mark_node
)
13983 return error_mark_node
;
13985 tree save_ccp
= current_class_ptr
;
13986 tree save_ccr
= current_class_ref
;
13987 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13988 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13989 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13992 /* DR 1207: 'this' is in scope in the trailing return type. */
13993 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13996 /* Substitute the return type. */
13997 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14001 current_class_ptr
= save_ccp
;
14002 current_class_ref
= save_ccr
;
14006 /* Substitute the return type. */
14007 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14009 if (return_type
== error_mark_node
)
14010 return error_mark_node
;
14011 /* DR 486 clarifies that creation of a function type with an
14012 invalid return type is a deduction failure. */
14013 if (TREE_CODE (return_type
) == ARRAY_TYPE
14014 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14016 if (complain
& tf_error
)
14018 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14019 error ("function returning an array");
14021 error ("function returning a function");
14023 return error_mark_node
;
14026 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14027 return error_mark_node
;
14029 if (!late_return_type_p
)
14031 /* Substitute the argument types. */
14032 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14033 complain
, in_decl
);
14034 if (arg_types
== error_mark_node
)
14035 return error_mark_node
;
14038 /* Construct a new type node and return it. */
14039 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14041 fntype
= build_function_type (return_type
, arg_types
);
14042 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14046 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14047 /* Don't pick up extra function qualifiers from the basetype. */
14048 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14049 if (! MAYBE_CLASS_TYPE_P (r
))
14053 Type deduction may fail for any of the following
14056 -- Attempting to create "pointer to member of T" when T
14057 is not a class type. */
14058 if (complain
& tf_error
)
14059 error ("creating pointer to member function of non-class type %qT",
14061 return error_mark_node
;
14064 fntype
= build_method_type_directly (r
, return_type
,
14065 TREE_CHAIN (arg_types
));
14067 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14069 /* See comment above. */
14070 tree raises
= NULL_TREE
;
14071 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14072 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14077 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14078 ARGS into that specification, and return the substituted
14079 specification. If there is no specification, return NULL_TREE. */
14082 tsubst_exception_specification (tree fntype
,
14084 tsubst_flags_t complain
,
14091 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14092 new_specs
= NULL_TREE
;
14093 if (specs
&& TREE_PURPOSE (specs
))
14095 /* A noexcept-specifier. */
14096 tree expr
= TREE_PURPOSE (specs
);
14097 if (TREE_CODE (expr
) == INTEGER_CST
)
14101 /* Defer instantiation of noexcept-specifiers to avoid
14102 excessive instantiations (c++/49107). */
14103 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14104 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14106 /* We already partially instantiated this member template,
14107 so combine the new args with the old. */
14108 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14109 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14110 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14111 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14115 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14116 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14120 new_specs
= tsubst_copy_and_build
14121 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14122 /*integral_constant_expression_p=*/true);
14123 new_specs
= build_noexcept_spec (new_specs
, complain
);
14127 if (! TREE_VALUE (specs
))
14134 tree expanded_specs
= NULL_TREE
;
14136 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14138 /* Expand the pack expansion type. */
14139 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14143 if (expanded_specs
== error_mark_node
)
14144 return error_mark_node
;
14145 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14146 len
= TREE_VEC_LENGTH (expanded_specs
);
14149 /* We're substituting into a member template, so
14150 we got a TYPE_PACK_EXPANSION back. Add that
14151 expansion and move on. */
14152 gcc_assert (TREE_CODE (expanded_specs
)
14153 == TYPE_PACK_EXPANSION
);
14154 new_specs
= add_exception_specifier (new_specs
,
14157 specs
= TREE_CHAIN (specs
);
14162 for (i
= 0; i
< len
; ++i
)
14164 if (expanded_specs
)
14165 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14167 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14168 if (spec
== error_mark_node
)
14170 new_specs
= add_exception_specifier (new_specs
, spec
,
14174 specs
= TREE_CHAIN (specs
);
14180 /* Take the tree structure T and replace template parameters used
14181 therein with the argument vector ARGS. IN_DECL is an associated
14182 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14183 Issue error and warning messages under control of COMPLAIN. Note
14184 that we must be relatively non-tolerant of extensions here, in
14185 order to preserve conformance; if we allow substitutions that
14186 should not be allowed, we may allow argument deductions that should
14187 not succeed, and therefore report ambiguous overload situations
14188 where there are none. In theory, we could allow the substitution,
14189 but indicate that it should have failed, and allow our caller to
14190 make sure that the right thing happens, but we don't try to do this
14193 This function is used for dealing with types, decls and the like;
14194 for expressions, use tsubst_expr or tsubst_copy. */
14197 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14199 enum tree_code code
;
14200 tree type
, r
= NULL_TREE
;
14202 if (t
== NULL_TREE
|| t
== error_mark_node
14203 || t
== integer_type_node
14204 || t
== void_type_node
14205 || t
== char_type_node
14206 || t
== unknown_type_node
14207 || TREE_CODE (t
) == NAMESPACE_DECL
14208 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14212 return tsubst_decl (t
, args
, complain
);
14214 if (args
== NULL_TREE
)
14217 code
= TREE_CODE (t
);
14219 if (code
== IDENTIFIER_NODE
)
14220 type
= IDENTIFIER_TYPE_VALUE (t
);
14222 type
= TREE_TYPE (t
);
14224 gcc_assert (type
!= unknown_type_node
);
14226 /* Reuse typedefs. We need to do this to handle dependent attributes,
14227 such as attribute aligned. */
14229 && typedef_variant_p (t
))
14231 tree decl
= TYPE_NAME (t
);
14233 if (alias_template_specialization_p (t
))
14235 /* DECL represents an alias template and we want to
14237 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14238 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14239 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14241 else if (DECL_CLASS_SCOPE_P (decl
)
14242 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14243 && uses_template_parms (DECL_CONTEXT (decl
)))
14245 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14246 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14247 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14249 else if (DECL_FUNCTION_SCOPE_P (decl
)
14250 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14251 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14252 r
= retrieve_local_specialization (decl
);
14254 /* The typedef is from a non-template context. */
14260 r
= cp_build_qualified_type_real
14261 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14262 complain
| tf_ignore_bad_quals
);
14267 /* We don't have an instantiation yet, so drop the typedef. */
14268 int quals
= cp_type_quals (t
);
14269 t
= DECL_ORIGINAL_TYPE (decl
);
14270 t
= cp_build_qualified_type_real (t
, quals
,
14271 complain
| tf_ignore_bad_quals
);
14275 bool fndecl_type
= (complain
& tf_fndecl_type
);
14276 complain
&= ~tf_fndecl_type
;
14279 && code
!= TYPENAME_TYPE
14280 && code
!= TEMPLATE_TYPE_PARM
14281 && code
!= TEMPLATE_PARM_INDEX
14282 && code
!= IDENTIFIER_NODE
14283 && code
!= FUNCTION_TYPE
14284 && code
!= METHOD_TYPE
)
14285 type
= tsubst (type
, args
, complain
, in_decl
);
14286 if (type
== error_mark_node
)
14287 return error_mark_node
;
14293 case ENUMERAL_TYPE
:
14294 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14295 /*entering_scope=*/0);
14298 case IDENTIFIER_NODE
:
14309 if (t
== integer_type_node
)
14312 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14313 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14317 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14319 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14320 /*integral_constant_expression_p=*/false);
14322 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14324 if (TREE_CODE (max
) == NOP_EXPR
14325 && TREE_SIDE_EFFECTS (omax
)
14326 && !TREE_TYPE (max
))
14327 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14329 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14330 with TREE_SIDE_EFFECTS that indicates this is not an integral
14331 constant expression. */
14332 if (processing_template_decl
14333 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14335 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14336 TREE_SIDE_EFFECTS (max
) = 1;
14339 return compute_array_index_type (NULL_TREE
, max
, complain
);
14342 case TEMPLATE_TYPE_PARM
:
14343 case TEMPLATE_TEMPLATE_PARM
:
14344 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14345 case TEMPLATE_PARM_INDEX
:
14350 tree arg
= NULL_TREE
;
14352 /* Early in template argument deduction substitution, we don't
14353 want to reduce the level of 'auto', or it will be confused
14354 with a normal template parm in subsequent deduction. */
14355 if (is_auto (t
) && (complain
& tf_partial
))
14360 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14361 template_parm_level_and_index (t
, &level
, &idx
);
14363 levels
= TMPL_ARGS_DEPTH (args
);
14364 if (level
<= levels
14365 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14367 arg
= TMPL_ARG (args
, level
, idx
);
14369 /* See through ARGUMENT_PACK_SELECT arguments. */
14370 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14371 arg
= argument_pack_select_arg (arg
);
14374 if (arg
== error_mark_node
)
14375 return error_mark_node
;
14376 else if (arg
!= NULL_TREE
)
14378 if (ARGUMENT_PACK_P (arg
))
14379 /* If ARG is an argument pack, we don't actually want to
14380 perform a substitution here, because substitutions
14381 for argument packs are only done
14382 element-by-element. We can get to this point when
14383 substituting the type of a non-type template
14384 parameter pack, when that type actually contains
14385 template parameter packs from an outer template, e.g.,
14387 template<typename... Types> struct A {
14388 template<Types... Values> struct B { };
14392 if (code
== TEMPLATE_TYPE_PARM
)
14395 gcc_assert (TYPE_P (arg
));
14397 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14399 return cp_build_qualified_type_real
14400 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14402 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14404 /* We are processing a type constructed from a
14405 template template parameter. */
14406 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14407 args
, complain
, in_decl
);
14408 if (argvec
== error_mark_node
)
14409 return error_mark_node
;
14411 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14412 || TREE_CODE (arg
) == TEMPLATE_DECL
14413 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14415 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14416 /* Consider this code:
14418 template <template <class> class Template>
14420 template <class Arg> using Bind = Template<Arg>;
14423 template <template <class> class Template, class Arg>
14424 using Instantiate = Template<Arg>; //#0
14426 template <template <class> class Template,
14429 Instantiate<Internal<Template>::template Bind,
14432 When #1 is parsed, the
14433 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14434 parameter `Template' in #0 matches the
14435 UNBOUND_CLASS_TEMPLATE representing the argument
14436 `Internal<Template>::template Bind'; We then want
14437 to assemble the type `Bind<Argument>' that can't
14438 be fully created right now, because
14439 `Internal<Template>' not being complete, the Bind
14440 template cannot be looked up in that context. So
14441 we need to "store" `Bind<Argument>' for later
14442 when the context of Bind becomes complete. Let's
14443 store that in a TYPENAME_TYPE. */
14444 return make_typename_type (TYPE_CONTEXT (arg
),
14445 build_nt (TEMPLATE_ID_EXPR
,
14446 TYPE_IDENTIFIER (arg
),
14451 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14452 are resolving nested-types in the signature of a
14453 member function templates. Otherwise ARG is a
14454 TEMPLATE_DECL and is the real template to be
14456 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14457 arg
= TYPE_NAME (arg
);
14459 r
= lookup_template_class (arg
,
14461 DECL_CONTEXT (arg
),
14462 /*entering_scope=*/0,
14464 return cp_build_qualified_type_real
14465 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14467 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14470 /* TEMPLATE_PARM_INDEX. */
14471 return convert_from_reference (unshare_expr (arg
));
14475 /* This can happen during the attempted tsubst'ing in
14476 unify. This means that we don't yet have any information
14477 about the template parameter in question. */
14480 /* If we get here, we must have been looking at a parm for a
14481 more deeply nested template. Make a new version of this
14482 template parameter, but with a lower level. */
14485 case TEMPLATE_TYPE_PARM
:
14486 case TEMPLATE_TEMPLATE_PARM
:
14487 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14488 if (cp_type_quals (t
))
14490 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14491 r
= cp_build_qualified_type_real
14492 (r
, cp_type_quals (t
),
14493 complain
| (code
== TEMPLATE_TYPE_PARM
14494 ? tf_ignore_bad_quals
: 0));
14496 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14497 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14498 && (r
= (TEMPLATE_PARM_DESCENDANTS
14499 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14500 && (r
= TREE_TYPE (r
))
14501 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14502 /* Break infinite recursion when substituting the constraints
14503 of a constrained placeholder. */;
14504 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14505 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14506 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
14507 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
14508 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
14509 && (TEMPLATE_PARM_LEVEL (r
)
14510 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
14511 /* Cache the simple case of lowering a type parameter. */
14516 TEMPLATE_TYPE_PARM_INDEX (r
)
14517 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14518 r
, levels
, args
, complain
);
14519 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14520 TYPE_MAIN_VARIANT (r
) = r
;
14521 TYPE_POINTER_TO (r
) = NULL_TREE
;
14522 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14524 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14526 /* Propagate constraints on placeholders. */
14527 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14528 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14529 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14530 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14532 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14533 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14537 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14538 /* We have reduced the level of the template
14539 template parameter, but not the levels of its
14540 template parameters, so canonical_type_parameter
14541 will not be able to find the canonical template
14542 template parameter for this level. Thus, we
14543 require structural equality checking to compare
14544 TEMPLATE_TEMPLATE_PARMs. */
14545 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14546 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14547 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14549 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14551 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14553 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14554 /* We might need to substitute into the types of non-type
14555 template parameters. */
14556 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14557 complain
, in_decl
);
14558 if (tmpl
== error_mark_node
)
14559 return error_mark_node
;
14560 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14561 complain
, in_decl
);
14562 if (argvec
== error_mark_node
)
14563 return error_mark_node
;
14565 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14566 = build_template_info (tmpl
, argvec
);
14571 case TEMPLATE_PARM_INDEX
:
14572 /* OK, now substitute the type of the non-type parameter. We
14573 couldn't do it earlier because it might be an auto parameter,
14574 and we wouldn't need to if we had an argument. */
14575 type
= tsubst (type
, args
, complain
, in_decl
);
14576 if (type
== error_mark_node
)
14577 return error_mark_node
;
14578 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14582 gcc_unreachable ();
14590 tree purpose
, value
, chain
;
14592 if (t
== void_list_node
)
14595 purpose
= TREE_PURPOSE (t
);
14598 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14599 if (purpose
== error_mark_node
)
14600 return error_mark_node
;
14602 value
= TREE_VALUE (t
);
14605 value
= tsubst (value
, args
, complain
, in_decl
);
14606 if (value
== error_mark_node
)
14607 return error_mark_node
;
14609 chain
= TREE_CHAIN (t
);
14610 if (chain
&& chain
!= void_type_node
)
14612 chain
= tsubst (chain
, args
, complain
, in_decl
);
14613 if (chain
== error_mark_node
)
14614 return error_mark_node
;
14616 if (purpose
== TREE_PURPOSE (t
)
14617 && value
== TREE_VALUE (t
)
14618 && chain
== TREE_CHAIN (t
))
14620 return hash_tree_cons (purpose
, value
, chain
);
14624 /* We should never be tsubsting a binfo. */
14625 gcc_unreachable ();
14628 /* A vector of template arguments. */
14629 gcc_assert (!type
);
14630 return tsubst_template_args (t
, args
, complain
, in_decl
);
14633 case REFERENCE_TYPE
:
14635 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14640 Type deduction may fail for any of the following
14643 -- Attempting to create a pointer to reference type.
14644 -- Attempting to create a reference to a reference type or
14645 a reference to void.
14647 Core issue 106 says that creating a reference to a reference
14648 during instantiation is no longer a cause for failure. We
14649 only enforce this check in strict C++98 mode. */
14650 if ((TYPE_REF_P (type
)
14651 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14652 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14654 static location_t last_loc
;
14656 /* We keep track of the last time we issued this error
14657 message to avoid spewing a ton of messages during a
14658 single bad template instantiation. */
14659 if (complain
& tf_error
14660 && last_loc
!= input_location
)
14662 if (VOID_TYPE_P (type
))
14663 error ("forming reference to void");
14664 else if (code
== POINTER_TYPE
)
14665 error ("forming pointer to reference type %qT", type
);
14667 error ("forming reference to reference type %qT", type
);
14668 last_loc
= input_location
;
14671 return error_mark_node
;
14673 else if (TREE_CODE (type
) == FUNCTION_TYPE
14674 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14675 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14677 if (complain
& tf_error
)
14679 if (code
== POINTER_TYPE
)
14680 error ("forming pointer to qualified function type %qT",
14683 error ("forming reference to qualified function type %qT",
14686 return error_mark_node
;
14688 else if (code
== POINTER_TYPE
)
14690 r
= build_pointer_type (type
);
14691 if (TREE_CODE (type
) == METHOD_TYPE
)
14692 r
= build_ptrmemfunc_type (r
);
14694 else if (TYPE_REF_P (type
))
14695 /* In C++0x, during template argument substitution, when there is an
14696 attempt to create a reference to a reference type, reference
14697 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14699 "If a template-argument for a template-parameter T names a type
14700 that is a reference to a type A, an attempt to create the type
14701 'lvalue reference to cv T' creates the type 'lvalue reference to
14702 A,' while an attempt to create the type type rvalue reference to
14703 cv T' creates the type T"
14705 r
= cp_build_reference_type
14707 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14709 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14710 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14712 if (r
!= error_mark_node
)
14713 /* Will this ever be needed for TYPE_..._TO values? */
14720 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14721 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14725 Type deduction may fail for any of the following
14728 -- Attempting to create "pointer to member of T" when T
14729 is not a class type. */
14730 if (complain
& tf_error
)
14731 error ("creating pointer to member of non-class type %qT", r
);
14732 return error_mark_node
;
14734 if (TYPE_REF_P (type
))
14736 if (complain
& tf_error
)
14737 error ("creating pointer to member reference type %qT", type
);
14738 return error_mark_node
;
14740 if (VOID_TYPE_P (type
))
14742 if (complain
& tf_error
)
14743 error ("creating pointer to member of type void");
14744 return error_mark_node
;
14746 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14747 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14749 /* The type of the implicit object parameter gets its
14750 cv-qualifiers from the FUNCTION_TYPE. */
14753 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14754 type_memfn_rqual (type
));
14755 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14756 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14760 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14764 case FUNCTION_TYPE
:
14769 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14770 if (fntype
== error_mark_node
)
14771 return error_mark_node
;
14773 /* Substitute the exception specification. */
14774 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14775 /*defer_ok*/fndecl_type
);
14776 if (specs
== error_mark_node
)
14777 return error_mark_node
;
14779 fntype
= build_exception_variant (fntype
, specs
);
14784 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14785 if (domain
== error_mark_node
)
14786 return error_mark_node
;
14788 /* As an optimization, we avoid regenerating the array type if
14789 it will obviously be the same as T. */
14790 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14793 /* These checks should match the ones in create_array_type_for_decl.
14797 The deduction may fail for any of the following reasons:
14799 -- Attempting to create an array with an element type that
14800 is void, a function type, or a reference type, or [DR337]
14801 an abstract class type. */
14802 if (VOID_TYPE_P (type
)
14803 || TREE_CODE (type
) == FUNCTION_TYPE
14804 || (TREE_CODE (type
) == ARRAY_TYPE
14805 && TYPE_DOMAIN (type
) == NULL_TREE
)
14806 || TYPE_REF_P (type
))
14808 if (complain
& tf_error
)
14809 error ("creating array of %qT", type
);
14810 return error_mark_node
;
14813 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14814 return error_mark_node
;
14816 r
= build_cplus_array_type (type
, domain
);
14818 if (!valid_array_size_p (input_location
, r
, in_decl
,
14819 (complain
& tf_error
)))
14820 return error_mark_node
;
14822 if (TYPE_USER_ALIGN (t
))
14824 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14825 TYPE_USER_ALIGN (r
) = 1;
14831 case TYPENAME_TYPE
:
14833 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14834 in_decl
, /*entering_scope=*/1);
14835 if (ctx
== error_mark_node
)
14836 return error_mark_node
;
14838 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14839 complain
, in_decl
);
14840 if (f
== error_mark_node
)
14841 return error_mark_node
;
14843 if (!MAYBE_CLASS_TYPE_P (ctx
))
14845 if (complain
& tf_error
)
14846 error ("%qT is not a class, struct, or union type", ctx
);
14847 return error_mark_node
;
14849 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14851 /* Normally, make_typename_type does not require that the CTX
14852 have complete type in order to allow things like:
14854 template <class T> struct S { typename S<T>::X Y; };
14856 But, such constructs have already been resolved by this
14857 point, so here CTX really should have complete type, unless
14858 it's a partial instantiation. */
14859 ctx
= complete_type (ctx
);
14860 if (!COMPLETE_TYPE_P (ctx
))
14862 if (complain
& tf_error
)
14863 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14864 return error_mark_node
;
14868 f
= make_typename_type (ctx
, f
, typename_type
,
14869 complain
| tf_keep_type_decl
);
14870 if (f
== error_mark_node
)
14872 if (TREE_CODE (f
) == TYPE_DECL
)
14874 complain
|= tf_ignore_bad_quals
;
14878 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14880 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14882 if (complain
& tf_error
)
14883 error ("%qT resolves to %qT, which is not an enumeration type",
14886 return error_mark_node
;
14888 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14890 if (complain
& tf_error
)
14891 error ("%qT resolves to %qT, which is is not a class type",
14894 return error_mark_node
;
14898 return cp_build_qualified_type_real
14899 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14902 case UNBOUND_CLASS_TEMPLATE
:
14904 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14905 in_decl
, /*entering_scope=*/1);
14906 tree name
= TYPE_IDENTIFIER (t
);
14907 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14909 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14910 return error_mark_node
;
14913 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14914 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14921 ++cp_unevaluated_operand
;
14922 ++c_inhibit_evaluation_warnings
;
14924 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14926 /*integral_constant_expression_p=*/false);
14928 --cp_unevaluated_operand
;
14929 --c_inhibit_evaluation_warnings
;
14931 type
= finish_typeof (type
);
14932 return cp_build_qualified_type_real (type
,
14934 | cp_type_quals (type
),
14938 case DECLTYPE_TYPE
:
14942 ++cp_unevaluated_operand
;
14943 ++c_inhibit_evaluation_warnings
;
14945 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14946 complain
|tf_decltype
, in_decl
,
14947 /*function_p*/false,
14948 /*integral_constant_expression*/false);
14950 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14952 if (type
== NULL_TREE
)
14954 if (complain
& tf_error
)
14955 error ("empty initializer in lambda init-capture");
14956 type
= error_mark_node
;
14958 else if (TREE_CODE (type
) == TREE_LIST
)
14959 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14962 --cp_unevaluated_operand
;
14963 --c_inhibit_evaluation_warnings
;
14965 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14966 type
= lambda_capture_field_type (type
,
14967 DECLTYPE_FOR_INIT_CAPTURE (t
),
14968 DECLTYPE_FOR_REF_CAPTURE (t
));
14969 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14970 type
= lambda_proxy_type (type
);
14973 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14974 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14976 /* In a template ~id could be either a complement expression
14977 or an unqualified-id naming a destructor; if instantiating
14978 it produces an expression, it's not an id-expression or
14981 type
= finish_decltype_type (type
, id
, complain
);
14983 return cp_build_qualified_type_real (type
,
14985 | cp_type_quals (type
),
14986 complain
| tf_ignore_bad_quals
);
14989 case UNDERLYING_TYPE
:
14991 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14992 complain
, in_decl
);
14993 return finish_underlying_type (type
);
14996 case TYPE_ARGUMENT_PACK
:
14997 case NONTYPE_ARGUMENT_PACK
:
15001 if (code
== NONTYPE_ARGUMENT_PACK
)
15002 r
= make_node (code
);
15004 r
= cxx_make_type (code
);
15006 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15007 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15008 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15026 /* We should use one of the expression tsubsts for these codes. */
15027 gcc_unreachable ();
15030 sorry ("use of %qs in template", get_tree_code_name (code
));
15031 return error_mark_node
;
15035 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15036 expression on the left-hand side of the "." or "->" operator. We
15037 only do the lookup if we had a dependent BASELINK. Otherwise we
15038 adjust it onto the instantiated heirarchy. */
15041 tsubst_baselink (tree baselink
, tree object_type
,
15042 tree args
, tsubst_flags_t complain
, tree in_decl
)
15044 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15045 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15046 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15048 tree optype
= BASELINK_OPTYPE (baselink
);
15049 optype
= tsubst (optype
, args
, complain
, in_decl
);
15051 tree template_args
= NULL_TREE
;
15052 bool template_id_p
= false;
15053 tree fns
= BASELINK_FUNCTIONS (baselink
);
15054 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15056 template_id_p
= true;
15057 template_args
= TREE_OPERAND (fns
, 1);
15058 fns
= TREE_OPERAND (fns
, 0);
15060 template_args
= tsubst_template_args (template_args
, args
,
15061 complain
, in_decl
);
15064 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15065 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15066 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15070 tree name
= OVL_NAME (fns
);
15071 if (IDENTIFIER_CONV_OP_P (name
))
15072 name
= make_conv_op_name (optype
);
15074 if (name
== complete_dtor_identifier
)
15075 /* Treat as-if non-dependent below. */
15076 dependent_p
= false;
15078 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15081 if ((complain
& tf_error
)
15082 && constructor_name_p (name
, qualifying_scope
))
15083 error ("cannot call constructor %<%T::%D%> directly",
15084 qualifying_scope
, name
);
15085 return error_mark_node
;
15088 if (BASELINK_P (baselink
))
15089 fns
= BASELINK_FUNCTIONS (baselink
);
15092 /* We're going to overwrite pieces below, make a duplicate. */
15093 baselink
= copy_node (baselink
);
15095 /* If lookup found a single function, mark it as used at this point.
15096 (If lookup found multiple functions the one selected later by
15097 overload resolution will be marked as used at that point.) */
15098 if (!template_id_p
&& !really_overloaded_fn (fns
))
15100 tree fn
= OVL_FIRST (fns
);
15101 bool ok
= mark_used (fn
, complain
);
15102 if (!ok
&& !(complain
& tf_error
))
15103 return error_mark_node
;
15104 if (ok
&& BASELINK_P (baselink
))
15105 /* We might have instantiated an auto function. */
15106 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15109 if (BASELINK_P (baselink
))
15111 /* Add back the template arguments, if present. */
15113 BASELINK_FUNCTIONS (baselink
)
15114 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15116 /* Update the conversion operator type. */
15117 BASELINK_OPTYPE (baselink
) = optype
;
15121 object_type
= current_class_type
;
15123 if (qualified_p
|| !dependent_p
)
15125 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15129 /* We need to call adjust_result_of_qualified_name_lookup in case the
15130 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15131 so that we still get virtual function binding. */
15132 BASELINK_QUALIFIED_P (baselink
) = false;
15138 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15139 true if the qualified-id will be a postfix-expression in-and-of
15140 itself; false if more of the postfix-expression follows the
15141 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15145 tsubst_qualified_id (tree qualified_id
, tree args
,
15146 tsubst_flags_t complain
, tree in_decl
,
15147 bool done
, bool address_p
)
15153 tree template_args
;
15154 location_t loc
= UNKNOWN_LOCATION
;
15156 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15158 /* Figure out what name to look up. */
15159 name
= TREE_OPERAND (qualified_id
, 1);
15160 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15162 is_template
= true;
15163 loc
= EXPR_LOCATION (name
);
15164 template_args
= TREE_OPERAND (name
, 1);
15166 template_args
= tsubst_template_args (template_args
, args
,
15167 complain
, in_decl
);
15168 if (template_args
== error_mark_node
)
15169 return error_mark_node
;
15170 name
= TREE_OPERAND (name
, 0);
15174 is_template
= false;
15175 template_args
= NULL_TREE
;
15178 /* Substitute into the qualifying scope. When there are no ARGS, we
15179 are just trying to simplify a non-dependent expression. In that
15180 case the qualifying scope may be dependent, and, in any case,
15181 substituting will not help. */
15182 scope
= TREE_OPERAND (qualified_id
, 0);
15185 scope
= tsubst (scope
, args
, complain
, in_decl
);
15186 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15191 if (dependent_scope_p (scope
))
15194 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15195 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15196 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15197 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15201 if (!BASELINK_P (name
) && !DECL_P (expr
))
15203 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15205 /* A BIT_NOT_EXPR is used to represent a destructor. */
15206 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15208 error ("qualifying type %qT does not match destructor name ~%qT",
15209 scope
, TREE_OPERAND (expr
, 0));
15210 expr
= error_mark_node
;
15213 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15214 /*is_type_p=*/0, false);
15217 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15218 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15219 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15221 if (complain
& tf_error
)
15223 error ("dependent-name %qE is parsed as a non-type, but "
15224 "instantiation yields a type", qualified_id
);
15225 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15227 return error_mark_node
;
15233 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15235 /* Remember that there was a reference to this entity. */
15236 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15237 return error_mark_node
;
15240 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15242 if (complain
& tf_error
)
15243 qualified_name_lookup_error (scope
,
15244 TREE_OPERAND (qualified_id
, 1),
15245 expr
, input_location
);
15246 return error_mark_node
;
15251 /* We may be repeating a check already done during parsing, but
15252 if it was well-formed and passed then, it will pass again
15253 now, and if it didn't, we wouldn't have got here. The case
15254 we want to catch is when we couldn't tell then, and can now,
15255 namely when templ prior to substitution was an
15257 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15258 return error_mark_node
;
15260 if (variable_template_p (expr
))
15261 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15264 expr
= lookup_template_function (expr
, template_args
);
15267 if (expr
== error_mark_node
&& complain
& tf_error
)
15268 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15269 expr
, input_location
);
15270 else if (TYPE_P (scope
))
15272 expr
= (adjust_result_of_qualified_name_lookup
15273 (expr
, scope
, current_nonlambda_class_type ()));
15274 expr
= (finish_qualified_id_expr
15275 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15276 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15277 /*template_arg_p=*/false, complain
));
15280 /* Expressions do not generally have reference type. */
15281 if (TREE_CODE (expr
) != SCOPE_REF
15282 /* However, if we're about to form a pointer-to-member, we just
15283 want the referenced member referenced. */
15284 && TREE_CODE (expr
) != OFFSET_REF
)
15285 expr
= convert_from_reference (expr
);
15287 if (REF_PARENTHESIZED_P (qualified_id
))
15288 expr
= force_paren_expr (expr
);
15293 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15294 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15298 tsubst_init (tree init
, tree decl
, tree args
,
15299 tsubst_flags_t complain
, tree in_decl
)
15304 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15306 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
15308 /* If we had an initializer but it
15309 instantiated to nothing,
15310 value-initialize the object. This will
15311 only occur when the initializer was a
15312 pack expansion where the parameter packs
15313 used in that expansion were of length
15315 init
= build_value_init (TREE_TYPE (decl
),
15317 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
15318 init
= get_target_expr_sfinae (init
, complain
);
15319 if (TREE_CODE (init
) == TARGET_EXPR
)
15320 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
15326 /* Like tsubst, but deals with expressions. This function just replaces
15327 template parms; to finish processing the resultant expression, use
15328 tsubst_copy_and_build or tsubst_expr. */
15331 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15333 enum tree_code code
;
15336 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15339 code
= TREE_CODE (t
);
15344 r
= retrieve_local_specialization (t
);
15346 if (r
== NULL_TREE
)
15348 /* We get here for a use of 'this' in an NSDMI. */
15349 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15350 return current_class_ptr
;
15352 /* This can happen for a parameter name used later in a function
15353 declaration (such as in a late-specified return type). Just
15354 make a dummy decl, since it's only used for its type. */
15355 gcc_assert (cp_unevaluated_operand
!= 0);
15356 r
= tsubst_decl (t
, args
, complain
);
15357 /* Give it the template pattern as its context; its true context
15358 hasn't been instantiated yet and this is good enough for
15360 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15363 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15364 r
= argument_pack_select_arg (r
);
15365 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15366 return error_mark_node
;
15374 if (DECL_TEMPLATE_PARM_P (t
))
15375 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15376 /* There is no need to substitute into namespace-scope
15378 if (DECL_NAMESPACE_SCOPE_P (t
))
15380 /* If ARGS is NULL, then T is known to be non-dependent. */
15381 if (args
== NULL_TREE
)
15382 return scalar_constant_value (t
);
15384 /* Unfortunately, we cannot just call lookup_name here.
15387 template <int I> int f() {
15389 struct S { void g() { E e = a; } };
15392 When we instantiate f<7>::S::g(), say, lookup_name is not
15393 clever enough to find f<7>::a. */
15395 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15396 /*entering_scope=*/0);
15398 for (v
= TYPE_VALUES (enum_type
);
15400 v
= TREE_CHAIN (v
))
15401 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15402 return TREE_VALUE (v
);
15404 /* We didn't find the name. That should never happen; if
15405 name-lookup found it during preliminary parsing, we
15406 should find it again here during instantiation. */
15407 gcc_unreachable ();
15412 if (DECL_CONTEXT (t
))
15416 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15417 /*entering_scope=*/1);
15418 if (ctx
!= DECL_CONTEXT (t
))
15420 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15423 if (complain
& tf_error
)
15424 error ("using invalid field %qD", t
);
15425 return error_mark_node
;
15434 case FUNCTION_DECL
:
15435 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15436 r
= tsubst (t
, args
, complain
, in_decl
);
15437 else if (local_variable_p (t
)
15438 && uses_template_parms (DECL_CONTEXT (t
)))
15440 r
= retrieve_local_specialization (t
);
15441 if (r
== NULL_TREE
)
15443 /* First try name lookup to find the instantiation. */
15444 r
= lookup_name (DECL_NAME (t
));
15445 if (r
&& !is_capture_proxy (r
))
15447 /* Make sure that the one we found is the one we want. */
15448 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15449 if (ctx
!= DECL_CONTEXT (r
))
15457 /* This can happen for a variable used in a
15458 late-specified return type of a local lambda, or for a
15459 local static or constant. Building a new VAR_DECL
15460 should be OK in all those cases. */
15461 r
= tsubst_decl (t
, args
, complain
);
15462 if (local_specializations
)
15463 /* Avoid infinite recursion (79640). */
15464 register_local_specialization (r
, t
);
15465 if (decl_maybe_constant_var_p (r
))
15467 /* We can't call cp_finish_decl, so handle the
15468 initializer by hand. */
15469 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15470 complain
, in_decl
);
15471 if (!processing_template_decl
)
15472 init
= maybe_constant_init (init
);
15473 if (processing_template_decl
15474 ? potential_constant_expression (init
)
15475 : reduced_constant_expression_p (init
))
15476 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15477 = TREE_CONSTANT (r
) = true;
15478 DECL_INITIAL (r
) = init
;
15479 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15481 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15482 complain
, adc_variable_type
);
15484 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15485 || decl_constant_var_p (r
)
15486 || errorcount
|| sorrycount
);
15487 if (!processing_template_decl
15488 && !TREE_STATIC (r
))
15489 r
= process_outer_var_ref (r
, complain
);
15491 /* Remember this for subsequent uses. */
15492 if (local_specializations
)
15493 register_local_specialization (r
, t
);
15495 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15496 r
= argument_pack_select_arg (r
);
15500 if (!mark_used (r
, complain
))
15501 return error_mark_node
;
15504 case NAMESPACE_DECL
:
15508 /* An OVERLOAD will always be a non-dependent overload set; an
15509 overload set from function scope will just be represented with an
15510 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15511 gcc_assert (!uses_template_parms (t
));
15512 /* We must have marked any lookups as persistent. */
15513 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
15517 return tsubst_baselink (t
, current_nonlambda_class_type (),
15518 args
, complain
, in_decl
);
15520 case TEMPLATE_DECL
:
15521 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15522 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15523 args
, complain
, in_decl
);
15524 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15525 return tsubst (t
, args
, complain
, in_decl
);
15526 else if (DECL_CLASS_SCOPE_P (t
)
15527 && uses_template_parms (DECL_CONTEXT (t
)))
15529 /* Template template argument like the following example need
15532 template <template <class> class TT> struct C {};
15533 template <class T> struct D {
15534 template <class U> struct E {};
15539 We are processing the template argument `E' in #1 for
15540 the template instantiation #2. Originally, `E' is a
15541 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15542 have to substitute this with one having context `D<int>'. */
15544 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15545 if (dependent_scope_p (context
))
15547 /* When rewriting a constructor into a deduction guide, a
15548 non-dependent name can become dependent, so memtmpl<args>
15549 becomes context::template memtmpl<args>. */
15550 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15551 return build_qualified_name (type
, context
, DECL_NAME (t
),
15554 return lookup_field (context
, DECL_NAME(t
), 0, false);
15557 /* Ordinary template template argument. */
15560 case NON_LVALUE_EXPR
:
15561 case VIEW_CONVERT_EXPR
:
15563 /* Handle location wrappers by substituting the wrapped node
15564 first, *then* reusing the resulting type. Doing the type
15565 first ensures that we handle template parameters and
15566 parameter pack expansions. */
15567 gcc_assert (location_wrapper_p (t
));
15568 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15569 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15573 case REINTERPRET_CAST_EXPR
:
15574 case CONST_CAST_EXPR
:
15575 case STATIC_CAST_EXPR
:
15576 case DYNAMIC_CAST_EXPR
:
15577 case IMPLICIT_CONV_EXPR
:
15581 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15582 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15583 return build1 (code
, type
, op0
);
15587 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15588 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15590 tree expanded
, op
= TREE_OPERAND (t
, 0);
15593 if (SIZEOF_EXPR_TYPE_P (t
))
15594 op
= TREE_TYPE (op
);
15596 ++cp_unevaluated_operand
;
15597 ++c_inhibit_evaluation_warnings
;
15598 /* We only want to compute the number of arguments. */
15599 if (PACK_EXPANSION_P (op
))
15600 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15602 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15603 args
, complain
, in_decl
);
15604 --cp_unevaluated_operand
;
15605 --c_inhibit_evaluation_warnings
;
15607 if (TREE_CODE (expanded
) == TREE_VEC
)
15609 len
= TREE_VEC_LENGTH (expanded
);
15610 /* Set TREE_USED for the benefit of -Wunused. */
15611 for (int i
= 0; i
< len
; i
++)
15612 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15613 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15616 if (expanded
== error_mark_node
)
15617 return error_mark_node
;
15618 else if (PACK_EXPANSION_P (expanded
)
15619 || (TREE_CODE (expanded
) == TREE_VEC
15620 && pack_expansion_args_count (expanded
)))
15623 if (PACK_EXPANSION_P (expanded
))
15625 else if (TREE_VEC_LENGTH (expanded
) == 1)
15626 expanded
= TREE_VEC_ELT (expanded
, 0);
15628 expanded
= make_argument_pack (expanded
);
15630 if (TYPE_P (expanded
))
15631 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15633 complain
& tf_error
);
15635 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15636 complain
& tf_error
);
15639 return build_int_cst (size_type_node
, len
);
15641 if (SIZEOF_EXPR_TYPE_P (t
))
15643 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15644 args
, complain
, in_decl
);
15645 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15646 r
= build1 (SIZEOF_EXPR
,
15647 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15648 SIZEOF_EXPR_TYPE_P (r
) = 1;
15655 case TRUTH_NOT_EXPR
:
15658 case UNARY_PLUS_EXPR
: /* Unary + */
15660 case AT_ENCODE_EXPR
:
15664 case REALPART_EXPR
:
15665 case IMAGPART_EXPR
:
15668 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15669 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15670 r
= build1 (code
, type
, op0
);
15671 if (code
== ALIGNOF_EXPR
)
15672 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
15676 case COMPONENT_REF
:
15681 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15682 name
= TREE_OPERAND (t
, 1);
15683 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15685 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15686 complain
, in_decl
);
15687 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15689 else if (TREE_CODE (name
) == SCOPE_REF
15690 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15692 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15693 complain
, in_decl
);
15694 name
= TREE_OPERAND (name
, 1);
15695 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15696 complain
, in_decl
);
15697 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15698 name
= build_qualified_name (/*type=*/NULL_TREE
,
15700 /*template_p=*/false);
15702 else if (BASELINK_P (name
))
15703 name
= tsubst_baselink (name
,
15704 non_reference (TREE_TYPE (object
)),
15708 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15709 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15715 case TRUNC_DIV_EXPR
:
15716 case CEIL_DIV_EXPR
:
15717 case FLOOR_DIV_EXPR
:
15718 case ROUND_DIV_EXPR
:
15719 case EXACT_DIV_EXPR
:
15723 case TRUNC_MOD_EXPR
:
15724 case FLOOR_MOD_EXPR
:
15725 case TRUTH_ANDIF_EXPR
:
15726 case TRUTH_ORIF_EXPR
:
15727 case TRUTH_AND_EXPR
:
15728 case TRUTH_OR_EXPR
:
15741 case COMPOUND_EXPR
:
15744 case PREDECREMENT_EXPR
:
15745 case PREINCREMENT_EXPR
:
15746 case POSTDECREMENT_EXPR
:
15747 case POSTINCREMENT_EXPR
:
15749 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15750 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15751 return build_nt (code
, op0
, op1
);
15756 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15757 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15758 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15759 QUALIFIED_NAME_IS_TEMPLATE (t
));
15764 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15765 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15766 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15771 int n
= VL_EXP_OPERAND_LENGTH (t
);
15772 tree result
= build_vl_exp (CALL_EXPR
, n
);
15774 for (i
= 0; i
< n
; i
++)
15775 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15776 complain
, in_decl
);
15782 case PSEUDO_DTOR_EXPR
:
15783 case VEC_PERM_EXPR
:
15785 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15786 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15787 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15788 r
= build_nt (code
, op0
, op1
, op2
);
15789 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15795 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15796 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15797 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15798 r
= build_nt (code
, op0
, op1
, op2
);
15799 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15805 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15806 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15807 r
= build_nt (code
, op0
, op1
);
15808 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15809 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15813 case TEMPLATE_ID_EXPR
:
15815 /* Substituted template arguments */
15816 tree fn
= TREE_OPERAND (t
, 0);
15817 tree targs
= TREE_OPERAND (t
, 1);
15819 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15821 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15823 return lookup_template_function (fn
, targs
);
15828 tree purpose
, value
, chain
;
15830 if (t
== void_list_node
)
15833 purpose
= TREE_PURPOSE (t
);
15835 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15836 value
= TREE_VALUE (t
);
15838 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15839 chain
= TREE_CHAIN (t
);
15840 if (chain
&& chain
!= void_type_node
)
15841 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15842 if (purpose
== TREE_PURPOSE (t
)
15843 && value
== TREE_VALUE (t
)
15844 && chain
== TREE_CHAIN (t
))
15846 return tree_cons (purpose
, value
, chain
);
15851 case ENUMERAL_TYPE
:
15853 case TEMPLATE_TYPE_PARM
:
15854 case TEMPLATE_TEMPLATE_PARM
:
15855 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15856 case TEMPLATE_PARM_INDEX
:
15858 case REFERENCE_TYPE
:
15860 case FUNCTION_TYPE
:
15863 case TYPENAME_TYPE
:
15864 case UNBOUND_CLASS_TEMPLATE
:
15866 case DECLTYPE_TYPE
:
15868 return tsubst (t
, args
, complain
, in_decl
);
15872 /* Fall through. */
15873 case IDENTIFIER_NODE
:
15874 if (IDENTIFIER_CONV_OP_P (t
))
15876 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15877 return make_conv_op_name (new_type
);
15883 /* This is handled by tsubst_copy_and_build. */
15884 gcc_unreachable ();
15888 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15889 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15890 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15893 case CLEANUP_POINT_EXPR
:
15894 /* We shouldn't have built any of these during initial template
15895 generation. Instead, they should be built during instantiation
15896 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15897 gcc_unreachable ();
15901 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15902 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15903 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15904 r
= build2 (code
, type
, op0
, op1
);
15905 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15906 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15907 && !(complain
& tf_error
))
15908 return error_mark_node
;
15912 case EXPR_PACK_EXPANSION
:
15913 error ("invalid use of pack expansion expression");
15914 return error_mark_node
;
15916 case NONTYPE_ARGUMENT_PACK
:
15917 error ("use %<...%> to expand argument pack");
15918 return error_mark_node
;
15921 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15929 /* Instantiate any typedefs in the type. */
15930 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15931 r
= fold_convert (type
, t
);
15932 gcc_assert (TREE_CODE (r
) == code
);
15937 /* These can sometimes show up in a partial instantiation, but never
15938 involve template parms. */
15939 gcc_assert (!uses_template_parms (t
));
15942 case UNARY_LEFT_FOLD_EXPR
:
15943 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15944 case UNARY_RIGHT_FOLD_EXPR
:
15945 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15946 case BINARY_LEFT_FOLD_EXPR
:
15947 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15948 case BINARY_RIGHT_FOLD_EXPR
:
15949 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15953 case DEBUG_BEGIN_STMT
:
15954 /* ??? There's no point in copying it for now, but maybe some
15955 day it will contain more information, such as a pointer back
15956 to the containing function, inlined copy or so. */
15960 /* We shouldn't get here, but keep going if !flag_checking. */
15962 gcc_unreachable ();
15967 /* Helper function for tsubst_omp_clauses, used for instantiation of
15968 OMP_CLAUSE_DECL of clauses. */
15971 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15974 if (decl
== NULL_TREE
)
15977 /* Handle an OpenMP array section represented as a TREE_LIST (or
15978 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15979 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15980 TREE_LIST. We can handle it exactly the same as an array section
15981 (purpose, value, and a chain), even though the nomenclature
15982 (low_bound, length, etc) is different. */
15983 if (TREE_CODE (decl
) == TREE_LIST
)
15986 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15987 /*integral_constant_expression_p=*/false);
15988 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15989 /*integral_constant_expression_p=*/false);
15990 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15992 if (TREE_PURPOSE (decl
) == low_bound
15993 && TREE_VALUE (decl
) == length
15994 && TREE_CHAIN (decl
) == chain
)
15996 tree ret
= tree_cons (low_bound
, length
, chain
);
15997 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15998 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16001 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16002 /*integral_constant_expression_p=*/false);
16003 /* Undo convert_from_reference tsubst_expr could have called. */
16005 && REFERENCE_REF_P (ret
)
16006 && !REFERENCE_REF_P (decl
))
16007 ret
= TREE_OPERAND (ret
, 0);
16011 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16014 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16015 tree args
, tsubst_flags_t complain
, tree in_decl
)
16017 tree new_clauses
= NULL_TREE
, nc
, oc
;
16018 tree linear_no_step
= NULL_TREE
;
16020 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16022 nc
= copy_node (oc
);
16023 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16026 switch (OMP_CLAUSE_CODE (nc
))
16028 case OMP_CLAUSE_LASTPRIVATE
:
16029 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16031 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16032 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16033 in_decl
, /*integral_constant_expression_p=*/false);
16034 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16035 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16038 case OMP_CLAUSE_PRIVATE
:
16039 case OMP_CLAUSE_SHARED
:
16040 case OMP_CLAUSE_FIRSTPRIVATE
:
16041 case OMP_CLAUSE_COPYIN
:
16042 case OMP_CLAUSE_COPYPRIVATE
:
16043 case OMP_CLAUSE_UNIFORM
:
16044 case OMP_CLAUSE_DEPEND
:
16045 case OMP_CLAUSE_FROM
:
16046 case OMP_CLAUSE_TO
:
16047 case OMP_CLAUSE_MAP
:
16048 case OMP_CLAUSE_USE_DEVICE_PTR
:
16049 case OMP_CLAUSE_IS_DEVICE_PTR
:
16050 OMP_CLAUSE_DECL (nc
)
16051 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16054 case OMP_CLAUSE_TILE
:
16055 case OMP_CLAUSE_IF
:
16056 case OMP_CLAUSE_NUM_THREADS
:
16057 case OMP_CLAUSE_SCHEDULE
:
16058 case OMP_CLAUSE_COLLAPSE
:
16059 case OMP_CLAUSE_FINAL
:
16060 case OMP_CLAUSE_DEVICE
:
16061 case OMP_CLAUSE_DIST_SCHEDULE
:
16062 case OMP_CLAUSE_NUM_TEAMS
:
16063 case OMP_CLAUSE_THREAD_LIMIT
:
16064 case OMP_CLAUSE_SAFELEN
:
16065 case OMP_CLAUSE_SIMDLEN
:
16066 case OMP_CLAUSE_NUM_TASKS
:
16067 case OMP_CLAUSE_GRAINSIZE
:
16068 case OMP_CLAUSE_PRIORITY
:
16069 case OMP_CLAUSE_ORDERED
:
16070 case OMP_CLAUSE_HINT
:
16071 case OMP_CLAUSE_NUM_GANGS
:
16072 case OMP_CLAUSE_NUM_WORKERS
:
16073 case OMP_CLAUSE_VECTOR_LENGTH
:
16074 case OMP_CLAUSE_WORKER
:
16075 case OMP_CLAUSE_VECTOR
:
16076 case OMP_CLAUSE_ASYNC
:
16077 case OMP_CLAUSE_WAIT
:
16078 OMP_CLAUSE_OPERAND (nc
, 0)
16079 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16080 in_decl
, /*integral_constant_expression_p=*/false);
16082 case OMP_CLAUSE_REDUCTION
:
16083 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16085 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16086 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16088 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16089 complain
, in_decl
);
16090 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16091 = build_qualified_name (NULL_TREE
, scope
,
16092 TREE_OPERAND (placeholder
, 1),
16096 gcc_assert (identifier_p (placeholder
));
16098 OMP_CLAUSE_DECL (nc
)
16099 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16102 case OMP_CLAUSE_GANG
:
16103 case OMP_CLAUSE_ALIGNED
:
16104 OMP_CLAUSE_DECL (nc
)
16105 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16107 OMP_CLAUSE_OPERAND (nc
, 1)
16108 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16109 in_decl
, /*integral_constant_expression_p=*/false);
16111 case OMP_CLAUSE_LINEAR
:
16112 OMP_CLAUSE_DECL (nc
)
16113 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16115 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16117 gcc_assert (!linear_no_step
);
16118 linear_no_step
= nc
;
16120 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16121 OMP_CLAUSE_LINEAR_STEP (nc
)
16122 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16123 complain
, in_decl
);
16125 OMP_CLAUSE_LINEAR_STEP (nc
)
16126 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16128 /*integral_constant_expression_p=*/false);
16130 case OMP_CLAUSE_NOWAIT
:
16131 case OMP_CLAUSE_DEFAULT
:
16132 case OMP_CLAUSE_UNTIED
:
16133 case OMP_CLAUSE_MERGEABLE
:
16134 case OMP_CLAUSE_INBRANCH
:
16135 case OMP_CLAUSE_NOTINBRANCH
:
16136 case OMP_CLAUSE_PROC_BIND
:
16137 case OMP_CLAUSE_FOR
:
16138 case OMP_CLAUSE_PARALLEL
:
16139 case OMP_CLAUSE_SECTIONS
:
16140 case OMP_CLAUSE_TASKGROUP
:
16141 case OMP_CLAUSE_NOGROUP
:
16142 case OMP_CLAUSE_THREADS
:
16143 case OMP_CLAUSE_SIMD
:
16144 case OMP_CLAUSE_DEFAULTMAP
:
16145 case OMP_CLAUSE_INDEPENDENT
:
16146 case OMP_CLAUSE_AUTO
:
16147 case OMP_CLAUSE_SEQ
:
16148 case OMP_CLAUSE_IF_PRESENT
:
16149 case OMP_CLAUSE_FINALIZE
:
16152 gcc_unreachable ();
16154 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16155 switch (OMP_CLAUSE_CODE (nc
))
16157 case OMP_CLAUSE_SHARED
:
16158 case OMP_CLAUSE_PRIVATE
:
16159 case OMP_CLAUSE_FIRSTPRIVATE
:
16160 case OMP_CLAUSE_LASTPRIVATE
:
16161 case OMP_CLAUSE_COPYPRIVATE
:
16162 case OMP_CLAUSE_LINEAR
:
16163 case OMP_CLAUSE_REDUCTION
:
16164 case OMP_CLAUSE_USE_DEVICE_PTR
:
16165 case OMP_CLAUSE_IS_DEVICE_PTR
:
16166 /* tsubst_expr on SCOPE_REF results in returning
16167 finish_non_static_data_member result. Undo that here. */
16168 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16169 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16170 == IDENTIFIER_NODE
))
16172 tree t
= OMP_CLAUSE_DECL (nc
);
16175 switch (TREE_CODE (v
))
16177 case COMPONENT_REF
:
16181 case POINTER_PLUS_EXPR
:
16182 v
= TREE_OPERAND (v
, 0);
16185 if (DECL_CONTEXT (v
) == current_function_decl
16186 && DECL_ARTIFICIAL (v
)
16187 && DECL_NAME (v
) == this_identifier
)
16188 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16195 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16196 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16197 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16198 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16199 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16201 tree decl
= OMP_CLAUSE_DECL (nc
);
16204 retrofit_lang_decl (decl
);
16205 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16214 new_clauses
= nreverse (new_clauses
);
16215 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16217 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16218 if (linear_no_step
)
16219 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16220 if (nc
== linear_no_step
)
16222 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16226 return new_clauses
;
16229 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16232 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16235 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16237 tree purpose
, value
, chain
;
16242 if (TREE_CODE (t
) != TREE_LIST
)
16243 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16244 /*function_p=*/false,
16245 /*integral_constant_expression_p=*/false);
16247 if (t
== void_list_node
)
16250 purpose
= TREE_PURPOSE (t
);
16252 purpose
= RECUR (purpose
);
16253 value
= TREE_VALUE (t
);
16256 if (TREE_CODE (value
) != LABEL_DECL
)
16257 value
= RECUR (value
);
16260 value
= lookup_label (DECL_NAME (value
));
16261 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
16262 TREE_USED (value
) = 1;
16265 chain
= TREE_CHAIN (t
);
16266 if (chain
&& chain
!= void_type_node
)
16267 chain
= RECUR (chain
);
16268 return tree_cons (purpose
, value
, chain
);
16272 /* Used to temporarily communicate the list of #pragma omp parallel
16273 clauses to #pragma omp for instantiation if they are combined
16276 static tree
*omp_parallel_combined_clauses
;
16278 /* Substitute one OMP_FOR iterator. */
16281 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
16282 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
16283 tree args
, tsubst_flags_t complain
, tree in_decl
,
16284 bool integral_constant_expression_p
)
16286 #define RECUR(NODE) \
16287 tsubst_expr ((NODE), args, complain, in_decl, \
16288 integral_constant_expression_p)
16289 tree decl
, init
, cond
, incr
;
16291 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
16292 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
16294 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
16296 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
16297 if (TREE_CODE (o
) == TREE_LIST
)
16298 TREE_VEC_ELT (orig_declv
, i
)
16299 = tree_cons (RECUR (TREE_PURPOSE (o
)),
16300 RECUR (TREE_VALUE (o
)), NULL_TREE
);
16302 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
16305 decl
= TREE_OPERAND (init
, 0);
16306 init
= TREE_OPERAND (init
, 1);
16307 tree decl_expr
= NULL_TREE
;
16308 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
16310 /* We need to jump through some hoops to handle declarations in the
16311 init-statement, since we might need to handle auto deduction,
16312 but we need to keep control of initialization. */
16314 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
16315 decl
= tsubst_decl (decl
, args
, complain
);
16319 if (TREE_CODE (decl
) == SCOPE_REF
)
16321 decl
= RECUR (decl
);
16322 if (TREE_CODE (decl
) == COMPONENT_REF
)
16326 switch (TREE_CODE (v
))
16328 case COMPONENT_REF
:
16332 case POINTER_PLUS_EXPR
:
16333 v
= TREE_OPERAND (v
, 0);
16336 if (DECL_CONTEXT (v
) == current_function_decl
16337 && DECL_ARTIFICIAL (v
)
16338 && DECL_NAME (v
) == this_identifier
)
16340 decl
= TREE_OPERAND (decl
, 1);
16341 decl
= omp_privatize_field (decl
, false);
16351 decl
= RECUR (decl
);
16353 init
= RECUR (init
);
16355 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16356 if (auto_node
&& init
)
16358 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16360 gcc_assert (!type_dependent_expression_p (decl
));
16362 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16366 /* Declare the variable, but don't let that initialize it. */
16367 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16368 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16370 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16373 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16374 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16375 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16377 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16378 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16379 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16380 NOP_EXPR
, rhs
, complain
);
16383 incr
= RECUR (incr
);
16384 TREE_VEC_ELT (declv
, i
) = decl
;
16385 TREE_VEC_ELT (initv
, i
) = init
;
16386 TREE_VEC_ELT (condv
, i
) = cond
;
16387 TREE_VEC_ELT (incrv
, i
) = incr
;
16393 /* Declare and initialize the variable. */
16401 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
16403 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16405 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16406 && OMP_CLAUSE_DECL (*pc
) == decl
)
16408 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16409 && OMP_CLAUSE_DECL (*pc
) == decl
)
16413 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16415 *pc
= OMP_CLAUSE_CHAIN (c
);
16416 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16419 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16420 && OMP_CLAUSE_DECL (*pc
) == decl
)
16422 error ("iteration variable %qD should not be firstprivate",
16424 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16426 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16427 && OMP_CLAUSE_DECL (*pc
) == decl
)
16429 error ("iteration variable %qD should not be reduction",
16431 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16434 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16439 if (*pc
== NULL_TREE
)
16441 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16442 OMP_CLAUSE_DECL (c
) = decl
;
16443 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16446 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16451 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16452 if (COMPARISON_CLASS_P (cond
))
16454 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16455 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16456 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16459 cond
= RECUR (cond
);
16460 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16461 switch (TREE_CODE (incr
))
16463 case PREINCREMENT_EXPR
:
16464 case PREDECREMENT_EXPR
:
16465 case POSTINCREMENT_EXPR
:
16466 case POSTDECREMENT_EXPR
:
16467 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16468 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16471 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16472 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16474 tree rhs
= TREE_OPERAND (incr
, 1);
16475 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16476 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16477 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16478 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16479 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16483 incr
= RECUR (incr
);
16486 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16487 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16489 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16490 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16491 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16492 TREE_TYPE (decl
), lhs
,
16493 RECUR (TREE_OPERAND (incr
, 2))));
16495 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16496 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16497 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16499 tree rhs
= TREE_OPERAND (incr
, 2);
16500 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16501 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16502 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16503 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16504 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16508 incr
= RECUR (incr
);
16511 incr
= RECUR (incr
);
16515 TREE_VEC_ELT (declv
, i
) = decl
;
16516 TREE_VEC_ELT (initv
, i
) = init
;
16517 TREE_VEC_ELT (condv
, i
) = cond
;
16518 TREE_VEC_ELT (incrv
, i
) = incr
;
16522 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16523 of OMP_TARGET's body. */
16526 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16528 *walk_subtrees
= 0;
16529 switch (TREE_CODE (*tp
))
16534 case STATEMENT_LIST
:
16535 *walk_subtrees
= 1;
16543 /* Helper function for tsubst_expr. For decomposition declaration
16544 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16545 also the corresponding decls representing the identifiers
16546 of the decomposition declaration. Return DECL if successful
16547 or error_mark_node otherwise, set *FIRST to the first decl
16548 in the list chained through DECL_CHAIN and *CNT to the number
16552 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16553 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16556 tree decl2
, decl3
, prev
= decl
;
16558 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16559 for (decl2
= DECL_CHAIN (pattern_decl
);
16562 && DECL_DECOMPOSITION_P (decl2
)
16563 && DECL_NAME (decl2
);
16564 decl2
= DECL_CHAIN (decl2
))
16566 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16568 gcc_assert (errorcount
);
16569 return error_mark_node
;
16572 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16573 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16574 tree v
= DECL_VALUE_EXPR (decl2
);
16575 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16576 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16577 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16578 SET_DECL_VALUE_EXPR (decl2
, v
);
16579 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16581 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16584 gcc_assert (errorcount
);
16585 decl
= error_mark_node
;
16588 maybe_push_decl (decl3
);
16589 if (error_operand_p (decl3
))
16590 decl
= error_mark_node
;
16591 else if (decl
!= error_mark_node
16592 && DECL_CHAIN (decl3
) != prev
16595 gcc_assert (errorcount
);
16596 decl
= error_mark_node
;
16605 /* Like tsubst_copy for expressions, etc. but also does semantic
16609 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16610 bool integral_constant_expression_p
)
16612 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16613 #define RECUR(NODE) \
16614 tsubst_expr ((NODE), args, complain, in_decl, \
16615 integral_constant_expression_p)
16621 if (t
== NULL_TREE
|| t
== error_mark_node
)
16624 loc
= input_location
;
16625 if (location_t eloc
= cp_expr_location (t
))
16626 input_location
= eloc
;
16627 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16628 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16630 switch (TREE_CODE (t
))
16632 case STATEMENT_LIST
:
16634 tree_stmt_iterator i
;
16635 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16636 RECUR (tsi_stmt (i
));
16640 case CTOR_INITIALIZER
:
16641 finish_mem_initializers (tsubst_initializer_list
16642 (TREE_OPERAND (t
, 0), args
));
16646 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16650 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16651 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16652 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16654 finish_expr_stmt (tmp
);
16658 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16659 /*attribs=*/NULL_TREE
);
16664 tree decl
, pattern_decl
;
16667 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16668 if (TREE_CODE (decl
) == LABEL_DECL
)
16669 finish_label_decl (DECL_NAME (decl
));
16670 else if (TREE_CODE (decl
) == USING_DECL
)
16672 tree scope
= USING_DECL_SCOPE (decl
);
16673 tree name
= DECL_NAME (decl
);
16675 scope
= tsubst (scope
, args
, complain
, in_decl
);
16676 decl
= lookup_qualified_name (scope
, name
,
16677 /*is_type_p=*/false,
16678 /*complain=*/false);
16679 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16680 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16682 finish_local_using_decl (decl
, scope
, name
);
16684 else if (is_capture_proxy (decl
)
16685 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16687 /* We're in tsubst_lambda_expr, we've already inserted a new
16688 capture proxy, so look it up and register it. */
16690 if (DECL_PACK_P (decl
))
16692 inst
= (retrieve_local_specialization
16693 (DECL_CAPTURED_VARIABLE (decl
)));
16694 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16698 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16699 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16700 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16702 register_local_specialization (inst
, decl
);
16705 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16706 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16707 /* Don't copy the old closure; we'll create a new one in
16708 tsubst_lambda_expr. */
16712 init
= DECL_INITIAL (decl
);
16713 decl
= tsubst (decl
, args
, complain
, in_decl
);
16714 if (decl
!= error_mark_node
)
16716 /* By marking the declaration as instantiated, we avoid
16717 trying to instantiate it. Since instantiate_decl can't
16718 handle local variables, and since we've already done
16719 all that needs to be done, that's the right thing to
16722 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16723 if (VAR_P (decl
) && !DECL_NAME (decl
)
16724 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16725 /* Anonymous aggregates are a special case. */
16726 finish_anon_union (decl
);
16727 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16729 DECL_CONTEXT (decl
) = current_function_decl
;
16730 if (DECL_NAME (decl
) == this_identifier
)
16732 tree lam
= DECL_CONTEXT (current_function_decl
);
16733 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16734 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16736 insert_capture_proxy (decl
);
16738 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16739 /* We already did a pushtag. */;
16740 else if (TREE_CODE (decl
) == FUNCTION_DECL
16741 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16742 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16744 DECL_CONTEXT (decl
) = NULL_TREE
;
16746 DECL_CONTEXT (decl
) = current_function_decl
;
16747 cp_check_omp_declare_reduction (decl
);
16751 int const_init
= false;
16752 unsigned int cnt
= 0;
16753 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
16754 maybe_push_decl (decl
);
16757 && DECL_DECOMPOSITION_P (decl
)
16758 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16759 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
16760 complain
, in_decl
, &first
,
16764 && DECL_PRETTY_FUNCTION_P (decl
))
16766 /* For __PRETTY_FUNCTION__ we have to adjust the
16768 const char *const name
16769 = cxx_printable_name (current_function_decl
, 2);
16770 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16773 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16776 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16779 if (ndecl
!= error_mark_node
)
16780 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16782 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16784 if (ndecl
!= error_mark_node
)
16785 cp_finish_decomp (ndecl
, first
, cnt
);
16794 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16795 RECUR (FOR_INIT_STMT (t
));
16796 finish_init_stmt (stmt
);
16797 tmp
= RECUR (FOR_COND (t
));
16798 finish_for_cond (tmp
, stmt
, false, 0);
16799 tmp
= RECUR (FOR_EXPR (t
));
16800 finish_for_expr (tmp
, stmt
);
16802 bool prev
= note_iteration_stmt_body_start ();
16803 RECUR (FOR_BODY (t
));
16804 note_iteration_stmt_body_end (prev
);
16806 finish_for_stmt (stmt
);
16809 case RANGE_FOR_STMT
:
16811 /* Construct another range_for, if this is not a final
16812 substitution (for inside inside a generic lambda of a
16813 template). Otherwise convert to a regular for. */
16815 stmt
= (processing_template_decl
16816 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16817 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16818 decl
= RANGE_FOR_DECL (t
);
16819 decl
= tsubst (decl
, args
, complain
, in_decl
);
16820 maybe_push_decl (decl
);
16821 expr
= RECUR (RANGE_FOR_EXPR (t
));
16823 tree decomp_first
= NULL_TREE
;
16824 unsigned decomp_cnt
= 0;
16825 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16826 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16828 &decomp_first
, &decomp_cnt
);
16830 if (processing_template_decl
)
16832 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16833 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16834 finish_range_for_decl (stmt
, decl
, expr
);
16838 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16839 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16840 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16841 decomp_first
, decomp_cnt
,
16842 RANGE_FOR_IVDEP (t
), unroll
);
16845 bool prev
= note_iteration_stmt_body_start ();
16846 RECUR (RANGE_FOR_BODY (t
));
16847 note_iteration_stmt_body_end (prev
);
16848 finish_for_stmt (stmt
);
16853 stmt
= begin_while_stmt ();
16854 tmp
= RECUR (WHILE_COND (t
));
16855 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16857 bool prev
= note_iteration_stmt_body_start ();
16858 RECUR (WHILE_BODY (t
));
16859 note_iteration_stmt_body_end (prev
);
16861 finish_while_stmt (stmt
);
16865 stmt
= begin_do_stmt ();
16867 bool prev
= note_iteration_stmt_body_start ();
16868 RECUR (DO_BODY (t
));
16869 note_iteration_stmt_body_end (prev
);
16871 finish_do_body (stmt
);
16872 tmp
= RECUR (DO_COND (t
));
16873 finish_do_stmt (tmp
, stmt
, false, 0);
16877 stmt
= begin_if_stmt ();
16878 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16879 if (IF_STMT_CONSTEXPR_P (t
))
16880 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
16881 tmp
= RECUR (IF_COND (t
));
16882 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16883 if (IF_STMT_CONSTEXPR_P (t
)
16884 && instantiation_dependent_expression_p (tmp
))
16886 /* We're partially instantiating a generic lambda, but the condition
16887 of the constexpr if is still dependent. Don't substitute into the
16888 branches now, just remember the template arguments. */
16889 do_poplevel (IF_SCOPE (stmt
));
16890 IF_COND (stmt
) = IF_COND (t
);
16891 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
16892 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
16893 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
16897 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16898 /* Don't instantiate the THEN_CLAUSE. */;
16901 tree folded
= fold_non_dependent_expr (tmp
, complain
);
16902 bool inhibit
= integer_zerop (folded
);
16904 ++c_inhibit_evaluation_warnings
;
16905 RECUR (THEN_CLAUSE (t
));
16907 --c_inhibit_evaluation_warnings
;
16909 finish_then_clause (stmt
);
16911 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16912 /* Don't instantiate the ELSE_CLAUSE. */;
16913 else if (ELSE_CLAUSE (t
))
16915 tree folded
= fold_non_dependent_expr (tmp
, complain
);
16916 bool inhibit
= integer_nonzerop (folded
);
16917 begin_else_clause (stmt
);
16919 ++c_inhibit_evaluation_warnings
;
16920 RECUR (ELSE_CLAUSE (t
));
16922 --c_inhibit_evaluation_warnings
;
16923 finish_else_clause (stmt
);
16926 finish_if_stmt (stmt
);
16930 if (BIND_EXPR_BODY_BLOCK (t
))
16931 stmt
= begin_function_body ();
16933 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16934 ? BCS_TRY_BLOCK
: 0);
16936 RECUR (BIND_EXPR_BODY (t
));
16938 if (BIND_EXPR_BODY_BLOCK (t
))
16939 finish_function_body (stmt
);
16941 finish_compound_stmt (stmt
);
16945 finish_break_stmt ();
16948 case CONTINUE_STMT
:
16949 finish_continue_stmt ();
16953 stmt
= begin_switch_stmt ();
16954 tmp
= RECUR (SWITCH_STMT_COND (t
));
16955 finish_switch_cond (tmp
, stmt
);
16956 RECUR (SWITCH_STMT_BODY (t
));
16957 finish_switch_stmt (stmt
);
16960 case CASE_LABEL_EXPR
:
16962 tree low
= RECUR (CASE_LOW (t
));
16963 tree high
= RECUR (CASE_HIGH (t
));
16964 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16965 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16966 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16967 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16973 tree decl
= LABEL_EXPR_LABEL (t
);
16976 label
= finish_label_stmt (DECL_NAME (decl
));
16977 if (TREE_CODE (label
) == LABEL_DECL
)
16978 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16979 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16980 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16985 tmp
= GOTO_DESTINATION (t
);
16986 if (TREE_CODE (tmp
) != LABEL_DECL
)
16987 /* Computed goto's must be tsubst'd into. On the other hand,
16988 non-computed gotos must not be; the identifier in question
16989 will have no binding. */
16992 tmp
= DECL_NAME (tmp
);
16993 finish_goto_stmt (tmp
);
16998 tree string
= RECUR (ASM_STRING (t
));
16999 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17000 complain
, in_decl
);
17001 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17002 complain
, in_decl
);
17003 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17004 complain
, in_decl
);
17005 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17006 complain
, in_decl
);
17007 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
17009 tree asm_expr
= tmp
;
17010 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17011 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17012 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17019 stmt
= begin_try_block ();
17020 RECUR (TRY_STMTS (t
));
17021 finish_cleanup_try_block (stmt
);
17022 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17026 tree compound_stmt
= NULL_TREE
;
17028 if (FN_TRY_BLOCK_P (t
))
17029 stmt
= begin_function_try_block (&compound_stmt
);
17031 stmt
= begin_try_block ();
17033 RECUR (TRY_STMTS (t
));
17035 if (FN_TRY_BLOCK_P (t
))
17036 finish_function_try_block (stmt
);
17038 finish_try_block (stmt
);
17040 RECUR (TRY_HANDLERS (t
));
17041 if (FN_TRY_BLOCK_P (t
))
17042 finish_function_handler_sequence (stmt
, compound_stmt
);
17044 finish_handler_sequence (stmt
);
17050 tree decl
= HANDLER_PARMS (t
);
17054 decl
= tsubst (decl
, args
, complain
, in_decl
);
17055 /* Prevent instantiate_decl from trying to instantiate
17056 this variable. We've already done all that needs to be
17058 if (decl
!= error_mark_node
)
17059 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17061 stmt
= begin_handler ();
17062 finish_handler_parms (decl
, stmt
);
17063 RECUR (HANDLER_BODY (t
));
17064 finish_handler (stmt
);
17069 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17070 if (CLASS_TYPE_P (tmp
))
17072 /* Local classes are not independent templates; they are
17073 instantiated along with their containing function. And this
17074 way we don't have to deal with pushing out of one local class
17075 to instantiate a member of another local class. */
17076 /* Closures are handled by the LAMBDA_EXPR. */
17077 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17078 complete_type (tmp
);
17079 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17081 || (TREE_CODE (fld
) == FUNCTION_DECL
17082 && !DECL_ARTIFICIAL (fld
)))
17083 && DECL_TEMPLATE_INSTANTIATION (fld
))
17084 instantiate_decl (fld
, /*defer_ok=*/false,
17085 /*expl_inst_class=*/false);
17089 case STATIC_ASSERT
:
17093 ++c_inhibit_evaluation_warnings
;
17095 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17098 /*integral_constant_expression_p=*/true);
17099 --c_inhibit_evaluation_warnings
;
17101 finish_static_assert (condition
,
17102 STATIC_ASSERT_MESSAGE (t
),
17103 STATIC_ASSERT_SOURCE_LOCATION (t
),
17104 /*member_p=*/false);
17109 case OACC_PARALLEL
:
17110 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
17112 stmt
= begin_omp_parallel ();
17113 RECUR (OMP_BODY (t
));
17114 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
17118 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
17119 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
17120 complain
, in_decl
);
17121 if (OMP_PARALLEL_COMBINED (t
))
17122 omp_parallel_combined_clauses
= &tmp
;
17123 stmt
= begin_omp_parallel ();
17124 RECUR (OMP_PARALLEL_BODY (t
));
17125 gcc_assert (omp_parallel_combined_clauses
== NULL
);
17126 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
17127 = OMP_PARALLEL_COMBINED (t
);
17128 pop_omp_privatization_clauses (r
);
17132 r
= push_omp_privatization_clauses (false);
17133 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17134 complain
, in_decl
);
17135 stmt
= begin_omp_task ();
17136 RECUR (OMP_TASK_BODY (t
));
17137 finish_omp_task (tmp
, stmt
);
17138 pop_omp_privatization_clauses (r
);
17143 case OMP_DISTRIBUTE
:
17147 tree clauses
, body
, pre_body
;
17148 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
17149 tree orig_declv
= NULL_TREE
;
17150 tree incrv
= NULL_TREE
;
17151 enum c_omp_region_type ort
= C_ORT_OMP
;
17154 if (TREE_CODE (t
) == OACC_LOOP
)
17157 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
17158 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
17160 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17162 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17163 if (OMP_FOR_ORIG_DECLS (t
))
17164 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17165 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17166 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17167 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17170 stmt
= begin_omp_structured_block ();
17172 pre_body
= push_stmt_list ();
17173 RECUR (OMP_FOR_PRE_BODY (t
));
17174 pre_body
= pop_stmt_list (pre_body
);
17176 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17177 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17178 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
17179 incrv
, &clauses
, args
, complain
, in_decl
,
17180 integral_constant_expression_p
);
17181 omp_parallel_combined_clauses
= NULL
;
17183 body
= push_stmt_list ();
17184 RECUR (OMP_FOR_BODY (t
));
17185 body
= pop_stmt_list (body
);
17187 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17188 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
17189 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
17193 t
= make_node (TREE_CODE (t
));
17194 TREE_TYPE (t
) = void_type_node
;
17195 OMP_FOR_BODY (t
) = body
;
17196 OMP_FOR_PRE_BODY (t
) = pre_body
;
17197 OMP_FOR_CLAUSES (t
) = clauses
;
17198 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
17202 add_stmt (finish_omp_structured_block (stmt
));
17203 pop_omp_privatization_clauses (r
);
17208 omp_parallel_combined_clauses
= NULL
;
17213 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
17214 && OMP_TEAMS_COMBINED (t
));
17215 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
17217 stmt
= push_stmt_list ();
17218 RECUR (OMP_BODY (t
));
17219 stmt
= pop_stmt_list (stmt
);
17222 OMP_BODY (t
) = stmt
;
17223 OMP_CLAUSES (t
) = tmp
;
17225 pop_omp_privatization_clauses (r
);
17229 case OMP_TARGET_DATA
:
17231 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
17232 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
17234 keep_next_level (true);
17235 stmt
= begin_omp_structured_block ();
17237 RECUR (OMP_BODY (t
));
17238 stmt
= finish_omp_structured_block (stmt
);
17241 OMP_BODY (t
) = stmt
;
17242 OMP_CLAUSES (t
) = tmp
;
17243 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
17245 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
17248 /* For combined target teams, ensure the num_teams and
17249 thread_limit clause expressions are evaluated on the host,
17250 before entering the target construct. */
17252 for (c
= OMP_TEAMS_CLAUSES (teams
);
17253 c
; c
= OMP_CLAUSE_CHAIN (c
))
17254 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
17255 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
17256 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
17258 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
17259 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
17260 if (expr
== error_mark_node
)
17262 tmp
= TARGET_EXPR_SLOT (expr
);
17264 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
17265 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
17266 OMP_CLAUSE_FIRSTPRIVATE
);
17267 OMP_CLAUSE_DECL (tc
) = tmp
;
17268 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
17269 OMP_TARGET_CLAUSES (t
) = tc
;
17278 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
17279 complain
, in_decl
);
17280 OACC_DECLARE_CLAUSES (t
) = tmp
;
17284 case OMP_TARGET_UPDATE
:
17285 case OMP_TARGET_ENTER_DATA
:
17286 case OMP_TARGET_EXIT_DATA
:
17287 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
17288 complain
, in_decl
);
17290 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17294 case OACC_ENTER_DATA
:
17295 case OACC_EXIT_DATA
:
17297 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
17298 complain
, in_decl
);
17300 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17305 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
17306 complain
, in_decl
);
17307 stmt
= push_stmt_list ();
17308 RECUR (OMP_BODY (t
));
17309 stmt
= pop_stmt_list (stmt
);
17312 OMP_BODY (t
) = stmt
;
17313 OMP_ORDERED_CLAUSES (t
) = tmp
;
17319 case OMP_TASKGROUP
:
17320 stmt
= push_stmt_list ();
17321 RECUR (OMP_BODY (t
));
17322 stmt
= pop_stmt_list (stmt
);
17325 OMP_BODY (t
) = stmt
;
17330 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
17331 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
17333 tree op1
= TREE_OPERAND (t
, 1);
17334 tree rhs1
= NULL_TREE
;
17336 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
17338 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
17339 op1
= TREE_OPERAND (op1
, 1);
17341 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17342 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17343 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
17344 NULL_TREE
, NULL_TREE
, rhs1
,
17345 OMP_ATOMIC_SEQ_CST (t
));
17349 tree op1
= TREE_OPERAND (t
, 1);
17350 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
17351 tree rhs1
= NULL_TREE
;
17352 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
17353 enum tree_code opcode
= NOP_EXPR
;
17354 if (code
== OMP_ATOMIC_READ
)
17356 v
= RECUR (TREE_OPERAND (op1
, 0));
17357 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17359 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17360 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17362 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17363 v
= RECUR (TREE_OPERAND (op1
, 0));
17364 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17365 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17367 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17368 op11
= TREE_OPERAND (op11
, 1);
17370 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17371 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17372 opcode
= TREE_CODE (op11
);
17373 if (opcode
== MODIFY_EXPR
)
17379 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17380 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17382 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
17383 OMP_ATOMIC_SEQ_CST (t
));
17387 case TRANSACTION_EXPR
:
17390 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17391 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17393 if (TRANSACTION_EXPR_IS_STMT (t
))
17395 tree body
= TRANSACTION_EXPR_BODY (t
);
17396 tree noex
= NULL_TREE
;
17397 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17399 noex
= MUST_NOT_THROW_COND (body
);
17400 if (noex
== NULL_TREE
)
17401 noex
= boolean_true_node
;
17402 body
= TREE_OPERAND (body
, 0);
17404 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17406 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17410 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17411 RECUR (TRANSACTION_EXPR_BODY (t
)),
17418 case MUST_NOT_THROW_EXPR
:
17420 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17421 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17422 RETURN (build_must_not_throw_expr (op0
, cond
));
17425 case EXPR_PACK_EXPANSION
:
17426 error ("invalid use of pack expansion expression");
17427 RETURN (error_mark_node
);
17429 case NONTYPE_ARGUMENT_PACK
:
17430 error ("use %<...%> to expand argument pack");
17431 RETURN (error_mark_node
);
17433 case COMPOUND_EXPR
:
17434 tmp
= RECUR (TREE_OPERAND (t
, 0));
17435 if (tmp
== NULL_TREE
)
17436 /* If the first operand was a statement, we're done with it. */
17437 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17438 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17439 RECUR (TREE_OPERAND (t
, 1)),
17442 case ANNOTATE_EXPR
:
17443 tmp
= RECUR (TREE_OPERAND (t
, 0));
17444 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17445 TREE_TYPE (tmp
), tmp
,
17446 RECUR (TREE_OPERAND (t
, 1)),
17447 RECUR (TREE_OPERAND (t
, 2))));
17450 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17452 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17453 /*function_p=*/false,
17454 integral_constant_expression_p
));
17457 RETURN (NULL_TREE
);
17459 input_location
= loc
;
17465 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17466 function. For description of the body see comment above
17467 cp_parser_omp_declare_reduction_exprs. */
17470 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17472 if (t
== NULL_TREE
|| t
== error_mark_node
)
17475 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17477 tree_stmt_iterator tsi
;
17480 memset (stmts
, 0, sizeof stmts
);
17481 for (i
= 0, tsi
= tsi_start (t
);
17482 i
< 7 && !tsi_end_p (tsi
);
17483 i
++, tsi_next (&tsi
))
17484 stmts
[i
] = tsi_stmt (tsi
);
17485 gcc_assert (tsi_end_p (tsi
));
17489 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17490 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17491 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17492 args
, complain
, in_decl
);
17493 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17494 args
, complain
, in_decl
);
17495 DECL_CONTEXT (omp_out
) = current_function_decl
;
17496 DECL_CONTEXT (omp_in
) = current_function_decl
;
17497 keep_next_level (true);
17498 tree block
= begin_omp_structured_block ();
17499 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17500 block
= finish_omp_structured_block (block
);
17501 block
= maybe_cleanup_point_expr_void (block
);
17502 add_decl_expr (omp_out
);
17503 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17504 TREE_NO_WARNING (omp_out
) = 1;
17505 add_decl_expr (omp_in
);
17506 finish_expr_stmt (block
);
17510 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17511 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17512 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17513 args
, complain
, in_decl
);
17514 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17515 args
, complain
, in_decl
);
17516 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17517 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17518 keep_next_level (true);
17519 tree block
= begin_omp_structured_block ();
17520 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17521 block
= finish_omp_structured_block (block
);
17522 block
= maybe_cleanup_point_expr_void (block
);
17523 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17524 add_decl_expr (omp_priv
);
17525 add_decl_expr (omp_orig
);
17526 finish_expr_stmt (block
);
17528 add_decl_expr (omp_orig
);
17532 /* T is a postfix-expression that is not being used in a function
17533 call. Return the substituted version of T. */
17536 tsubst_non_call_postfix_expression (tree t
, tree args
,
17537 tsubst_flags_t complain
,
17540 if (TREE_CODE (t
) == SCOPE_REF
)
17541 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17542 /*done=*/false, /*address_p=*/false);
17544 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17545 /*function_p=*/false,
17546 /*integral_constant_expression_p=*/false);
17551 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17552 instantiation context. Instantiating a pack expansion containing a lambda
17553 might result in multiple lambdas all based on the same lambda in the
17557 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17559 tree oldfn
= lambda_function (t
);
17562 tree r
= build_lambda_expr ();
17564 LAMBDA_EXPR_LOCATION (r
)
17565 = LAMBDA_EXPR_LOCATION (t
);
17566 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17567 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17568 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17570 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17571 /* A lambda in a default argument outside a class gets no
17572 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17573 tsubst_default_argument calls start_lambda_scope, so we need to
17574 specifically ignore it here, and use the global scope. */
17575 record_null_lambda_scope (r
);
17577 record_lambda_scope (r
);
17579 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17580 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17582 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17583 cap
= TREE_CHAIN (cap
))
17585 tree field
= TREE_PURPOSE (cap
);
17586 if (PACK_EXPANSION_P (field
))
17587 field
= PACK_EXPANSION_PATTERN (field
);
17588 field
= tsubst_decl (field
, args
, complain
);
17590 if (field
== error_mark_node
)
17591 return error_mark_node
;
17593 tree init
= TREE_VALUE (cap
);
17594 if (PACK_EXPANSION_P (init
))
17595 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17597 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17598 /*fn*/false, /*constexpr*/false);
17600 if (TREE_CODE (field
) == TREE_VEC
)
17602 int len
= TREE_VEC_LENGTH (field
);
17603 gcc_assert (TREE_CODE (init
) == TREE_VEC
17604 && TREE_VEC_LENGTH (init
) == len
);
17605 for (int i
= 0; i
< len
; ++i
)
17606 LAMBDA_EXPR_CAPTURE_LIST (r
)
17607 = tree_cons (TREE_VEC_ELT (field
, i
),
17608 TREE_VEC_ELT (init
, i
),
17609 LAMBDA_EXPR_CAPTURE_LIST (r
));
17613 LAMBDA_EXPR_CAPTURE_LIST (r
)
17614 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17616 if (id_equal (DECL_NAME (field
), "__this"))
17617 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17621 tree type
= begin_lambda_type (r
);
17622 if (type
== error_mark_node
)
17623 return error_mark_node
;
17625 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17626 determine_visibility (TYPE_NAME (type
));
17628 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17630 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17631 ? DECL_TI_TEMPLATE (oldfn
)
17634 tree fntype
= static_fn_type (oldfn
);
17636 ++processing_template_decl
;
17637 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17639 --processing_template_decl
;
17641 if (fntype
== error_mark_node
)
17642 r
= error_mark_node
;
17645 /* Fix the type of 'this'. */
17646 fntype
= build_memfn_type (fntype
, type
,
17647 type_memfn_quals (fntype
),
17648 type_memfn_rqual (fntype
));
17652 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17653 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17654 finish_member_declaration (tmpl
);
17659 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17660 finish_member_declaration (fn
);
17663 /* Let finish_function set this. */
17664 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17666 bool nested
= cfun
;
17668 push_function_context ();
17670 /* Still increment function_depth so that we don't GC in the
17671 middle of an expression. */
17674 local_specialization_stack
s (lss_copy
);
17676 tree body
= start_lambda_function (fn
, r
);
17678 register_parameter_specializations (oldfn
, fn
);
17682 /* We might not partially instantiate some parts of the function, so
17683 copy these flags from the original template. */
17684 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
17685 current_function_returns_value
= ol
->returns_value
;
17686 current_function_returns_null
= ol
->returns_null
;
17687 current_function_returns_abnormally
= ol
->returns_abnormally
;
17688 current_function_infinite_loop
= ol
->infinite_loop
;
17691 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17692 /*constexpr*/false);
17694 finish_lambda_function (body
);
17697 pop_function_context ();
17701 /* The capture list was built up in reverse order; fix that now. */
17702 LAMBDA_EXPR_CAPTURE_LIST (r
)
17703 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17705 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17707 maybe_add_lambda_conv_op (type
);
17710 finish_struct (type
, /*attr*/NULL_TREE
);
17712 insert_pending_capture_proxies ();
17717 /* Like tsubst but deals with expressions and performs semantic
17718 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17721 tsubst_copy_and_build (tree t
,
17723 tsubst_flags_t complain
,
17726 bool integral_constant_expression_p
)
17728 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17729 #define RECUR(NODE) \
17730 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17731 /*function_p=*/false, \
17732 integral_constant_expression_p)
17737 if (t
== NULL_TREE
|| t
== error_mark_node
)
17740 loc
= input_location
;
17741 if (location_t eloc
= cp_expr_location (t
))
17742 input_location
= eloc
;
17744 /* N3276 decltype magic only applies to calls at the top level or on the
17745 right side of a comma. */
17746 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17747 complain
&= ~tf_decltype
;
17749 switch (TREE_CODE (t
))
17753 /* Fall through. */
17754 case IDENTIFIER_NODE
:
17758 bool non_integral_constant_expression_p
;
17759 const char *error_msg
;
17761 if (IDENTIFIER_CONV_OP_P (t
))
17763 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17764 t
= make_conv_op_name (new_type
);
17767 /* Look up the name. */
17768 decl
= lookup_name (t
);
17770 /* By convention, expressions use ERROR_MARK_NODE to indicate
17771 failure, not NULL_TREE. */
17772 if (decl
== NULL_TREE
)
17773 decl
= error_mark_node
;
17775 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17777 integral_constant_expression_p
,
17778 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17779 &non_integral_constant_expression_p
,
17780 /*template_p=*/false,
17782 /*address_p=*/false,
17783 /*template_arg_p=*/false,
17788 if (!function_p
&& identifier_p (decl
))
17790 if (complain
& tf_error
)
17791 unqualified_name_lookup_error (decl
);
17792 decl
= error_mark_node
;
17797 case TEMPLATE_ID_EXPR
:
17800 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17801 tree targs
= TREE_OPERAND (t
, 1);
17804 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17805 if (targs
== error_mark_node
)
17806 RETURN (error_mark_node
);
17808 if (TREE_CODE (templ
) == SCOPE_REF
)
17810 tree name
= TREE_OPERAND (templ
, 1);
17811 tree tid
= lookup_template_function (name
, targs
);
17812 TREE_OPERAND (templ
, 1) = tid
;
17816 if (variable_template_p (templ
))
17817 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17819 if (TREE_CODE (templ
) == COMPONENT_REF
)
17821 object
= TREE_OPERAND (templ
, 0);
17822 templ
= TREE_OPERAND (templ
, 1);
17825 object
= NULL_TREE
;
17826 templ
= lookup_template_function (templ
, targs
);
17829 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17830 object
, templ
, NULL_TREE
));
17832 RETURN (baselink_for_fns (templ
));
17837 tree r
= RECUR (TREE_OPERAND (t
, 0));
17839 if (REFERENCE_REF_P (t
))
17841 /* A type conversion to reference type will be enclosed in
17842 such an indirect ref, but the substitution of the cast
17843 will have also added such an indirect ref. */
17844 r
= convert_from_reference (r
);
17847 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17848 complain
|decltype_flag
);
17850 if (REF_PARENTHESIZED_P (t
))
17851 r
= force_paren_expr (r
);
17858 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17859 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17860 RETURN (build_nop (type
, op0
));
17863 case IMPLICIT_CONV_EXPR
:
17865 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17866 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17867 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17869 retval
= copy_node (t
);
17870 TREE_TYPE (retval
) = type
;
17871 TREE_OPERAND (retval
, 0) = expr
;
17874 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17875 /* We'll pass this to convert_nontype_argument again, we don't need
17876 to actually perform any conversion here. */
17878 int flags
= LOOKUP_IMPLICIT
;
17879 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17880 flags
= LOOKUP_NORMAL
;
17881 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17887 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17888 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17889 if (op0
== error_mark_node
)
17890 RETURN (error_mark_node
);
17891 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17895 case REINTERPRET_CAST_EXPR
:
17896 case CONST_CAST_EXPR
:
17897 case DYNAMIC_CAST_EXPR
:
17898 case STATIC_CAST_EXPR
:
17901 tree op
, r
= NULL_TREE
;
17903 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17904 if (integral_constant_expression_p
17905 && !cast_valid_in_integral_constant_expression_p (type
))
17907 if (complain
& tf_error
)
17908 error ("a cast to a type other than an integral or "
17909 "enumeration type cannot appear in a constant-expression");
17910 RETURN (error_mark_node
);
17913 op
= RECUR (TREE_OPERAND (t
, 0));
17915 warning_sentinel
s(warn_useless_cast
);
17916 warning_sentinel
s2(warn_ignored_qualifiers
);
17917 switch (TREE_CODE (t
))
17920 r
= build_functional_cast (type
, op
, complain
);
17922 case REINTERPRET_CAST_EXPR
:
17923 r
= build_reinterpret_cast (type
, op
, complain
);
17925 case CONST_CAST_EXPR
:
17926 r
= build_const_cast (type
, op
, complain
);
17928 case DYNAMIC_CAST_EXPR
:
17929 r
= build_dynamic_cast (type
, op
, complain
);
17931 case STATIC_CAST_EXPR
:
17932 r
= build_static_cast (type
, op
, complain
);
17935 gcc_unreachable ();
17941 case POSTDECREMENT_EXPR
:
17942 case POSTINCREMENT_EXPR
:
17943 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17944 args
, complain
, in_decl
);
17945 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17946 complain
|decltype_flag
));
17948 case PREDECREMENT_EXPR
:
17949 case PREINCREMENT_EXPR
:
17953 case TRUTH_NOT_EXPR
:
17954 case UNARY_PLUS_EXPR
: /* Unary + */
17955 case REALPART_EXPR
:
17956 case IMAGPART_EXPR
:
17957 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17958 RECUR (TREE_OPERAND (t
, 0)),
17959 complain
|decltype_flag
));
17961 case FIX_TRUNC_EXPR
:
17962 gcc_unreachable ();
17965 op1
= TREE_OPERAND (t
, 0);
17966 if (TREE_CODE (op1
) == LABEL_DECL
)
17967 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17968 EXPR_LOCATION (op1
)));
17969 if (TREE_CODE (op1
) == SCOPE_REF
)
17970 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17971 /*done=*/true, /*address_p=*/true);
17973 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17975 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17976 complain
|decltype_flag
));
17981 case TRUNC_DIV_EXPR
:
17982 case CEIL_DIV_EXPR
:
17983 case FLOOR_DIV_EXPR
:
17984 case ROUND_DIV_EXPR
:
17985 case EXACT_DIV_EXPR
:
17989 case TRUNC_MOD_EXPR
:
17990 case FLOOR_MOD_EXPR
:
17991 case TRUTH_ANDIF_EXPR
:
17992 case TRUTH_ORIF_EXPR
:
17993 case TRUTH_AND_EXPR
:
17994 case TRUTH_OR_EXPR
:
18010 warning_sentinel
s1(warn_type_limits
);
18011 warning_sentinel
s2(warn_div_by_zero
);
18012 warning_sentinel
s3(warn_logical_op
);
18013 warning_sentinel
s4(warn_tautological_compare
);
18014 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18015 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18016 tree r
= build_x_binary_op
18017 (input_location
, TREE_CODE (t
),
18019 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
18021 : TREE_CODE (TREE_OPERAND (t
, 0))),
18023 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
18025 : TREE_CODE (TREE_OPERAND (t
, 1))),
18027 complain
|decltype_flag
);
18028 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
18029 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18034 case POINTER_PLUS_EXPR
:
18036 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18037 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18038 RETURN (fold_build_pointer_plus (op0
, op1
));
18042 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
18043 /*address_p=*/false));
18045 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18046 args
, complain
, in_decl
);
18047 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
18048 RECUR (TREE_OPERAND (t
, 1)),
18049 complain
|decltype_flag
));
18052 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
18053 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
18054 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18061 op1
= TREE_OPERAND (t
, 0);
18062 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
18063 op1
= TREE_TYPE (op1
);
18064 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
18065 && ALIGNOF_EXPR_STD_P (t
));
18068 /* When there are no ARGS, we are trying to evaluate a
18069 non-dependent expression from the parser. Trying to do
18070 the substitutions may not work. */
18072 op1
= TREE_TYPE (op1
);
18076 ++cp_unevaluated_operand
;
18077 ++c_inhibit_evaluation_warnings
;
18079 op1
= tsubst (op1
, args
, complain
, in_decl
);
18081 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18082 /*function_p=*/false,
18083 /*integral_constant_expression_p=*/
18085 --cp_unevaluated_operand
;
18086 --c_inhibit_evaluation_warnings
;
18089 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
18090 complain
& tf_error
);
18092 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
18093 complain
& tf_error
);
18094 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
18096 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
18098 if (!processing_template_decl
&& TYPE_P (op1
))
18100 r
= build_min (SIZEOF_EXPR
, size_type_node
,
18101 build1 (NOP_EXPR
, op1
, error_mark_node
));
18102 SIZEOF_EXPR_TYPE_P (r
) = 1;
18105 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
18106 TREE_SIDE_EFFECTS (r
) = 0;
18107 TREE_READONLY (r
) = 1;
18109 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
18114 case AT_ENCODE_EXPR
:
18116 op1
= TREE_OPERAND (t
, 0);
18117 ++cp_unevaluated_operand
;
18118 ++c_inhibit_evaluation_warnings
;
18119 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18120 /*function_p=*/false,
18121 /*integral_constant_expression_p=*/false);
18122 --cp_unevaluated_operand
;
18123 --c_inhibit_evaluation_warnings
;
18124 RETURN (objc_build_encode_expr (op1
));
18127 case NOEXCEPT_EXPR
:
18128 op1
= TREE_OPERAND (t
, 0);
18129 ++cp_unevaluated_operand
;
18130 ++c_inhibit_evaluation_warnings
;
18131 ++cp_noexcept_operand
;
18132 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18133 /*function_p=*/false,
18134 /*integral_constant_expression_p=*/false);
18135 --cp_unevaluated_operand
;
18136 --c_inhibit_evaluation_warnings
;
18137 --cp_noexcept_operand
;
18138 RETURN (finish_noexcept_expr (op1
, complain
));
18142 warning_sentinel
s(warn_div_by_zero
);
18143 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
18144 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
18145 tree r
= build_x_modify_expr
18146 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
18147 complain
|decltype_flag
);
18148 /* TREE_NO_WARNING must be set if either the expression was
18149 parenthesized or it uses an operator such as >>= rather
18150 than plain assignment. In the former case, it was already
18151 set and must be copied. In the latter case,
18152 build_x_modify_expr sets it and it must not be reset
18154 if (TREE_NO_WARNING (t
))
18155 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18161 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18162 args
, complain
, in_decl
);
18163 /* Remember that there was a reference to this entity. */
18165 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
18166 RETURN (error_mark_node
);
18167 RETURN (build_x_arrow (input_location
, op1
, complain
));
18171 tree placement
= RECUR (TREE_OPERAND (t
, 0));
18172 tree init
= RECUR (TREE_OPERAND (t
, 3));
18173 vec
<tree
, va_gc
> *placement_vec
;
18174 vec
<tree
, va_gc
> *init_vec
;
18177 if (placement
== NULL_TREE
)
18178 placement_vec
= NULL
;
18181 placement_vec
= make_tree_vector ();
18182 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
18183 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
18186 /* If there was an initializer in the original tree, but it
18187 instantiated to an empty list, then we should pass a
18188 non-NULL empty vector to tell build_new that it was an
18189 empty initializer() rather than no initializer. This can
18190 only happen when the initializer is a pack expansion whose
18191 parameter packs are of length zero. */
18192 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
18196 init_vec
= make_tree_vector ();
18197 if (init
== void_node
)
18198 gcc_assert (init_vec
!= NULL
);
18201 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
18202 vec_safe_push (init_vec
, TREE_VALUE (init
));
18206 /* Avoid passing an enclosing decl to valid_array_size_p. */
18207 in_decl
= NULL_TREE
;
18209 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
18210 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18211 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
18212 NEW_EXPR_USE_GLOBAL (t
),
18215 if (placement_vec
!= NULL
)
18216 release_tree_vector (placement_vec
);
18217 if (init_vec
!= NULL
)
18218 release_tree_vector (init_vec
);
18225 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18226 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18227 RETURN (delete_sanity (op0
, op1
,
18228 DELETE_EXPR_USE_VEC (t
),
18229 DELETE_EXPR_USE_GLOBAL (t
),
18233 case COMPOUND_EXPR
:
18235 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
18236 complain
& ~tf_decltype
, in_decl
,
18237 /*function_p=*/false,
18238 integral_constant_expression_p
);
18239 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
18241 RECUR (TREE_OPERAND (t
, 1)),
18242 complain
|decltype_flag
));
18248 vec
<tree
, va_gc
> *call_args
;
18249 unsigned int nargs
, i
;
18254 function
= CALL_EXPR_FN (t
);
18255 /* Internal function with no arguments. */
18256 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
18259 /* When we parsed the expression, we determined whether or
18260 not Koenig lookup should be performed. */
18261 koenig_p
= KOENIG_LOOKUP_P (t
);
18262 if (function
== NULL_TREE
)
18265 qualified_p
= false;
18267 else if (TREE_CODE (function
) == SCOPE_REF
)
18269 qualified_p
= true;
18270 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
18272 /*address_p=*/false);
18274 else if (koenig_p
&& identifier_p (function
))
18276 /* Do nothing; calling tsubst_copy_and_build on an identifier
18277 would incorrectly perform unqualified lookup again.
18279 Note that we can also have an IDENTIFIER_NODE if the earlier
18280 unqualified lookup found a member function; in that case
18281 koenig_p will be false and we do want to do the lookup
18282 again to find the instantiated member function.
18284 FIXME but doing that causes c++/15272, so we need to stop
18285 using IDENTIFIER_NODE in that situation. */
18286 qualified_p
= false;
18290 if (TREE_CODE (function
) == COMPONENT_REF
)
18292 tree op
= TREE_OPERAND (function
, 1);
18294 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
18295 || (BASELINK_P (op
)
18296 && BASELINK_QUALIFIED_P (op
)));
18299 qualified_p
= false;
18301 if (TREE_CODE (function
) == ADDR_EXPR
18302 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
18303 /* Avoid error about taking the address of a constructor. */
18304 function
= TREE_OPERAND (function
, 0);
18306 function
= tsubst_copy_and_build (function
, args
, complain
,
18309 integral_constant_expression_p
);
18311 if (BASELINK_P (function
))
18312 qualified_p
= true;
18315 nargs
= call_expr_nargs (t
);
18316 call_args
= make_tree_vector ();
18317 for (i
= 0; i
< nargs
; ++i
)
18319 tree arg
= CALL_EXPR_ARG (t
, i
);
18321 if (!PACK_EXPANSION_P (arg
))
18322 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
18325 /* Expand the pack expansion and push each entry onto
18327 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
18328 if (TREE_CODE (arg
) == TREE_VEC
)
18330 unsigned int len
, j
;
18332 len
= TREE_VEC_LENGTH (arg
);
18333 for (j
= 0; j
< len
; ++j
)
18335 tree value
= TREE_VEC_ELT (arg
, j
);
18336 if (value
!= NULL_TREE
)
18337 value
= convert_from_reference (value
);
18338 vec_safe_push (call_args
, value
);
18343 /* A partial substitution. Add one entry. */
18344 vec_safe_push (call_args
, arg
);
18349 /* We do not perform argument-dependent lookup if normal
18350 lookup finds a non-function, in accordance with the
18351 expected resolution of DR 218. */
18353 && ((is_overloaded_fn (function
)
18354 /* If lookup found a member function, the Koenig lookup is
18355 not appropriate, even if an unqualified-name was used
18356 to denote the function. */
18357 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
18358 || identifier_p (function
))
18359 /* Only do this when substitution turns a dependent call
18360 into a non-dependent call. */
18361 && type_dependent_expression_p_push (t
)
18362 && !any_type_dependent_arguments_p (call_args
))
18363 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
18365 if (function
!= NULL_TREE
18366 && identifier_p (function
)
18367 && !any_type_dependent_arguments_p (call_args
))
18369 if (koenig_p
&& (complain
& tf_warning_or_error
))
18371 /* For backwards compatibility and good diagnostics, try
18372 the unqualified lookup again if we aren't in SFINAE
18374 tree unq
= (tsubst_copy_and_build
18375 (function
, args
, complain
, in_decl
, true,
18376 integral_constant_expression_p
));
18377 if (unq
== error_mark_node
)
18379 release_tree_vector (call_args
);
18380 RETURN (error_mark_node
);
18383 if (unq
!= function
)
18385 /* In a lambda fn, we have to be careful to not
18386 introduce new this captures. Legacy code can't
18387 be using lambdas anyway, so it's ok to be
18389 bool in_lambda
= (current_class_type
18390 && LAMBDA_TYPE_P (current_class_type
));
18391 char const *const msg
18392 = G_("%qD was not declared in this scope, "
18393 "and no declarations were found by "
18394 "argument-dependent lookup at the point "
18395 "of instantiation");
18399 error_at (cp_expr_loc_or_loc (t
, input_location
),
18402 diag
= permerror (cp_expr_loc_or_loc (t
, input_location
),
18408 if (INDIRECT_REF_P (fn
))
18409 fn
= TREE_OPERAND (fn
, 0);
18410 if (is_overloaded_fn (fn
))
18411 fn
= get_first_fn (fn
);
18414 /* Can't say anything more. */;
18415 else if (DECL_CLASS_SCOPE_P (fn
))
18417 location_t loc
= cp_expr_loc_or_loc (t
,
18420 "declarations in dependent base %qT are "
18421 "not found by unqualified lookup",
18422 DECL_CLASS_CONTEXT (fn
));
18423 if (current_class_ptr
)
18425 "use %<this->%D%> instead", function
);
18428 "use %<%T::%D%> instead",
18429 current_class_name
, function
);
18432 inform (DECL_SOURCE_LOCATION (fn
),
18433 "%qD declared here, later in the "
18434 "translation unit", fn
);
18437 release_tree_vector (call_args
);
18438 RETURN (error_mark_node
);
18445 if (identifier_p (function
))
18447 if (complain
& tf_error
)
18448 unqualified_name_lookup_error (function
);
18449 release_tree_vector (call_args
);
18450 RETURN (error_mark_node
);
18454 /* Remember that there was a reference to this entity. */
18455 if (function
!= NULL_TREE
18456 && DECL_P (function
)
18457 && !mark_used (function
, complain
) && !(complain
& tf_error
))
18459 release_tree_vector (call_args
);
18460 RETURN (error_mark_node
);
18463 /* Put back tf_decltype for the actual call. */
18464 complain
|= decltype_flag
;
18466 if (function
== NULL_TREE
)
18467 switch (CALL_EXPR_IFN (t
))
18470 gcc_assert (nargs
== 1);
18471 if (vec_safe_length (call_args
) != 1)
18473 error_at (cp_expr_loc_or_loc (t
, input_location
),
18474 "wrong number of arguments to "
18475 "%<__builtin_launder%>");
18476 ret
= error_mark_node
;
18479 ret
= finish_builtin_launder (cp_expr_loc_or_loc (t
,
18481 (*call_args
)[0], complain
);
18485 /* Unsupported internal function with arguments. */
18486 gcc_unreachable ();
18488 else if (TREE_CODE (function
) == OFFSET_REF
18489 || TREE_CODE (function
) == DOTSTAR_EXPR
18490 || TREE_CODE (function
) == MEMBER_REF
)
18491 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
18493 else if (TREE_CODE (function
) == COMPONENT_REF
)
18495 tree instance
= TREE_OPERAND (function
, 0);
18496 tree fn
= TREE_OPERAND (function
, 1);
18498 if (processing_template_decl
18499 && (type_dependent_expression_p (instance
)
18500 || (!BASELINK_P (fn
)
18501 && TREE_CODE (fn
) != FIELD_DECL
)
18502 || type_dependent_expression_p (fn
)
18503 || any_type_dependent_arguments_p (call_args
)))
18504 ret
= build_min_nt_call_vec (function
, call_args
);
18505 else if (!BASELINK_P (fn
))
18506 ret
= finish_call_expr (function
, &call_args
,
18507 /*disallow_virtual=*/false,
18508 /*koenig_p=*/false,
18511 ret
= (build_new_method_call
18513 &call_args
, NULL_TREE
,
18514 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18519 ret
= finish_call_expr (function
, &call_args
,
18520 /*disallow_virtual=*/qualified_p
,
18524 release_tree_vector (call_args
);
18526 if (ret
!= error_mark_node
)
18528 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18529 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18530 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18531 bool thk
= CALL_FROM_THUNK_P (t
);
18532 if (op
|| ord
|| rev
|| thk
)
18534 function
= extract_call_expr (ret
);
18535 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18536 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18537 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18540 if (TREE_CODE (function
) == CALL_EXPR
)
18541 CALL_FROM_THUNK_P (function
) = true;
18543 AGGR_INIT_FROM_THUNK_P (function
) = true;
18544 /* The thunk location is not interesting. */
18545 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
18555 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18556 cond
= mark_rvalue_use (cond
);
18557 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
18560 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
18562 if (integer_zerop (folded_cond
))
18564 ++c_inhibit_evaluation_warnings
;
18565 exp1
= RECUR (TREE_OPERAND (t
, 1));
18566 --c_inhibit_evaluation_warnings
;
18567 exp2
= RECUR (TREE_OPERAND (t
, 2));
18571 exp1
= RECUR (TREE_OPERAND (t
, 1));
18572 ++c_inhibit_evaluation_warnings
;
18573 exp2
= RECUR (TREE_OPERAND (t
, 2));
18574 --c_inhibit_evaluation_warnings
;
18576 cond
= folded_cond
;
18580 exp1
= RECUR (TREE_OPERAND (t
, 1));
18581 exp2
= RECUR (TREE_OPERAND (t
, 2));
18584 warning_sentinel
s(warn_duplicated_branches
);
18585 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
18586 cond
, exp1
, exp2
, complain
));
18589 case PSEUDO_DTOR_EXPR
:
18591 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18592 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18593 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
18594 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
18600 tree purpose
, value
, chain
;
18602 if (t
== void_list_node
)
18605 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
18606 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
18608 /* We have pack expansions, so expand those and
18609 create a new list out of it. */
18610 tree purposevec
= NULL_TREE
;
18611 tree valuevec
= NULL_TREE
;
18615 /* Expand the argument expressions. */
18616 if (TREE_PURPOSE (t
))
18617 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18618 complain
, in_decl
);
18619 if (TREE_VALUE (t
))
18620 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18621 complain
, in_decl
);
18623 /* Build the rest of the list. */
18624 chain
= TREE_CHAIN (t
);
18625 if (chain
&& chain
!= void_type_node
)
18626 chain
= RECUR (chain
);
18628 /* Determine the number of arguments. */
18629 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18631 len
= TREE_VEC_LENGTH (purposevec
);
18632 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18634 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18635 len
= TREE_VEC_LENGTH (valuevec
);
18638 /* Since we only performed a partial substitution into
18639 the argument pack, we only RETURN (a single list
18641 if (purposevec
== TREE_PURPOSE (t
)
18642 && valuevec
== TREE_VALUE (t
)
18643 && chain
== TREE_CHAIN (t
))
18646 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18649 /* Convert the argument vectors into a TREE_LIST */
18653 /* Grab the Ith values. */
18655 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18658 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18661 /* Build the list (backwards). */
18662 chain
= tree_cons (purpose
, value
, chain
);
18668 purpose
= TREE_PURPOSE (t
);
18670 purpose
= RECUR (purpose
);
18671 value
= TREE_VALUE (t
);
18673 value
= RECUR (value
);
18674 chain
= TREE_CHAIN (t
);
18675 if (chain
&& chain
!= void_type_node
)
18676 chain
= RECUR (chain
);
18677 if (purpose
== TREE_PURPOSE (t
)
18678 && value
== TREE_VALUE (t
)
18679 && chain
== TREE_CHAIN (t
))
18681 RETURN (tree_cons (purpose
, value
, chain
));
18684 case COMPONENT_REF
:
18691 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18692 args
, complain
, in_decl
);
18693 /* Remember that there was a reference to this entity. */
18694 if (DECL_P (object
)
18695 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18696 RETURN (error_mark_node
);
18697 object_type
= TREE_TYPE (object
);
18699 member
= TREE_OPERAND (t
, 1);
18700 if (BASELINK_P (member
))
18701 member
= tsubst_baselink (member
,
18702 non_reference (TREE_TYPE (object
)),
18703 args
, complain
, in_decl
);
18705 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18706 if (member
== error_mark_node
)
18707 RETURN (error_mark_node
);
18709 if (TREE_CODE (member
) == FIELD_DECL
)
18711 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18712 if (TREE_CODE (r
) == COMPONENT_REF
)
18713 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18716 else if (type_dependent_expression_p (object
))
18717 /* We can't do much here. */;
18718 else if (!CLASS_TYPE_P (object_type
))
18720 if (scalarish_type_p (object_type
))
18722 tree s
= NULL_TREE
;
18723 tree dtor
= member
;
18725 if (TREE_CODE (dtor
) == SCOPE_REF
)
18727 s
= TREE_OPERAND (dtor
, 0);
18728 dtor
= TREE_OPERAND (dtor
, 1);
18730 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18732 dtor
= TREE_OPERAND (dtor
, 0);
18734 RETURN (finish_pseudo_destructor_expr
18735 (object
, s
, dtor
, input_location
));
18739 else if (TREE_CODE (member
) == SCOPE_REF
18740 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18742 /* Lookup the template functions now that we know what the
18744 tree scope
= TREE_OPERAND (member
, 0);
18745 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18746 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18747 member
= lookup_qualified_name (scope
, tmpl
,
18748 /*is_type_p=*/false,
18749 /*complain=*/false);
18750 if (BASELINK_P (member
))
18752 BASELINK_FUNCTIONS (member
)
18753 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18755 member
= (adjust_result_of_qualified_name_lookup
18756 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18761 qualified_name_lookup_error (scope
, tmpl
, member
,
18763 RETURN (error_mark_node
);
18766 else if (TREE_CODE (member
) == SCOPE_REF
18767 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18768 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18770 if (complain
& tf_error
)
18772 if (TYPE_P (TREE_OPERAND (member
, 0)))
18773 error ("%qT is not a class or namespace",
18774 TREE_OPERAND (member
, 0));
18776 error ("%qD is not a class or namespace",
18777 TREE_OPERAND (member
, 0));
18779 RETURN (error_mark_node
);
18782 r
= finish_class_member_access_expr (object
, member
,
18783 /*template_p=*/false,
18785 if (TREE_CODE (r
) == COMPONENT_REF
)
18786 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18791 RETURN (build_throw
18792 (RECUR (TREE_OPERAND (t
, 0))));
18796 vec
<constructor_elt
, va_gc
> *n
;
18797 constructor_elt
*ce
;
18798 unsigned HOST_WIDE_INT idx
;
18799 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18800 bool process_index_p
;
18802 bool need_copy_p
= false;
18805 if (type
== error_mark_node
)
18806 RETURN (error_mark_node
);
18808 /* We do not want to process the index of aggregate
18809 initializers as they are identifier nodes which will be
18810 looked up by digest_init. */
18811 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18813 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18814 newlen
= vec_safe_length (n
);
18815 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18817 if (ce
->index
&& process_index_p
18818 /* An identifier index is looked up in the type
18819 being initialized, not the current scope. */
18820 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18821 ce
->index
= RECUR (ce
->index
);
18823 if (PACK_EXPANSION_P (ce
->value
))
18825 /* Substitute into the pack expansion. */
18826 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18829 if (ce
->value
== error_mark_node
18830 || PACK_EXPANSION_P (ce
->value
))
18832 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18833 /* Just move the argument into place. */
18834 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18837 /* Update the length of the final CONSTRUCTOR
18838 arguments vector, and note that we will need to
18840 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18841 need_copy_p
= true;
18845 ce
->value
= RECUR (ce
->value
);
18850 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18852 vec_alloc (n
, newlen
);
18853 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18855 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18857 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18858 for (i
= 0; i
< len
; ++i
)
18859 CONSTRUCTOR_APPEND_ELT (n
, 0,
18860 TREE_VEC_ELT (ce
->value
, i
));
18863 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18867 r
= build_constructor (init_list_type_node
, n
);
18868 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18870 if (TREE_HAS_CONSTRUCTOR (t
))
18872 fcl_t cl
= fcl_functional
;
18873 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18875 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18878 TREE_TYPE (r
) = type
;
18884 tree operand_0
= TREE_OPERAND (t
, 0);
18885 if (TYPE_P (operand_0
))
18887 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18888 RETURN (get_typeid (operand_0
, complain
));
18892 operand_0
= RECUR (operand_0
);
18893 RETURN (build_typeid (operand_0
, complain
));
18904 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18905 /* ??? We're doing a subset of finish_id_expression here. */
18907 && !processing_template_decl
18908 && !cp_unevaluated_operand
18909 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18910 && CP_DECL_THREAD_LOCAL_P (r
))
18912 if (tree wrap
= get_tls_wrapper_fn (r
))
18913 /* Replace an evaluated use of the thread_local variable with
18914 a call to its wrapper. */
18915 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18917 else if (outer_automatic_var_p (r
))
18918 r
= process_outer_var_ref (r
, complain
);
18920 if (!TYPE_REF_P (TREE_TYPE (t
)))
18921 /* If the original type was a reference, we'll be wrapped in
18922 the appropriate INDIRECT_REF. */
18923 r
= convert_from_reference (r
);
18929 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18930 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18931 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18934 case OFFSETOF_EXPR
:
18937 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18938 in_decl
, /*function_p=*/false,
18939 /*integral_constant_expression_p=*/false);
18940 RETURN (finish_offsetof (object_ptr
,
18941 RECUR (TREE_OPERAND (t
, 0)),
18942 EXPR_LOCATION (t
)));
18945 case ADDRESSOF_EXPR
:
18946 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18947 RECUR (TREE_OPERAND (t
, 0)), complain
));
18951 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18952 complain
, in_decl
);
18954 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18955 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18956 type2
= RECUR (type2
);
18958 type2
= tsubst (type2
, args
, complain
, in_decl
);
18960 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18965 tree old_stmt_expr
= cur_stmt_expr
;
18966 tree stmt_expr
= begin_stmt_expr ();
18968 cur_stmt_expr
= stmt_expr
;
18969 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18970 integral_constant_expression_p
);
18971 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18972 cur_stmt_expr
= old_stmt_expr
;
18974 /* If the resulting list of expression statement is empty,
18975 fold it further into void_node. */
18976 if (empty_expr_stmt_p (stmt_expr
))
18977 stmt_expr
= void_node
;
18979 RETURN (stmt_expr
);
18984 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18986 RETURN (build_lambda_object (r
));
18990 /* We can get here for a constant initializer of non-dependent type.
18991 FIXME stop folding in cp_parser_initializer_clause. */
18993 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18998 case TRANSACTION_EXPR
:
18999 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
19000 integral_constant_expression_p
));
19003 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
19005 case VEC_PERM_EXPR
:
19007 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19008 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19009 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19010 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
19014 case REQUIRES_EXPR
:
19015 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
19017 case NON_LVALUE_EXPR
:
19018 case VIEW_CONVERT_EXPR
:
19019 /* We should only see these for location wrapper nodes, or within
19020 instantiate_non_dependent_expr (when args is NULL_TREE). */
19021 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
19022 if (location_wrapper_p (t
))
19023 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
19024 EXPR_LOCATION (t
)));
19028 /* Handle Objective-C++ constructs, if appropriate. */
19031 = objcp_tsubst_copy_and_build (t
, args
, complain
,
19032 in_decl
, /*function_p=*/false);
19036 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19042 input_location
= loc
;
19046 /* Verify that the instantiated ARGS are valid. For type arguments,
19047 make sure that the type's linkage is ok. For non-type arguments,
19048 make sure they are constants if they are integral or enumerations.
19049 Emit an error under control of COMPLAIN, and return TRUE on error. */
19052 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
19054 if (dependent_template_arg_p (t
))
19056 if (ARGUMENT_PACK_P (t
))
19058 tree vec
= ARGUMENT_PACK_ARGS (t
);
19059 int len
= TREE_VEC_LENGTH (vec
);
19060 bool result
= false;
19063 for (i
= 0; i
< len
; ++i
)
19064 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
19068 else if (TYPE_P (t
))
19070 /* [basic.link]: A name with no linkage (notably, the name
19071 of a class or enumeration declared in a local scope)
19072 shall not be used to declare an entity with linkage.
19073 This implies that names with no linkage cannot be used as
19076 DR 757 relaxes this restriction for C++0x. */
19077 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
19078 : no_linkage_check (t
, /*relaxed_p=*/false));
19082 /* DR 488 makes use of a type with no linkage cause
19083 type deduction to fail. */
19084 if (complain
& tf_error
)
19086 if (TYPE_UNNAMED_P (nt
))
19087 error ("%qT is/uses unnamed type", t
);
19089 error ("template argument for %qD uses local type %qT",
19094 /* In order to avoid all sorts of complications, we do not
19095 allow variably-modified types as template arguments. */
19096 else if (variably_modified_type_p (t
, NULL_TREE
))
19098 if (complain
& tf_error
)
19099 error ("%qT is a variably modified type", t
);
19103 /* Class template and alias template arguments should be OK. */
19104 else if (DECL_TYPE_TEMPLATE_P (t
))
19106 /* A non-type argument of integral or enumerated type must be a
19108 else if (TREE_TYPE (t
)
19109 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
19110 && !REFERENCE_REF_P (t
)
19111 && !TREE_CONSTANT (t
))
19113 if (complain
& tf_error
)
19114 error ("integral expression %qE is not constant", t
);
19121 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
19123 int ix
, len
= DECL_NTPARMS (tmpl
);
19124 bool result
= false;
19126 for (ix
= 0; ix
!= len
; ix
++)
19128 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
19131 if (result
&& (complain
& tf_error
))
19132 error (" trying to instantiate %qD", tmpl
);
19136 /* We're out of SFINAE context now, so generate diagnostics for the access
19137 errors we saw earlier when instantiating D from TMPL and ARGS. */
19140 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
19142 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19143 tree type
= TREE_TYPE (pattern
);
19144 location_t loc
= input_location
;
19146 push_access_scope (d
);
19147 push_deferring_access_checks (dk_no_deferred
);
19148 input_location
= DECL_SOURCE_LOCATION (pattern
);
19149 tsubst (type
, args
, tf_warning_or_error
, d
);
19150 input_location
= loc
;
19151 pop_deferring_access_checks ();
19152 pop_access_scope (d
);
19155 /* Instantiate the indicated variable, function, or alias template TMPL with
19156 the template arguments in TARG_PTR. */
19159 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19161 tree targ_ptr
= orig_args
;
19165 bool access_ok
= true;
19167 if (tmpl
== error_mark_node
)
19168 return error_mark_node
;
19170 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
19172 /* If this function is a clone, handle it specially. */
19173 if (DECL_CLONED_FUNCTION_P (tmpl
))
19178 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19179 DECL_CLONED_FUNCTION. */
19180 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
19181 targ_ptr
, complain
);
19182 if (spec
== error_mark_node
)
19183 return error_mark_node
;
19185 /* Look for the clone. */
19186 FOR_EACH_CLONE (clone
, spec
)
19187 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
19189 /* We should always have found the clone by now. */
19190 gcc_unreachable ();
19194 if (targ_ptr
== error_mark_node
)
19195 return error_mark_node
;
19197 /* Check to see if we already have this specialization. */
19198 gen_tmpl
= most_general_template (tmpl
);
19199 if (TMPL_ARGS_DEPTH (targ_ptr
)
19200 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
19201 /* targ_ptr only has the innermost template args, so add the outer ones
19202 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19203 the case of a non-dependent call within a template definition). */
19204 targ_ptr
= (add_outermost_template_args
19205 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
19208 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19209 but it doesn't seem to be on the hot path. */
19210 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
19212 gcc_assert (tmpl
== gen_tmpl
19213 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
19215 || fndecl
== NULL_TREE
);
19217 if (spec
!= NULL_TREE
)
19219 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
19221 if (complain
& tf_error
)
19222 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
19223 return error_mark_node
;
19228 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
19230 return error_mark_node
;
19232 /* We are building a FUNCTION_DECL, during which the access of its
19233 parameters and return types have to be checked. However this
19234 FUNCTION_DECL which is the desired context for access checking
19235 is not built yet. We solve this chicken-and-egg problem by
19236 deferring all checks until we have the FUNCTION_DECL. */
19237 push_deferring_access_checks (dk_deferred
);
19239 /* Instantiation of the function happens in the context of the function
19240 template, not the context of the overload resolution we're doing. */
19241 push_to_top_level ();
19242 /* If there are dependent arguments, e.g. because we're doing partial
19243 ordering, make sure processing_template_decl stays set. */
19244 if (uses_template_parms (targ_ptr
))
19245 ++processing_template_decl
;
19246 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19248 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
19249 complain
, gen_tmpl
, true);
19250 push_nested_class (ctx
);
19253 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
19255 fndecl
= NULL_TREE
;
19256 if (VAR_P (pattern
))
19258 /* We need to determine if we're using a partial or explicit
19259 specialization now, because the type of the variable could be
19261 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
19262 tree elt
= most_specialized_partial_spec (tid
, complain
);
19263 if (elt
== error_mark_node
)
19264 pattern
= error_mark_node
;
19267 tree partial_tmpl
= TREE_VALUE (elt
);
19268 tree partial_args
= TREE_PURPOSE (elt
);
19269 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
19270 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
19274 /* Substitute template parameters to obtain the specialization. */
19275 if (fndecl
== NULL_TREE
)
19276 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
19277 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19278 pop_nested_class ();
19279 pop_from_top_level ();
19281 if (fndecl
== error_mark_node
)
19283 pop_deferring_access_checks ();
19284 return error_mark_node
;
19287 /* The DECL_TI_TEMPLATE should always be the immediate parent
19288 template, not the most general template. */
19289 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
19290 DECL_TI_ARGS (fndecl
) = targ_ptr
;
19292 /* Now we know the specialization, compute access previously
19293 deferred. Do no access control for inheriting constructors,
19294 as we already checked access for the inherited constructor. */
19295 if (!(flag_new_inheriting_ctors
19296 && DECL_INHERITED_CTOR (fndecl
)))
19298 push_access_scope (fndecl
);
19299 if (!perform_deferred_access_checks (complain
))
19301 pop_access_scope (fndecl
);
19303 pop_deferring_access_checks ();
19305 /* If we've just instantiated the main entry point for a function,
19306 instantiate all the alternate entry points as well. We do this
19307 by cloning the instantiation of the main entry point, not by
19308 instantiating the template clones. */
19309 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
19310 clone_function_decl (fndecl
, /*update_methods=*/false);
19314 if (!(complain
& tf_error
))
19316 /* Remember to reinstantiate when we're out of SFINAE so the user
19317 can see the errors. */
19318 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
19320 return error_mark_node
;
19325 /* Wrapper for instantiate_template_1. */
19328 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19331 timevar_push (TV_TEMPLATE_INST
);
19332 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
19333 timevar_pop (TV_TEMPLATE_INST
);
19337 /* Instantiate the alias template TMPL with ARGS. Also push a template
19338 instantiation level, which instantiate_template doesn't do because
19339 functions and variables have sufficient context established by the
19343 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
19345 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
19346 return error_mark_node
;
19347 if (!push_tinst_level (tmpl
, args
))
19348 return error_mark_node
;
19351 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
19352 args
, tmpl
, complain
,
19353 /*require_all_args=*/true,
19354 /*use_default_args=*/true);
19356 tree r
= instantiate_template (tmpl
, args
, complain
);
19357 pop_tinst_level ();
19362 /* PARM is a template parameter pack for FN. Returns true iff
19363 PARM is used in a deducible way in the argument list of FN. */
19366 pack_deducible_p (tree parm
, tree fn
)
19368 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19369 for (; t
; t
= TREE_CHAIN (t
))
19371 tree type
= TREE_VALUE (t
);
19373 if (!PACK_EXPANSION_P (type
))
19375 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19376 packs
; packs
= TREE_CHAIN (packs
))
19377 if (template_args_equal (TREE_VALUE (packs
), parm
))
19379 /* The template parameter pack is used in a function parameter
19380 pack. If this is the end of the parameter list, the
19381 template parameter pack is deducible. */
19382 if (TREE_CHAIN (t
) == void_list_node
)
19385 /* Otherwise, not. Well, it could be deduced from
19386 a non-pack parameter, but doing so would end up with
19387 a deduction mismatch, so don't bother. */
19391 /* The template parameter pack isn't used in any function parameter
19392 packs, but it might be used deeper, e.g. tuple<Args...>. */
19396 /* Subroutine of fn_type_unification: check non-dependent parms for
19400 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
19401 tree fn
, unification_kind_t strict
, int flags
,
19402 struct conversion
**convs
, bool explain_p
)
19404 /* Non-constructor methods need to leave a conversion for 'this', which
19405 isn't included in nargs here. */
19406 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19407 && !DECL_CONSTRUCTOR_P (fn
));
19409 for (unsigned ia
= 0;
19410 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19412 tree parm
= TREE_VALUE (parms
);
19414 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19415 && (!TREE_CHAIN (parms
)
19416 || TREE_CHAIN (parms
) == void_list_node
))
19417 /* For a function parameter pack that occurs at the end of the
19418 parameter-declaration-list, the type A of each remaining
19419 argument of the call is compared with the type P of the
19420 declarator-id of the function parameter pack. */
19423 parms
= TREE_CHAIN (parms
);
19425 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19426 /* For a function parameter pack that does not occur at the
19427 end of the parameter-declaration-list, the type of the
19428 parameter pack is a non-deduced context. */
19431 if (!uses_template_parms (parm
))
19433 tree arg
= args
[ia
];
19434 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
19435 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
19437 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
19438 conv_p
, explain_p
))
19448 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19449 NARGS elements of the arguments that are being used when calling
19450 it. TARGS is a vector into which the deduced template arguments
19453 Returns either a FUNCTION_DECL for the matching specialization of FN or
19454 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19455 true, diagnostics will be printed to explain why it failed.
19457 If FN is a conversion operator, or we are trying to produce a specific
19458 specialization, RETURN_TYPE is the return type desired.
19460 The EXPLICIT_TARGS are explicit template arguments provided via a
19463 The parameter STRICT is one of:
19466 We are deducing arguments for a function call, as in
19467 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19468 deducing arguments for a call to the result of a conversion
19469 function template, as in [over.call.object].
19472 We are deducing arguments for a conversion function, as in
19473 [temp.deduct.conv].
19476 We are deducing arguments when doing an explicit instantiation
19477 as in [temp.explicit], when determining an explicit specialization
19478 as in [temp.expl.spec], or when taking the address of a function
19479 template, as in [temp.deduct.funcaddr]. */
19482 fn_type_unification (tree fn
,
19483 tree explicit_targs
,
19486 unsigned int nargs
,
19488 unification_kind_t strict
,
19490 struct conversion
**convs
,
19496 tree decl
= NULL_TREE
;
19497 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19499 static int deduction_depth
;
19502 if (flag_new_inheriting_ctors
)
19503 fn
= strip_inheriting_ctors (fn
);
19505 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
19506 tree r
= error_mark_node
;
19508 tree full_targs
= targs
;
19509 if (TMPL_ARGS_DEPTH (targs
)
19510 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
19511 full_targs
= (add_outermost_template_args
19512 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
19516 complain
|= tf_decltype
;
19518 /* In C++0x, it's possible to have a function template whose type depends
19519 on itself recursively. This is most obvious with decltype, but can also
19520 occur with enumeration scope (c++/48969). So we need to catch infinite
19521 recursion and reject the substitution at deduction time; this function
19522 will return error_mark_node for any repeated substitution.
19524 This also catches excessive recursion such as when f<N> depends on
19525 f<N-1> across all integers, and returns error_mark_node for all the
19526 substitutions back up to the initial one.
19528 This is, of course, not reentrant. */
19529 if (excessive_deduction_depth
)
19530 return error_mark_node
;
19533 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
19535 fntype
= TREE_TYPE (fn
);
19536 if (explicit_targs
)
19540 The specified template arguments must match the template
19541 parameters in kind (i.e., type, nontype, template), and there
19542 must not be more arguments than there are parameters;
19543 otherwise type deduction fails.
19545 Nontype arguments must match the types of the corresponding
19546 nontype template parameters, or must be convertible to the
19547 types of the corresponding nontype parameters as specified in
19548 _temp.arg.nontype_, otherwise type deduction fails.
19550 All references in the function type of the function template
19551 to the corresponding template parameters are replaced by the
19552 specified template argument values. If a substitution in a
19553 template parameter or in the function type of the function
19554 template results in an invalid type, type deduction fails. */
19555 int i
, len
= TREE_VEC_LENGTH (tparms
);
19556 location_t loc
= input_location
;
19557 bool incomplete
= false;
19559 if (explicit_targs
== error_mark_node
)
19562 if (TMPL_ARGS_DEPTH (explicit_targs
)
19563 < TMPL_ARGS_DEPTH (full_targs
))
19564 explicit_targs
= add_outermost_template_args (full_targs
,
19567 /* Adjust any explicit template arguments before entering the
19568 substitution context. */
19570 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
19572 /*require_all_args=*/false,
19573 /*use_default_args=*/false));
19574 if (explicit_targs
== error_mark_node
)
19577 /* Substitute the explicit args into the function type. This is
19578 necessary so that, for instance, explicitly declared function
19579 arguments can match null pointed constants. If we were given
19580 an incomplete set of explicit args, we must not do semantic
19581 processing during substitution as we could create partial
19583 for (i
= 0; i
< len
; i
++)
19585 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
19586 bool parameter_pack
= false;
19587 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
19589 /* Dig out the actual parm. */
19590 if (TREE_CODE (parm
) == TYPE_DECL
19591 || TREE_CODE (parm
) == TEMPLATE_DECL
)
19593 parm
= TREE_TYPE (parm
);
19594 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
19596 else if (TREE_CODE (parm
) == PARM_DECL
)
19598 parm
= DECL_INITIAL (parm
);
19599 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
19602 if (!parameter_pack
&& targ
== NULL_TREE
)
19603 /* No explicit argument for this template parameter. */
19606 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
19608 /* Mark the argument pack as "incomplete". We could
19609 still deduce more arguments during unification.
19610 We remove this mark in type_unification_real. */
19613 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
19614 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
19615 = ARGUMENT_PACK_ARGS (targ
);
19618 /* We have some incomplete argument packs. */
19623 if (!push_tinst_level (fn
, explicit_targs
))
19625 excessive_deduction_depth
= true;
19628 processing_template_decl
+= incomplete
;
19629 input_location
= DECL_SOURCE_LOCATION (fn
);
19630 /* Ignore any access checks; we'll see them again in
19631 instantiate_template and they might have the wrong
19632 access path at this point. */
19633 push_deferring_access_checks (dk_deferred
);
19634 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
19635 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
19636 pop_deferring_access_checks ();
19637 input_location
= loc
;
19638 processing_template_decl
-= incomplete
;
19639 pop_tinst_level ();
19641 if (fntype
== error_mark_node
)
19644 /* Place the explicitly specified arguments in TARGS. */
19645 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
19646 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
19647 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
19650 /* Never do unification on the 'this' parameter. */
19651 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
19653 if (return_type
&& strict
== DEDUCE_CALL
)
19655 /* We're deducing for a call to the result of a template conversion
19656 function. The parms we really want are in return_type. */
19657 if (INDIRECT_TYPE_P (return_type
))
19658 return_type
= TREE_TYPE (return_type
);
19659 parms
= TYPE_ARG_TYPES (return_type
);
19661 else if (return_type
)
19665 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19666 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19667 new_args
[0] = return_type
;
19668 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19673 /* We allow incomplete unification without an error message here
19674 because the standard doesn't seem to explicitly prohibit it. Our
19675 callers must be ready to deal with unification failures in any
19678 /* If we aren't explaining yet, push tinst context so we can see where
19679 any errors (e.g. from class instantiations triggered by instantiation
19680 of default template arguments) come from. If we are explaining, this
19681 context is redundant. */
19682 if (!explain_p
&& !push_tinst_level (fn
, targs
))
19684 excessive_deduction_depth
= true;
19688 /* type_unification_real will pass back any access checks from default
19689 template argument substitution. */
19690 vec
<deferred_access_check
, va_gc
> *checks
;
19693 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19694 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19695 strict
, &checks
, explain_p
);
19697 pop_tinst_level ();
19701 /* Now that we have bindings for all of the template arguments,
19702 ensure that the arguments deduced for the template template
19703 parameters have compatible template parameter lists. We cannot
19704 check this property before we have deduced all template
19705 arguments, because the template parameter types of a template
19706 template parameter might depend on prior template parameters
19707 deduced after the template template parameter. The following
19708 ill-formed example illustrates this issue:
19710 template<typename T, template<T> class C> void f(C<5>, T);
19712 template<int N> struct X {};
19715 f(X<5>(), 5l); // error: template argument deduction fails
19718 The template parameter list of 'C' depends on the template type
19719 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19720 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19721 time that we deduce 'C'. */
19722 if (!template_template_parm_bindings_ok_p
19723 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19725 unify_inconsistent_template_template_parameters (explain_p
);
19729 /* DR 1391: All parameters have args, now check non-dependent parms for
19731 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
19735 /* All is well so far. Now, check:
19739 When all template arguments have been deduced, all uses of
19740 template parameters in nondeduced contexts are replaced with
19741 the corresponding deduced argument values. If the
19742 substitution results in an invalid type, as described above,
19743 type deduction fails. */
19744 if (!push_tinst_level (fn
, targs
))
19746 excessive_deduction_depth
= true;
19750 /* Also collect access checks from the instantiation. */
19751 reopen_deferring_access_checks (checks
);
19753 decl
= instantiate_template (fn
, targs
, complain
);
19755 checks
= get_deferred_access_checks ();
19756 pop_deferring_access_checks ();
19758 pop_tinst_level ();
19760 if (decl
== error_mark_node
)
19763 /* Now perform any access checks encountered during substitution. */
19764 push_access_scope (decl
);
19765 ok
= perform_access_checks (checks
, complain
);
19766 pop_access_scope (decl
);
19770 /* If we're looking for an exact match, check that what we got
19771 is indeed an exact match. It might not be if some template
19772 parameters are used in non-deduced contexts. But don't check
19773 for an exact match if we have dependent template arguments;
19774 in that case we're doing partial ordering, and we already know
19775 that we have two candidates that will provide the actual type. */
19776 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19778 tree substed
= TREE_TYPE (decl
);
19782 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19784 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19785 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19786 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19788 unify_type_mismatch (explain_p
, args
[i
],
19789 TREE_VALUE (sarg
));
19794 /* After doing deduction with the inherited constructor, actually return an
19795 instantiation of the inheriting constructor. */
19797 decl
= instantiate_template (orig_fn
, targs
, complain
);
19803 if (excessive_deduction_depth
)
19805 if (deduction_depth
== 0)
19806 /* Reset once we're all the way out. */
19807 excessive_deduction_depth
= false;
19813 /* Adjust types before performing type deduction, as described in
19814 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19815 sections are symmetric. PARM is the type of a function parameter
19816 or the return type of the conversion function. ARG is the type of
19817 the argument passed to the call, or the type of the value
19818 initialized with the result of the conversion function.
19819 ARG_EXPR is the original argument expression, which may be null. */
19822 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19835 /* Swap PARM and ARG throughout the remainder of this
19836 function; the handling is precisely symmetric since PARM
19837 will initialize ARG rather than vice versa. */
19838 std::swap (parm
, arg
);
19842 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19843 too, but here handle it by stripping the reference from PARM
19844 rather than by adding it to ARG. */
19845 if (TYPE_REF_P (*parm
)
19846 && TYPE_REF_IS_RVALUE (*parm
)
19847 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19848 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19849 && TYPE_REF_P (*arg
)
19850 && !TYPE_REF_IS_RVALUE (*arg
))
19851 *parm
= TREE_TYPE (*parm
);
19852 /* Nothing else to do in this case. */
19856 gcc_unreachable ();
19859 if (!TYPE_REF_P (*parm
))
19861 /* [temp.deduct.call]
19863 If P is not a reference type:
19865 --If A is an array type, the pointer type produced by the
19866 array-to-pointer standard conversion (_conv.array_) is
19867 used in place of A for type deduction; otherwise,
19869 --If A is a function type, the pointer type produced by
19870 the function-to-pointer standard conversion
19871 (_conv.func_) is used in place of A for type deduction;
19874 --If A is a cv-qualified type, the top level
19875 cv-qualifiers of A's type are ignored for type
19877 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19878 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19879 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19880 *arg
= build_pointer_type (*arg
);
19882 *arg
= TYPE_MAIN_VARIANT (*arg
);
19885 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19886 reference to a cv-unqualified template parameter that does not represent a
19887 template parameter of a class template (during class template argument
19888 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19889 an lvalue, the type "lvalue reference to A" is used in place of A for type
19891 if (TYPE_REF_P (*parm
)
19892 && TYPE_REF_IS_RVALUE (*parm
)
19893 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19894 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19895 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19896 && (arg_expr
? lvalue_p (arg_expr
)
19897 /* try_one_overload doesn't provide an arg_expr, but
19898 functions are always lvalues. */
19899 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19900 *arg
= build_reference_type (*arg
);
19902 /* [temp.deduct.call]
19904 If P is a cv-qualified type, the top level cv-qualifiers
19905 of P's type are ignored for type deduction. If P is a
19906 reference type, the type referred to by P is used for
19908 *parm
= TYPE_MAIN_VARIANT (*parm
);
19909 if (TYPE_REF_P (*parm
))
19911 *parm
= TREE_TYPE (*parm
);
19912 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19915 /* DR 322. For conversion deduction, remove a reference type on parm
19916 too (which has been swapped into ARG). */
19917 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
19918 *arg
= TREE_TYPE (*arg
);
19923 /* Subroutine of fn_type_unification. PARM is a function parameter of a
19924 template which doesn't contain any deducible template parameters; check if
19925 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19926 unify_one_argument. */
19929 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19930 int flags
, struct conversion
**conv_p
,
19936 type
= TREE_TYPE (arg
);
19940 if (same_type_p (parm
, type
))
19941 return unify_success (explain_p
);
19943 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19944 if (strict
== DEDUCE_CONV
)
19946 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
19947 return unify_success (explain_p
);
19949 else if (strict
!= DEDUCE_EXACT
)
19952 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
19954 /* Avoid recalculating this in add_function_candidate. */
19956 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
19958 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
19960 return unify_success (explain_p
);
19963 if (strict
== DEDUCE_EXACT
)
19964 return unify_type_mismatch (explain_p
, parm
, arg
);
19966 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19969 static bool uses_deducible_template_parms (tree type
);
19971 /* Returns true iff the expression EXPR is one from which a template
19972 argument can be deduced. In other words, if it's an undecorated
19973 use of a template non-type parameter. */
19976 deducible_expression (tree expr
)
19978 /* Strip implicit conversions. */
19979 while (CONVERT_EXPR_P (expr
))
19980 expr
= TREE_OPERAND (expr
, 0);
19981 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19984 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19985 deducible way; that is, if it has a max value of <PARM> - 1. */
19988 deducible_array_bound (tree domain
)
19990 if (domain
== NULL_TREE
)
19993 tree max
= TYPE_MAX_VALUE (domain
);
19994 if (TREE_CODE (max
) != MINUS_EXPR
)
19997 return deducible_expression (TREE_OPERAND (max
, 0));
20000 /* Returns true iff the template arguments ARGS use a template parameter
20001 in a deducible way. */
20004 deducible_template_args (tree args
)
20006 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
20009 tree elt
= TREE_VEC_ELT (args
, i
);
20010 if (ARGUMENT_PACK_P (elt
))
20011 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
20014 if (PACK_EXPANSION_P (elt
))
20015 elt
= PACK_EXPANSION_PATTERN (elt
);
20016 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
20018 else if (TYPE_P (elt
))
20019 deducible
= uses_deducible_template_parms (elt
);
20021 deducible
= deducible_expression (elt
);
20029 /* Returns true iff TYPE contains any deducible references to template
20030 parameters, as per 14.8.2.5. */
20033 uses_deducible_template_parms (tree type
)
20035 if (PACK_EXPANSION_P (type
))
20036 type
= PACK_EXPANSION_PATTERN (type
);
20043 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20044 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20050 if (INDIRECT_TYPE_P (type
))
20051 return uses_deducible_template_parms (TREE_TYPE (type
));
20053 /* T[integer-constant ]
20055 if (TREE_CODE (type
) == ARRAY_TYPE
)
20056 return (uses_deducible_template_parms (TREE_TYPE (type
))
20057 || deducible_array_bound (TYPE_DOMAIN (type
)));
20069 if (TYPE_PTRMEM_P (type
))
20070 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
20071 || (uses_deducible_template_parms
20072 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
20074 /* template-name <T> (where template-name refers to a class template)
20075 template-name <i> (where template-name refers to a class template) */
20076 if (CLASS_TYPE_P (type
)
20077 && CLASSTYPE_TEMPLATE_INFO (type
)
20078 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
20079 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20080 (CLASSTYPE_TI_ARGS (type
)));
20085 if (TREE_CODE (type
) == FUNCTION_TYPE
20086 || TREE_CODE (type
) == METHOD_TYPE
)
20088 if (uses_deducible_template_parms (TREE_TYPE (type
)))
20090 tree parm
= TYPE_ARG_TYPES (type
);
20091 if (TREE_CODE (type
) == METHOD_TYPE
)
20092 parm
= TREE_CHAIN (parm
);
20093 for (; parm
; parm
= TREE_CHAIN (parm
))
20094 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
20101 /* Subroutine of type_unification_real and unify_pack_expansion to
20102 handle unification of a single P/A pair. Parameters are as
20103 for those functions. */
20106 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
20107 int subr
, unification_kind_t strict
,
20110 tree arg_expr
= NULL_TREE
;
20113 if (arg
== error_mark_node
|| parm
== error_mark_node
)
20114 return unify_invalid (explain_p
);
20115 if (arg
== unknown_type_node
)
20116 /* We can't deduce anything from this, but we might get all the
20117 template args from other function args. */
20118 return unify_success (explain_p
);
20120 /* Implicit conversions (Clause 4) will be performed on a function
20121 argument to convert it to the type of the corresponding function
20122 parameter if the parameter type contains no template-parameters that
20123 participate in template argument deduction. */
20124 if (strict
!= DEDUCE_EXACT
20125 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
20126 /* For function parameters with no deducible template parameters,
20127 just return. We'll check non-dependent conversions later. */
20128 return unify_success (explain_p
);
20133 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
20134 | UNIFY_ALLOW_MORE_CV_QUAL
20135 | UNIFY_ALLOW_DERIVED
);
20139 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
20143 arg_strict
= UNIFY_ALLOW_NONE
;
20147 gcc_unreachable ();
20150 /* We only do these transformations if this is the top-level
20151 parameter_type_list in a call or declaration matching; in other
20152 situations (nested function declarators, template argument lists) we
20153 won't be comparing a type to an expression, and we don't do any type
20159 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
20160 if (type_unknown_p (arg
))
20162 /* [temp.deduct.type] A template-argument can be
20163 deduced from a pointer to function or pointer
20164 to member function argument if the set of
20165 overloaded functions does not contain function
20166 templates and at most one of a set of
20167 overloaded functions provides a unique
20169 resolve_overloaded_unification (tparms
, targs
, parm
,
20171 arg_strict
, explain_p
);
20172 /* If a unique match was not found, this is a
20173 non-deduced context, so we still succeed. */
20174 return unify_success (explain_p
);
20178 arg
= unlowered_expr_type (arg
);
20179 if (arg
== error_mark_node
)
20180 return unify_invalid (explain_p
);
20184 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
20187 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
20188 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
20189 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20191 /* For deduction from an init-list we need the actual list. */
20192 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
20194 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
20197 /* for_each_template_parm callback that always returns 0. */
20200 zero_r (tree
, void *)
20205 /* for_each_template_parm any_fn callback to handle deduction of a template
20206 type argument from the type of an array bound. */
20209 array_deduction_r (tree t
, void *data
)
20211 tree_pair_p d
= (tree_pair_p
)data
;
20212 tree
&tparms
= d
->purpose
;
20213 tree
&targs
= d
->value
;
20215 if (TREE_CODE (t
) == ARRAY_TYPE
)
20216 if (tree dom
= TYPE_DOMAIN (t
))
20217 if (tree max
= TYPE_MAX_VALUE (dom
))
20219 if (TREE_CODE (max
) == MINUS_EXPR
)
20220 max
= TREE_OPERAND (max
, 0);
20221 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
20222 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
20223 UNIFY_ALLOW_NONE
, /*explain*/false);
20226 /* Keep walking. */
20230 /* Try to deduce any not-yet-deduced template type arguments from the type of
20231 an array bound. This is handled separately from unify because 14.8.2.5 says
20232 "The type of a type parameter is only deduced from an array bound if it is
20233 not otherwise deduced." */
20236 try_array_deduction (tree tparms
, tree targs
, tree parm
)
20238 tree_pair_s data
= { tparms
, targs
};
20239 hash_set
<tree
> visited
;
20240 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
20241 /*nondeduced*/false, array_deduction_r
);
20244 /* Most parms like fn_type_unification.
20246 If SUBR is 1, we're being called recursively (to unify the
20247 arguments of a function or method parameter of a function
20250 CHECKS is a pointer to a vector of access checks encountered while
20251 substituting default template arguments. */
20254 type_unification_real (tree tparms
,
20258 unsigned int xnargs
,
20260 unification_kind_t strict
,
20261 vec
<deferred_access_check
, va_gc
> **checks
,
20266 int ntparms
= TREE_VEC_LENGTH (tparms
);
20267 int saw_undeduced
= 0;
20270 unsigned int nargs
;
20273 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
20274 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
20275 gcc_assert (ntparms
> 0);
20277 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
20279 /* Reset the number of non-defaulted template arguments contained
20281 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
20289 while (parms
&& parms
!= void_list_node
20292 parm
= TREE_VALUE (parms
);
20294 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20295 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
20296 /* For a function parameter pack that occurs at the end of the
20297 parameter-declaration-list, the type A of each remaining
20298 argument of the call is compared with the type P of the
20299 declarator-id of the function parameter pack. */
20302 parms
= TREE_CHAIN (parms
);
20304 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20305 /* For a function parameter pack that does not occur at the
20306 end of the parameter-declaration-list, the type of the
20307 parameter pack is a non-deduced context. */
20313 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
20319 && parms
!= void_list_node
20320 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
20322 /* Unify the remaining arguments with the pack expansion type. */
20324 tree parmvec
= make_tree_vec (1);
20326 /* Allocate a TREE_VEC and copy in all of the arguments */
20327 argvec
= make_tree_vec (nargs
- ia
);
20328 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
20329 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
20331 /* Copy the parameter into parmvec. */
20332 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
20333 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
20334 /*subr=*/subr
, explain_p
))
20337 /* Advance to the end of the list of parameters. */
20338 parms
= TREE_CHAIN (parms
);
20341 /* Fail if we've reached the end of the parm list, and more args
20342 are present, and the parm list isn't variadic. */
20343 if (ia
< nargs
&& parms
== void_list_node
)
20344 return unify_too_many_arguments (explain_p
, nargs
, ia
);
20345 /* Fail if parms are left and they don't have default values and
20346 they aren't all deduced as empty packs (c++/57397). This is
20347 consistent with sufficient_parms_p. */
20348 if (parms
&& parms
!= void_list_node
20349 && TREE_PURPOSE (parms
) == NULL_TREE
)
20351 unsigned int count
= nargs
;
20356 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
20359 p
= TREE_CHAIN (p
);
20361 while (p
&& p
!= void_list_node
);
20362 if (count
!= nargs
)
20363 return unify_too_few_arguments (explain_p
, ia
, count
,
20369 tsubst_flags_t complain
= (explain_p
20370 ? tf_warning_or_error
20372 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
20374 for (i
= 0; i
< ntparms
; i
++)
20376 tree targ
= TREE_VEC_ELT (targs
, i
);
20377 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20379 /* Clear the "incomplete" flags on all argument packs now so that
20380 substituting them into later default arguments works. */
20381 if (targ
&& ARGUMENT_PACK_P (targ
))
20383 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
20384 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
20387 if (targ
|| tparm
== error_mark_node
)
20389 tparm
= TREE_VALUE (tparm
);
20391 if (TREE_CODE (tparm
) == TYPE_DECL
20392 && !tried_array_deduction
)
20394 try_array_deduction (tparms
, targs
, xparms
);
20395 tried_array_deduction
= true;
20396 if (TREE_VEC_ELT (targs
, i
))
20400 /* If this is an undeduced nontype parameter that depends on
20401 a type parameter, try another pass; its type may have been
20402 deduced from a later argument than the one from which
20403 this parameter can be deduced. */
20404 if (TREE_CODE (tparm
) == PARM_DECL
20405 && uses_template_parms (TREE_TYPE (tparm
))
20406 && saw_undeduced
< 2)
20412 /* Core issue #226 (C++0x) [temp.deduct]:
20414 If a template argument has not been deduced, its
20415 default template argument, if any, is used.
20417 When we are in C++98 mode, TREE_PURPOSE will either
20418 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20419 to explicitly check cxx_dialect here. */
20420 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
20421 /* OK, there is a default argument. Wait until after the
20422 conversion check to do substitution. */
20425 /* If the type parameter is a parameter pack, then it will
20426 be deduced to an empty parameter pack. */
20427 if (template_parameter_pack_p (tparm
))
20431 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
20433 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
20434 TREE_CONSTANT (arg
) = 1;
20437 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20439 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
20441 TREE_VEC_ELT (targs
, i
) = arg
;
20445 return unify_parameter_deduction_failure (explain_p
, tparm
);
20448 /* Now substitute into the default template arguments. */
20449 for (i
= 0; i
< ntparms
; i
++)
20451 tree targ
= TREE_VEC_ELT (targs
, i
);
20452 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20454 if (targ
|| tparm
== error_mark_node
)
20456 tree parm
= TREE_VALUE (tparm
);
20457 tree arg
= TREE_PURPOSE (tparm
);
20458 reopen_deferring_access_checks (*checks
);
20459 location_t save_loc
= input_location
;
20461 input_location
= DECL_SOURCE_LOCATION (parm
);
20463 if (saw_undeduced
== 1
20464 && TREE_CODE (parm
) == PARM_DECL
20465 && uses_template_parms (TREE_TYPE (parm
)))
20467 /* The type of this non-type parameter depends on undeduced
20468 parameters. Don't try to use its default argument yet,
20469 since we might deduce an argument for it on the next pass,
20470 but do check whether the arguments we already have cause
20471 substitution failure, so that that happens before we try
20472 later default arguments (78489). */
20473 ++processing_template_decl
;
20474 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
20476 --processing_template_decl
;
20477 if (type
== error_mark_node
)
20478 arg
= error_mark_node
;
20484 tree substed
= NULL_TREE
;
20485 if (saw_undeduced
== 1 && processing_template_decl
== 0)
20487 /* First instatiate in template context, in case we still
20488 depend on undeduced template parameters. */
20489 ++processing_template_decl
;
20490 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20492 --processing_template_decl
;
20493 if (substed
!= error_mark_node
20494 && !uses_template_parms (substed
))
20495 /* We replaced all the tparms, substitute again out of
20496 template context. */
20497 substed
= NULL_TREE
;
20500 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20503 if (!uses_template_parms (substed
))
20504 arg
= convert_template_argument (parm
, substed
, full_targs
,
20505 complain
, i
, NULL_TREE
);
20506 else if (saw_undeduced
== 1)
20509 arg
= error_mark_node
;
20512 input_location
= save_loc
;
20513 *checks
= get_deferred_access_checks ();
20514 pop_deferring_access_checks ();
20516 if (arg
== error_mark_node
)
20520 TREE_VEC_ELT (targs
, i
) = arg
;
20521 /* The position of the first default template argument,
20522 is also the number of non-defaulted arguments in TARGS.
20524 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20525 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
20529 if (saw_undeduced
++ == 1)
20533 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20534 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
20536 return unify_success (explain_p
);
20539 /* Subroutine of type_unification_real. Args are like the variables
20540 at the call site. ARG is an overloaded function (or template-id);
20541 we try deducing template args from each of the overloads, and if
20542 only one succeeds, we go with that. Modifies TARGS and returns
20543 true on success. */
20546 resolve_overloaded_unification (tree tparms
,
20550 unification_kind_t strict
,
20554 tree tempargs
= copy_node (targs
);
20556 tree goodfn
= NULL_TREE
;
20559 if (TREE_CODE (arg
) == ADDR_EXPR
)
20561 arg
= TREE_OPERAND (arg
, 0);
20567 if (TREE_CODE (arg
) == COMPONENT_REF
)
20568 /* Handle `&x' where `x' is some static or non-static member
20570 arg
= TREE_OPERAND (arg
, 1);
20572 if (TREE_CODE (arg
) == OFFSET_REF
)
20573 arg
= TREE_OPERAND (arg
, 1);
20575 /* Strip baselink information. */
20576 if (BASELINK_P (arg
))
20577 arg
= BASELINK_FUNCTIONS (arg
);
20579 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
20581 /* If we got some explicit template args, we need to plug them into
20582 the affected templates before we try to unify, in case the
20583 explicit args will completely resolve the templates in question. */
20586 tree expl_subargs
= TREE_OPERAND (arg
, 1);
20587 arg
= TREE_OPERAND (arg
, 0);
20589 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20592 tree subargs
, elem
;
20594 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20597 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20598 expl_subargs
, NULL_TREE
, tf_none
,
20599 /*require_all_args=*/true,
20600 /*use_default_args=*/true);
20601 if (subargs
!= error_mark_node
20602 && !any_dependent_template_arguments_p (subargs
))
20604 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
20605 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
20606 elem
, strict
, sub_strict
, addr_p
, explain_p
)
20607 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
20616 /* If no templates (or more than one) are fully resolved by the
20617 explicit arguments, this template-id is a non-deduced context; it
20618 could still be OK if we deduce all template arguments for the
20619 enclosing call through other arguments. */
20623 else if (TREE_CODE (arg
) != OVERLOAD
20624 && TREE_CODE (arg
) != FUNCTION_DECL
)
20625 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20626 -- but the deduction does not succeed because the expression is
20627 not just the function on its own. */
20630 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20633 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
20634 strict
, sub_strict
, addr_p
, explain_p
)
20635 && (!goodfn
|| !decls_match (goodfn
, fn
)))
20642 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20643 to function or pointer to member function argument if the set of
20644 overloaded functions does not contain function templates and at most
20645 one of a set of overloaded functions provides a unique match.
20647 So if we found multiple possibilities, we return success but don't
20648 deduce anything. */
20652 int i
= TREE_VEC_LENGTH (targs
);
20654 if (TREE_VEC_ELT (tempargs
, i
))
20656 tree old
= TREE_VEC_ELT (targs
, i
);
20657 tree new_
= TREE_VEC_ELT (tempargs
, i
);
20658 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
20659 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
20660 /* Don't forget explicit template arguments in a pack. */
20661 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
20662 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
20663 TREE_VEC_ELT (targs
, i
) = new_
;
20672 /* Core DR 115: In contexts where deduction is done and fails, or in
20673 contexts where deduction is not done, if a template argument list is
20674 specified and it, along with any default template arguments, identifies
20675 a single function template specialization, then the template-id is an
20676 lvalue for the function template specialization. */
20679 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20681 tree expr
, offset
, baselink
;
20684 if (!type_unknown_p (orig_expr
))
20689 offset
= NULL_TREE
;
20690 baselink
= NULL_TREE
;
20692 if (TREE_CODE (expr
) == ADDR_EXPR
)
20694 expr
= TREE_OPERAND (expr
, 0);
20697 if (TREE_CODE (expr
) == OFFSET_REF
)
20700 expr
= TREE_OPERAND (expr
, 1);
20702 if (BASELINK_P (expr
))
20705 expr
= BASELINK_FUNCTIONS (expr
);
20708 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20711 tree goodfn
= NULL_TREE
;
20713 /* If we got some explicit template args, we need to plug them into
20714 the affected templates before we try to unify, in case the
20715 explicit args will completely resolve the templates in question. */
20717 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20718 tree arg
= TREE_OPERAND (expr
, 0);
20719 tree badfn
= NULL_TREE
;
20720 tree badargs
= NULL_TREE
;
20722 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20725 tree subargs
, elem
;
20727 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20730 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20731 expl_subargs
, NULL_TREE
, tf_none
,
20732 /*require_all_args=*/true,
20733 /*use_default_args=*/true);
20734 if (subargs
!= error_mark_node
20735 && !any_dependent_template_arguments_p (subargs
))
20737 elem
= instantiate_template (fn
, subargs
, tf_none
);
20738 if (elem
== error_mark_node
)
20743 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20752 mark_used (goodfn
);
20755 expr
= build_baselink (BASELINK_BINFO (baselink
),
20756 BASELINK_ACCESS_BINFO (baselink
),
20757 expr
, BASELINK_OPTYPE (baselink
));
20761 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20762 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20765 expr
= cp_build_addr_expr (expr
, complain
);
20768 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20769 /* There were no good options and at least one bad one, so let the
20770 user know what the problem is. */
20771 instantiate_template (badfn
, badargs
, complain
);
20776 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20777 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20778 different overloads deduce different arguments for a given parm.
20779 ADDR_P is true if the expression for which deduction is being
20780 performed was of the form "& fn" rather than simply "fn".
20782 Returns 1 on success. */
20785 try_one_overload (tree tparms
,
20790 unification_kind_t strict
,
20799 if (arg
== error_mark_node
)
20802 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20803 to function or pointer to member function argument if the set of
20804 overloaded functions does not contain function templates and at most
20805 one of a set of overloaded functions provides a unique match.
20807 So if this is a template, just return success. */
20809 if (uses_template_parms (arg
))
20812 if (TREE_CODE (arg
) == METHOD_TYPE
)
20813 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20815 arg
= build_pointer_type (arg
);
20817 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20819 /* We don't copy orig_targs for this because if we have already deduced
20820 some template args from previous args, unify would complain when we
20821 try to deduce a template parameter for the same argument, even though
20822 there isn't really a conflict. */
20823 nargs
= TREE_VEC_LENGTH (targs
);
20824 tempargs
= make_tree_vec (nargs
);
20826 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20829 /* First make sure we didn't deduce anything that conflicts with
20830 explicitly specified args. */
20831 for (i
= nargs
; i
--; )
20833 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20834 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20838 else if (uses_template_parms (elt
))
20839 /* Since we're unifying against ourselves, we will fill in
20840 template args used in the function parm list with our own
20841 template parms. Discard them. */
20842 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20843 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20845 /* Check that the argument at each index of the deduced argument pack
20846 is equivalent to the corresponding explicitly specified argument.
20847 We may have deduced more arguments than were explicitly specified,
20850 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20851 that's wrong if we deduce the same argument pack from multiple
20852 function arguments: it's only incomplete the first time. */
20854 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20855 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20857 if (TREE_VEC_LENGTH (deduced_pack
)
20858 < TREE_VEC_LENGTH (explicit_pack
))
20861 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20862 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20863 TREE_VEC_ELT (deduced_pack
, j
)))
20866 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20870 for (i
= nargs
; i
--; )
20872 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20875 TREE_VEC_ELT (targs
, i
) = elt
;
20881 /* PARM is a template class (perhaps with unbound template
20882 parameters). ARG is a fully instantiated type. If ARG can be
20883 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20884 TARGS are as for unify. */
20887 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20890 tree copy_of_targs
;
20892 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20894 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20895 /* Matches anything. */;
20896 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20897 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20900 /* We need to make a new template argument vector for the call to
20901 unify. If we used TARGS, we'd clutter it up with the result of
20902 the attempted unification, even if this class didn't work out.
20903 We also don't want to commit ourselves to all the unifications
20904 we've already done, since unification is supposed to be done on
20905 an argument-by-argument basis. In other words, consider the
20906 following pathological case:
20908 template <int I, int J, int K>
20911 template <int I, int J>
20912 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20914 template <int I, int J, int K>
20915 void f(S<I, J, K>, S<I, I, I>);
20924 Now, by the time we consider the unification involving `s2', we
20925 already know that we must have `f<0, 0, 0>'. But, even though
20926 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20927 because there are two ways to unify base classes of S<0, 1, 2>
20928 with S<I, I, I>. If we kept the already deduced knowledge, we
20929 would reject the possibility I=1. */
20930 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20932 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20934 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20939 /* If unification failed, we're done. */
20940 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20941 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20947 /* Given a template type PARM and a class type ARG, find the unique
20948 base type in ARG that is an instance of PARM. We do not examine
20949 ARG itself; only its base-classes. If there is not exactly one
20950 appropriate base class, return NULL_TREE. PARM may be the type of
20951 a partial specialization, as well as a plain template type. Used
20954 static enum template_base_result
20955 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20956 bool explain_p
, tree
*result
)
20958 tree rval
= NULL_TREE
;
20961 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20963 binfo
= TYPE_BINFO (complete_type (arg
));
20966 /* The type could not be completed. */
20967 *result
= NULL_TREE
;
20968 return tbr_incomplete_type
;
20971 /* Walk in inheritance graph order. The search order is not
20972 important, and this avoids multiple walks of virtual bases. */
20973 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20975 tree r
= try_class_unification (tparms
, targs
, parm
,
20976 BINFO_TYPE (binfo
), explain_p
);
20980 /* If there is more than one satisfactory baseclass, then:
20984 If they yield more than one possible deduced A, the type
20988 if (rval
&& !same_type_p (r
, rval
))
20990 *result
= NULL_TREE
;
20991 return tbr_ambiguous_baseclass
;
20999 return tbr_success
;
21002 /* Returns the level of DECL, which declares a template parameter. */
21005 template_decl_level (tree decl
)
21007 switch (TREE_CODE (decl
))
21010 case TEMPLATE_DECL
:
21011 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
21014 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
21017 gcc_unreachable ();
21022 /* Decide whether ARG can be unified with PARM, considering only the
21023 cv-qualifiers of each type, given STRICT as documented for unify.
21024 Returns nonzero iff the unification is OK on that basis. */
21027 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
21029 int arg_quals
= cp_type_quals (arg
);
21030 int parm_quals
= cp_type_quals (parm
);
21032 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21033 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21035 /* Although a CVR qualifier is ignored when being applied to a
21036 substituted template parameter ([8.3.2]/1 for example), that
21037 does not allow us to unify "const T" with "int&" because both
21038 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21039 It is ok when we're allowing additional CV qualifiers
21040 at the outer level [14.8.2.1]/3,1st bullet. */
21041 if ((TYPE_REF_P (arg
)
21042 || TREE_CODE (arg
) == FUNCTION_TYPE
21043 || TREE_CODE (arg
) == METHOD_TYPE
)
21044 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
21047 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
21048 && (parm_quals
& TYPE_QUAL_RESTRICT
))
21052 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21053 && (arg_quals
& parm_quals
) != parm_quals
)
21056 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
21057 && (parm_quals
& arg_quals
) != arg_quals
)
21063 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21065 template_parm_level_and_index (tree parm
, int* level
, int* index
)
21067 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21068 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21069 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21071 *index
= TEMPLATE_TYPE_IDX (parm
);
21072 *level
= TEMPLATE_TYPE_LEVEL (parm
);
21076 *index
= TEMPLATE_PARM_IDX (parm
);
21077 *level
= TEMPLATE_PARM_LEVEL (parm
);
21081 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21083 if (unify (TP, TA, P, A, S, EP)) \
21087 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21088 expansion at the end of PACKED_PARMS. Returns 0 if the type
21089 deduction succeeds, 1 otherwise. STRICT is the same as in
21090 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21091 function call argument list. We'll need to adjust the arguments to make them
21092 types. SUBR tells us if this is from a recursive call to
21093 type_unification_real, or for comparing two template argument
21097 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
21098 tree packed_args
, unification_kind_t strict
,
21099 bool subr
, bool explain_p
)
21102 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
21103 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
21104 tree pack
, packs
= NULL_TREE
;
21105 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
21107 /* Add in any args remembered from an earlier partial instantiation. */
21108 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
21109 int levels
= TMPL_ARGS_DEPTH (targs
);
21111 packed_args
= expand_template_argument_pack (packed_args
);
21113 int len
= TREE_VEC_LENGTH (packed_args
);
21115 /* Determine the parameter packs we will be deducing from the
21116 pattern, and record their current deductions. */
21117 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
21118 pack
; pack
= TREE_CHAIN (pack
))
21120 tree parm_pack
= TREE_VALUE (pack
);
21123 /* Only template parameter packs can be deduced, not e.g. function
21124 parameter packs or __bases or __integer_pack. */
21125 if (!TEMPLATE_PARM_P (parm_pack
))
21128 /* Determine the index and level of this parameter pack. */
21129 template_parm_level_and_index (parm_pack
, &level
, &idx
);
21130 if (level
< levels
)
21133 /* Keep track of the parameter packs and their corresponding
21135 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
21136 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
21139 /* Loop through all of the arguments that have not yet been
21140 unified and unify each with the pattern. */
21141 for (i
= start
; i
< len
; i
++)
21144 bool any_explicit
= false;
21145 tree arg
= TREE_VEC_ELT (packed_args
, i
);
21147 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21148 or the element of its argument pack at the current index if
21149 this argument was explicitly specified. */
21150 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21154 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21157 if (TREE_VALUE (pack
)
21158 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
21159 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
21161 any_explicit
= true;
21162 arg
= TREE_VEC_ELT (pargs
, i
- start
);
21164 TMPL_ARG (targs
, level
, idx
) = arg
;
21167 /* If we had explicit template arguments, substitute them into the
21168 pattern before deduction. */
21171 /* Some arguments might still be unspecified or dependent. */
21173 ++processing_template_decl
;
21174 dependent
= any_dependent_template_arguments_p (targs
);
21176 --processing_template_decl
;
21177 parm
= tsubst (pattern
, targs
,
21178 explain_p
? tf_warning_or_error
: tf_none
,
21181 --processing_template_decl
;
21182 if (parm
== error_mark_node
)
21188 /* Unify the pattern with the current argument. */
21189 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
21193 /* For each parameter pack, collect the deduced value. */
21194 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21197 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21199 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
21200 TMPL_ARG (targs
, level
, idx
);
21204 /* Verify that the results of unification with the parameter packs
21205 produce results consistent with what we've seen before, and make
21206 the deduced argument packs available. */
21207 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21209 tree old_pack
= TREE_VALUE (pack
);
21210 tree new_args
= TREE_TYPE (pack
);
21211 int i
, len
= TREE_VEC_LENGTH (new_args
);
21213 bool nondeduced_p
= false;
21215 /* By default keep the original deduced argument pack.
21216 If necessary, more specific code is going to update the
21217 resulting deduced argument later down in this function. */
21218 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21219 TMPL_ARG (targs
, level
, idx
) = old_pack
;
21221 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21222 actually deduce anything. */
21223 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
21224 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
21225 nondeduced_p
= true;
21229 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
21231 /* If we had fewer function args than explicit template args,
21232 just use the explicits. */
21233 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21234 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
21235 if (len
< explicit_len
)
21236 new_args
= explicit_args
;
21242 /* Build the deduced *_ARGUMENT_PACK. */
21243 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
21245 result
= make_node (NONTYPE_ARGUMENT_PACK
);
21246 TREE_CONSTANT (result
) = 1;
21249 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21251 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
21253 /* Note the deduced argument packs for this parameter
21255 TMPL_ARG (targs
, level
, idx
) = result
;
21257 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
21258 && (ARGUMENT_PACK_ARGS (old_pack
)
21259 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
21261 /* We only had the explicitly-provided arguments before, but
21262 now we have a complete set of arguments. */
21263 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21265 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
21266 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
21267 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
21271 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
21272 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
21274 if (!comp_template_args (old_args
, new_args
,
21275 &bad_old_arg
, &bad_new_arg
))
21276 /* Inconsistent unification of this parameter pack. */
21277 return unify_parameter_pack_inconsistent (explain_p
,
21283 return unify_success (explain_p
);
21286 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21287 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21288 parameters and return value are as for unify. */
21291 unify_array_domain (tree tparms
, tree targs
,
21292 tree parm_dom
, tree arg_dom
,
21300 /* Our representation of array types uses "N - 1" as the
21301 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21302 not an integer constant. We cannot unify arbitrarily
21303 complex expressions, so we eliminate the MINUS_EXPRs
21305 parm_max
= TYPE_MAX_VALUE (parm_dom
);
21306 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
21309 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
21310 parm_max
= TREE_OPERAND (parm_max
, 0);
21312 arg_max
= TYPE_MAX_VALUE (arg_dom
);
21313 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
21316 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21317 trying to unify the type of a variable with the type
21318 of a template parameter. For example:
21320 template <unsigned int N>
21321 void f (char (&) [N]);
21328 Here, the type of the ARG will be "int [g(i)]", and
21329 may be a SAVE_EXPR, etc. */
21330 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
21331 return unify_vla_arg (explain_p
, arg_dom
);
21332 arg_max
= TREE_OPERAND (arg_max
, 0);
21335 /* If only one of the bounds used a MINUS_EXPR, compensate
21336 by adding one to the other bound. */
21337 if (parm_cst
&& !arg_cst
)
21338 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21342 else if (arg_cst
&& !parm_cst
)
21343 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21348 return unify (tparms
, targs
, parm_max
, arg_max
,
21349 UNIFY_ALLOW_INTEGER
, explain_p
);
21352 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21354 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
21359 if (PACK_EXPANSION_P (t
))
21360 t
= PACK_EXPANSION_PATTERN (t
);
21361 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
21362 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
21363 || DECL_TYPE_TEMPLATE_P (t
))
21365 else if (TYPE_P (t
))
21371 /* Deduce the value of template parameters. TPARMS is the (innermost)
21372 set of template parameters to a template. TARGS is the bindings
21373 for those template parameters, as determined thus far; TARGS may
21374 include template arguments for outer levels of template parameters
21375 as well. PARM is a parameter to a template function, or a
21376 subcomponent of that parameter; ARG is the corresponding argument.
21377 This function attempts to match PARM with ARG in a manner
21378 consistent with the existing assignments in TARGS. If more values
21379 are deduced, then TARGS is updated.
21381 Returns 0 if the type deduction succeeds, 1 otherwise. The
21382 parameter STRICT is a bitwise or of the following flags:
21385 Require an exact match between PARM and ARG.
21386 UNIFY_ALLOW_MORE_CV_QUAL:
21387 Allow the deduced ARG to be more cv-qualified (by qualification
21388 conversion) than ARG.
21389 UNIFY_ALLOW_LESS_CV_QUAL:
21390 Allow the deduced ARG to be less cv-qualified than ARG.
21391 UNIFY_ALLOW_DERIVED:
21392 Allow the deduced ARG to be a template base class of ARG,
21393 or a pointer to a template base class of the type pointed to by
21395 UNIFY_ALLOW_INTEGER:
21396 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21397 case for more information.
21398 UNIFY_ALLOW_OUTER_LEVEL:
21399 This is the outermost level of a deduction. Used to determine validity
21400 of qualification conversions. A valid qualification conversion must
21401 have const qualified pointers leading up to the inner type which
21402 requires additional CV quals, except at the outer level, where const
21403 is not required [conv.qual]. It would be normal to set this flag in
21404 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21405 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21406 This is the outermost level of a deduction, and PARM can be more CV
21407 qualified at this point.
21408 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21409 This is the outermost level of a deduction, and PARM can be less CV
21410 qualified at this point. */
21413 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
21419 int strict_in
= strict
;
21420 tsubst_flags_t complain
= (explain_p
21421 ? tf_warning_or_error
21424 /* I don't think this will do the right thing with respect to types.
21425 But the only case I've seen it in so far has been array bounds, where
21426 signedness is the only information lost, and I think that will be
21428 while (CONVERT_EXPR_P (parm
))
21429 parm
= TREE_OPERAND (parm
, 0);
21431 if (arg
== error_mark_node
)
21432 return unify_invalid (explain_p
);
21433 if (arg
== unknown_type_node
21434 || arg
== init_list_type_node
)
21435 /* We can't deduce anything from this, but we might get all the
21436 template args from other function args. */
21437 return unify_success (explain_p
);
21439 if (parm
== any_targ_node
|| arg
== any_targ_node
)
21440 return unify_success (explain_p
);
21442 /* If PARM uses template parameters, then we can't bail out here,
21443 even if ARG == PARM, since we won't record unifications for the
21444 template parameters. We might need them if we're trying to
21445 figure out which of two things is more specialized. */
21446 if (arg
== parm
&& !uses_template_parms (parm
))
21447 return unify_success (explain_p
);
21449 /* Handle init lists early, so the rest of the function can assume
21450 we're dealing with a type. */
21451 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
21455 tree orig_parm
= parm
;
21457 /* Replace T with std::initializer_list<T> for deduction. */
21458 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21459 && flag_deduce_init_list
)
21460 parm
= listify (parm
);
21462 if (!is_std_init_list (parm
)
21463 && TREE_CODE (parm
) != ARRAY_TYPE
)
21464 /* We can only deduce from an initializer list argument if the
21465 parameter is std::initializer_list or an array; otherwise this
21466 is a non-deduced context. */
21467 return unify_success (explain_p
);
21469 if (TREE_CODE (parm
) == ARRAY_TYPE
)
21470 elttype
= TREE_TYPE (parm
);
21473 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
21474 /* Deduction is defined in terms of a single type, so just punt
21475 on the (bizarre) std::initializer_list<T...>. */
21476 if (PACK_EXPANSION_P (elttype
))
21477 return unify_success (explain_p
);
21480 if (strict
!= DEDUCE_EXACT
21481 && TYPE_P (elttype
)
21482 && !uses_deducible_template_parms (elttype
))
21483 /* If ELTTYPE has no deducible template parms, skip deduction from
21484 the list elements. */;
21486 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
21488 int elt_strict
= strict
;
21490 if (elt
== error_mark_node
)
21491 return unify_invalid (explain_p
);
21493 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
21495 tree type
= TREE_TYPE (elt
);
21496 if (type
== error_mark_node
)
21497 return unify_invalid (explain_p
);
21498 /* It should only be possible to get here for a call. */
21499 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
21500 elt_strict
|= maybe_adjust_types_for_deduction
21501 (DEDUCE_CALL
, &elttype
, &type
, elt
);
21505 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
21509 if (TREE_CODE (parm
) == ARRAY_TYPE
21510 && deducible_array_bound (TYPE_DOMAIN (parm
)))
21512 /* Also deduce from the length of the initializer list. */
21513 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
21514 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
21515 if (idx
== error_mark_node
)
21516 return unify_invalid (explain_p
);
21517 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21521 /* If the std::initializer_list<T> deduction worked, replace the
21522 deduced A with std::initializer_list<A>. */
21523 if (orig_parm
!= parm
)
21525 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
21526 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21527 targ
= listify (targ
);
21528 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
21530 return unify_success (explain_p
);
21533 /* If parm and arg aren't the same kind of thing (template, type, or
21534 expression), fail early. */
21535 if (pa_kind (parm
) != pa_kind (arg
))
21536 return unify_invalid (explain_p
);
21538 /* Immediately reject some pairs that won't unify because of
21539 cv-qualification mismatches. */
21540 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21542 /* It is the elements of the array which hold the cv quals of an array
21543 type, and the elements might be template type parms. We'll check
21544 when we recurse. */
21545 && TREE_CODE (arg
) != ARRAY_TYPE
21546 /* We check the cv-qualifiers when unifying with template type
21547 parameters below. We want to allow ARG `const T' to unify with
21548 PARM `T' for example, when computing which of two templates
21549 is more specialized, for example. */
21550 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
21551 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
21552 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21554 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
21555 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
21556 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
21557 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
21558 strict
&= ~UNIFY_ALLOW_DERIVED
;
21559 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21560 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
21562 switch (TREE_CODE (parm
))
21564 case TYPENAME_TYPE
:
21566 case UNBOUND_CLASS_TEMPLATE
:
21567 /* In a type which contains a nested-name-specifier, template
21568 argument values cannot be deduced for template parameters used
21569 within the nested-name-specifier. */
21570 return unify_success (explain_p
);
21572 case TEMPLATE_TYPE_PARM
:
21573 case TEMPLATE_TEMPLATE_PARM
:
21574 case BOUND_TEMPLATE_TEMPLATE_PARM
:
21575 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21576 if (error_operand_p (tparm
))
21577 return unify_invalid (explain_p
);
21579 if (TEMPLATE_TYPE_LEVEL (parm
)
21580 != template_decl_level (tparm
))
21581 /* The PARM is not one we're trying to unify. Just check
21582 to see if it matches ARG. */
21584 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21585 && (is_auto (parm
) ? is_auto (arg
)
21586 : same_type_p (parm
, arg
)))
21587 return unify_success (explain_p
);
21589 return unify_type_mismatch (explain_p
, parm
, arg
);
21591 idx
= TEMPLATE_TYPE_IDX (parm
);
21592 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21593 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
21594 if (error_operand_p (tparm
))
21595 return unify_invalid (explain_p
);
21597 /* Check for mixed types and values. */
21598 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21599 && TREE_CODE (tparm
) != TYPE_DECL
)
21600 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21601 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
21602 gcc_unreachable ();
21604 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21606 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
21607 && CLASS_TYPE_P (arg
))
21609 /* First try to match ARG directly. */
21610 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
21614 /* Otherwise, look for a suitable base of ARG, as below. */
21615 enum template_base_result r
;
21616 r
= get_template_base (tparms
, targs
, parm
, arg
,
21619 return unify_no_common_base (explain_p
, r
, parm
, arg
);
21623 /* ARG must be constructed from a template class or a template
21624 template parameter. */
21625 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
21626 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21627 return unify_template_deduction_failure (explain_p
, parm
, arg
);
21629 /* Deduce arguments T, i from TT<T> or TT<i>. */
21630 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
21633 arg
= TYPE_TI_TEMPLATE (arg
);
21635 /* Fall through to deduce template name. */
21638 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21639 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21641 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21643 /* Simple cases: Value already set, does match or doesn't. */
21644 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
21645 return unify_success (explain_p
);
21647 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21651 /* If PARM is `const T' and ARG is only `int', we don't have
21652 a match unless we are allowing additional qualification.
21653 If ARG is `const int' and PARM is just `T' that's OK;
21654 that binds `const int' to `T'. */
21655 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
21657 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21659 /* Consider the case where ARG is `const volatile int' and
21660 PARM is `const T'. Then, T should be `volatile int'. */
21661 arg
= cp_build_qualified_type_real
21662 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
21663 if (arg
== error_mark_node
)
21664 return unify_invalid (explain_p
);
21666 /* Simple cases: Value already set, does match or doesn't. */
21667 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
21668 return unify_success (explain_p
);
21670 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21672 /* Make sure that ARG is not a variable-sized array. (Note
21673 that were talking about variable-sized arrays (like
21674 `int[n]'), rather than arrays of unknown size (like
21675 `int[]').) We'll get very confused by such a type since
21676 the bound of the array is not constant, and therefore
21677 not mangleable. Besides, such types are not allowed in
21678 ISO C++, so we can do as we please here. We do allow
21679 them for 'auto' deduction, since that isn't ABI-exposed. */
21680 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21681 return unify_vla_arg (explain_p
, arg
);
21683 /* Strip typedefs as in convert_template_argument. */
21684 arg
= canonicalize_type_argument (arg
, tf_none
);
21687 /* If ARG is a parameter pack or an expansion, we cannot unify
21688 against it unless PARM is also a parameter pack. */
21689 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21690 && !template_parameter_pack_p (parm
))
21691 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21693 /* If the argument deduction results is a METHOD_TYPE,
21694 then there is a problem.
21695 METHOD_TYPE doesn't map to any real C++ type the result of
21696 the deduction can not be of that type. */
21697 if (TREE_CODE (arg
) == METHOD_TYPE
)
21698 return unify_method_type_error (explain_p
, arg
);
21700 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21701 return unify_success (explain_p
);
21703 case TEMPLATE_PARM_INDEX
:
21704 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21705 if (error_operand_p (tparm
))
21706 return unify_invalid (explain_p
);
21708 if (TEMPLATE_PARM_LEVEL (parm
)
21709 != template_decl_level (tparm
))
21711 /* The PARM is not one we're trying to unify. Just check
21712 to see if it matches ARG. */
21713 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21714 && cp_tree_equal (parm
, arg
));
21716 unify_expression_unequal (explain_p
, parm
, arg
);
21720 idx
= TEMPLATE_PARM_IDX (parm
);
21721 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21725 if ((strict
& UNIFY_ALLOW_INTEGER
)
21726 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21727 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21728 /* We're deducing from an array bound, the type doesn't matter. */
21729 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21730 int x
= !cp_tree_equal (targ
, arg
);
21732 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21736 /* [temp.deduct.type] If, in the declaration of a function template
21737 with a non-type template-parameter, the non-type
21738 template-parameter is used in an expression in the function
21739 parameter-list and, if the corresponding template-argument is
21740 deduced, the template-argument type shall match the type of the
21741 template-parameter exactly, except that a template-argument
21742 deduced from an array bound may be of any integral type.
21743 The non-type parameter might use already deduced type parameters. */
21744 tparm
= TREE_TYPE (parm
);
21745 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
21746 /* We don't have enough levels of args to do any substitution. This
21747 can happen in the context of -fnew-ttp-matching. */;
21750 ++processing_template_decl
;
21751 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
21752 --processing_template_decl
;
21754 if (tree a
= type_uses_auto (tparm
))
21756 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21757 if (tparm
== error_mark_node
)
21762 if (!TREE_TYPE (arg
))
21763 /* Template-parameter dependent expression. Just accept it for now.
21764 It will later be processed in convert_template_argument. */
21766 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21767 non_reference (tparm
)))
21769 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21770 && CP_INTEGRAL_TYPE_P (tparm
))
21771 /* Convert the ARG to the type of PARM; the deduced non-type
21772 template argument must exactly match the types of the
21773 corresponding parameter. */
21774 arg
= fold (build_nop (tparm
, arg
));
21775 else if (uses_template_parms (tparm
))
21777 /* We haven't deduced the type of this parameter yet. */
21778 if (cxx_dialect
>= cxx17
21779 /* We deduce from array bounds in try_array_deduction. */
21780 && !(strict
& UNIFY_ALLOW_INTEGER
))
21782 /* Deduce it from the non-type argument. */
21783 tree atype
= TREE_TYPE (arg
);
21784 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21786 UNIFY_ALLOW_NONE
, explain_p
);
21789 /* Try again later. */
21790 return unify_success (explain_p
);
21793 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21795 /* If ARG is a parameter pack or an expansion, we cannot unify
21796 against it unless PARM is also a parameter pack. */
21797 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21798 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21799 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21802 bool removed_attr
= false;
21803 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21805 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21806 return unify_success (explain_p
);
21810 /* A pointer-to-member constant can be unified only with
21811 another constant. */
21812 if (TREE_CODE (arg
) != PTRMEM_CST
)
21813 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21815 /* Just unify the class member. It would be useless (and possibly
21816 wrong, depending on the strict flags) to unify also
21817 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21818 arg refer to the same variable, even if through different
21819 classes. For instance:
21821 struct A { int x; };
21824 Unification of &A::x and &B::x must succeed. */
21825 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21826 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21831 if (!TYPE_PTR_P (arg
))
21832 return unify_type_mismatch (explain_p
, parm
, arg
);
21834 /* [temp.deduct.call]
21836 A can be another pointer or pointer to member type that can
21837 be converted to the deduced A via a qualification
21838 conversion (_conv.qual_).
21840 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21841 This will allow for additional cv-qualification of the
21842 pointed-to types if appropriate. */
21844 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21845 /* The derived-to-base conversion only persists through one
21846 level of pointers. */
21847 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21849 return unify (tparms
, targs
, TREE_TYPE (parm
),
21850 TREE_TYPE (arg
), strict
, explain_p
);
21853 case REFERENCE_TYPE
:
21854 if (!TYPE_REF_P (arg
))
21855 return unify_type_mismatch (explain_p
, parm
, arg
);
21856 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21857 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21860 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21861 return unify_type_mismatch (explain_p
, parm
, arg
);
21862 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21863 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21864 return unify_type_mismatch (explain_p
, parm
, arg
);
21865 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21866 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21867 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21868 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21869 TYPE_DOMAIN (arg
), explain_p
);
21870 return unify_success (explain_p
);
21877 case ENUMERAL_TYPE
:
21880 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21881 return unify_type_mismatch (explain_p
, parm
, arg
);
21883 /* We have already checked cv-qualification at the top of the
21885 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21886 return unify_type_mismatch (explain_p
, parm
, arg
);
21888 /* As far as unification is concerned, this wins. Later checks
21889 will invalidate it if necessary. */
21890 return unify_success (explain_p
);
21892 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21893 /* Type INTEGER_CST can come from ordinary constant template args. */
21895 while (CONVERT_EXPR_P (arg
))
21896 arg
= TREE_OPERAND (arg
, 0);
21898 if (TREE_CODE (arg
) != INTEGER_CST
)
21899 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21900 return (tree_int_cst_equal (parm
, arg
)
21901 ? unify_success (explain_p
)
21902 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21906 int i
, len
, argslen
;
21907 int parm_variadic_p
= 0;
21909 if (TREE_CODE (arg
) != TREE_VEC
)
21910 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21912 len
= TREE_VEC_LENGTH (parm
);
21913 argslen
= TREE_VEC_LENGTH (arg
);
21915 /* Check for pack expansions in the parameters. */
21916 for (i
= 0; i
< len
; ++i
)
21918 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21921 /* We can unify against something with a trailing
21923 parm_variadic_p
= 1;
21925 /* [temp.deduct.type]/9: If the template argument list of
21926 P contains a pack expansion that is not the last
21927 template argument, the entire template argument list
21928 is a non-deduced context. */
21929 return unify_success (explain_p
);
21933 /* If we don't have enough arguments to satisfy the parameters
21934 (not counting the pack expression at the end), or we have
21935 too many arguments for a parameter list that doesn't end in
21936 a pack expression, we can't unify. */
21937 if (parm_variadic_p
21938 ? argslen
< len
- parm_variadic_p
21940 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21942 /* Unify all of the parameters that precede the (optional)
21943 pack expression. */
21944 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21946 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21947 TREE_VEC_ELT (parm
, i
),
21948 TREE_VEC_ELT (arg
, i
),
21949 UNIFY_ALLOW_NONE
, explain_p
);
21951 if (parm_variadic_p
)
21952 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21954 /*subr=*/true, explain_p
);
21955 return unify_success (explain_p
);
21960 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21961 return unify_type_mismatch (explain_p
, parm
, arg
);
21963 if (TYPE_PTRMEMFUNC_P (parm
))
21965 if (!TYPE_PTRMEMFUNC_P (arg
))
21966 return unify_type_mismatch (explain_p
, parm
, arg
);
21968 return unify (tparms
, targs
,
21969 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21970 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21971 strict
, explain_p
);
21973 else if (TYPE_PTRMEMFUNC_P (arg
))
21974 return unify_type_mismatch (explain_p
, parm
, arg
);
21976 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21978 tree t
= NULL_TREE
;
21980 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21982 /* First, we try to unify the PARM and ARG directly. */
21983 t
= try_class_unification (tparms
, targs
,
21984 parm
, arg
, explain_p
);
21988 /* Fallback to the special case allowed in
21989 [temp.deduct.call]:
21991 If P is a class, and P has the form
21992 template-id, then A can be a derived class of
21993 the deduced A. Likewise, if P is a pointer to
21994 a class of the form template-id, A can be a
21995 pointer to a derived class pointed to by the
21997 enum template_base_result r
;
21998 r
= get_template_base (tparms
, targs
, parm
, arg
,
22003 /* Don't give the derived diagnostic if we're
22004 already dealing with the same template. */
22006 = (CLASSTYPE_TEMPLATE_INFO (arg
)
22007 && (CLASSTYPE_TI_TEMPLATE (parm
)
22008 == CLASSTYPE_TI_TEMPLATE (arg
)));
22009 return unify_no_common_base (explain_p
&& !same_template
,
22014 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
22015 && (CLASSTYPE_TI_TEMPLATE (parm
)
22016 == CLASSTYPE_TI_TEMPLATE (arg
)))
22017 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22018 Then, we should unify `int' and `U'. */
22021 /* There's no chance of unification succeeding. */
22022 return unify_type_mismatch (explain_p
, parm
, arg
);
22024 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
22025 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
22027 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
22028 return unify_type_mismatch (explain_p
, parm
, arg
);
22029 return unify_success (explain_p
);
22032 case FUNCTION_TYPE
:
22034 unsigned int nargs
;
22039 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22040 return unify_type_mismatch (explain_p
, parm
, arg
);
22042 /* CV qualifications for methods can never be deduced, they must
22043 match exactly. We need to check them explicitly here,
22044 because type_unification_real treats them as any other
22045 cv-qualified parameter. */
22046 if (TREE_CODE (parm
) == METHOD_TYPE
22047 && (!check_cv_quals_for_unify
22049 class_of_this_parm (arg
),
22050 class_of_this_parm (parm
))))
22051 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22052 if (TREE_CODE (arg
) == FUNCTION_TYPE
22053 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
22054 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22055 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
22056 return unify_type_mismatch (explain_p
, parm
, arg
);
22058 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
22059 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
22061 nargs
= list_length (TYPE_ARG_TYPES (arg
));
22062 args
= XALLOCAVEC (tree
, nargs
);
22063 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
22064 a
!= NULL_TREE
&& a
!= void_list_node
;
22065 a
= TREE_CHAIN (a
), ++i
)
22066 args
[i
] = TREE_VALUE (a
);
22069 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
22070 args
, nargs
, 1, DEDUCE_EXACT
,
22074 if (flag_noexcept_type
)
22076 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
22077 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
22078 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
22079 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
22080 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
22081 && uses_template_parms (TREE_PURPOSE (pspec
)))
22082 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
22083 TREE_PURPOSE (aspec
),
22084 UNIFY_ALLOW_NONE
, explain_p
);
22085 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
22086 return unify_type_mismatch (explain_p
, parm
, arg
);
22093 /* Unify a pointer to member with a pointer to member function, which
22094 deduces the type of the member as a function type. */
22095 if (TYPE_PTRMEMFUNC_P (arg
))
22097 /* Check top-level cv qualifiers */
22098 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
22099 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22101 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22102 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
22103 UNIFY_ALLOW_NONE
, explain_p
);
22105 /* Determine the type of the function we are unifying against. */
22106 tree fntype
= static_fn_type (arg
);
22108 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
22111 if (TREE_CODE (arg
) != OFFSET_TYPE
)
22112 return unify_type_mismatch (explain_p
, parm
, arg
);
22113 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22114 TYPE_OFFSET_BASETYPE (arg
),
22115 UNIFY_ALLOW_NONE
, explain_p
);
22116 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22117 strict
, explain_p
);
22120 if (DECL_TEMPLATE_PARM_P (parm
))
22121 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
22122 if (arg
!= scalar_constant_value (parm
))
22123 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22124 return unify_success (explain_p
);
22127 case TEMPLATE_DECL
:
22128 /* Matched cases are handled by the ARG == PARM test above. */
22129 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22132 /* We might get a variable as a non-type template argument in parm if the
22133 corresponding parameter is type-dependent. Make any necessary
22134 adjustments based on whether arg is a reference. */
22135 if (CONSTANT_CLASS_P (arg
))
22136 parm
= fold_non_dependent_expr (parm
, complain
);
22137 else if (REFERENCE_REF_P (arg
))
22139 tree sub
= TREE_OPERAND (arg
, 0);
22141 if (TREE_CODE (sub
) == ADDR_EXPR
)
22142 arg
= TREE_OPERAND (sub
, 0);
22144 /* Now use the normal expression code to check whether they match. */
22147 case TYPE_ARGUMENT_PACK
:
22148 case NONTYPE_ARGUMENT_PACK
:
22149 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
22150 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
22153 case DECLTYPE_TYPE
:
22154 case UNDERLYING_TYPE
:
22155 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22156 or UNDERLYING_TYPE nodes. */
22157 return unify_success (explain_p
);
22160 /* Unification fails if we hit an error node. */
22161 return unify_invalid (explain_p
);
22164 if (REFERENCE_REF_P (parm
))
22166 bool pexp
= PACK_EXPANSION_P (arg
);
22168 arg
= PACK_EXPANSION_PATTERN (arg
);
22169 if (REFERENCE_REF_P (arg
))
22170 arg
= TREE_OPERAND (arg
, 0);
22172 arg
= make_pack_expansion (arg
, complain
);
22173 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
22174 strict
, explain_p
);
22179 /* An unresolved overload is a nondeduced context. */
22180 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
22181 return unify_success (explain_p
);
22182 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
22184 /* We must be looking at an expression. This can happen with
22188 void foo(S<I>, S<I + 2>);
22190 This is a "nondeduced context":
22194 The nondeduced contexts are:
22196 --A type that is a template-id in which one or more of
22197 the template-arguments is an expression that references
22198 a template-parameter.
22200 In these cases, we assume deduction succeeded, but don't
22201 actually infer any unifications. */
22203 if (!uses_template_parms (parm
)
22204 && !template_args_equal (parm
, arg
))
22205 return unify_expression_unequal (explain_p
, parm
, arg
);
22207 return unify_success (explain_p
);
22210 #undef RECUR_AND_CHECK_FAILURE
22212 /* Note that DECL can be defined in this translation unit, if
22216 mark_definable (tree decl
)
22219 DECL_NOT_REALLY_EXTERN (decl
) = 1;
22220 FOR_EACH_CLONE (clone
, decl
)
22221 DECL_NOT_REALLY_EXTERN (clone
) = 1;
22224 /* Called if RESULT is explicitly instantiated, or is a member of an
22225 explicitly instantiated class. */
22228 mark_decl_instantiated (tree result
, int extern_p
)
22230 SET_DECL_EXPLICIT_INSTANTIATION (result
);
22232 /* If this entity has already been written out, it's too late to
22233 make any modifications. */
22234 if (TREE_ASM_WRITTEN (result
))
22237 /* For anonymous namespace we don't need to do anything. */
22238 if (decl_anon_ns_mem_p (result
))
22240 gcc_assert (!TREE_PUBLIC (result
));
22244 if (TREE_CODE (result
) != FUNCTION_DECL
)
22245 /* The TREE_PUBLIC flag for function declarations will have been
22246 set correctly by tsubst. */
22247 TREE_PUBLIC (result
) = 1;
22249 /* This might have been set by an earlier implicit instantiation. */
22250 DECL_COMDAT (result
) = 0;
22253 DECL_NOT_REALLY_EXTERN (result
) = 0;
22256 mark_definable (result
);
22257 mark_needed (result
);
22258 /* Always make artificials weak. */
22259 if (DECL_ARTIFICIAL (result
) && flag_weak
)
22260 comdat_linkage (result
);
22261 /* For WIN32 we also want to put explicit instantiations in
22262 linkonce sections. */
22263 else if (TREE_PUBLIC (result
))
22264 maybe_make_one_only (result
);
22265 if (TREE_CODE (result
) == FUNCTION_DECL
22266 && DECL_TEMPLATE_INSTANTIATED (result
))
22267 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22268 since start_preparsed_function wouldn't have if we had an earlier
22269 extern explicit instantiation. */
22270 DECL_EXTERNAL (result
) = 0;
22273 /* If EXTERN_P, then this function will not be emitted -- unless
22274 followed by an explicit instantiation, at which point its linkage
22275 will be adjusted. If !EXTERN_P, then this function will be
22276 emitted here. In neither circumstance do we want
22277 import_export_decl to adjust the linkage. */
22278 DECL_INTERFACE_KNOWN (result
) = 1;
22281 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22282 important template arguments. If any are missing, we check whether
22283 they're important by using error_mark_node for substituting into any
22284 args that were used for partial ordering (the ones between ARGS and END)
22285 and seeing if it bubbles up. */
22288 check_undeduced_parms (tree targs
, tree args
, tree end
)
22290 bool found
= false;
22292 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
22293 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
22296 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
22300 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
22301 if (substed
== error_mark_node
)
22307 /* Given two function templates PAT1 and PAT2, return:
22309 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22310 -1 if PAT2 is more specialized than PAT1.
22311 0 if neither is more specialized.
22313 LEN indicates the number of parameters we should consider
22314 (defaulted parameters should not be considered).
22316 The 1998 std underspecified function template partial ordering, and
22317 DR214 addresses the issue. We take pairs of arguments, one from
22318 each of the templates, and deduce them against each other. One of
22319 the templates will be more specialized if all the *other*
22320 template's arguments deduce against its arguments and at least one
22321 of its arguments *does* *not* deduce against the other template's
22322 corresponding argument. Deduction is done as for class templates.
22323 The arguments used in deduction have reference and top level cv
22324 qualifiers removed. Iff both arguments were originally reference
22325 types *and* deduction succeeds in both directions, an lvalue reference
22326 wins against an rvalue reference and otherwise the template
22327 with the more cv-qualified argument wins for that pairing (if
22328 neither is more cv-qualified, they both are equal). Unlike regular
22329 deduction, after all the arguments have been deduced in this way,
22330 we do *not* verify the deduced template argument values can be
22331 substituted into non-deduced contexts.
22333 The logic can be a bit confusing here, because we look at deduce1 and
22334 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22335 can find template arguments for pat1 to make arg1 look like arg2, that
22336 means that arg2 is at least as specialized as arg1. */
22339 more_specialized_fn (tree pat1
, tree pat2
, int len
)
22341 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
22342 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
22343 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
22344 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
22345 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
22346 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
22347 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
22348 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
22349 tree origs1
, origs2
;
22350 bool lose1
= false;
22351 bool lose2
= false;
22353 /* Remove the this parameter from non-static member functions. If
22354 one is a non-static member function and the other is not a static
22355 member function, remove the first parameter from that function
22356 also. This situation occurs for operator functions where we
22357 locate both a member function (with this pointer) and non-member
22358 operator (with explicit first operand). */
22359 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
22361 len
--; /* LEN is the number of significant arguments for DECL1 */
22362 args1
= TREE_CHAIN (args1
);
22363 if (!DECL_STATIC_FUNCTION_P (decl2
))
22364 args2
= TREE_CHAIN (args2
);
22366 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
22368 args2
= TREE_CHAIN (args2
);
22369 if (!DECL_STATIC_FUNCTION_P (decl1
))
22372 args1
= TREE_CHAIN (args1
);
22376 /* If only one is a conversion operator, they are unordered. */
22377 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
22380 /* Consider the return type for a conversion function */
22381 if (DECL_CONV_FN_P (decl1
))
22383 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
22384 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
22388 processing_template_decl
++;
22394 /* Stop when an ellipsis is seen. */
22395 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
22397 tree arg1
= TREE_VALUE (args1
);
22398 tree arg2
= TREE_VALUE (args2
);
22399 int deduce1
, deduce2
;
22405 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22406 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22408 /* When both arguments are pack expansions, we need only
22409 unify the patterns themselves. */
22410 arg1
= PACK_EXPANSION_PATTERN (arg1
);
22411 arg2
= PACK_EXPANSION_PATTERN (arg2
);
22413 /* This is the last comparison we need to do. */
22417 /* DR 1847: If a particular P contains no template-parameters that
22418 participate in template argument deduction, that P is not used to
22419 determine the ordering. */
22420 if (!uses_deducible_template_parms (arg1
)
22421 && !uses_deducible_template_parms (arg2
))
22424 if (TYPE_REF_P (arg1
))
22426 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
22427 arg1
= TREE_TYPE (arg1
);
22428 quals1
= cp_type_quals (arg1
);
22431 if (TYPE_REF_P (arg2
))
22433 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
22434 arg2
= TREE_TYPE (arg2
);
22435 quals2
= cp_type_quals (arg2
);
22438 arg1
= TYPE_MAIN_VARIANT (arg1
);
22439 arg2
= TYPE_MAIN_VARIANT (arg2
);
22441 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
22443 int i
, len2
= remaining_arguments (args2
);
22444 tree parmvec
= make_tree_vec (1);
22445 tree argvec
= make_tree_vec (len2
);
22448 /* Setup the parameter vector, which contains only ARG1. */
22449 TREE_VEC_ELT (parmvec
, 0) = arg1
;
22451 /* Setup the argument vector, which contains the remaining
22453 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
22454 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22456 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
22457 argvec
, DEDUCE_EXACT
,
22458 /*subr=*/true, /*explain_p=*/false)
22461 /* We cannot deduce in the other direction, because ARG1 is
22462 a pack expansion but ARG2 is not. */
22465 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22467 int i
, len1
= remaining_arguments (args1
);
22468 tree parmvec
= make_tree_vec (1);
22469 tree argvec
= make_tree_vec (len1
);
22472 /* Setup the parameter vector, which contains only ARG1. */
22473 TREE_VEC_ELT (parmvec
, 0) = arg2
;
22475 /* Setup the argument vector, which contains the remaining
22477 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
22478 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22480 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
22481 argvec
, DEDUCE_EXACT
,
22482 /*subr=*/true, /*explain_p=*/false)
22485 /* We cannot deduce in the other direction, because ARG2 is
22486 a pack expansion but ARG1 is not.*/
22492 /* The normal case, where neither argument is a pack
22494 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
22495 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22497 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
22498 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22502 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22503 arg2, then arg2 is not as specialized as arg1. */
22509 /* "If, for a given type, deduction succeeds in both directions
22510 (i.e., the types are identical after the transformations above)
22511 and both P and A were reference types (before being replaced with
22512 the type referred to above):
22513 - if the type from the argument template was an lvalue reference and
22514 the type from the parameter template was not, the argument type is
22515 considered to be more specialized than the other; otherwise,
22516 - if the type from the argument template is more cv-qualified
22517 than the type from the parameter template (as described above),
22518 the argument type is considered to be more specialized than the other;
22520 - neither type is more specialized than the other." */
22522 if (deduce1
&& deduce2
)
22524 if (ref1
&& ref2
&& ref1
!= ref2
)
22531 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
22533 if ((quals1
& quals2
) == quals2
)
22535 if ((quals1
& quals2
) == quals1
)
22540 if (lose1
&& lose2
)
22541 /* We've failed to deduce something in either direction.
22542 These must be unordered. */
22547 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22548 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22549 /* We have already processed all of the arguments in our
22550 handing of the pack expansion type. */
22553 args1
= TREE_CHAIN (args1
);
22554 args2
= TREE_CHAIN (args2
);
22557 /* "In most cases, all template parameters must have values in order for
22558 deduction to succeed, but for partial ordering purposes a template
22559 parameter may remain without a value provided it is not used in the
22560 types being used for partial ordering."
22562 Thus, if we are missing any of the targs1 we need to substitute into
22563 origs1, then pat2 is not as specialized as pat1. This can happen when
22564 there is a nondeduced context. */
22565 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
22567 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
22570 processing_template_decl
--;
22572 /* If both deductions succeed, the partial ordering selects the more
22573 constrained template. */
22574 if (!lose1
&& !lose2
)
22576 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
22577 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
22578 lose1
= !subsumes_constraints (c1
, c2
);
22579 lose2
= !subsumes_constraints (c2
, c1
);
22582 /* All things being equal, if the next argument is a pack expansion
22583 for one function but not for the other, prefer the
22584 non-variadic function. FIXME this is bogus; see c++/41958. */
22586 && args1
&& TREE_VALUE (args1
)
22587 && args2
&& TREE_VALUE (args2
))
22589 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
22590 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
22593 if (lose1
== lose2
)
22601 /* Determine which of two partial specializations of TMPL is more
22604 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22605 to the first partial specialization. The TREE_PURPOSE is the
22606 innermost set of template parameters for the partial
22607 specialization. PAT2 is similar, but for the second template.
22609 Return 1 if the first partial specialization is more specialized;
22610 -1 if the second is more specialized; 0 if neither is more
22613 See [temp.class.order] for information about determining which of
22614 two templates is more specialized. */
22617 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
22621 bool any_deductions
= false;
22623 tree tmpl1
= TREE_VALUE (pat1
);
22624 tree tmpl2
= TREE_VALUE (pat2
);
22625 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
22626 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
22628 /* Just like what happens for functions, if we are ordering between
22629 different template specializations, we may encounter dependent
22630 types in the arguments, and we need our dependency check functions
22631 to behave correctly. */
22632 ++processing_template_decl
;
22633 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
22637 any_deductions
= true;
22640 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
22644 any_deductions
= true;
22646 --processing_template_decl
;
22648 /* If both deductions succeed, the partial ordering selects the more
22649 constrained template. */
22650 if (!winner
&& any_deductions
)
22651 return more_constrained (tmpl1
, tmpl2
);
22653 /* In the case of a tie where at least one of the templates
22654 has a parameter pack at the end, the template with the most
22655 non-packed parameters wins. */
22658 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
22659 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
22661 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
22662 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
22663 int len1
= TREE_VEC_LENGTH (args1
);
22664 int len2
= TREE_VEC_LENGTH (args2
);
22666 /* We don't count the pack expansion at the end. */
22667 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
22669 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
22674 else if (len1
< len2
)
22681 /* Return the template arguments that will produce the function signature
22682 DECL from the function template FN, with the explicit template
22683 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22684 also match. Return NULL_TREE if no satisfactory arguments could be
22688 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
22690 int ntparms
= DECL_NTPARMS (fn
);
22691 tree targs
= make_tree_vec (ntparms
);
22692 tree decl_type
= TREE_TYPE (decl
);
22693 tree decl_arg_types
;
22695 unsigned int nargs
, ix
;
22698 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22700 /* Never do unification on the 'this' parameter. */
22701 decl_arg_types
= skip_artificial_parms_for (decl
,
22702 TYPE_ARG_TYPES (decl_type
));
22704 nargs
= list_length (decl_arg_types
);
22705 args
= XALLOCAVEC (tree
, nargs
);
22706 for (arg
= decl_arg_types
, ix
= 0;
22707 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22708 arg
= TREE_CHAIN (arg
), ++ix
)
22709 args
[ix
] = TREE_VALUE (arg
);
22711 if (fn_type_unification (fn
, explicit_args
, targs
,
22713 (check_rettype
|| DECL_CONV_FN_P (fn
)
22714 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22715 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
22716 /*explain_p=*/false,
22718 == error_mark_node
)
22724 /* Return the innermost template arguments that, when applied to a partial
22725 specialization SPEC_TMPL of TMPL, yield the ARGS.
22727 For example, suppose we have:
22729 template <class T, class U> struct S {};
22730 template <class T> struct S<T*, int> {};
22732 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22733 partial specialization and the ARGS will be {double*, int}. The resulting
22734 vector will be {double}, indicating that `T' is bound to `double'. */
22737 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22739 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22741 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22742 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22744 tree innermost_deduced_args
;
22746 innermost_deduced_args
= make_tree_vec (ntparms
);
22747 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22749 deduced_args
= copy_node (args
);
22750 SET_TMPL_ARGS_LEVEL (deduced_args
,
22751 TMPL_ARGS_DEPTH (deduced_args
),
22752 innermost_deduced_args
);
22755 deduced_args
= innermost_deduced_args
;
22757 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22759 if (unify (tparms
, deduced_args
,
22760 INNERMOST_TEMPLATE_ARGS (spec_args
),
22761 INNERMOST_TEMPLATE_ARGS (args
),
22762 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22765 for (i
= 0; i
< ntparms
; ++i
)
22766 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22768 if (!tried_array_deduction
)
22770 try_array_deduction (tparms
, innermost_deduced_args
,
22771 INNERMOST_TEMPLATE_ARGS (spec_args
));
22772 tried_array_deduction
= true;
22773 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22779 if (!push_tinst_level (spec_tmpl
, deduced_args
))
22781 excessive_deduction_depth
= true;
22785 /* Verify that nondeduced template arguments agree with the type
22786 obtained from argument deduction.
22790 struct A { typedef int X; };
22791 template <class T, class U> struct C {};
22792 template <class T> struct C<T, typename T::X> {};
22794 Then with the instantiation `C<A, int>', we can deduce that
22795 `T' is `A' but unify () does not check whether `typename T::X'
22797 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22799 if (spec_args
!= error_mark_node
)
22800 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22801 INNERMOST_TEMPLATE_ARGS (spec_args
),
22802 tmpl
, tf_none
, false, false);
22804 pop_tinst_level ();
22806 if (spec_args
== error_mark_node
22807 /* We only need to check the innermost arguments; the other
22808 arguments will always agree. */
22809 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22810 INNERMOST_TEMPLATE_ARGS (args
)))
22813 /* Now that we have bindings for all of the template arguments,
22814 ensure that the arguments deduced for the template template
22815 parameters have compatible template parameter lists. See the use
22816 of template_template_parm_bindings_ok_p in fn_type_unification
22817 for more information. */
22818 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22821 return deduced_args
;
22824 // Compare two function templates T1 and T2 by deducing bindings
22825 // from one against the other. If both deductions succeed, compare
22826 // constraints to see which is more constrained.
22828 more_specialized_inst (tree t1
, tree t2
)
22833 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22839 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22845 // If both deductions succeed, then one may be more constrained.
22846 if (count
== 2 && fate
== 0)
22847 fate
= more_constrained (t1
, t2
);
22852 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22853 Return the TREE_LIST node with the most specialized template, if
22854 any. If there is no most specialized template, the error_mark_node
22857 Note that this function does not look at, or modify, the
22858 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22859 returned is one of the elements of INSTANTIATIONS, callers may
22860 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22861 and retrieve it from the value returned. */
22864 most_specialized_instantiation (tree templates
)
22868 ++processing_template_decl
;
22871 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22873 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22874 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22879 /* Equally specialized, move to next function. If there
22880 is no next function, nothing's most specialized. */
22881 fn
= TREE_CHAIN (fn
);
22889 /* Now verify that champ is better than everything earlier in the
22890 instantiation list. */
22891 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22892 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22899 processing_template_decl
--;
22902 return error_mark_node
;
22907 /* If DECL is a specialization of some template, return the most
22908 general such template. Otherwise, returns NULL_TREE.
22910 For example, given:
22912 template <class T> struct S { template <class U> void f(U); };
22914 if TMPL is `template <class U> void S<int>::f(U)' this will return
22915 the full template. This function will not trace past partial
22916 specializations, however. For example, given in addition:
22918 template <class T> struct S<T*> { template <class U> void f(U); };
22920 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22921 `template <class T> template <class U> S<T*>::f(U)'. */
22924 most_general_template (tree decl
)
22926 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22928 if (tree tinfo
= get_template_info (decl
))
22929 decl
= TI_TEMPLATE (tinfo
);
22930 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22931 template friend, or a FIELD_DECL for a capture pack. */
22932 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22936 /* Look for more and more general templates. */
22937 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22939 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22940 (See cp-tree.h for details.) */
22941 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22944 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22945 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22946 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22949 /* Stop if we run into an explicitly specialized class template. */
22950 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22951 && DECL_CONTEXT (decl
)
22952 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22955 decl
= DECL_TI_TEMPLATE (decl
);
22961 /* Return the most specialized of the template partial specializations
22962 which can produce TARGET, a specialization of some class or variable
22963 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22964 a TEMPLATE_DECL node corresponding to the partial specialization, while
22965 the TREE_PURPOSE is the set of template arguments that must be
22966 substituted into the template pattern in order to generate TARGET.
22968 If the choice of partial specialization is ambiguous, a diagnostic
22969 is issued, and the error_mark_node is returned. If there are no
22970 partial specializations matching TARGET, then NULL_TREE is
22971 returned, indicating that the primary template should be used. */
22974 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22976 tree list
= NULL_TREE
;
22981 tree outer_args
= NULL_TREE
;
22984 if (TYPE_P (target
))
22986 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22987 tmpl
= TI_TEMPLATE (tinfo
);
22988 args
= TI_ARGS (tinfo
);
22990 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22992 tmpl
= TREE_OPERAND (target
, 0);
22993 args
= TREE_OPERAND (target
, 1);
22995 else if (VAR_P (target
))
22997 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22998 tmpl
= TI_TEMPLATE (tinfo
);
22999 args
= TI_ARGS (tinfo
);
23002 gcc_unreachable ();
23004 tree main_tmpl
= most_general_template (tmpl
);
23006 /* For determining which partial specialization to use, only the
23007 innermost args are interesting. */
23008 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23010 outer_args
= strip_innermost_template_args (args
, 1);
23011 args
= INNERMOST_TEMPLATE_ARGS (args
);
23014 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
23017 tree spec_tmpl
= TREE_VALUE (t
);
23021 /* Substitute in the template args from the enclosing class. */
23022 ++processing_template_decl
;
23023 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
23024 --processing_template_decl
;
23027 if (spec_tmpl
== error_mark_node
)
23028 return error_mark_node
;
23030 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
23034 spec_args
= add_to_template_args (outer_args
, spec_args
);
23036 /* Keep the candidate only if the constraints are satisfied,
23037 or if we're not compiling with concepts. */
23039 || constraints_satisfied_p (spec_tmpl
, spec_args
))
23041 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
23042 TREE_TYPE (list
) = TREE_TYPE (t
);
23050 ambiguous_p
= false;
23053 t
= TREE_CHAIN (t
);
23054 for (; t
; t
= TREE_CHAIN (t
))
23056 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23063 t
= TREE_CHAIN (t
);
23066 ambiguous_p
= true;
23075 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
23077 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23080 ambiguous_p
= true;
23088 char *spaces
= NULL
;
23089 if (!(complain
& tf_error
))
23090 return error_mark_node
;
23091 if (TYPE_P (target
))
23092 error ("ambiguous template instantiation for %q#T", target
);
23094 error ("ambiguous template instantiation for %q#D", target
);
23095 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
23096 for (t
= list
; t
; t
= TREE_CHAIN (t
))
23098 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
23099 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
23100 "%s %#qS", spaces
? spaces
: str
, subst
);
23101 spaces
= spaces
? spaces
: get_spaces (str
);
23104 return error_mark_node
;
23110 /* Explicitly instantiate DECL. */
23113 do_decl_instantiation (tree decl
, tree storage
)
23115 tree result
= NULL_TREE
;
23118 if (!decl
|| decl
== error_mark_node
)
23119 /* An error occurred, for which grokdeclarator has already issued
23120 an appropriate message. */
23122 else if (! DECL_LANG_SPECIFIC (decl
))
23124 error ("explicit instantiation of non-template %q#D", decl
);
23128 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
23129 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
23131 if (VAR_P (decl
) && !var_templ
)
23133 /* There is an asymmetry here in the way VAR_DECLs and
23134 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23135 the latter, the DECL we get back will be marked as a
23136 template instantiation, and the appropriate
23137 DECL_TEMPLATE_INFO will be set up. This does not happen for
23138 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23139 should handle VAR_DECLs as it currently handles
23141 if (!DECL_CLASS_SCOPE_P (decl
))
23143 error ("%qD is not a static data member of a class template", decl
);
23146 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
23147 if (!result
|| !VAR_P (result
))
23149 error ("no matching template for %qD found", decl
);
23152 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
23154 error ("type %qT for explicit instantiation %qD does not match "
23155 "declared type %qT", TREE_TYPE (result
), decl
,
23160 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
23162 error ("explicit instantiation of %q#D", decl
);
23168 /* Check for various error cases. Note that if the explicit
23169 instantiation is valid the RESULT will currently be marked as an
23170 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23171 until we get here. */
23173 if (DECL_TEMPLATE_SPECIALIZATION (result
))
23175 /* DR 259 [temp.spec].
23177 Both an explicit instantiation and a declaration of an explicit
23178 specialization shall not appear in a program unless the explicit
23179 instantiation follows a declaration of the explicit specialization.
23181 For a given set of template parameters, if an explicit
23182 instantiation of a template appears after a declaration of an
23183 explicit specialization for that template, the explicit
23184 instantiation has no effect. */
23187 else if (DECL_EXPLICIT_INSTANTIATION (result
))
23191 No program shall explicitly instantiate any template more
23194 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23195 the first instantiation was `extern' and the second is not,
23196 and EXTERN_P for the opposite case. */
23197 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
23198 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
23199 /* If an "extern" explicit instantiation follows an ordinary
23200 explicit instantiation, the template is instantiated. */
23204 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
23206 error ("no matching template for %qD found", result
);
23209 else if (!DECL_TEMPLATE_INFO (result
))
23211 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
23215 if (storage
== NULL_TREE
)
23217 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23219 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
23220 pedwarn (input_location
, OPT_Wpedantic
,
23221 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23226 error ("storage class %qD applied to template instantiation", storage
);
23228 check_explicit_instantiation_namespace (result
);
23229 mark_decl_instantiated (result
, extern_p
);
23231 instantiate_decl (result
, /*defer_ok=*/true,
23232 /*expl_inst_class_mem_p=*/false);
23236 mark_class_instantiated (tree t
, int extern_p
)
23238 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
23239 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
23240 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
23241 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
23244 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
23245 rest_of_type_compilation (t
, 1);
23249 /* Called from do_type_instantiation through binding_table_foreach to
23250 do recursive instantiation for the type bound in ENTRY. */
23252 bt_instantiate_type_proc (binding_entry entry
, void *data
)
23254 tree storage
= *(tree
*) data
;
23256 if (MAYBE_CLASS_TYPE_P (entry
->type
)
23257 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
23258 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
23259 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
23262 /* Perform an explicit instantiation of template class T. STORAGE, if
23263 non-null, is the RID for extern, inline or static. COMPLAIN is
23264 nonzero if this is called from the parser, zero if called recursively,
23265 since the standard is unclear (as detailed below). */
23268 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
23273 int previous_instantiation_extern_p
= 0;
23275 if (TREE_CODE (t
) == TYPE_DECL
)
23278 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
23281 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
23283 error ("explicit instantiation of non-class template %qD", tmpl
);
23285 error ("explicit instantiation of non-template type %qT", t
);
23291 if (!COMPLETE_TYPE_P (t
))
23293 if (complain
& tf_error
)
23294 error ("explicit instantiation of %q#T before definition of template",
23299 if (storage
!= NULL_TREE
)
23301 if (!in_system_header_at (input_location
))
23303 if (storage
== ridpointers
[(int) RID_EXTERN
])
23305 if (cxx_dialect
== cxx98
)
23306 pedwarn (input_location
, OPT_Wpedantic
,
23307 "ISO C++ 1998 forbids the use of %<extern%> on "
23308 "explicit instantiations");
23311 pedwarn (input_location
, OPT_Wpedantic
,
23312 "ISO C++ forbids the use of %qE"
23313 " on explicit instantiations", storage
);
23316 if (storage
== ridpointers
[(int) RID_INLINE
])
23318 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23320 else if (storage
== ridpointers
[(int) RID_STATIC
])
23324 error ("storage class %qD applied to template instantiation",
23330 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
23332 /* DR 259 [temp.spec].
23334 Both an explicit instantiation and a declaration of an explicit
23335 specialization shall not appear in a program unless the explicit
23336 instantiation follows a declaration of the explicit specialization.
23338 For a given set of template parameters, if an explicit
23339 instantiation of a template appears after a declaration of an
23340 explicit specialization for that template, the explicit
23341 instantiation has no effect. */
23344 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
23348 No program shall explicitly instantiate any template more
23351 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23352 instantiation was `extern'. If EXTERN_P then the second is.
23353 These cases are OK. */
23354 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
23356 if (!previous_instantiation_extern_p
&& !extern_p
23357 && (complain
& tf_error
))
23358 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
23360 /* If we've already instantiated the template, just return now. */
23361 if (!CLASSTYPE_INTERFACE_ONLY (t
))
23365 check_explicit_instantiation_namespace (TYPE_NAME (t
));
23366 mark_class_instantiated (t
, extern_p
);
23371 /* In contrast to implicit instantiation, where only the
23372 declarations, and not the definitions, of members are
23373 instantiated, we have here:
23377 The explicit instantiation of a class template specialization
23378 implies the instantiation of all of its members not
23379 previously explicitly specialized in the translation unit
23380 containing the explicit instantiation.
23382 Of course, we can't instantiate member template classes, since we
23383 don't have any arguments for them. Note that the standard is
23384 unclear on whether the instantiation of the members are
23385 *explicit* instantiations or not. However, the most natural
23386 interpretation is that it should be an explicit
23388 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
23390 || (TREE_CODE (fld
) == FUNCTION_DECL
23392 && user_provided_p (fld
)))
23393 && DECL_TEMPLATE_INSTANTIATION (fld
))
23395 mark_decl_instantiated (fld
, extern_p
);
23397 instantiate_decl (fld
, /*defer_ok=*/true,
23398 /*expl_inst_class_mem_p=*/true);
23401 if (CLASSTYPE_NESTED_UTDS (t
))
23402 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
23403 bt_instantiate_type_proc
, &storage
);
23406 /* Given a function DECL, which is a specialization of TMPL, modify
23407 DECL to be a re-instantiation of TMPL with the same template
23408 arguments. TMPL should be the template into which tsubst'ing
23409 should occur for DECL, not the most general template.
23411 One reason for doing this is a scenario like this:
23414 void f(const T&, int i);
23416 void g() { f(3, 7); }
23419 void f(const T& t, const int i) { }
23421 Note that when the template is first instantiated, with
23422 instantiate_template, the resulting DECL will have no name for the
23423 first parameter, and the wrong type for the second. So, when we go
23424 to instantiate the DECL, we regenerate it. */
23427 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
23429 /* The arguments used to instantiate DECL, from the most general
23433 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
23435 /* Make sure that we can see identifiers, and compute access
23437 push_access_scope (decl
);
23439 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23447 args_depth
= TMPL_ARGS_DEPTH (args
);
23448 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
23449 if (args_depth
> parms_depth
)
23450 args
= get_innermost_template_args (args
, parms_depth
);
23452 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
23453 args
, tf_error
, NULL_TREE
,
23454 /*defer_ok*/false);
23455 if (specs
&& specs
!= error_mark_node
)
23456 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
23459 /* Merge parameter declarations. */
23460 decl_parm
= skip_artificial_parms_for (decl
,
23461 DECL_ARGUMENTS (decl
));
23463 = skip_artificial_parms_for (code_pattern
,
23464 DECL_ARGUMENTS (code_pattern
));
23465 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
23470 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23471 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
23472 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
23474 parm_type
= type_decays_to (parm_type
);
23475 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23476 TREE_TYPE (decl_parm
) = parm_type
;
23477 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23478 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23480 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23481 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23483 decl_parm
= DECL_CHAIN (decl_parm
);
23484 pattern_parm
= DECL_CHAIN (pattern_parm
);
23486 /* Merge any parameters that match with the function parameter
23488 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
23491 tree expanded_types
;
23492 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23493 the parameters in this function parameter pack. */
23494 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
23495 args
, tf_error
, NULL_TREE
);
23496 len
= TREE_VEC_LENGTH (expanded_types
);
23497 for (i
= 0; i
< len
; i
++)
23502 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23503 /* Rename the parameter to include the index. */
23504 DECL_NAME (decl_parm
) =
23505 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
23506 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
23507 parm_type
= type_decays_to (parm_type
);
23508 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23509 TREE_TYPE (decl_parm
) = parm_type
;
23510 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23511 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23513 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23514 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23516 decl_parm
= DECL_CHAIN (decl_parm
);
23519 /* Merge additional specifiers from the CODE_PATTERN. */
23520 if (DECL_DECLARED_INLINE_P (code_pattern
)
23521 && !DECL_DECLARED_INLINE_P (decl
))
23522 DECL_DECLARED_INLINE_P (decl
) = 1;
23524 else if (VAR_P (decl
))
23526 start_lambda_scope (decl
);
23527 DECL_INITIAL (decl
) =
23528 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
23529 tf_error
, DECL_TI_TEMPLATE (decl
),
23530 /*integral_constant_expression_p=*/false);
23531 finish_lambda_scope ();
23532 if (VAR_HAD_UNKNOWN_BOUND (decl
))
23533 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
23534 tf_error
, DECL_TI_TEMPLATE (decl
));
23537 gcc_unreachable ();
23539 pop_access_scope (decl
);
23542 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23543 substituted to get DECL. */
23546 template_for_substitution (tree decl
)
23548 tree tmpl
= DECL_TI_TEMPLATE (decl
);
23550 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23551 for the instantiation. This is not always the most general
23552 template. Consider, for example:
23555 struct S { template <class U> void f();
23556 template <> void f<int>(); };
23558 and an instantiation of S<double>::f<int>. We want TD to be the
23559 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23560 while (/* An instantiation cannot have a definition, so we need a
23561 more general template. */
23562 DECL_TEMPLATE_INSTANTIATION (tmpl
)
23563 /* We must also deal with friend templates. Given:
23565 template <class T> struct S {
23566 template <class U> friend void f() {};
23569 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23570 so far as the language is concerned, but that's still
23571 where we get the pattern for the instantiation from. On
23572 other hand, if the definition comes outside the class, say:
23574 template <class T> struct S {
23575 template <class U> friend void f();
23577 template <class U> friend void f() {}
23579 we don't need to look any further. That's what the check for
23580 DECL_INITIAL is for. */
23581 || (TREE_CODE (decl
) == FUNCTION_DECL
23582 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
23583 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
23585 /* The present template, TD, should not be a definition. If it
23586 were a definition, we should be using it! Note that we
23587 cannot restructure the loop to just keep going until we find
23588 a template with a definition, since that might go too far if
23589 a specialization was declared, but not defined. */
23591 /* Fetch the more general template. */
23592 tmpl
= DECL_TI_TEMPLATE (tmpl
);
23598 /* Returns true if we need to instantiate this template instance even if we
23599 know we aren't going to emit it. */
23602 always_instantiate_p (tree decl
)
23604 /* We always instantiate inline functions so that we can inline them. An
23605 explicit instantiation declaration prohibits implicit instantiation of
23606 non-inline functions. With high levels of optimization, we would
23607 normally inline non-inline functions -- but we're not allowed to do
23608 that for "extern template" functions. Therefore, we check
23609 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23610 return ((TREE_CODE (decl
) == FUNCTION_DECL
23611 && (DECL_DECLARED_INLINE_P (decl
)
23612 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
23613 /* And we need to instantiate static data members so that
23614 their initializers are available in integral constant
23617 && decl_maybe_constant_var_p (decl
)));
23620 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23621 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23622 error, true otherwise. */
23625 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
23627 tree fntype
, spec
, noex
, clone
;
23629 /* Don't instantiate a noexcept-specification from template context. */
23630 if (processing_template_decl
23631 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
23634 if (DECL_CLONED_FUNCTION_P (fn
))
23635 fn
= DECL_CLONED_FUNCTION (fn
);
23636 fntype
= TREE_TYPE (fn
);
23637 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
23639 if (!spec
|| !TREE_PURPOSE (spec
))
23642 noex
= TREE_PURPOSE (spec
);
23644 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
23646 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
23647 bool added
= false;
23648 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
23649 spec
= get_defaulted_eh_spec (fn
, complain
);
23650 else if (!(added
= !fns
->add (fn
)))
23652 /* If hash_set::add returns true, the element was already there. */
23653 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
23654 DECL_SOURCE_LOCATION (fn
));
23656 "exception specification of %qD depends on itself",
23658 spec
= noexcept_false_spec
;
23660 else if (push_tinst_level (fn
))
23662 push_access_scope (fn
);
23663 push_deferring_access_checks (dk_no_deferred
);
23664 input_location
= DECL_SOURCE_LOCATION (fn
);
23665 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
23666 DEFERRED_NOEXCEPT_ARGS (noex
),
23667 tf_warning_or_error
, fn
,
23668 /*function_p=*/false,
23669 /*integral_constant_expression_p=*/true);
23670 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
23671 pop_deferring_access_checks ();
23672 pop_access_scope (fn
);
23673 pop_tinst_level ();
23674 if (spec
== error_mark_node
)
23675 spec
= noexcept_false_spec
;
23678 spec
= noexcept_false_spec
;
23683 if (spec
== error_mark_node
)
23686 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
23689 FOR_EACH_CLONE (clone
, fn
)
23691 if (TREE_TYPE (clone
) == fntype
)
23692 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23694 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23700 /* We're starting to process the function INST, an instantiation of PATTERN;
23701 add their parameters to local_specializations. */
23704 register_parameter_specializations (tree pattern
, tree inst
)
23706 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23707 tree spec_parm
= DECL_ARGUMENTS (inst
);
23708 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23710 register_local_specialization (spec_parm
, tmpl_parm
);
23711 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23712 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23714 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23716 if (!DECL_PACK_P (tmpl_parm
))
23718 register_local_specialization (spec_parm
, tmpl_parm
);
23719 spec_parm
= DECL_CHAIN (spec_parm
);
23723 /* Register the (value) argument pack as a specialization of
23724 TMPL_PARM, then move on. */
23725 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23726 register_local_specialization (argpack
, tmpl_parm
);
23729 gcc_assert (!spec_parm
);
23732 /* Produce the definition of D, a _DECL generated from a template. If
23733 DEFER_OK is true, then we don't have to actually do the
23734 instantiation now; we just have to do it sometime. Normally it is
23735 an error if this is an explicit instantiation but D is undefined.
23736 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23737 instantiated class template. */
23740 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23742 tree tmpl
= DECL_TI_TEMPLATE (d
);
23749 bool pattern_defined
;
23750 location_t saved_loc
= input_location
;
23751 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23752 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23756 /* This function should only be used to instantiate templates for
23757 functions and static member variables. */
23758 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23760 /* A concept is never instantiated. */
23761 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23763 /* Variables are never deferred; if instantiation is required, they
23764 are instantiated right away. That allows for better code in the
23765 case that an expression refers to the value of the variable --
23766 if the variable has a constant value the referring expression can
23767 take advantage of that fact. */
23771 /* Don't instantiate cloned functions. Instead, instantiate the
23772 functions they cloned. */
23773 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23774 d
= DECL_CLONED_FUNCTION (d
);
23776 if (DECL_TEMPLATE_INSTANTIATED (d
)
23777 || (TREE_CODE (d
) == FUNCTION_DECL
23778 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23779 || DECL_TEMPLATE_SPECIALIZATION (d
))
23780 /* D has already been instantiated or explicitly specialized, so
23781 there's nothing for us to do here.
23783 It might seem reasonable to check whether or not D is an explicit
23784 instantiation, and, if so, stop here. But when an explicit
23785 instantiation is deferred until the end of the compilation,
23786 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23787 the instantiation. */
23790 /* Check to see whether we know that this template will be
23791 instantiated in some other file, as with "extern template"
23793 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23795 /* In general, we do not instantiate such templates. */
23796 if (external_p
&& !always_instantiate_p (d
))
23799 gen_tmpl
= most_general_template (tmpl
);
23800 gen_args
= DECL_TI_ARGS (d
);
23802 if (tmpl
!= gen_tmpl
)
23803 /* We should already have the extra args. */
23804 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23805 == TMPL_ARGS_DEPTH (gen_args
));
23806 /* And what's in the hash table should match D. */
23807 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23808 || spec
== NULL_TREE
);
23810 /* This needs to happen before any tsubsting. */
23811 if (! push_tinst_level (d
))
23814 timevar_push (TV_TEMPLATE_INST
);
23816 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23817 for the instantiation. */
23818 td
= template_for_substitution (d
);
23823 /* Look up an explicit specialization, if any. */
23824 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23825 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23826 if (elt
&& elt
!= error_mark_node
)
23828 td
= TREE_VALUE (elt
);
23829 args
= TREE_PURPOSE (elt
);
23833 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23835 /* We should never be trying to instantiate a member of a class
23836 template or partial specialization. */
23837 gcc_assert (d
!= code_pattern
);
23839 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23840 || DECL_TEMPLATE_SPECIALIZATION (td
))
23841 /* In the case of a friend template whose definition is provided
23842 outside the class, we may have too many arguments. Drop the
23843 ones we don't need. The same is true for specializations. */
23844 args
= get_innermost_template_args
23845 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23847 if (TREE_CODE (d
) == FUNCTION_DECL
)
23849 deleted_p
= DECL_DELETED_FN (code_pattern
);
23850 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23851 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23852 || DECL_DEFAULTED_FN (code_pattern
)
23858 if (DECL_CLASS_SCOPE_P (code_pattern
))
23859 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23860 || DECL_INLINE_VAR_P (code_pattern
));
23862 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23865 /* We may be in the middle of deferred access check. Disable it now. */
23866 push_deferring_access_checks (dk_no_deferred
);
23868 /* Unless an explicit instantiation directive has already determined
23869 the linkage of D, remember that a definition is available for
23871 if (pattern_defined
23872 && !DECL_INTERFACE_KNOWN (d
)
23873 && !DECL_NOT_REALLY_EXTERN (d
))
23874 mark_definable (d
);
23876 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23877 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23878 input_location
= DECL_SOURCE_LOCATION (d
);
23880 /* If D is a member of an explicitly instantiated class template,
23881 and no definition is available, treat it like an implicit
23883 if (!pattern_defined
&& expl_inst_class_mem_p
23884 && DECL_EXPLICIT_INSTANTIATION (d
))
23886 /* Leave linkage flags alone on instantiations with anonymous
23888 if (TREE_PUBLIC (d
))
23890 DECL_NOT_REALLY_EXTERN (d
) = 0;
23891 DECL_INTERFACE_KNOWN (d
) = 0;
23893 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23896 /* Defer all other templates, unless we have been explicitly
23897 forbidden from doing so. */
23898 if (/* If there is no definition, we cannot instantiate the
23901 /* If it's OK to postpone instantiation, do so. */
23903 /* If this is a static data member that will be defined
23904 elsewhere, we don't want to instantiate the entire data
23905 member, but we do want to instantiate the initializer so that
23906 we can substitute that elsewhere. */
23907 || (external_p
&& VAR_P (d
))
23908 /* Handle here a deleted function too, avoid generating
23909 its body (c++/61080). */
23912 /* The definition of the static data member is now required so
23913 we must substitute the initializer. */
23915 && !DECL_INITIAL (d
)
23916 && DECL_INITIAL (code_pattern
))
23920 bool const_init
= false;
23921 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23923 ns
= decl_namespace_context (d
);
23924 push_nested_namespace (ns
);
23926 push_nested_class (DECL_CONTEXT (d
));
23927 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23929 tf_warning_or_error
, NULL_TREE
,
23930 /*integral_constant_expression_p=*/false);
23931 /* If instantiating the initializer involved instantiating this
23932 again, don't call cp_finish_decl twice. */
23933 if (!DECL_INITIAL (d
))
23935 /* Make sure the initializer is still constant, in case of
23936 circular dependency (template/instantiate6.C). */
23938 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23939 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23940 /*asmspec_tree=*/NULL_TREE
,
23941 LOOKUP_ONLYCONVERTING
);
23944 pop_nested_class ();
23945 pop_nested_namespace (ns
);
23948 /* We restore the source position here because it's used by
23949 add_pending_template. */
23950 input_location
= saved_loc
;
23952 if (at_eof
&& !pattern_defined
23953 && DECL_EXPLICIT_INSTANTIATION (d
)
23954 && DECL_NOT_REALLY_EXTERN (d
))
23957 The definition of a non-exported function template, a
23958 non-exported member function template, or a non-exported
23959 member function or static data member of a class template
23960 shall be present in every translation unit in which it is
23961 explicitly instantiated. */
23962 permerror (input_location
, "explicit instantiation of %qD "
23963 "but no definition available", d
);
23965 /* If we're in unevaluated context, we just wanted to get the
23966 constant value; this isn't an odr use, so don't queue
23967 a full instantiation. */
23968 if (cp_unevaluated_operand
!= 0)
23970 /* ??? Historically, we have instantiated inline functions, even
23971 when marked as "extern template". */
23972 if (!(external_p
&& VAR_P (d
)))
23973 add_pending_template (d
);
23976 /* Tell the repository that D is available in this translation unit
23977 -- and see if it is supposed to be instantiated here. */
23978 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23980 /* In a PCH file, despite the fact that the repository hasn't
23981 requested instantiation in the PCH it is still possible that
23982 an instantiation will be required in a file that includes the
23985 add_pending_template (d
);
23986 /* Instantiate inline functions so that the inliner can do its
23987 job, even though we'll not be emitting a copy of this
23989 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23993 bool push_to_top
, nested
;
23995 fn_context
= decl_function_context (d
);
23996 if (LAMBDA_FUNCTION_P (d
))
23997 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23998 fn_context
= NULL_TREE
;
23999 nested
= current_function_decl
!= NULL_TREE
;
24000 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
24002 vec
<tree
> omp_privatization_save
;
24004 save_omp_privatization_clauses (omp_privatization_save
);
24007 push_to_top_level ();
24010 gcc_assert (!processing_template_decl
);
24011 push_function_context ();
24012 cp_unevaluated_operand
= 0;
24013 c_inhibit_evaluation_warnings
= 0;
24016 /* Mark D as instantiated so that recursive calls to
24017 instantiate_decl do not try to instantiate it again. */
24018 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
24020 /* Regenerate the declaration in case the template has been modified
24021 by a subsequent redeclaration. */
24022 regenerate_decl_from_template (d
, td
, args
);
24024 /* We already set the file and line above. Reset them now in case
24025 they changed as a result of calling regenerate_decl_from_template. */
24026 input_location
= DECL_SOURCE_LOCATION (d
);
24031 bool const_init
= false;
24033 /* Clear out DECL_RTL; whatever was there before may not be right
24034 since we've reset the type of the declaration. */
24035 SET_DECL_RTL (d
, NULL
);
24036 DECL_IN_AGGR_P (d
) = 0;
24038 /* The initializer is placed in DECL_INITIAL by
24039 regenerate_decl_from_template so we don't need to
24040 push/pop_access_scope again here. Pull it out so that
24041 cp_finish_decl can process it. */
24042 init
= DECL_INITIAL (d
);
24043 DECL_INITIAL (d
) = NULL_TREE
;
24044 DECL_INITIALIZED_P (d
) = 0;
24046 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24047 initializer. That function will defer actual emission until
24048 we have a chance to determine linkage. */
24049 DECL_EXTERNAL (d
) = 0;
24051 /* Enter the scope of D so that access-checking works correctly. */
24052 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24054 push_nested_class (DECL_CONTEXT (d
));
24056 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24057 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
24060 pop_nested_class ();
24062 if (variable_template_p (gen_tmpl
))
24063 note_variable_template_instantiation (d
);
24065 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
24066 synthesize_method (d
);
24067 else if (TREE_CODE (d
) == FUNCTION_DECL
)
24069 /* Set up the list of local specializations. */
24070 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
24071 tree block
= NULL_TREE
;
24073 /* Set up context. */
24074 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24075 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24076 block
= push_stmt_list ();
24078 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
24080 /* Some typedefs referenced from within the template code need to be
24081 access checked at template instantiation time, i.e now. These
24082 types were added to the template at parsing time. Let's get those
24083 and perform the access checks then. */
24084 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
24087 /* Create substitution entries for the parameters. */
24088 register_parameter_specializations (code_pattern
, d
);
24090 /* Substitute into the body of the function. */
24091 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24092 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
24093 tf_warning_or_error
, tmpl
);
24096 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
24097 tf_warning_or_error
, tmpl
,
24098 /*integral_constant_expression_p=*/false);
24100 /* Set the current input_location to the end of the function
24101 so that finish_function knows where we are. */
24103 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
24105 /* Remember if we saw an infinite loop in the template. */
24106 current_function_infinite_loop
24107 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
24110 /* Finish the function. */
24111 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24112 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24113 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
24116 d
= finish_function (/*inline_p=*/false);
24117 expand_or_defer_fn (d
);
24120 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24121 cp_check_omp_declare_reduction (d
);
24124 /* We're not deferring instantiation any more. */
24125 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
24128 pop_from_top_level ();
24130 pop_function_context ();
24133 restore_omp_privatization_clauses (omp_privatization_save
);
24136 pop_deferring_access_checks ();
24137 timevar_pop (TV_TEMPLATE_INST
);
24138 pop_tinst_level ();
24139 input_location
= saved_loc
;
24140 cp_unevaluated_operand
= saved_unevaluated_operand
;
24141 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24146 /* Run through the list of templates that we wish we could
24147 instantiate, and instantiate any we can. RETRIES is the
24148 number of times we retry pending template instantiation. */
24151 instantiate_pending_templates (int retries
)
24154 location_t saved_loc
= input_location
;
24156 /* Instantiating templates may trigger vtable generation. This in turn
24157 may require further template instantiations. We place a limit here
24158 to avoid infinite loop. */
24159 if (pending_templates
&& retries
>= max_tinst_depth
)
24161 tree decl
= pending_templates
->tinst
->maybe_get_node ();
24163 fatal_error (input_location
,
24164 "template instantiation depth exceeds maximum of %d"
24165 " instantiating %q+D, possibly from virtual table generation"
24166 " (use -ftemplate-depth= to increase the maximum)",
24167 max_tinst_depth
, decl
);
24168 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24169 /* Pretend that we defined it. */
24170 DECL_INITIAL (decl
) = error_mark_node
;
24176 struct pending_template
**t
= &pending_templates
;
24177 struct pending_template
*last
= NULL
;
24181 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
24182 bool complete
= false;
24184 if (TYPE_P (instantiation
))
24186 if (!COMPLETE_TYPE_P (instantiation
))
24188 instantiate_class_template (instantiation
);
24189 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
24190 for (tree fld
= TYPE_FIELDS (instantiation
);
24191 fld
; fld
= TREE_CHAIN (fld
))
24193 || (TREE_CODE (fld
) == FUNCTION_DECL
24194 && !DECL_ARTIFICIAL (fld
)))
24195 && DECL_TEMPLATE_INSTANTIATION (fld
))
24196 instantiate_decl (fld
,
24197 /*defer_ok=*/false,
24198 /*expl_inst_class_mem_p=*/false);
24200 if (COMPLETE_TYPE_P (instantiation
))
24204 complete
= COMPLETE_TYPE_P (instantiation
);
24208 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24209 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
24212 = instantiate_decl (instantiation
,
24213 /*defer_ok=*/false,
24214 /*expl_inst_class_mem_p=*/false);
24215 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
24219 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24220 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
24225 /* If INSTANTIATION has been instantiated, then we don't
24226 need to consider it again in the future. */
24227 struct pending_template
*drop
= *t
;
24229 set_refcount_ptr (drop
->tinst
);
24230 pending_template_freelist ().free (drop
);
24238 set_refcount_ptr (current_tinst_level
);
24240 last_pending_template
= last
;
24242 while (reconsider
);
24244 input_location
= saved_loc
;
24247 /* Substitute ARGVEC into T, which is a list of initializers for
24248 either base class or a non-static data member. The TREE_PURPOSEs
24249 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24250 instantiate_decl. */
24253 tsubst_initializer_list (tree t
, tree argvec
)
24255 tree inits
= NULL_TREE
;
24256 tree target_ctor
= error_mark_node
;
24258 for (; t
; t
= TREE_CHAIN (t
))
24262 tree expanded_bases
= NULL_TREE
;
24263 tree expanded_arguments
= NULL_TREE
;
24266 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
24271 /* Expand the base class expansion type into separate base
24273 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
24274 tf_warning_or_error
,
24276 if (expanded_bases
== error_mark_node
)
24279 /* We'll be building separate TREE_LISTs of arguments for
24281 len
= TREE_VEC_LENGTH (expanded_bases
);
24282 expanded_arguments
= make_tree_vec (len
);
24283 for (i
= 0; i
< len
; i
++)
24284 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
24286 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24287 expand each argument in the TREE_VALUE of t. */
24288 expr
= make_node (EXPR_PACK_EXPANSION
);
24289 PACK_EXPANSION_LOCAL_P (expr
) = true;
24290 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
24291 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
24293 if (TREE_VALUE (t
) == void_type_node
)
24294 /* VOID_TYPE_NODE is used to indicate
24295 value-initialization. */
24297 for (i
= 0; i
< len
; i
++)
24298 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
24302 /* Substitute parameter packs into each argument in the
24304 in_base_initializer
= 1;
24305 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
24307 tree expanded_exprs
;
24309 /* Expand the argument. */
24310 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
24312 = tsubst_pack_expansion (expr
, argvec
,
24313 tf_warning_or_error
,
24315 if (expanded_exprs
== error_mark_node
)
24318 /* Prepend each of the expanded expressions to the
24319 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24320 for (i
= 0; i
< len
; i
++)
24322 TREE_VEC_ELT (expanded_arguments
, i
) =
24323 tree_cons (NULL_TREE
,
24324 TREE_VEC_ELT (expanded_exprs
, i
),
24325 TREE_VEC_ELT (expanded_arguments
, i
));
24328 in_base_initializer
= 0;
24330 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24331 since we built them backwards. */
24332 for (i
= 0; i
< len
; i
++)
24334 TREE_VEC_ELT (expanded_arguments
, i
) =
24335 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
24340 for (i
= 0; i
< len
; ++i
)
24342 if (expanded_bases
)
24344 decl
= TREE_VEC_ELT (expanded_bases
, i
);
24345 decl
= expand_member_init (decl
);
24346 init
= TREE_VEC_ELT (expanded_arguments
, i
);
24351 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
24352 tf_warning_or_error
, NULL_TREE
);
24354 decl
= expand_member_init (decl
);
24355 if (decl
&& !DECL_P (decl
))
24356 in_base_initializer
= 1;
24358 init
= TREE_VALUE (t
);
24360 if (init
!= void_type_node
)
24361 init
= tsubst_expr (init
, argvec
,
24362 tf_warning_or_error
, NULL_TREE
,
24363 /*integral_constant_expression_p=*/false);
24364 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
24365 /* If we had an initializer but it instantiated to nothing,
24366 value-initialize the object. This will only occur when
24367 the initializer was a pack expansion where the parameter
24368 packs used in that expansion were of length zero. */
24369 init
= void_type_node
;
24370 in_base_initializer
= 0;
24373 if (target_ctor
!= error_mark_node
24374 && init
!= error_mark_node
)
24376 error ("mem-initializer for %qD follows constructor delegation",
24380 /* Look for a target constructor. */
24381 if (init
!= error_mark_node
24382 && decl
&& CLASS_TYPE_P (decl
)
24383 && same_type_p (decl
, current_class_type
))
24385 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
24388 error ("constructor delegation follows mem-initializer for %qD",
24389 TREE_PURPOSE (inits
));
24392 target_ctor
= init
;
24397 init
= build_tree_list (decl
, init
);
24398 TREE_CHAIN (init
) = inits
;
24406 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24409 set_current_access_from_decl (tree decl
)
24411 if (TREE_PRIVATE (decl
))
24412 current_access_specifier
= access_private_node
;
24413 else if (TREE_PROTECTED (decl
))
24414 current_access_specifier
= access_protected_node
;
24416 current_access_specifier
= access_public_node
;
24419 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24420 is the instantiation (which should have been created with
24421 start_enum) and ARGS are the template arguments to use. */
24424 tsubst_enum (tree tag
, tree newtag
, tree args
)
24428 if (SCOPED_ENUM_P (newtag
))
24429 begin_scope (sk_scoped_enum
, newtag
);
24431 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
24436 decl
= TREE_VALUE (e
);
24437 /* Note that in a template enum, the TREE_VALUE is the
24438 CONST_DECL, not the corresponding INTEGER_CST. */
24439 value
= tsubst_expr (DECL_INITIAL (decl
),
24440 args
, tf_warning_or_error
, NULL_TREE
,
24441 /*integral_constant_expression_p=*/true);
24443 /* Give this enumeration constant the correct access. */
24444 set_current_access_from_decl (decl
);
24446 /* Actually build the enumerator itself. Here we're assuming that
24447 enumerators can't have dependent attributes. */
24448 build_enumerator (DECL_NAME (decl
), value
, newtag
,
24449 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
24452 if (SCOPED_ENUM_P (newtag
))
24455 finish_enum_value_list (newtag
);
24456 finish_enum (newtag
);
24458 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
24459 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
24462 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24463 its type -- but without substituting the innermost set of template
24464 arguments. So, innermost set of template parameters will appear in
24468 get_mostly_instantiated_function_type (tree decl
)
24470 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24471 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
24474 /* Return truthvalue if we're processing a template different from
24475 the last one involved in diagnostics. */
24477 problematic_instantiation_changed (void)
24479 return current_tinst_level
!= last_error_tinst_level
;
24482 /* Remember current template involved in diagnostics. */
24484 record_last_problematic_instantiation (void)
24486 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
24489 struct tinst_level
*
24490 current_instantiation (void)
24492 return current_tinst_level
;
24495 /* Return TRUE if current_function_decl is being instantiated, false
24499 instantiating_current_function_p (void)
24501 return (current_instantiation ()
24502 && (current_instantiation ()->maybe_get_node ()
24503 == current_function_decl
));
24506 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24507 type. Return false for ok, true for disallowed. Issue error and
24508 inform messages under control of COMPLAIN. */
24511 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
24513 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
24515 else if (TYPE_PTR_P (type
))
24517 else if (TYPE_REF_P (type
)
24518 && !TYPE_REF_IS_RVALUE (type
))
24520 else if (TYPE_PTRMEM_P (type
))
24522 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
24524 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
24526 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
24528 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
24530 /* A bound template template parm could later be instantiated to have a valid
24531 nontype parm type via an alias template. */
24532 else if (cxx_dialect
>= cxx11
24533 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24536 if (complain
& tf_error
)
24538 if (type
== error_mark_node
)
24539 inform (input_location
, "invalid template non-type parameter");
24541 error ("%q#T is not a valid type for a template non-type parameter",
24547 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24548 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24551 dependent_type_p_r (tree type
)
24557 A type is dependent if it is:
24559 -- a template parameter. Template template parameters are types
24560 for us (since TYPE_P holds true for them) so we handle
24562 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
24563 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
24565 /* -- a qualified-id with a nested-name-specifier which contains a
24566 class-name that names a dependent type or whose unqualified-id
24567 names a dependent type. */
24568 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24571 /* An alias template specialization can be dependent even if the
24572 resulting type is not. */
24573 if (dependent_alias_template_spec_p (type
))
24576 /* -- a cv-qualified type where the cv-unqualified type is
24578 No code is necessary for this bullet; the code below handles
24579 cv-qualified types, and we don't want to strip aliases with
24580 TYPE_MAIN_VARIANT because of DR 1558. */
24581 /* -- a compound type constructed from any dependent type. */
24582 if (TYPE_PTRMEM_P (type
))
24583 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
24584 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24586 else if (INDIRECT_TYPE_P (type
))
24587 return dependent_type_p (TREE_TYPE (type
));
24588 else if (TREE_CODE (type
) == FUNCTION_TYPE
24589 || TREE_CODE (type
) == METHOD_TYPE
)
24593 if (dependent_type_p (TREE_TYPE (type
)))
24595 for (arg_type
= TYPE_ARG_TYPES (type
);
24597 arg_type
= TREE_CHAIN (arg_type
))
24598 if (dependent_type_p (TREE_VALUE (arg_type
)))
24600 if (cxx_dialect
>= cxx17
)
24601 /* A value-dependent noexcept-specifier makes the type dependent. */
24602 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
24603 if (tree noex
= TREE_PURPOSE (spec
))
24604 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24605 affect overload resolution and treating it as dependent breaks
24607 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
24608 && value_dependent_expression_p (noex
))
24612 /* -- an array type constructed from any dependent type or whose
24613 size is specified by a constant expression that is
24616 We checked for type- and value-dependence of the bounds in
24617 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24618 if (TREE_CODE (type
) == ARRAY_TYPE
)
24620 if (TYPE_DOMAIN (type
)
24621 && dependent_type_p (TYPE_DOMAIN (type
)))
24623 return dependent_type_p (TREE_TYPE (type
));
24626 /* -- a template-id in which either the template name is a template
24628 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24630 /* ... or any of the template arguments is a dependent type or
24631 an expression that is type-dependent or value-dependent. */
24632 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
24633 && (any_dependent_template_arguments_p
24634 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
24637 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24638 dependent; if the argument of the `typeof' expression is not
24639 type-dependent, then it should already been have resolved. */
24640 if (TREE_CODE (type
) == TYPEOF_TYPE
24641 || TREE_CODE (type
) == DECLTYPE_TYPE
24642 || TREE_CODE (type
) == UNDERLYING_TYPE
)
24645 /* A template argument pack is dependent if any of its packed
24647 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
24649 tree args
= ARGUMENT_PACK_ARGS (type
);
24650 int i
, len
= TREE_VEC_LENGTH (args
);
24651 for (i
= 0; i
< len
; ++i
)
24652 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24656 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24657 be template parameters. */
24658 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
24661 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
24664 /* The standard does not specifically mention types that are local
24665 to template functions or local classes, but they should be
24666 considered dependent too. For example:
24668 template <int I> void f() {
24673 The size of `E' cannot be known until the value of `I' has been
24674 determined. Therefore, `E' must be considered dependent. */
24675 scope
= TYPE_CONTEXT (type
);
24676 if (scope
&& TYPE_P (scope
))
24677 return dependent_type_p (scope
);
24678 /* Don't use type_dependent_expression_p here, as it can lead
24679 to infinite recursion trying to determine whether a lambda
24680 nested in a lambda is dependent (c++/47687). */
24681 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
24682 && DECL_LANG_SPECIFIC (scope
)
24683 && DECL_TEMPLATE_INFO (scope
)
24684 && (any_dependent_template_arguments_p
24685 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
24688 /* Other types are non-dependent. */
24692 /* Returns TRUE if TYPE is dependent, in the sense of
24693 [temp.dep.type]. Note that a NULL type is considered dependent. */
24696 dependent_type_p (tree type
)
24698 /* If there are no template parameters in scope, then there can't be
24699 any dependent types. */
24700 if (!processing_template_decl
)
24702 /* If we are not processing a template, then nobody should be
24703 providing us with a dependent type. */
24705 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
24709 /* If the type is NULL, we have not computed a type for the entity
24710 in question; in that case, the type is dependent. */
24714 /* Erroneous types can be considered non-dependent. */
24715 if (type
== error_mark_node
)
24718 /* Getting here with global_type_node means we improperly called this
24719 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24720 gcc_checking_assert (type
!= global_type_node
);
24722 /* If we have not already computed the appropriate value for TYPE,
24724 if (!TYPE_DEPENDENT_P_VALID (type
))
24726 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
24727 TYPE_DEPENDENT_P_VALID (type
) = 1;
24730 return TYPE_DEPENDENT_P (type
);
24733 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24734 lookup. In other words, a dependent type that is not the current
24738 dependent_scope_p (tree scope
)
24740 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24741 && !currently_open_class (scope
));
24744 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24745 an unknown base of 'this' (and is therefore instantiation-dependent). */
24748 unknown_base_ref_p (tree t
)
24750 if (!current_class_ptr
)
24753 tree mem
= TREE_OPERAND (t
, 1);
24754 if (shared_member_p (mem
))
24757 tree cur
= current_nonlambda_class_type ();
24758 if (!any_dependent_bases_p (cur
))
24761 tree ctx
= TREE_OPERAND (t
, 0);
24762 if (DERIVED_FROM_P (ctx
, cur
))
24768 /* T is a SCOPE_REF; return whether we need to consider it
24769 instantiation-dependent so that we can check access at instantiation
24770 time even though we know which member it resolves to. */
24773 instantiation_dependent_scope_ref_p (tree t
)
24775 if (DECL_P (TREE_OPERAND (t
, 1))
24776 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24777 && !unknown_base_ref_p (t
)
24778 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24779 TREE_OPERAND (t
, 1)))
24785 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24786 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24789 /* Note that this predicate is not appropriate for general expressions;
24790 only constant expressions (that satisfy potential_constant_expression)
24791 can be tested for value dependence. */
24794 value_dependent_expression_p (tree expression
)
24796 if (!processing_template_decl
|| expression
== NULL_TREE
)
24799 /* A type-dependent expression is also value-dependent. */
24800 if (type_dependent_expression_p (expression
))
24803 switch (TREE_CODE (expression
))
24806 /* A dependent member function of the current instantiation. */
24807 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24809 case FUNCTION_DECL
:
24810 /* A dependent member function of the current instantiation. */
24811 if (DECL_CLASS_SCOPE_P (expression
)
24812 && dependent_type_p (DECL_CONTEXT (expression
)))
24816 case IDENTIFIER_NODE
:
24817 /* A name that has not been looked up -- must be dependent. */
24820 case TEMPLATE_PARM_INDEX
:
24821 /* A non-type template parm. */
24825 /* A non-type template parm. */
24826 if (DECL_TEMPLATE_PARM_P (expression
))
24828 return value_dependent_expression_p (DECL_INITIAL (expression
));
24831 /* A constant with literal type and is initialized
24832 with an expression that is value-dependent. */
24833 if (DECL_DEPENDENT_INIT_P (expression
)
24834 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24835 || TYPE_REF_P (TREE_TYPE (expression
)))
24837 if (DECL_HAS_VALUE_EXPR_P (expression
))
24839 tree value_expr
= DECL_VALUE_EXPR (expression
);
24840 if (value_dependent_expression_p (value_expr
))
24845 case DYNAMIC_CAST_EXPR
:
24846 case STATIC_CAST_EXPR
:
24847 case CONST_CAST_EXPR
:
24848 case REINTERPRET_CAST_EXPR
:
24850 case IMPLICIT_CONV_EXPR
:
24851 /* These expressions are value-dependent if the type to which
24852 the cast occurs is dependent or the expression being casted
24853 is value-dependent. */
24855 tree type
= TREE_TYPE (expression
);
24857 if (dependent_type_p (type
))
24860 /* A functional cast has a list of operands. */
24861 expression
= TREE_OPERAND (expression
, 0);
24864 /* If there are no operands, it must be an expression such
24865 as "int()". This should not happen for aggregate types
24866 because it would form non-constant expressions. */
24867 gcc_assert (cxx_dialect
>= cxx11
24868 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24873 if (TREE_CODE (expression
) == TREE_LIST
)
24874 return any_value_dependent_elements_p (expression
);
24876 return value_dependent_expression_p (expression
);
24880 if (SIZEOF_EXPR_TYPE_P (expression
))
24881 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24885 /* A `sizeof' expression is value-dependent if the operand is
24886 type-dependent or is a pack expansion. */
24887 expression
= TREE_OPERAND (expression
, 0);
24888 if (PACK_EXPANSION_P (expression
))
24890 else if (TYPE_P (expression
))
24891 return dependent_type_p (expression
);
24892 return instantiation_dependent_uneval_expression_p (expression
);
24894 case AT_ENCODE_EXPR
:
24895 /* An 'encode' expression is value-dependent if the operand is
24897 expression
= TREE_OPERAND (expression
, 0);
24898 return dependent_type_p (expression
);
24900 case NOEXCEPT_EXPR
:
24901 expression
= TREE_OPERAND (expression
, 0);
24902 return instantiation_dependent_uneval_expression_p (expression
);
24905 /* All instantiation-dependent expressions should also be considered
24906 value-dependent. */
24907 return instantiation_dependent_scope_ref_p (expression
);
24909 case COMPONENT_REF
:
24910 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24911 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24913 case NONTYPE_ARGUMENT_PACK
:
24914 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24915 is value-dependent. */
24917 tree values
= ARGUMENT_PACK_ARGS (expression
);
24918 int i
, len
= TREE_VEC_LENGTH (values
);
24920 for (i
= 0; i
< len
; ++i
)
24921 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24929 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24931 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24937 if (TREE_CODE (type2
) != TREE_LIST
)
24938 return dependent_type_p (type2
);
24940 for (; type2
; type2
= TREE_CHAIN (type2
))
24941 if (dependent_type_p (TREE_VALUE (type2
)))
24948 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24949 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24952 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24953 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24957 tree op
= TREE_OPERAND (expression
, 0);
24958 return (value_dependent_expression_p (op
)
24959 || has_value_dependent_address (op
));
24962 case REQUIRES_EXPR
:
24963 /* Treat all requires-expressions as value-dependent so
24964 we don't try to fold them. */
24968 return dependent_type_p (TREE_OPERAND (expression
, 0));
24972 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24974 tree fn
= get_callee_fndecl (expression
);
24976 nargs
= call_expr_nargs (expression
);
24977 for (i
= 0; i
< nargs
; ++i
)
24979 tree op
= CALL_EXPR_ARG (expression
, i
);
24980 /* In a call to a constexpr member function, look through the
24981 implicit ADDR_EXPR on the object argument so that it doesn't
24982 cause the call to be considered value-dependent. We also
24983 look through it in potential_constant_expression. */
24984 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24985 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24986 && TREE_CODE (op
) == ADDR_EXPR
)
24987 op
= TREE_OPERAND (op
, 0);
24988 if (value_dependent_expression_p (op
))
24994 case TEMPLATE_ID_EXPR
:
24995 return variable_concept_p (TREE_OPERAND (expression
, 0));
25001 if (dependent_type_p (TREE_TYPE (expression
)))
25003 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
25004 if (value_dependent_expression_p (val
))
25010 /* Treat a GNU statement expression as dependent to avoid crashing
25011 under instantiate_non_dependent_expr; it can't be constant. */
25015 /* A constant expression is value-dependent if any subexpression is
25016 value-dependent. */
25017 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
25019 case tcc_reference
:
25021 case tcc_comparison
:
25023 case tcc_expression
:
25026 int i
, len
= cp_tree_operand_length (expression
);
25028 for (i
= 0; i
< len
; i
++)
25030 tree t
= TREE_OPERAND (expression
, i
);
25032 /* In some cases, some of the operands may be missing.
25033 (For example, in the case of PREDECREMENT_EXPR, the
25034 amount to increment by may be missing.) That doesn't
25035 make the expression dependent. */
25036 if (t
&& value_dependent_expression_p (t
))
25047 /* The expression is not value-dependent. */
25051 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25052 [temp.dep.expr]. Note that an expression with no type is
25053 considered dependent. Other parts of the compiler arrange for an
25054 expression with type-dependent subexpressions to have no type, so
25055 this function doesn't have to be fully recursive. */
25058 type_dependent_expression_p (tree expression
)
25060 if (!processing_template_decl
)
25063 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
25066 STRIP_ANY_LOCATION_WRAPPER (expression
);
25068 /* An unresolved name is always dependent. */
25069 if (identifier_p (expression
)
25070 || TREE_CODE (expression
) == USING_DECL
25071 || TREE_CODE (expression
) == WILDCARD_DECL
)
25074 /* A fold expression is type-dependent. */
25075 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
25076 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
25077 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
25078 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
25081 /* Some expression forms are never type-dependent. */
25082 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
25083 || TREE_CODE (expression
) == SIZEOF_EXPR
25084 || TREE_CODE (expression
) == ALIGNOF_EXPR
25085 || TREE_CODE (expression
) == AT_ENCODE_EXPR
25086 || TREE_CODE (expression
) == NOEXCEPT_EXPR
25087 || TREE_CODE (expression
) == TRAIT_EXPR
25088 || TREE_CODE (expression
) == TYPEID_EXPR
25089 || TREE_CODE (expression
) == DELETE_EXPR
25090 || TREE_CODE (expression
) == VEC_DELETE_EXPR
25091 || TREE_CODE (expression
) == THROW_EXPR
25092 || TREE_CODE (expression
) == REQUIRES_EXPR
)
25095 /* The types of these expressions depends only on the type to which
25096 the cast occurs. */
25097 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
25098 || TREE_CODE (expression
) == STATIC_CAST_EXPR
25099 || TREE_CODE (expression
) == CONST_CAST_EXPR
25100 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
25101 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
25102 || TREE_CODE (expression
) == CAST_EXPR
)
25103 return dependent_type_p (TREE_TYPE (expression
));
25105 /* The types of these expressions depends only on the type created
25106 by the expression. */
25107 if (TREE_CODE (expression
) == NEW_EXPR
25108 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
25110 /* For NEW_EXPR tree nodes created inside a template, either
25111 the object type itself or a TREE_LIST may appear as the
25113 tree type
= TREE_OPERAND (expression
, 1);
25114 if (TREE_CODE (type
) == TREE_LIST
)
25115 /* This is an array type. We need to check array dimensions
25117 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
25118 || value_dependent_expression_p
25119 (TREE_OPERAND (TREE_VALUE (type
), 1));
25121 return dependent_type_p (type
);
25124 if (TREE_CODE (expression
) == SCOPE_REF
)
25126 tree scope
= TREE_OPERAND (expression
, 0);
25127 tree name
= TREE_OPERAND (expression
, 1);
25129 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25130 contains an identifier associated by name lookup with one or more
25131 declarations declared with a dependent type, or...a
25132 nested-name-specifier or qualified-id that names a member of an
25133 unknown specialization. */
25134 return (type_dependent_expression_p (name
)
25135 || dependent_scope_p (scope
));
25138 if (TREE_CODE (expression
) == TEMPLATE_DECL
25139 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
25140 return uses_outer_template_parms (expression
);
25142 if (TREE_CODE (expression
) == STMT_EXPR
)
25143 expression
= stmt_expr_value_expr (expression
);
25145 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
25150 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
25152 if (type_dependent_expression_p (elt
))
25158 /* A static data member of the current instantiation with incomplete
25159 array type is type-dependent, as the definition and specializations
25160 can have different bounds. */
25161 if (VAR_P (expression
)
25162 && DECL_CLASS_SCOPE_P (expression
)
25163 && dependent_type_p (DECL_CONTEXT (expression
))
25164 && VAR_HAD_UNKNOWN_BOUND (expression
))
25167 /* An array of unknown bound depending on a variadic parameter, eg:
25169 template<typename... Args>
25170 void foo (Args... args)
25172 int arr[] = { args... };
25175 template<int... vals>
25178 int arr[] = { vals... };
25181 If the array has no length and has an initializer, it must be that
25182 we couldn't determine its length in cp_complete_array_type because
25183 it is dependent. */
25184 if (VAR_P (expression
)
25185 && TREE_TYPE (expression
) != NULL_TREE
25186 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
25187 && !TYPE_DOMAIN (TREE_TYPE (expression
))
25188 && DECL_INITIAL (expression
))
25191 /* A function or variable template-id is type-dependent if it has any
25192 dependent template arguments. */
25193 if (VAR_OR_FUNCTION_DECL_P (expression
)
25194 && DECL_LANG_SPECIFIC (expression
)
25195 && DECL_TEMPLATE_INFO (expression
))
25197 /* Consider the innermost template arguments, since those are the ones
25198 that come from the template-id; the template arguments for the
25199 enclosing class do not make it type-dependent unless they are used in
25200 the type of the decl. */
25201 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
25202 && (any_dependent_template_arguments_p
25203 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
25207 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25208 type-dependent. Checking this is important for functions with auto return
25209 type, which looks like a dependent type. */
25210 if (TREE_CODE (expression
) == FUNCTION_DECL
25211 && !(DECL_CLASS_SCOPE_P (expression
)
25212 && dependent_type_p (DECL_CONTEXT (expression
)))
25213 && !(DECL_LANG_SPECIFIC (expression
)
25214 && DECL_FRIEND_P (expression
)
25215 && (!DECL_FRIEND_CONTEXT (expression
)
25216 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
25217 && !DECL_LOCAL_FUNCTION_P (expression
))
25219 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
25220 || undeduced_auto_decl (expression
));
25224 /* Always dependent, on the number of arguments if nothing else. */
25225 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
25228 if (TREE_TYPE (expression
) == unknown_type_node
)
25230 if (TREE_CODE (expression
) == ADDR_EXPR
)
25231 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
25232 if (TREE_CODE (expression
) == COMPONENT_REF
25233 || TREE_CODE (expression
) == OFFSET_REF
)
25235 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25237 expression
= TREE_OPERAND (expression
, 1);
25238 if (identifier_p (expression
))
25241 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25242 if (TREE_CODE (expression
) == SCOPE_REF
)
25245 if (BASELINK_P (expression
))
25247 if (BASELINK_OPTYPE (expression
)
25248 && dependent_type_p (BASELINK_OPTYPE (expression
)))
25250 expression
= BASELINK_FUNCTIONS (expression
);
25253 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
25255 if (any_dependent_template_arguments_p
25256 (TREE_OPERAND (expression
, 1)))
25258 expression
= TREE_OPERAND (expression
, 0);
25259 if (identifier_p (expression
))
25263 gcc_assert (TREE_CODE (expression
) == OVERLOAD
25264 || TREE_CODE (expression
) == FUNCTION_DECL
);
25266 for (lkp_iterator
iter (expression
); iter
; ++iter
)
25267 if (type_dependent_expression_p (*iter
))
25273 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
25275 /* Dependent type attributes might not have made it from the decl to
25277 if (DECL_P (expression
)
25278 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
25281 return (dependent_type_p (TREE_TYPE (expression
)));
25284 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25285 type-dependent if the expression refers to a member of the current
25286 instantiation and the type of the referenced member is dependent, or the
25287 class member access expression refers to a member of an unknown
25290 This function returns true if the OBJECT in such a class member access
25291 expression is of an unknown specialization. */
25294 type_dependent_object_expression_p (tree object
)
25296 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25298 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
25300 tree scope
= TREE_TYPE (object
);
25301 return (!scope
|| dependent_scope_p (scope
));
25304 /* walk_tree callback function for instantiation_dependent_expression_p,
25305 below. Returns non-zero if a dependent subexpression is found. */
25308 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
25313 /* We don't have to worry about decltype currently because decltype
25314 of an instantiation-dependent expr is a dependent type. This
25315 might change depending on the resolution of DR 1172. */
25316 *walk_subtrees
= false;
25319 enum tree_code code
= TREE_CODE (*tp
);
25322 /* Don't treat an argument list as dependent just because it has no
25326 case NONTYPE_ARGUMENT_PACK
:
25329 case TEMPLATE_PARM_INDEX
:
25332 /* Handle expressions with type operands. */
25336 case AT_ENCODE_EXPR
:
25338 tree op
= TREE_OPERAND (*tp
, 0);
25339 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
25340 op
= TREE_TYPE (op
);
25343 if (dependent_type_p (op
))
25347 *walk_subtrees
= false;
25354 case COMPONENT_REF
:
25355 if (identifier_p (TREE_OPERAND (*tp
, 1)))
25356 /* In a template, finish_class_member_access_expr creates a
25357 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25358 type-dependent, so that we can check access control at
25359 instantiation time (PR 42277). See also Core issue 1273. */
25364 if (instantiation_dependent_scope_ref_p (*tp
))
25369 /* Treat statement-expressions as dependent. */
25373 /* Treat requires-expressions as dependent. */
25374 case REQUIRES_EXPR
:
25378 /* Treat calls to function concepts as dependent. */
25379 if (function_concept_check_p (*tp
))
25383 case TEMPLATE_ID_EXPR
:
25384 /* And variable concepts. */
25385 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
25393 if (type_dependent_expression_p (*tp
))
25399 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25400 sense defined by the ABI:
25402 "An expression is instantiation-dependent if it is type-dependent
25403 or value-dependent, or it has a subexpression that is type-dependent
25404 or value-dependent."
25406 Except don't actually check value-dependence for unevaluated expressions,
25407 because in sizeof(i) we don't care about the value of i. Checking
25408 type-dependence will in turn check value-dependence of array bounds/template
25409 arguments as needed. */
25412 instantiation_dependent_uneval_expression_p (tree expression
)
25416 if (!processing_template_decl
)
25419 if (expression
== error_mark_node
)
25422 result
= cp_walk_tree_without_duplicates (&expression
,
25423 instantiation_dependent_r
, NULL
);
25424 return result
!= NULL_TREE
;
25427 /* As above, but also check value-dependence of the expression as a whole. */
25430 instantiation_dependent_expression_p (tree expression
)
25432 return (instantiation_dependent_uneval_expression_p (expression
)
25433 || value_dependent_expression_p (expression
));
25436 /* Like type_dependent_expression_p, but it also works while not processing
25437 a template definition, i.e. during substitution or mangling. */
25440 type_dependent_expression_p_push (tree expr
)
25443 ++processing_template_decl
;
25444 b
= type_dependent_expression_p (expr
);
25445 --processing_template_decl
;
25449 /* Returns TRUE if ARGS contains a type-dependent expression. */
25452 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
25457 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
25459 if (type_dependent_expression_p (arg
))
25465 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25466 expressions) contains any type-dependent expressions. */
25469 any_type_dependent_elements_p (const_tree list
)
25471 for (; list
; list
= TREE_CHAIN (list
))
25472 if (type_dependent_expression_p (TREE_VALUE (list
)))
25478 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25479 expressions) contains any value-dependent expressions. */
25482 any_value_dependent_elements_p (const_tree list
)
25484 for (; list
; list
= TREE_CHAIN (list
))
25485 if (value_dependent_expression_p (TREE_VALUE (list
)))
25491 /* Returns TRUE if the ARG (a template argument) is dependent. */
25494 dependent_template_arg_p (tree arg
)
25496 if (!processing_template_decl
)
25499 /* Assume a template argument that was wrongly written by the user
25500 is dependent. This is consistent with what
25501 any_dependent_template_arguments_p [that calls this function]
25503 if (!arg
|| arg
== error_mark_node
)
25506 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
25507 arg
= argument_pack_select_arg (arg
);
25509 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
25511 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25513 if (DECL_TEMPLATE_PARM_P (arg
))
25515 /* A member template of a dependent class is not necessarily
25516 type-dependent, but it is a dependent template argument because it
25517 will be a member of an unknown specialization to that template. */
25518 tree scope
= CP_DECL_CONTEXT (arg
);
25519 return TYPE_P (scope
) && dependent_type_p (scope
);
25521 else if (ARGUMENT_PACK_P (arg
))
25523 tree args
= ARGUMENT_PACK_ARGS (arg
);
25524 int i
, len
= TREE_VEC_LENGTH (args
);
25525 for (i
= 0; i
< len
; ++i
)
25527 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25533 else if (TYPE_P (arg
))
25534 return dependent_type_p (arg
);
25536 return (type_dependent_expression_p (arg
)
25537 || value_dependent_expression_p (arg
));
25540 /* Returns true if ARGS (a collection of template arguments) contains
25541 any types that require structural equality testing. */
25544 any_template_arguments_need_structural_equality_p (tree args
)
25551 if (args
== error_mark_node
)
25554 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25556 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25557 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25559 tree arg
= TREE_VEC_ELT (level
, j
);
25560 tree packed_args
= NULL_TREE
;
25563 if (ARGUMENT_PACK_P (arg
))
25565 /* Look inside the argument pack. */
25566 packed_args
= ARGUMENT_PACK_ARGS (arg
);
25567 len
= TREE_VEC_LENGTH (packed_args
);
25570 for (k
= 0; k
< len
; ++k
)
25573 arg
= TREE_VEC_ELT (packed_args
, k
);
25575 if (error_operand_p (arg
))
25577 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25579 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
25581 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
25582 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
25591 /* Returns true if ARGS (a collection of template arguments) contains
25592 any dependent arguments. */
25595 any_dependent_template_arguments_p (const_tree args
)
25602 if (args
== error_mark_node
)
25605 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25607 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25608 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25609 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
25616 /* Returns true if ARGS contains any errors. */
25619 any_erroneous_template_args_p (const_tree args
)
25624 if (args
== error_mark_node
)
25627 if (args
&& TREE_CODE (args
) != TREE_VEC
)
25629 if (tree ti
= get_template_info (args
))
25630 args
= TI_ARGS (ti
);
25638 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25640 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25641 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25642 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
25649 /* Returns TRUE if the template TMPL is type-dependent. */
25652 dependent_template_p (tree tmpl
)
25654 if (TREE_CODE (tmpl
) == OVERLOAD
)
25656 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
25657 if (dependent_template_p (*iter
))
25662 /* Template template parameters are dependent. */
25663 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
25664 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
25666 /* So are names that have not been looked up. */
25667 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
25672 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25675 dependent_template_id_p (tree tmpl
, tree args
)
25677 return (dependent_template_p (tmpl
)
25678 || any_dependent_template_arguments_p (args
));
25681 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25685 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
25689 if (!processing_template_decl
)
25692 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
25694 tree decl
= TREE_VEC_ELT (declv
, i
);
25695 tree init
= TREE_VEC_ELT (initv
, i
);
25696 tree cond
= TREE_VEC_ELT (condv
, i
);
25697 tree incr
= TREE_VEC_ELT (incrv
, i
);
25699 if (type_dependent_expression_p (decl
)
25700 || TREE_CODE (decl
) == SCOPE_REF
)
25703 if (init
&& type_dependent_expression_p (init
))
25706 if (type_dependent_expression_p (cond
))
25709 if (COMPARISON_CLASS_P (cond
)
25710 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
25711 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
25714 if (TREE_CODE (incr
) == MODOP_EXPR
)
25716 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
25717 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
25720 else if (type_dependent_expression_p (incr
))
25722 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
25724 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
25726 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
25728 tree t
= TREE_OPERAND (incr
, 1);
25729 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
25730 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
25739 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25740 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25741 no such TYPE can be found. Note that this function peers inside
25742 uninstantiated templates and therefore should be used only in
25743 extremely limited situations. ONLY_CURRENT_P restricts this
25744 peering to the currently open classes hierarchy (which is required
25745 when comparing types). */
25748 resolve_typename_type (tree type
, bool only_current_p
)
25757 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
25759 scope
= TYPE_CONTEXT (type
);
25760 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25761 gcc_checking_assert (uses_template_parms (scope
));
25763 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25764 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25765 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25766 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25767 identifier of the TYPENAME_TYPE anymore.
25768 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25769 TYPENAME_TYPE instead, we avoid messing up with a possible
25770 typedef variant case. */
25771 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25773 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25774 it first before we can figure out what NAME refers to. */
25775 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25777 if (TYPENAME_IS_RESOLVING_P (scope
))
25778 /* Given a class template A with a dependent base with nested type C,
25779 typedef typename A::C::C C will land us here, as trying to resolve
25780 the initial A::C leads to the local C typedef, which leads back to
25781 A::C::C. So we break the recursion now. */
25784 scope
= resolve_typename_type (scope
, only_current_p
);
25786 /* If we don't know what SCOPE refers to, then we cannot resolve the
25788 if (!CLASS_TYPE_P (scope
))
25790 /* If this is a typedef, we don't want to look inside (c++/11987). */
25791 if (typedef_variant_p (type
))
25793 /* If SCOPE isn't the template itself, it will not have a valid
25794 TYPE_FIELDS list. */
25795 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25796 /* scope is either the template itself or a compatible instantiation
25797 like X<T>, so look up the name in the original template. */
25798 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25799 /* If scope has no fields, it can't be a current instantiation. Check this
25800 before currently_open_class to avoid infinite recursion (71515). */
25801 if (!TYPE_FIELDS (scope
))
25803 /* If the SCOPE is not the current instantiation, there's no reason
25804 to look inside it. */
25805 if (only_current_p
&& !currently_open_class (scope
))
25807 /* Enter the SCOPE so that name lookup will be resolved as if we
25808 were in the class definition. In particular, SCOPE will no
25809 longer be considered a dependent type. */
25810 pushed_scope
= push_scope (scope
);
25811 /* Look up the declaration. */
25812 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25813 tf_warning_or_error
);
25815 result
= NULL_TREE
;
25817 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25818 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25819 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25822 else if (identifier_p (fullname
)
25823 && TREE_CODE (decl
) == TYPE_DECL
)
25825 result
= TREE_TYPE (decl
);
25826 if (result
== error_mark_node
)
25827 result
= NULL_TREE
;
25829 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25830 && DECL_CLASS_TEMPLATE_P (decl
))
25832 /* Obtain the template and the arguments. */
25833 tree tmpl
= TREE_OPERAND (fullname
, 0);
25834 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25836 /* We get here with a plain identifier because a previous tentative
25837 parse of the nested-name-specifier as part of a ptr-operator saw
25838 ::template X<A>. The use of ::template is necessary in a
25839 ptr-operator, but wrong in a declarator-id.
25841 [temp.names]: In a qualified-id of a declarator-id, the keyword
25842 template shall not appear at the top level. */
25843 pedwarn (cp_expr_loc_or_loc (fullname
, input_location
), OPT_Wpedantic
,
25844 "keyword %<template%> not allowed in declarator-id");
25847 tree args
= TREE_OPERAND (fullname
, 1);
25848 /* Instantiate the template. */
25849 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25850 /*entering_scope=*/true,
25851 tf_error
| tf_user
);
25852 if (result
== error_mark_node
)
25853 result
= NULL_TREE
;
25856 /* Leave the SCOPE. */
25858 pop_scope (pushed_scope
);
25860 /* If we failed to resolve it, return the original typename. */
25864 /* If lookup found a typename type, resolve that too. */
25865 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25867 /* Ill-formed programs can cause infinite recursion here, so we
25868 must catch that. */
25869 TYPENAME_IS_RESOLVING_P (result
) = 1;
25870 result
= resolve_typename_type (result
, only_current_p
);
25871 TYPENAME_IS_RESOLVING_P (result
) = 0;
25874 /* Qualify the resulting type. */
25875 quals
= cp_type_quals (type
);
25877 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25882 /* EXPR is an expression which is not type-dependent. Return a proxy
25883 for EXPR that can be used to compute the types of larger
25884 expressions containing EXPR. */
25887 build_non_dependent_expr (tree expr
)
25889 tree orig_expr
= expr
;
25892 /* When checking, try to get a constant value for all non-dependent
25893 expressions in order to expose bugs in *_dependent_expression_p
25894 and constexpr. This can affect code generation, see PR70704, so
25895 only do this for -fchecking=2. */
25896 if (flag_checking
> 1
25897 && cxx_dialect
>= cxx11
25898 /* Don't do this during nsdmi parsing as it can lead to
25899 unexpected recursive instantiations. */
25900 && !parsing_nsdmi ()
25901 /* Don't do this during concept expansion either and for
25902 the same reason. */
25903 && !expanding_concept ())
25904 fold_non_dependent_expr (expr
, tf_none
);
25906 STRIP_ANY_LOCATION_WRAPPER (expr
);
25908 /* Preserve OVERLOADs; the functions must be available to resolve
25911 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25912 inner_expr
= stmt_expr_value_expr (inner_expr
);
25913 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25914 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25915 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25916 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25917 if (is_overloaded_fn (inner_expr
)
25918 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25920 /* There is no need to return a proxy for a variable or enumerator. */
25921 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
25923 /* Preserve string constants; conversions from string constants to
25924 "char *" are allowed, even though normally a "const char *"
25925 cannot be used to initialize a "char *". */
25926 if (TREE_CODE (expr
) == STRING_CST
)
25928 /* Preserve void and arithmetic constants, as an optimization -- there is no
25929 reason to create a new node. */
25930 if (TREE_CODE (expr
) == VOID_CST
25931 || TREE_CODE (expr
) == INTEGER_CST
25932 || TREE_CODE (expr
) == REAL_CST
)
25934 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25935 There is at least one place where we want to know that a
25936 particular expression is a throw-expression: when checking a ?:
25937 expression, there are special rules if the second or third
25938 argument is a throw-expression. */
25939 if (TREE_CODE (expr
) == THROW_EXPR
)
25942 /* Don't wrap an initializer list, we need to be able to look inside. */
25943 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25946 /* Don't wrap a dummy object, we need to be able to test for it. */
25947 if (is_dummy_object (expr
))
25950 if (TREE_CODE (expr
) == COND_EXPR
)
25951 return build3 (COND_EXPR
,
25953 TREE_OPERAND (expr
, 0),
25954 (TREE_OPERAND (expr
, 1)
25955 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25956 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25957 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25958 if (TREE_CODE (expr
) == COMPOUND_EXPR
25959 && !COMPOUND_EXPR_OVERLOADED (expr
))
25960 return build2 (COMPOUND_EXPR
,
25962 TREE_OPERAND (expr
, 0),
25963 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25965 /* If the type is unknown, it can't really be non-dependent */
25966 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25968 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25969 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
25970 TREE_TYPE (expr
), expr
);
25973 /* ARGS is a vector of expressions as arguments to a function call.
25974 Replace the arguments with equivalent non-dependent expressions.
25975 This modifies ARGS in place. */
25978 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25983 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25985 tree newarg
= build_non_dependent_expr (arg
);
25987 (*args
)[ix
] = newarg
;
25991 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25992 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25993 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25996 make_auto_1 (tree name
, bool set_canonical
)
25998 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25999 TYPE_NAME (au
) = build_decl (input_location
,
26000 TYPE_DECL
, name
, au
);
26001 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
26002 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
26003 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
26004 TYPE_NAME (au
), NULL_TREE
);
26006 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
26007 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
26008 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
26014 make_decltype_auto (void)
26016 return make_auto_1 (decltype_auto_identifier
, true);
26022 return make_auto_1 (auto_identifier
, true);
26025 /* Return a C++17 deduction placeholder for class template TMPL. */
26028 make_template_placeholder (tree tmpl
)
26030 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
26031 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
26035 /* True iff T is a C++17 class template deduction placeholder. */
26038 template_placeholder_p (tree t
)
26040 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
26043 /* Make a "constrained auto" type-specifier. This is an
26044 auto type with constraints that must be associated after
26045 deduction. The constraint is formed from the given
26046 CONC and its optional sequence of arguments, which are
26047 non-null if written as partial-concept-id. */
26050 make_constrained_auto (tree con
, tree args
)
26052 tree type
= make_auto_1 (auto_identifier
, false);
26054 /* Build the constraint. */
26055 tree tmpl
= DECL_TI_TEMPLATE (con
);
26056 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
26057 expr
= build_concept_check (expr
, type
, args
);
26059 tree constr
= normalize_expression (expr
);
26060 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
26062 /* Our canonical type depends on the constraint. */
26063 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
26065 /* Attach the constraint to the type declaration. */
26066 tree decl
= TYPE_NAME (type
);
26070 /* Given type ARG, return std::initializer_list<ARG>. */
26075 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
26077 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
26079 gcc_rich_location
richloc (input_location
);
26080 maybe_add_include_fixit (&richloc
, "<initializer_list>");
26081 error_at (&richloc
,
26082 "deducing from brace-enclosed initializer list"
26083 " requires %<#include <initializer_list>%>");
26085 return error_mark_node
;
26087 tree argvec
= make_tree_vec (1);
26088 TREE_VEC_ELT (argvec
, 0) = arg
;
26090 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
26091 NULL_TREE
, 0, tf_warning_or_error
);
26094 /* Replace auto in TYPE with std::initializer_list<auto>. */
26097 listify_autos (tree type
, tree auto_node
)
26099 tree init_auto
= listify (auto_node
);
26100 tree argvec
= make_tree_vec (1);
26101 TREE_VEC_ELT (argvec
, 0) = init_auto
;
26102 if (processing_template_decl
)
26103 argvec
= add_to_template_args (current_template_args (), argvec
);
26104 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
26107 /* Hash traits for hashing possibly constrained 'auto'
26108 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26110 struct auto_hash
: default_hash_traits
<tree
>
26112 static inline hashval_t
hash (tree
);
26113 static inline bool equal (tree
, tree
);
26116 /* Hash the 'auto' T. */
26119 auto_hash::hash (tree t
)
26121 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
26122 /* Matching constrained-type-specifiers denote the same template
26123 parameter, so hash the constraint. */
26124 return hash_placeholder_constraint (c
);
26126 /* But unconstrained autos are all separate, so just hash the pointer. */
26127 return iterative_hash_object (t
, 0);
26130 /* Compare two 'auto's. */
26133 auto_hash::equal (tree t1
, tree t2
)
26138 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
26139 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
26141 /* Two unconstrained autos are distinct. */
26145 return equivalent_placeholder_constraints (c1
, c2
);
26148 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26149 constrained) auto, add it to the vector. */
26152 extract_autos_r (tree t
, void *data
)
26154 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
26157 /* All the autos were built with index 0; fix that up now. */
26158 tree
*p
= hash
.find_slot (t
, INSERT
);
26161 /* If this is a repeated constrained-type-specifier, use the index we
26163 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
26166 /* Otherwise this is new, so use the current count. */
26168 idx
= hash
.elements () - 1;
26170 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
26173 /* Always keep walking. */
26177 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26178 says they can appear anywhere in the type. */
26181 extract_autos (tree type
)
26183 hash_set
<tree
> visited
;
26184 hash_table
<auto_hash
> hash (2);
26186 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
26188 tree tree_vec
= make_tree_vec (hash
.elements());
26189 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
26190 iter
!= hash
.end(); ++iter
)
26193 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
26194 TREE_VEC_ELT (tree_vec
, i
)
26195 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
26201 /* The stem for deduction guide names. */
26202 const char *const dguide_base
= "__dguide_";
26204 /* Return the name for a deduction guide for class template TMPL. */
26207 dguide_name (tree tmpl
)
26209 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
26210 tree tname
= TYPE_IDENTIFIER (type
);
26211 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
26212 + IDENTIFIER_LENGTH (tname
));
26213 memcpy (buf
, dguide_base
, strlen (dguide_base
));
26214 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
26215 IDENTIFIER_LENGTH (tname
) + 1);
26216 tree dname
= get_identifier (buf
);
26217 TREE_TYPE (dname
) = type
;
26221 /* True if NAME is the name of a deduction guide. */
26224 dguide_name_p (tree name
)
26226 return (TREE_CODE (name
) == IDENTIFIER_NODE
26227 && TREE_TYPE (name
)
26228 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
26229 strlen (dguide_base
)));
26232 /* True if FN is a deduction guide. */
26235 deduction_guide_p (const_tree fn
)
26238 if (tree name
= DECL_NAME (fn
))
26239 return dguide_name_p (name
);
26243 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26246 copy_guide_p (const_tree fn
)
26248 gcc_assert (deduction_guide_p (fn
));
26249 if (!DECL_ARTIFICIAL (fn
))
26251 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
26252 return (TREE_CHAIN (parms
) == void_list_node
26253 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
26256 /* True if FN is a guide generated from a constructor template. */
26259 template_guide_p (const_tree fn
)
26261 gcc_assert (deduction_guide_p (fn
));
26262 if (!DECL_ARTIFICIAL (fn
))
26264 tree tmpl
= DECL_TI_TEMPLATE (fn
);
26265 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
26266 return PRIMARY_TEMPLATE_P (org
);
26270 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26271 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26272 template parameter types. Note that the handling of template template
26273 parameters relies on current_template_parms being set appropriately for the
26277 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
26278 tree tsubst_args
, tsubst_flags_t complain
)
26280 if (olddecl
== error_mark_node
)
26281 return error_mark_node
;
26283 tree oldidx
= get_template_parm_index (olddecl
);
26286 if (TREE_CODE (olddecl
) == TYPE_DECL
26287 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26289 tree oldtype
= TREE_TYPE (olddecl
);
26290 newtype
= cxx_make_type (TREE_CODE (oldtype
));
26291 TYPE_MAIN_VARIANT (newtype
) = newtype
;
26292 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
26293 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
26294 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
26298 newtype
= TREE_TYPE (olddecl
);
26299 if (type_uses_auto (newtype
))
26301 // Substitute once to fix references to other template parameters.
26302 newtype
= tsubst (newtype
, tsubst_args
,
26303 complain
|tf_partial
, NULL_TREE
);
26304 // Now substitute again to reduce the level of the auto.
26305 newtype
= tsubst (newtype
, current_template_args (),
26306 complain
, NULL_TREE
);
26309 newtype
= tsubst (newtype
, tsubst_args
,
26310 complain
, NULL_TREE
);
26314 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
26315 DECL_NAME (olddecl
), newtype
);
26316 SET_DECL_TEMPLATE_PARM_P (newdecl
);
26319 if (TREE_CODE (olddecl
) == TYPE_DECL
26320 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26322 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
26323 = build_template_parm_index (index
, level
, level
,
26325 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26326 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26327 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
26328 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
26330 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26332 DECL_TEMPLATE_RESULT (newdecl
)
26333 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
26334 DECL_NAME (olddecl
), newtype
);
26335 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
26336 // First create a copy (ttargs) of tsubst_args with an
26337 // additional level for the template template parameter's own
26338 // template parameters (ttparms).
26339 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
26340 (DECL_TEMPLATE_PARMS (olddecl
)));
26341 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
26342 tree ttargs
= make_tree_vec (depth
+ 1);
26343 for (int i
= 0; i
< depth
; ++i
)
26344 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
26345 TREE_VEC_ELT (ttargs
, depth
)
26346 = template_parms_level_to_args (ttparms
);
26347 // Substitute ttargs into ttparms to fix references to
26348 // other template parameters.
26349 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26350 complain
|tf_partial
);
26351 // Now substitute again with args based on tparms, to reduce
26352 // the level of the ttparms.
26353 ttargs
= current_template_args ();
26354 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26356 // Finally, tack the adjusted parms onto tparms.
26357 ttparms
= tree_cons (size_int (depth
), ttparms
,
26358 current_template_parms
);
26359 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
26364 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
26366 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
26367 TREE_CODE (oldconst
),
26368 DECL_NAME (oldconst
), newtype
);
26369 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
26370 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
26371 SET_DECL_TEMPLATE_PARM_P (newconst
);
26372 newidx
= build_template_parm_index (index
, level
, level
,
26373 newconst
, newtype
);
26374 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26375 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26376 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
26382 /* Returns a C++17 class deduction guide template based on the constructor
26383 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26384 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26387 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
26389 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
26390 bool memtmpl
= false;
26393 tree fn_tmpl
= NULL_TREE
;
26398 bool copy_p
= TYPE_REF_P (type
);
26401 type
= TREE_TYPE (type
);
26402 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
26405 fparms
= void_list_node
;
26407 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
26408 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
26409 targs
= CLASSTYPE_TI_ARGS (type
);
26412 loc
= DECL_SOURCE_LOCATION (ctmpl
);
26413 explicit_p
= false;
26417 ++processing_template_decl
;
26421 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
26422 : DECL_TI_TEMPLATE (ctor
));
26424 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
26425 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
26427 type
= DECL_CONTEXT (ctor
);
26429 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
26430 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26431 fully specialized args for the enclosing class. Strip those off, as
26432 the deduction guide won't have those template parameters. */
26433 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
26434 TMPL_PARMS_DEPTH (tparms
));
26435 /* Discard the 'this' parameter. */
26436 fparms
= FUNCTION_ARG_CHAIN (ctor
);
26437 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
26438 ci
= get_constraints (ctor
);
26439 loc
= DECL_SOURCE_LOCATION (ctor
);
26440 explicit_p
= DECL_NONCONVERTING_P (ctor
);
26442 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
26446 /* For a member template constructor, we need to flatten the two
26447 template parameter lists into one, and then adjust the function
26448 signature accordingly. This gets...complicated. */
26449 tree save_parms
= current_template_parms
;
26451 /* For a member template we should have two levels of parms/args, one
26452 for the class and one for the constructor. We stripped
26453 specialized args for further enclosing classes above. */
26454 const int depth
= 2;
26455 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
26457 /* Template args for translating references to the two-level template
26458 parameters into references to the one-level template parameters we
26460 tree tsubst_args
= copy_node (targs
);
26461 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
26462 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
26464 /* Template parms for the constructor template. */
26465 tree ftparms
= TREE_VALUE (tparms
);
26466 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
26467 /* Template parms for the class template. */
26468 tparms
= TREE_CHAIN (tparms
);
26469 tree ctparms
= TREE_VALUE (tparms
);
26470 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
26471 /* Template parms for the deduction guide start as a copy of the
26472 template parms for the class. We set current_template_parms for
26473 lookup_template_class_1. */
26474 current_template_parms
= tparms
= copy_node (tparms
);
26475 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
26476 for (unsigned i
= 0; i
< clen
; ++i
)
26477 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
26479 /* Now we need to rewrite the constructor parms to append them to the
26481 for (unsigned i
= 0; i
< flen
; ++i
)
26483 unsigned index
= i
+ clen
;
26484 unsigned level
= 1;
26485 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
26486 tree olddecl
= TREE_VALUE (oldelt
);
26487 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
26488 tsubst_args
, complain
);
26489 if (newdecl
== error_mark_node
)
26491 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
26492 tsubst_args
, complain
, ctor
);
26493 tree list
= build_tree_list (newdef
, newdecl
);
26494 TEMPLATE_PARM_CONSTRAINTS (list
)
26495 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
26496 tsubst_args
, complain
, ctor
);
26497 TREE_VEC_ELT (new_vec
, index
) = list
;
26498 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
26501 /* Now we have a final set of template parms to substitute into the
26502 function signature. */
26503 targs
= template_parms_to_args (tparms
);
26504 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
26506 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
26508 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
26510 current_template_parms
= save_parms
;
26513 --processing_template_decl
;
26515 return error_mark_node
;
26520 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26521 tparms
= copy_node (tparms
);
26522 INNERMOST_TEMPLATE_PARMS (tparms
)
26523 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
26526 tree fntype
= build_function_type (type
, fparms
);
26527 tree ded_fn
= build_lang_decl_loc (loc
,
26529 dguide_name (type
), fntype
);
26530 DECL_ARGUMENTS (ded_fn
) = fargs
;
26531 DECL_ARTIFICIAL (ded_fn
) = true;
26532 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
26533 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
26534 DECL_ARTIFICIAL (ded_tmpl
) = true;
26535 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
26536 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
26537 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
26538 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
26540 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
26542 set_constraints (ded_tmpl
, ci
);
26547 /* Deduce template arguments for the class template placeholder PTYPE for
26548 template TMPL based on the initializer INIT, and return the resulting
26552 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
26553 tsubst_flags_t complain
)
26555 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
26557 /* We should have handled this in the caller. */
26558 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26560 if (complain
& tf_error
)
26561 error ("non-class template %qT used without template arguments", tmpl
);
26562 return error_mark_node
;
26565 tree type
= TREE_TYPE (tmpl
);
26567 bool try_list_ctor
= false;
26569 vec
<tree
,va_gc
> *args
;
26570 if (init
== NULL_TREE
26571 || TREE_CODE (init
) == TREE_LIST
)
26572 args
= make_tree_vector_from_list (init
);
26573 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26575 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
26576 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
26578 /* As an exception, the first phase in 16.3.1.7 (considering the
26579 initializer list as a single argument) is omitted if the
26580 initializer list consists of a single expression of type cv U,
26581 where U is a specialization of C or a class derived from a
26582 specialization of C. */
26583 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
26584 tree etype
= TREE_TYPE (elt
);
26586 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
26587 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26588 int err
= unify (tparms
, targs
, type
, etype
,
26589 UNIFY_ALLOW_DERIVED
, /*explain*/false);
26591 try_list_ctor
= false;
26594 if (try_list_ctor
|| is_std_init_list (type
))
26595 args
= make_tree_vector_single (init
);
26597 args
= make_tree_vector_from_ctor (init
);
26600 args
= make_tree_vector_single (init
);
26602 tree dname
= dguide_name (tmpl
);
26603 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
26604 /*type*/false, /*complain*/false,
26606 bool elided
= false;
26607 if (cands
== error_mark_node
)
26610 /* Prune explicit deduction guides in copy-initialization context. */
26611 if (flags
& LOOKUP_ONLYCONVERTING
)
26613 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
26614 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
26619 /* Found a nonconverting guide, prune the candidates. */
26620 tree pruned
= NULL_TREE
;
26621 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26622 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
26623 pruned
= lookup_add (*iter
, pruned
);
26629 tree outer_args
= NULL_TREE
;
26630 if (DECL_CLASS_SCOPE_P (tmpl
)
26631 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
26633 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
26634 type
= TREE_TYPE (most_general_template (tmpl
));
26637 bool saw_ctor
= false;
26638 // FIXME cache artificial deduction guides
26639 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
26641 /* Skip inherited constructors. */
26642 if (iter
.using_p ())
26645 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
26646 if (guide
== error_mark_node
)
26647 return error_mark_node
;
26648 if ((flags
& LOOKUP_ONLYCONVERTING
)
26649 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
26652 cands
= lookup_add (guide
, cands
);
26657 tree call
= error_mark_node
;
26659 /* If this is list-initialization and the class has a list constructor, first
26660 try deducing from the list as a single argument, as [over.match.list]. */
26661 tree list_cands
= NULL_TREE
;
26662 if (try_list_ctor
&& cands
)
26663 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26666 if (is_list_ctor (dg
))
26667 list_cands
= lookup_add (dg
, list_cands
);
26671 ++cp_unevaluated_operand
;
26672 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
26673 --cp_unevaluated_operand
;
26675 if (call
== error_mark_node
)
26677 /* That didn't work, now try treating the list as a sequence of
26679 release_tree_vector (args
);
26680 args
= make_tree_vector_from_ctor (init
);
26684 /* Maybe generate an implicit deduction guide. */
26685 if (call
== error_mark_node
&& args
->length () < 2)
26687 tree gtype
= NULL_TREE
;
26689 if (args
->length () == 1)
26690 /* Generate a copy guide. */
26691 gtype
= build_reference_type (type
);
26692 else if (!saw_ctor
)
26693 /* Generate a default guide. */
26698 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
26699 if (guide
== error_mark_node
)
26700 return error_mark_node
;
26701 cands
= lookup_add (guide
, cands
);
26705 if (elided
&& !cands
)
26707 error ("cannot deduce template arguments for copy-initialization"
26708 " of %qT, as it has no non-explicit deduction guides or "
26709 "user-declared constructors", type
);
26710 return error_mark_node
;
26712 else if (!cands
&& call
== error_mark_node
)
26714 error ("cannot deduce template arguments of %qT, as it has no viable "
26715 "deduction guides", type
);
26716 return error_mark_node
;
26719 if (call
== error_mark_node
)
26721 ++cp_unevaluated_operand
;
26722 call
= build_new_function_call (cands
, &args
, tf_decltype
);
26723 --cp_unevaluated_operand
;
26726 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
26728 error ("class template argument deduction failed:");
26730 ++cp_unevaluated_operand
;
26731 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
26732 --cp_unevaluated_operand
;
26735 inform (input_location
, "explicit deduction guides not considered "
26736 "for copy-initialization");
26739 release_tree_vector (args
);
26741 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
26744 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26745 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26746 The CONTEXT determines the context in which auto deduction is performed
26747 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26748 OUTER_TARGS are used during template argument deduction
26749 (context == adc_unify) to properly substitute the result, and is ignored
26752 For partial-concept-ids, extra args may be appended to the list of deduced
26753 template arguments prior to determining constraint satisfaction. */
26756 do_auto_deduction (tree type
, tree init
, tree auto_node
,
26757 tsubst_flags_t complain
, auto_deduction_context context
,
26758 tree outer_targs
, int flags
)
26762 if (init
== error_mark_node
)
26763 return error_mark_node
;
26765 if (init
&& type_dependent_expression_p (init
)
26766 && context
!= adc_unify
)
26767 /* Defining a subset of type-dependent expressions that we can deduce
26768 from ahead of time isn't worth the trouble. */
26771 /* Similarly, we can't deduce from another undeduced decl. */
26772 if (init
&& undeduced_auto_decl (init
))
26775 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
26776 /* C++17 class template argument deduction. */
26777 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
26779 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
26780 /* Nothing we can do with this, even in deduction context. */
26783 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26784 with either a new invented type template parameter U or, if the
26785 initializer is a braced-init-list (8.5.4), with
26786 std::initializer_list<U>. */
26787 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26789 if (!DIRECT_LIST_INIT_P (init
))
26790 type
= listify_autos (type
, auto_node
);
26791 else if (CONSTRUCTOR_NELTS (init
) == 1)
26792 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26795 if (complain
& tf_warning_or_error
)
26797 if (permerror (input_location
, "direct-list-initialization of "
26798 "%<auto%> requires exactly one element"))
26799 inform (input_location
,
26800 "for deduction to %<std::initializer_list%>, use copy-"
26801 "list-initialization (i.e. add %<=%> before the %<{%>)");
26803 type
= listify_autos (type
, auto_node
);
26807 if (type
== error_mark_node
)
26808 return error_mark_node
;
26810 init
= resolve_nondeduced_context (init
, complain
);
26812 if (context
== adc_decomp_type
26813 && auto_node
== type
26814 && init
!= error_mark_node
26815 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26816 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26817 and initializer has array type, deduce cv-qualified array type. */
26818 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26820 else if (AUTO_IS_DECLTYPE (auto_node
))
26822 bool id
= (DECL_P (init
)
26823 || ((TREE_CODE (init
) == COMPONENT_REF
26824 || TREE_CODE (init
) == SCOPE_REF
)
26825 && !REF_PARENTHESIZED_P (init
)));
26826 targs
= make_tree_vec (1);
26827 TREE_VEC_ELT (targs
, 0)
26828 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26829 if (type
!= auto_node
)
26831 if (complain
& tf_error
)
26832 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26833 return error_mark_node
;
26838 tree parms
= build_tree_list (NULL_TREE
, type
);
26842 tparms
= extract_autos (type
);
26845 tparms
= make_tree_vec (1);
26846 TREE_VEC_ELT (tparms
, 0)
26847 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26850 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26851 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26853 NULL
, /*explain_p=*/false);
26856 if (processing_template_decl
)
26857 /* Try again at instantiation time. */
26859 if (type
&& type
!= error_mark_node
26860 && (complain
& tf_error
))
26861 /* If type is error_mark_node a diagnostic must have been
26862 emitted by now. Also, having a mention to '<type error>'
26863 in the diagnostic is not really useful to the user. */
26865 if (cfun
&& auto_node
== current_function_auto_return_pattern
26866 && LAMBDA_FUNCTION_P (current_function_decl
))
26867 error ("unable to deduce lambda return type from %qE", init
);
26869 error ("unable to deduce %qT from %qE", type
, init
);
26870 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26872 NULL
, /*explain_p=*/true);
26874 return error_mark_node
;
26878 /* Check any placeholder constraints against the deduced type. */
26879 if (flag_concepts
&& !processing_template_decl
)
26880 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26882 /* Use the deduced type to check the associated constraints. If we
26883 have a partial-concept-id, rebuild the argument list so that
26884 we check using the extra arguments. */
26885 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26886 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26887 if (TREE_VEC_LENGTH (cargs
) > 1)
26889 cargs
= copy_node (cargs
);
26890 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26894 if (!constraints_satisfied_p (constr
, cargs
))
26896 if (complain
& tf_warning_or_error
)
26898 auto_diagnostic_group d
;
26901 case adc_unspecified
:
26903 error("placeholder constraints not satisfied");
26905 case adc_variable_type
:
26906 case adc_decomp_type
:
26907 error ("deduced initializer does not satisfy "
26908 "placeholder constraints");
26910 case adc_return_type
:
26911 error ("deduced return type does not satisfy "
26912 "placeholder constraints");
26914 case adc_requirement
:
26915 error ("deduced expression type does not satisfy "
26916 "placeholder constraints");
26919 diagnose_constraints (input_location
, constr
, targs
);
26921 return error_mark_node
;
26925 if (processing_template_decl
&& context
!= adc_unify
)
26926 outer_targs
= current_template_args ();
26927 targs
= add_to_template_args (outer_targs
, targs
);
26928 return tsubst (type
, targs
, complain
, NULL_TREE
);
26931 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26935 splice_late_return_type (tree type
, tree late_return_type
)
26937 if (is_auto (type
))
26939 if (late_return_type
)
26940 return late_return_type
;
26942 tree idx
= get_template_parm_index (type
);
26943 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26944 /* In an abbreviated function template we didn't know we were dealing
26945 with a function template when we saw the auto return type, so update
26946 it to have the correct level. */
26947 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26952 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26953 'decltype(auto)' or a deduced class template. */
26956 is_auto (const_tree type
)
26958 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26959 && (TYPE_IDENTIFIER (type
) == auto_identifier
26960 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26961 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26967 /* for_each_template_parm callback for type_uses_auto. */
26970 is_auto_r (tree tp
, void */
*data*/
)
26972 return is_auto (tp
);
26975 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26976 a use of `auto'. Returns NULL_TREE otherwise. */
26979 type_uses_auto (tree type
)
26981 if (type
== NULL_TREE
)
26983 else if (flag_concepts
)
26985 /* The Concepts TS allows multiple autos in one type-specifier; just
26986 return the first one we find, do_auto_deduction will collect all of
26988 if (uses_template_parms (type
))
26989 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26990 /*visited*/NULL
, /*nondeduced*/false);
26995 return find_type_usage (type
, is_auto
);
26998 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
26999 concepts are enabled, auto is acceptable in template arguments, but
27000 only when TEMPL identifies a template class. Return TRUE if any
27001 such errors were reported. */
27004 check_auto_in_tmpl_args (tree tmpl
, tree args
)
27006 /* If there were previous errors, nevermind. */
27007 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
27010 /* If TMPL is an identifier, we're parsing and we can't tell yet
27011 whether TMPL is supposed to be a type, a function or a variable.
27012 We'll only be able to tell during template substitution, so we
27013 expect to be called again then. If concepts are enabled and we
27014 know we have a type, we're ok. */
27016 && (identifier_p (tmpl
)
27018 && (DECL_TYPE_TEMPLATE_P (tmpl
)
27019 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
27022 /* Quickly search for any occurrences of auto; usually there won't
27023 be any, and then we'll avoid allocating the vector. */
27024 if (!type_uses_auto (args
))
27027 bool errors
= false;
27029 tree vec
= extract_autos (args
);
27030 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
27032 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
27033 error_at (DECL_SOURCE_LOCATION (xauto
),
27034 "invalid use of %qT in template argument", xauto
);
27041 /* For a given template T, return the vector of typedefs referenced
27042 in T for which access check is needed at T instantiation time.
27043 T is either a FUNCTION_DECL or a RECORD_TYPE.
27044 Those typedefs were added to T by the function
27045 append_type_to_template_for_access_check. */
27047 vec
<qualified_typedef_usage_t
, va_gc
> *
27048 get_types_needing_access_check (tree t
)
27051 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
27053 if (!t
|| t
== error_mark_node
)
27056 if (!(ti
= get_template_info (t
)))
27059 if (CLASS_TYPE_P (t
)
27060 || TREE_CODE (t
) == FUNCTION_DECL
)
27062 if (!TI_TEMPLATE (ti
))
27065 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
27071 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27072 tied to T. That list of typedefs will be access checked at
27073 T instantiation time.
27074 T is either a FUNCTION_DECL or a RECORD_TYPE.
27075 TYPE_DECL is a TYPE_DECL node representing a typedef.
27076 SCOPE is the scope through which TYPE_DECL is accessed.
27077 LOCATION is the location of the usage point of TYPE_DECL.
27079 This function is a subroutine of
27080 append_type_to_template_for_access_check. */
27083 append_type_to_template_for_access_check_1 (tree t
,
27086 location_t location
)
27088 qualified_typedef_usage_t typedef_usage
;
27091 if (!t
|| t
== error_mark_node
)
27094 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
27095 || CLASS_TYPE_P (t
))
27097 && TREE_CODE (type_decl
) == TYPE_DECL
27100 if (!(ti
= get_template_info (t
)))
27103 gcc_assert (TI_TEMPLATE (ti
));
27105 typedef_usage
.typedef_decl
= type_decl
;
27106 typedef_usage
.context
= scope
;
27107 typedef_usage
.locus
= location
;
27109 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
27112 /* Append TYPE_DECL to the template TEMPL.
27113 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27114 At TEMPL instanciation time, TYPE_DECL will be checked to see
27115 if it can be accessed through SCOPE.
27116 LOCATION is the location of the usage point of TYPE_DECL.
27118 e.g. consider the following code snippet:
27125 template<class U> struct S
27127 C::myint mi; // <-- usage point of the typedef C::myint
27132 At S<char> instantiation time, we need to check the access of C::myint
27133 In other words, we need to check the access of the myint typedef through
27134 the C scope. For that purpose, this function will add the myint typedef
27135 and the scope C through which its being accessed to a list of typedefs
27136 tied to the template S. That list will be walked at template instantiation
27137 time and access check performed on each typedefs it contains.
27138 Note that this particular code snippet should yield an error because
27139 myint is private to C. */
27142 append_type_to_template_for_access_check (tree templ
,
27145 location_t location
)
27147 qualified_typedef_usage_t
*iter
;
27150 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
27152 /* Make sure we don't append the type to the template twice. */
27153 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
27154 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
27157 append_type_to_template_for_access_check_1 (templ
, type_decl
,
27161 /* Convert the generic type parameters in PARM that match the types given in the
27162 range [START_IDX, END_IDX) from the current_template_parms into generic type
27166 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
27168 tree current
= current_template_parms
;
27169 int depth
= TMPL_PARMS_DEPTH (current
);
27170 current
= INNERMOST_TEMPLATE_PARMS (current
);
27171 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
27173 for (int i
= 0; i
< start_idx
; ++i
)
27174 TREE_VEC_ELT (replacement
, i
)
27175 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27177 for (int i
= start_idx
; i
< end_idx
; ++i
)
27179 /* Create a distinct parameter pack type from the current parm and add it
27180 to the replacement args to tsubst below into the generic function
27183 tree o
= TREE_TYPE (TREE_VALUE
27184 (TREE_VEC_ELT (current
, i
)));
27185 tree t
= copy_type (o
);
27186 TEMPLATE_TYPE_PARM_INDEX (t
)
27187 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
27189 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
27190 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
27191 TYPE_MAIN_VARIANT (t
) = t
;
27192 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
27193 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27194 TREE_VEC_ELT (replacement
, i
) = t
;
27195 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
27198 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
27199 TREE_VEC_ELT (replacement
, i
)
27200 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27202 /* If there are more levels then build up the replacement with the outer
27205 replacement
= add_to_template_args (template_parms_to_args
27206 (TREE_CHAIN (current_template_parms
)),
27209 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
27212 /* Entries in the decl_constraint hash table. */
27213 struct GTY((for_user
)) constr_entry
27219 /* Hashing function and equality for constraint entries. */
27220 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
27222 static hashval_t
hash (constr_entry
*e
)
27224 return (hashval_t
)DECL_UID (e
->decl
);
27227 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
27229 return e1
->decl
== e2
->decl
;
27233 /* A mapping from declarations to constraint information. Note that
27234 both templates and their underlying declarations are mapped to the
27235 same constraint information.
27237 FIXME: This is defined in pt.c because garbage collection
27238 code is not being generated for constraint.cc. */
27240 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
27242 /* Returns the template constraints of declaration T. If T is not
27243 constrained, return NULL_TREE. Note that T must be non-null. */
27246 get_constraints (tree t
)
27248 if (!flag_concepts
)
27251 gcc_assert (DECL_P (t
));
27252 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27253 t
= DECL_TEMPLATE_RESULT (t
);
27254 constr_entry elt
= { t
, NULL_TREE
};
27255 constr_entry
* found
= decl_constraints
->find (&elt
);
27262 /* Associate the given constraint information CI with the declaration
27263 T. If T is a template, then the constraints are associated with
27264 its underlying declaration. Don't build associations if CI is
27268 set_constraints (tree t
, tree ci
)
27272 gcc_assert (t
&& flag_concepts
);
27273 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27274 t
= DECL_TEMPLATE_RESULT (t
);
27275 gcc_assert (!get_constraints (t
));
27276 constr_entry elt
= {t
, ci
};
27277 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
27278 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
27283 /* Remove the associated constraints of the declaration T. */
27286 remove_constraints (tree t
)
27288 gcc_assert (DECL_P (t
));
27289 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27290 t
= DECL_TEMPLATE_RESULT (t
);
27292 constr_entry elt
= {t
, NULL_TREE
};
27293 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
27295 decl_constraints
->clear_slot (slot
);
27298 /* Memoized satisfaction results for declarations. This
27299 maps the pair (constraint_info, arguments) to the result computed
27300 by constraints_satisfied_p. */
27302 struct GTY((for_user
)) constraint_sat_entry
27309 /* Hashing function and equality for constraint entries. */
27311 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
27313 static hashval_t
hash (constraint_sat_entry
*e
)
27315 hashval_t val
= iterative_hash_object(e
->ci
, 0);
27316 return iterative_hash_template_arg (e
->args
, val
);
27319 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
27321 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
27325 /* Memoized satisfaction results for concept checks. */
27327 struct GTY((for_user
)) concept_spec_entry
27334 /* Hashing function and equality for constraint entries. */
27336 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
27338 static hashval_t
hash (concept_spec_entry
*e
)
27340 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
27343 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
27345 ++comparing_specializations
;
27346 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
27347 --comparing_specializations
;
27352 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
27353 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
27355 /* Search for a memoized satisfaction result. Returns one of the
27356 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27359 lookup_constraint_satisfaction (tree ci
, tree args
)
27361 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
27362 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
27364 return found
->result
;
27369 /* Memoize the result of a satisfication test. Returns the saved result. */
27372 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
27374 constraint_sat_entry elt
= {ci
, args
, result
};
27375 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
27376 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
27382 /* Search for a memoized satisfaction result for a concept. */
27385 lookup_concept_satisfaction (tree tmpl
, tree args
)
27387 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27388 concept_spec_entry
* found
= concept_memos
->find (&elt
);
27390 return found
->result
;
27395 /* Memoize the result of a concept check. Returns the saved result. */
27398 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
27400 concept_spec_entry elt
= {tmpl
, args
, result
};
27401 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
27402 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27408 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
27410 /* Returns a prior concept specialization. This returns the substituted
27411 and normalized constraints defined by the concept. */
27414 get_concept_expansion (tree tmpl
, tree args
)
27416 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27417 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
27419 return found
->result
;
27424 /* Save a concept expansion for later. */
27427 save_concept_expansion (tree tmpl
, tree args
, tree def
)
27429 concept_spec_entry elt
= {tmpl
, args
, def
};
27430 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
27431 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27438 hash_subsumption_args (tree t1
, tree t2
)
27440 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
27441 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
27443 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
27444 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
27445 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
27446 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
27450 /* Compare the constraints of two subsumption entries. The LEFT1 and
27451 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27452 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27455 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
27457 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
27458 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
27459 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
27460 CHECK_CONSTR_ARGS (right1
)))
27461 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
27462 CHECK_CONSTR_ARGS (right2
));
27466 /* Key/value pair for learning and memoizing subsumption results. This
27467 associates a pair of check constraints (including arguments) with
27468 a boolean value indicating the result. */
27470 struct GTY((for_user
)) subsumption_entry
27477 /* Hashing function and equality for constraint entries. */
27479 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
27481 static hashval_t
hash (subsumption_entry
*e
)
27483 return hash_subsumption_args (e
->t1
, e
->t2
);
27486 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
27488 ++comparing_specializations
;
27489 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
27490 --comparing_specializations
;
27495 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
27497 /* Search for a previously cached subsumption result. */
27500 lookup_subsumption_result (tree t1
, tree t2
)
27502 subsumption_entry elt
= { t1
, t2
, false };
27503 subsumption_entry
* found
= subsumption_table
->find (&elt
);
27505 return &found
->result
;
27510 /* Save a subsumption result. */
27513 save_subsumption_result (tree t1
, tree t2
, bool result
)
27515 subsumption_entry elt
= {t1
, t2
, result
};
27516 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
27517 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
27523 /* Set up the hash table for constraint association. */
27526 init_constraint_processing (void)
27528 if (!flag_concepts
)
27531 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
27532 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
27533 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27534 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27535 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
27538 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27542 declare_integer_pack (void)
27544 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
27545 build_function_type_list (integer_type_node
,
27548 NULL_TREE
, ECF_CONST
);
27549 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
27550 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
27551 DECL_FUNCTION_CODE (ipfn
)
27552 = (enum built_in_function
) (int) CP_BUILT_IN_INTEGER_PACK
;
27555 /* Set up the hash tables for template instantiations. */
27558 init_template_processing (void)
27560 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27561 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27563 if (cxx_dialect
>= cxx11
)
27564 declare_integer_pack ();
27567 /* Print stats about the template hash tables for -fstats. */
27570 print_template_statistics (void)
27572 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
27573 "%f collisions\n", (long) decl_specializations
->size (),
27574 (long) decl_specializations
->elements (),
27575 decl_specializations
->collisions ());
27576 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
27577 "%f collisions\n", (long) type_specializations
->size (),
27578 (long) type_specializations
->elements (),
27579 type_specializations
->collisions ());
27584 namespace selftest
{
27586 /* Verify that build_non_dependent_expr () works, for various expressions,
27587 and that location wrappers don't affect the results. */
27590 test_build_non_dependent_expr ()
27592 location_t loc
= BUILTINS_LOCATION
;
27594 /* Verify constants, without and with location wrappers. */
27595 tree int_cst
= build_int_cst (integer_type_node
, 42);
27596 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
27598 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
27599 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
27600 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
27602 tree string_lit
= build_string (4, "foo");
27603 TREE_TYPE (string_lit
) = char_array_type_node
;
27604 string_lit
= fix_string_type (string_lit
);
27605 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
27607 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
27608 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
27609 ASSERT_EQ (wrapped_string_lit
,
27610 build_non_dependent_expr (wrapped_string_lit
));
27613 /* Verify that type_dependent_expression_p () works correctly, even
27614 in the presence of location wrapper nodes. */
27617 test_type_dependent_expression_p ()
27619 location_t loc
= BUILTINS_LOCATION
;
27621 tree name
= get_identifier ("foo");
27623 /* If no templates are involved, nothing is type-dependent. */
27624 gcc_assert (!processing_template_decl
);
27625 ASSERT_FALSE (type_dependent_expression_p (name
));
27627 ++processing_template_decl
;
27629 /* Within a template, an unresolved name is always type-dependent. */
27630 ASSERT_TRUE (type_dependent_expression_p (name
));
27632 /* Ensure it copes with NULL_TREE and errors. */
27633 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
27634 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
27636 /* A USING_DECL in a template should be type-dependent, even if wrapped
27637 with a location wrapper (PR c++/83799). */
27638 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
27639 TREE_TYPE (using_decl
) = integer_type_node
;
27640 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
27641 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
27642 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
27643 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
27645 --processing_template_decl
;
27648 /* Run all of the selftests within this file. */
27653 test_build_non_dependent_expr ();
27654 test_type_dependent_expression_p ();
27657 } // namespace selftest
27659 #endif /* #if CHECKING_P */
27661 #include "gt-cp-pt.h"